test_api.py
1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flashcards.models import User
from rest_framework.status import HTTP_201_CREATED, HTTP_200_OK
from rest_framework.test import APITestCase
class LoginTests(APITestCase):
def setUp(self):
email = "test@flashy.cards"
User.objects.create_user(email, email=email, password="1234")
def test_login(self):
url = '/api/login'
data = {'email': 'test@flashy.cards', 'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, HTTP_200_OK)
data = {'email': 'test@flashy.cards', 'password': '54321'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'Invalid email or password', status_code=400)
data = {'email': 'none@flashy.cards', 'password': '54321'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'Invalid email or password', status_code=400)
data = {'password': '54321'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'Email is required', status_code=400)
class RegistrationTest(APITestCase):
def test_create_account(self):
url = '/api/users/me'
data = {'email': 'none@none.com', 'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, HTTP_201_CREATED)
class ProfileViewTest(APITestCase):
def test_get_me(self):
url = '/api/users/me'
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, HTTP_200_OK)