Commit a3637e4b25f232de516268173288ac4e9b82e0c8
1 parent
f6068531bb
Exists in
master
Added some tests to start
Showing 4 changed files with 44 additions and 4 deletions Side-by-side Diff
flashcards/api.py
View file @
a3637e4
1 | 1 | from django.core.mail import send_mail |
2 | 2 | from django.contrib.auth import authenticate, login |
3 | 3 | from django.contrib.auth.tokens import default_token_generator |
4 | +from rest_framework.status import HTTP_201_CREATED, HTTP_204_NO_CONTENT | |
4 | 5 | from rest_framework.views import APIView |
5 | 6 | from rest_framework.response import Response |
6 | 7 | from rest_framework import status |
7 | 8 | |
... | ... | @@ -55,11 +56,11 @@ |
55 | 56 | |
56 | 57 | user = authenticate(email=email, password=request.data['password']) |
57 | 58 | login(request, user) |
58 | - return Response(UserSerializer(user).data) | |
59 | + return Response(UserSerializer(user).data, status=HTTP_201_CREATED) | |
59 | 60 | |
60 | 61 | def delete(self, request, format=None): |
61 | 62 | request.user.delete() |
62 | - return Response(status=status.HTTP_204_NO_CONTENT) | |
63 | + return Response(status=HTTP_204_NO_CONTENT) | |
63 | 64 | |
64 | 65 | |
65 | 66 | class UserLogin(APIView): |
... | ... | @@ -79,7 +80,7 @@ |
79 | 80 | |
80 | 81 | email = request.data['email'] |
81 | 82 | password = request.data['password'] |
82 | - user = authenticate(username=email, password=password) | |
83 | + user = authenticate(email=email, password=password) | |
83 | 84 | |
84 | 85 | if user is None: |
85 | 86 | raise ValidationError('Invalid email or password') |
flashcards/models.py
View file @
a3637e4
flashcards/tests/test_api.py
View file @
a3637e4
1 | +from flashcards.models import User | |
2 | +from rest_framework.status import HTTP_201_CREATED, HTTP_200_OK | |
3 | +from rest_framework.test import APITestCase | |
4 | + | |
5 | + | |
6 | +class LoginTests(APITestCase): | |
7 | + def setUp(self): | |
8 | + User.objects.create(email="test@flashy.cards", password='1234') | |
9 | + | |
10 | + def test_login(self): | |
11 | + url = '/api/login' | |
12 | + data = {'email': 'test@flashy.cards', 'password': '1234'} | |
13 | + response = self.client.post(url, data, format='json') | |
14 | + self.assertEqual(response.status_code, HTTP_200_OK) | |
15 | + | |
16 | + | |
17 | +class RegistrationTest(APITestCase): | |
18 | + def test_create_account(self): | |
19 | + url = '/api/users/me' | |
20 | + data = {'email': 'none@none.com', 'password': '1234'} | |
21 | + response = self.client.post(url, data, format='json') | |
22 | + self.assertEqual(response.status_code, HTTP_201_CREATED) | |
23 | + | |
24 | + | |
25 | +class ProfileViewTest(APITestCase): | |
26 | + def test_get_me(self): | |
27 | + url = '/api/users/me' | |
28 | + response = self.client.get(url, format='json') | |
29 | + self.assertEqual(response.status_code, HTTP_200_OK) |
flashcards/tests/test_models.py
View file @
a3637e4
1 | +from django.test import TestCase | |
2 | +from flashcards.models import User | |
3 | + | |
4 | + | |
5 | +class UserTests(TestCase): | |
6 | + def setUp(self): | |
7 | + u = User.objects.create(email="none@none.com",password="1234") | |
8 | + | |
9 | + def test_section_list(self): | |
10 | + u = User.objects.get(email="none@none.com") | |
11 | + u.sections.add(None) |