From a3637e4b25f232de516268173288ac4e9b82e0c8 Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Fri, 1 May 2015 11:51:16 -0700 Subject: [PATCH] Added some tests to start --- flashcards/api.py | 7 ++++--- flashcards/models.py | 1 - flashcards/tests/__init__.py | 0 flashcards/tests/test_api.py | 29 +++++++++++++++++++++++++++++ flashcards/tests/test_models.py | 11 +++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 flashcards/tests/__init__.py create mode 100644 flashcards/tests/test_api.py create mode 100644 flashcards/tests/test_models.py diff --git a/flashcards/api.py b/flashcards/api.py index e4e9ff0..b355e9b 100644 --- a/flashcards/api.py +++ b/flashcards/api.py @@ -1,6 +1,7 @@ from django.core.mail import send_mail from django.contrib.auth import authenticate, login from django.contrib.auth.tokens import default_token_generator +from rest_framework.status import HTTP_201_CREATED, HTTP_204_NO_CONTENT from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status @@ -55,11 +56,11 @@ class UserDetail(APIView): user = authenticate(email=email, password=request.data['password']) login(request, user) - return Response(UserSerializer(user).data) + return Response(UserSerializer(user).data, status=HTTP_201_CREATED) def delete(self, request, format=None): request.user.delete() - return Response(status=status.HTTP_204_NO_CONTENT) + return Response(status=HTTP_204_NO_CONTENT) class UserLogin(APIView): @@ -79,7 +80,7 @@ class UserLogin(APIView): email = request.data['email'] password = request.data['password'] - user = authenticate(username=email, password=password) + user = authenticate(email=email, password=password) if user is None: raise ValidationError('Invalid email or password') diff --git a/flashcards/models.py b/flashcards/models.py index f84a97f..fe57d18 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -1,5 +1,4 @@ from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin, AbstractUser -from django.contrib.auth.tests.custom_user import CustomUser from django.db.models import * from simple_email_confirmation import SimpleEmailConfirmationUserMixin diff --git a/flashcards/tests/__init__.py b/flashcards/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/flashcards/tests/test_api.py b/flashcards/tests/test_api.py new file mode 100644 index 0000000..aeb7ab8 --- /dev/null +++ b/flashcards/tests/test_api.py @@ -0,0 +1,29 @@ +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): + User.objects.create(email="test@flashy.cards", 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) + + +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) diff --git a/flashcards/tests/test_models.py b/flashcards/tests/test_models.py new file mode 100644 index 0000000..6b29ab8 --- /dev/null +++ b/flashcards/tests/test_models.py @@ -0,0 +1,11 @@ +from django.test import TestCase +from flashcards.models import User + + +class UserTests(TestCase): + def setUp(self): + u = User.objects.create(email="none@none.com",password="1234") + + def test_section_list(self): + u = User.objects.get(email="none@none.com") + u.sections.add(None) -- 1.9.1