test_api.py 14.2 KB
from django.core import mail
from flashcards.models import *
from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK, HTTP_403_FORBIDDEN
from rest_framework.test import APITestCase
from re import search
from django.utils.timezone import now
class LoginTests(APITestCase):
fixtures = ['testusers']
def test_login(self):
url = '/api/login'
data = {'email': 'none@none.com', 'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, HTTP_200_OK)
data = {'email': 'none@none.com', 'password': '4321'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'Invalid email or password', status_code=403)
data = {'email': 'bad@none.com', 'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'Invalid email or password', status_code=403)
data = {'password': '4321'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'email', status_code=400)
data = {'email': 'none@none.com'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'password', status_code=400)
user = User.objects.get(email="none@none.com")
user.is_active = False
user.save()
data = {'email': 'none@none.com', 'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'Account is disabled', status_code=403)
def test_logout(self):
self.client.login(email='none@none.com', password='1234')
response = self.client.post('/api/logout')
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
# since we're not logged in, we should get a 403 response
response = self.client.get('/api/me', format='json')
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
class PasswordResetTest(APITestCase):
fixtures = ['testusers']
def test_reset_password(self):
# submit the request to reset the password
url = '/api/request_password_reset'
post_data = {'email': 'none@none.com'}
self.client.post(url, post_data, format='json')
self.assertEqual(len(mail.outbox), 1)
self.assertIn('reset your password', mail.outbox[0].body)
# capture the reset token from the email
capture = search('https://flashy.cards/app/resetpassword/(\d+)/(.*)',
mail.outbox[0].body)
patch_data = {'new_password': '4321'}
patch_data['uid'] = capture.group(1)
reset_token = capture.group(2)
# try to reset the password with the wrong reset token
patch_data['token'] = 'wrong_token'
url = '/api/reset_password'
response = self.client.post(url, patch_data, format='json')
self.assertContains(response, 'Could not verify reset token', status_code=400)
# try to reset the password with the correct token
patch_data['token'] = reset_token
response = self.client.post(url, patch_data, format='json')
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
user = User.objects.get(id=patch_data['uid'])
assert user.check_password(patch_data['new_password'])
class RegistrationTest(APITestCase):
def test_create_account(self):
url = '/api/register'
# missing password
data = {'email': 'none@none.com'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'password', status_code=400)
# missing email
data = {'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertContains(response, 'email', status_code=400)
# create a user
data = {'email': 'none@none.com', 'password': '1234'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, HTTP_201_CREATED)
# user should not be confirmed
user = User.objects.get(email="none@none.com")
self.assertFalse(user.is_confirmed)
# check that the confirmation key was sent
self.assertEqual(len(mail.outbox), 1)
self.assertIn(user.confirmation_key, mail.outbox[0].body)
# log the user out
self.client.logout()
# log the user in with their registered credentials
self.client.login(email='none@none.com', password='1234')
# try activating with an invalid key
url = '/api/me'
response = self.client.patch(url, {'confirmation_key': 'NOT A KEY'})
self.assertContains(response, 'confirmation_key is invalid', status_code=400)
# try activating with the valid key
response = self.client.patch(url, {'confirmation_key': user.confirmation_key})
self.assertTrue(response.data['is_confirmed'])
class ProfileViewTest(APITestCase):
fixtures = ['testusers']
def test_get_me(self):
url = '/api/me'
response = self.client.get(url, format='json')
# since we're not logged in, we shouldn't be able to see this
self.assertEqual(response.status_code, 403)
self.client.login(email='none@none.com', password='1234')
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, HTTP_200_OK)
class PasswordChangeTest(APITestCase):
fixtures = ['testusers']
def test_change_password(self):
url = '/api/me'
user = User.objects.get(email='none@none.com')
self.assertTrue(user.check_password('1234'))
response = self.client.patch(url, {'new_password': '4321', 'old_password': '1234'}, format='json')
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
self.client.login(email='none@none.com', password='1234')
response = self.client.patch(url, {'new_password': '4321'}, format='json')
self.assertContains(response, 'old_password is required', status_code=400)
response = self.client.patch(url, {'new_password': '4321', 'old_password': '4321'}, format='json')
self.assertContains(response, 'old_password is incorrect', status_code=400)
response = self.client.patch(url, {'new_password': '4321', 'old_password': '1234'}, format='json')
self.assertEqual(response.status_code, 200)
user = User.objects.get(email='none@none.com')
self.assertFalse(user.check_password('1234'))
self.assertTrue(user.check_password('4321'))
class DeleteUserTest(APITestCase):
fixtures = ['testusers']
def test_delete_user(self):
url = '/api/me'
user = User.objects.get(email='none@none.com')
self.client.login(email='none@none.com', password='1234')
self.client.delete(url)
self.assertFalse(User.objects.filter(email='none@none.com').exists())
class FlashcardDetailTest(APITestCase):
fixtures = ['testusers', 'testsections']
def setUp(self):
section = Section.objects.get(pk=1)
user = User.objects.get(email='none@none.com')
section.enroll(user)
self.inaccessible_flashcard = Flashcard(text="you can't see me!", section=Section.objects.get(pk=2),
material_date=now(), author=user)
self.inaccessible_flashcard.save()
self.flashcard = Flashcard(text="jason", section=section, material_date=now(), author=user)
self.flashcard.save()
def test_edit_flashcard(self):
self.client.login(email='none@none.com', password='1234')
user = User.objects.get(email='none@none.com')
user.sections.add(Section.objects.get(pk=1))
user.save()
def test_create_flashcard(self):
self.client.login(email='none@none.com', password='1234')
user = User.objects.get(email='none@none.com')
user.sections.add(Section.objects.get(pk=1))
user.save()
data = {'text': 'this is a flashcard',
'material_date': now(),
'mask': '[]',
'section': '1',
'previous': None}
response = self.client.post("/api/flashcards/", data, format="json")
self.assertEqual(response.status_code, HTTP_201_CREATED)
self.assertEqual(response.data['text'], data['text'])
self.assertTrue(Flashcard.objects.filter(section__pk=1, text=data['text']).exists())
def test_get_flashcard(self):
self.client.login(email='none@none.com', password='1234')
response = self.client.get("/api/flashcards/%d/" % self.flashcard.id, format="json")
self.assertEqual(response.status_code, HTTP_200_OK)
self.assertEqual(response.data["text"], "jason")
class SectionViewSetTest(APITestCase):
fixtures = ['testusers', 'testsections']
def setUp(self):
self.client.login(email='none@none.com', password='1234')
self.user = User.objects.get(email='none@none.com')
self.flashcard = Flashcard(text="jason", section=Section.objects.get(pk=1), material_date=now(),
author=self.user)
self.flashcard.save()
self.section = Section.objects.get(pk=1)
def test_list_sections(self):
response = self.client.get("/api/sections/", format="json")
self.assertEqual(response.status_code, HTTP_200_OK)
def test_section_enroll(self):
section = self.section
self.assertFalse(self.user.sections.filter(pk=section.pk))
# test enrolling in a section without a whitelist
response = self.client.post('/api/sections/%d/enroll/' % section.pk)
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
self.assertTrue(self.user.sections.filter(pk=section.pk).exists())
section = Section.objects.get(pk=2)
WhitelistedAddress.objects.create(email='bad@none.com', section=section)
# test enrolling in a section when not on the whitelist
response = self.client.post('/api/sections/%d/enroll/' % section.pk)
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
self.assertFalse(self.user.sections.filter(pk=section.pk).exists())
WhitelistedAddress.objects.create(email=self.user.email, section=section)
# test enrolling in a section when on the whitelist
response = self.client.post('/api/sections/%d/enroll/' % section.pk)
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
self.assertTrue(self.user.sections.filter(pk=section.pk).exists())
def test_section_drop(self):
section = self.section
# test dropping a section that the user isn't in
response = self.client.post('/api/sections/%d/drop/' % section.pk)
self.assertEqual(response.status_code, 400)
self.user.sections.add(section)
self.assertTrue(self.user.sections.filter(pk=section.pk).exists())
# test dropping a section that the user is in
response = self.client.post('/api/sections/%d/drop/' % section.pk)
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
self.assertFalse(self.user.sections.filter(pk=section.pk).exists())
def test_section_flashcards(self):
# test to get flashcards for section 1
response = self.client.get('/api/sections/1/flashcards/')
self.assertEqual(response.status_code, HTTP_200_OK)
# test: Making FlashcardHide object, so no card should be seen.
flashcard_hide = FlashcardHide(user=self.user, flashcard=self.flashcard)
flashcard_hide.save()
response = self.client.get('/api/sections/1/flashcards/')
self.assertEqual(response.status_code, HTTP_200_OK)
self.assertEqual(response.content, '[]')
def test_section_search(self):
response = self.client.get('/api/sections/search/?q=Kramer')
self.assertEqual(response.status_code, HTTP_200_OK)
def test_section_deck(self):
self.user.sections.add(self.section)
self.user.save()
response = self.client.get('/api/sections/1/deck/')
self.assertEqual(response.status_code, HTTP_200_OK)
def test_section_feed(self):
response = self.client.get('/api/sections/1/feed/')
self.assertEqual(response.status_code, HTTP_200_OK)
self.assertEqual(response.data[0]['id'], 1)
def test_section_ordered_deck(self):
self.user.sections.add(self.section)
self.user.save()
response = self.client.get('/api/sections/1/ordered_deck/')
self.assertEqual(response.status_code, HTTP_200_OK)
class FlashcardViewSetTest(APITestCase):
fixtures = ['testusers', 'testsections']
def setUp(self):
self.client.login(email='none@none.com', password='1234')
self.user = User.objects.get(email='none@none.com')
self.section = Section.objects.get(pk=1)
self.section.enroll(self.user)
self.flashcard = Flashcard(text="jason", section=self.section, material_date=now(),
author=self.user)
self.flashcard.save()
self.inaccessible_flashcard = Flashcard(text="can't touch this", section=Section.objects.get(pk=2),
material_date=now(), author=self.user)
self.inaccessible_flashcard.save()
def test_hide_flashcard(self):
url = '/api/flashcards/1/hide/'
response = self.client.post(url, format='json')
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
response = self.client.post('/api/flashcards/%d/hide/' % self.inaccessible_flashcard.pk, format='json')
# This should fail because the user is not enrolled in section id 2
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
def test_unhide_flashcard(self):
url = '/api/flashcards/1/unhide/'
flashcard_hide = FlashcardHide(user=self.user, flashcard=self.flashcard)
flashcard_hide.save()
response = self.client.post(url, format='json')
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
response = self.client.post('/api/flashcards/%d/unhide/' % self.inaccessible_flashcard.pk, format='json')
# This should fail because the user is not enrolled in section id 2
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)