Commit 889c3c1048a71ea916dd416ac92311d33be44ee8
1 parent
f15f989dd0
Exists in
master
tests were wrong
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
flashcards/tests/test_api.py
View file @
889c3c1
from django.core import mail | 1 | 1 | from django.core import mail | |
from flashcards.models import * | 2 | 2 | from flashcards.models import * | |
from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK, HTTP_403_FORBIDDEN | 3 | 3 | from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK, HTTP_403_FORBIDDEN | |
from rest_framework.test import APITestCase | 4 | 4 | from rest_framework.test import APITestCase | |
from re import search | 5 | 5 | from re import search | |
import datetime | 6 | 6 | import datetime | |
from django.utils.timezone import now | 7 | 7 | from django.utils.timezone import now | |
from flashcards.validators import FlashcardMask | 8 | 8 | from flashcards.validators import FlashcardMask | |
from flashcards.serializers import FlashcardSerializer | 9 | 9 | from flashcards.serializers import FlashcardSerializer | |
10 | 10 | |||
11 | 11 | |||
class LoginTests(APITestCase): | 12 | 12 | class LoginTests(APITestCase): | |
fixtures = ['testusers'] | 13 | 13 | fixtures = ['testusers'] | |
14 | 14 | |||
def test_login(self): | 15 | 15 | def test_login(self): | |
url = '/api/login/' | 16 | 16 | url = '/api/login/' | |
data = {'email': 'none@none.com', 'password': '1234'} | 17 | 17 | data = {'email': 'none@none.com', 'password': '1234'} | |
response = self.client.post(url, data, format='json') | 18 | 18 | response = self.client.post(url, data, format='json') | |
self.assertEqual(response.status_code, HTTP_200_OK) | 19 | 19 | self.assertEqual(response.status_code, HTTP_200_OK) | |
20 | 20 | |||
data = {'email': 'none@none.com', 'password': '4321'} | 21 | 21 | data = {'email': 'none@none.com', 'password': '4321'} | |
response = self.client.post(url, data, format='json') | 22 | 22 | response = self.client.post(url, data, format='json') | |
self.assertContains(response, 'Invalid email or password', status_code=403) | 23 | 23 | self.assertContains(response, 'Invalid email or password', status_code=403) | |
24 | 24 | |||
data = {'email': 'bad@none.com', 'password': '1234'} | 25 | 25 | data = {'email': 'bad@none.com', 'password': '1234'} | |
response = self.client.post(url, data, format='json') | 26 | 26 | response = self.client.post(url, data, format='json') | |
self.assertContains(response, 'Invalid email or password', status_code=403) | 27 | 27 | self.assertContains(response, 'Invalid email or password', status_code=403) | |
28 | 28 | |||
data = {'password': '4321'} | 29 | 29 | data = {'password': '4321'} | |
response = self.client.post(url, data, format='json') | 30 | 30 | response = self.client.post(url, data, format='json') | |
self.assertContains(response, 'email', status_code=400) | 31 | 31 | self.assertContains(response, 'email', status_code=400) | |
32 | 32 | |||
data = {'email': 'none@none.com'} | 33 | 33 | data = {'email': 'none@none.com'} | |
response = self.client.post(url, data, format='json') | 34 | 34 | response = self.client.post(url, data, format='json') | |
self.assertContains(response, 'password', status_code=400) | 35 | 35 | self.assertContains(response, 'password', status_code=400) | |
36 | 36 | |||
user = User.objects.get(email="none@none.com") | 37 | 37 | user = User.objects.get(email="none@none.com") | |
user.is_active = False | 38 | 38 | user.is_active = False | |
user.save() | 39 | 39 | user.save() | |
40 | 40 | |||
data = {'email': 'none@none.com', 'password': '1234'} | 41 | 41 | data = {'email': 'none@none.com', 'password': '1234'} | |
response = self.client.post(url, data, format='json') | 42 | 42 | response = self.client.post(url, data, format='json') | |
self.assertContains(response, 'Account is disabled', status_code=403) | 43 | 43 | self.assertContains(response, 'Account is disabled', status_code=403) | |
44 | 44 | |||
def test_logout(self): | 45 | 45 | def test_logout(self): | |
self.client.login(email='none@none.com', password='1234') | 46 | 46 | self.client.login(email='none@none.com', password='1234') | |
response = self.client.post('/api/logout/') | 47 | 47 | response = self.client.post('/api/logout/') | |
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | 48 | 48 | self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | |
49 | 49 | |||
# since we're not logged in, we should get a 403 response | 50 | 50 | # since we're not logged in, we should get a 403 response | |
response = self.client.get('/api/me/', format='json') | 51 | 51 | response = self.client.get('/api/me/', format='json') | |
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN) | 52 | 52 | self.assertEqual(response.status_code, HTTP_403_FORBIDDEN) | |
53 | 53 | |||
54 | 54 | |||
class PasswordResetTest(APITestCase): | 55 | 55 | class PasswordResetTest(APITestCase): | |
fixtures = ['testusers'] | 56 | 56 | fixtures = ['testusers'] | |
57 | 57 | |||
def test_reset_password(self): | 58 | 58 | def test_reset_password(self): | |
# submit the request to reset the password | 59 | 59 | # submit the request to reset the password | |
url = '/api/request_password_reset/' | 60 | 60 | url = '/api/request_password_reset/' | |
post_data = {'email': 'none@none.com'} | 61 | 61 | post_data = {'email': 'none@none.com'} | |
self.client.post(url, post_data, format='json') | 62 | 62 | self.client.post(url, post_data, format='json') | |
self.assertEqual(len(mail.outbox), 1) | 63 | 63 | self.assertEqual(len(mail.outbox), 1) | |
self.assertIn('reset your password', mail.outbox[0].body) | 64 | 64 | self.assertIn('reset your password', mail.outbox[0].body) | |
65 | 65 | |||
# capture the reset token from the email | 66 | 66 | # capture the reset token from the email | |
capture = search('https://flashy.cards/app/resetpassword/(\d+)/(.*)', | 67 | 67 | capture = search('https://flashy.cards/app/resetpassword/(\d+)/(.*)', | |
mail.outbox[0].body) | 68 | 68 | mail.outbox[0].body) | |
patch_data = {'new_password': '4321'} | 69 | 69 | patch_data = {'new_password': '4321'} | |
patch_data['uid'] = capture.group(1) | 70 | 70 | patch_data['uid'] = capture.group(1) | |
reset_token = capture.group(2) | 71 | 71 | reset_token = capture.group(2) | |
72 | 72 | |||
# try to reset the password with the wrong reset token | 73 | 73 | # try to reset the password with the wrong reset token | |
patch_data['token'] = 'wrong_token' | 74 | 74 | patch_data['token'] = 'wrong_token' | |
url = '/api/reset_password/' | 75 | 75 | url = '/api/reset_password/' | |
response = self.client.post(url, patch_data, format='json') | 76 | 76 | response = self.client.post(url, patch_data, format='json') | |
self.assertContains(response, 'Could not verify reset token', status_code=400) | 77 | 77 | self.assertContains(response, 'Could not verify reset token', status_code=400) | |
78 | 78 | |||
# try to reset the password with the correct token | 79 | 79 | # try to reset the password with the correct token | |
patch_data['token'] = reset_token | 80 | 80 | patch_data['token'] = reset_token | |
response = self.client.post(url, patch_data, format='json') | 81 | 81 | response = self.client.post(url, patch_data, format='json') | |
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | 82 | 82 | self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | |
user = User.objects.get(id=patch_data['uid']) | 83 | 83 | user = User.objects.get(id=patch_data['uid']) | |
assert user.check_password(patch_data['new_password']) | 84 | 84 | assert user.check_password(patch_data['new_password']) | |
85 | 85 | |||
86 | 86 | |||
class RegistrationTest(APITestCase): | 87 | 87 | class RegistrationTest(APITestCase): | |
def test_create_account(self): | 88 | 88 | def test_create_account(self): | |
url = '/api/register/' | 89 | 89 | url = '/api/register/' | |
90 | 90 | |||
# missing password | 91 | 91 | # missing password | |
data = {'email': 'none@none.com'} | 92 | 92 | data = {'email': 'none@none.com'} | |
response = self.client.post(url, data, format='json') | 93 | 93 | response = self.client.post(url, data, format='json') | |
self.assertContains(response, 'password', status_code=400) | 94 | 94 | self.assertContains(response, 'password', status_code=400) | |
95 | 95 | |||
# missing email | 96 | 96 | # missing email | |
data = {'password': '1234'} | 97 | 97 | data = {'password': '1234'} | |
response = self.client.post(url, data, format='json') | 98 | 98 | response = self.client.post(url, data, format='json') | |
self.assertContains(response, 'email', status_code=400) | 99 | 99 | self.assertContains(response, 'email', status_code=400) | |
100 | 100 | |||
# create a user | 101 | 101 | # create a user | |
data = {'email': 'none@none.com', 'password': '1234'} | 102 | 102 | data = {'email': 'none@none.com', 'password': '1234'} | |
response = self.client.post(url, data, format='json') | 103 | 103 | response = self.client.post(url, data, format='json') | |
self.assertEqual(response.status_code, HTTP_201_CREATED) | 104 | 104 | self.assertEqual(response.status_code, HTTP_201_CREATED) | |
105 | 105 | |||
# user should not be confirmed | 106 | 106 | # user should not be confirmed | |
user = User.objects.get(email="none@none.com") | 107 | 107 | user = User.objects.get(email="none@none.com") | |
self.assertFalse(user.is_confirmed) | 108 | 108 | self.assertFalse(user.is_confirmed) | |
109 | 109 | |||
# check that the confirmation key was sent | 110 | 110 | # check that the confirmation key was sent | |
self.assertEqual(len(mail.outbox), 1) | 111 | 111 | self.assertEqual(len(mail.outbox), 1) | |
self.assertIn(user.confirmation_key, mail.outbox[0].body) | 112 | 112 | self.assertIn(user.confirmation_key, mail.outbox[0].body) | |
113 | 113 | |||
# log the user out | 114 | 114 | # log the user out | |
self.client.logout() | 115 | 115 | self.client.logout() | |
116 | 116 | |||
# log the user in with their registered credentials | 117 | 117 | # log the user in with their registered credentials | |
self.client.login(email='none@none.com', password='1234') | 118 | 118 | self.client.login(email='none@none.com', password='1234') | |
119 | 119 | |||
# try activating with an invalid key | 120 | 120 | # try activating with an invalid key | |
121 | 121 | |||
url = '/api/me/' | 122 | 122 | url = '/api/me/' | |
response = self.client.patch(url, {'confirmation_key': 'NOT A KEY'}) | 123 | 123 | response = self.client.patch(url, {'confirmation_key': 'NOT A KEY'}) | |
self.assertContains(response, 'confirmation_key is invalid', status_code=400) | 124 | 124 | self.assertContains(response, 'confirmation_key is invalid', status_code=400) | |
125 | 125 | |||
# try activating with the valid key | 126 | 126 | # try activating with the valid key | |
response = self.client.patch(url, {'confirmation_key': user.confirmation_key}) | 127 | 127 | response = self.client.patch(url, {'confirmation_key': user.confirmation_key}) | |
self.assertTrue(response.data['is_confirmed']) | 128 | 128 | self.assertTrue(response.data['is_confirmed']) | |
129 | 129 | |||
130 | 130 | |||
class ProfileViewTest(APITestCase): | 131 | 131 | class ProfileViewTest(APITestCase): | |
fixtures = ['testusers'] | 132 | 132 | fixtures = ['testusers'] | |
133 | 133 | |||
def test_get_me(self): | 134 | 134 | def test_get_me(self): | |
url = '/api/me/' | 135 | 135 | url = '/api/me/' | |
response = self.client.get(url, format='json') | 136 | 136 | response = self.client.get(url, format='json') | |
# since we're not logged in, we shouldn't be able to see this | 137 | 137 | # since we're not logged in, we shouldn't be able to see this | |
self.assertEqual(response.status_code, 403) | 138 | 138 | self.assertEqual(response.status_code, 403) | |
139 | 139 | |||
self.client.login(email='none@none.com', password='1234') | 140 | 140 | self.client.login(email='none@none.com', password='1234') | |
response = self.client.get(url, format='json') | 141 | 141 | response = self.client.get(url, format='json') | |
self.assertEqual(response.status_code, HTTP_200_OK) | 142 | 142 | self.assertEqual(response.status_code, HTTP_200_OK) | |
143 | 143 | |||
144 | 144 | |||
class UserSectionsTest(APITestCase): | 145 | 145 | class UserSectionsTest(APITestCase): | |
fixtures = ['testusers', 'testsections'] | 146 | 146 | fixtures = ['testusers', 'testsections'] | |
147 | 147 | |||
def setUp(self): | 148 | 148 | def setUp(self): | |
self.user = User.objects.get(pk=1) | 149 | 149 | self.user = User.objects.get(pk=1) | |
self.client.login(email='none@none.com', password='1234') | 150 | 150 | self.client.login(email='none@none.com', password='1234') | |
self.section = Section.objects.get(pk=1) | 151 | 151 | self.section = Section.objects.get(pk=1) | |
self.section.enroll(self.user) | 152 | 152 | self.section.enroll(self.user) | |
153 | 153 | |||
def test_get_user_sections(self): | 154 | 154 | def test_get_user_sections(self): | |
response = self.client.get('/api/me/sections/', format='json') | 155 | 155 | response = self.client.get('/api/me/sections/', format='json') | |
self.assertEqual(response.status_code, 200) | 156 | 156 | self.assertEqual(response.status_code, 200) | |
self.assertContains(response, 'Goldstein') | 157 | 157 | self.assertContains(response, 'Goldstein') | |
158 | 158 | |||
159 | 159 | |||
class PasswordChangeTest(APITestCase): | 160 | 160 | class PasswordChangeTest(APITestCase): | |
fixtures = ['testusers'] | 161 | 161 | fixtures = ['testusers'] | |
162 | 162 | |||
def test_change_password(self): | 163 | 163 | def test_change_password(self): | |
url = '/api/me/' | 164 | 164 | url = '/api/me/' | |
user = User.objects.get(email='none@none.com') | 165 | 165 | user = User.objects.get(email='none@none.com') | |
self.assertTrue(user.check_password('1234')) | 166 | 166 | self.assertTrue(user.check_password('1234')) | |
167 | 167 | |||
response = self.client.patch(url, {'new_password': '4321', 'old_password': '1234'}, format='json') | 168 | 168 | response = self.client.patch(url, {'new_password': '4321', 'old_password': '1234'}, format='json') | |
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN) | 169 | 169 | self.assertEqual(response.status_code, HTTP_403_FORBIDDEN) | |
170 | 170 | |||
self.client.login(email='none@none.com', password='1234') | 171 | 171 | self.client.login(email='none@none.com', password='1234') | |
response = self.client.patch(url, {'new_password': '4321'}, format='json') | 172 | 172 | response = self.client.patch(url, {'new_password': '4321'}, format='json') | |
self.assertContains(response, 'old_password is required', status_code=400) | 173 | 173 | self.assertContains(response, 'old_password is required', status_code=400) | |
174 | 174 | |||
response = self.client.patch(url, {'new_password': '4321', 'old_password': '4321'}, format='json') | 175 | 175 | response = self.client.patch(url, {'new_password': '4321', 'old_password': '4321'}, format='json') | |
self.assertContains(response, 'old_password is incorrect', status_code=400) | 176 | 176 | self.assertContains(response, 'old_password is incorrect', status_code=400) | |
177 | 177 | |||
response = self.client.patch(url, {'new_password': '4321', 'old_password': '1234'}, format='json') | 178 | 178 | response = self.client.patch(url, {'new_password': '4321', 'old_password': '1234'}, format='json') | |
self.assertEqual(response.status_code, 200) | 179 | 179 | self.assertEqual(response.status_code, 200) | |
user = User.objects.get(email='none@none.com') | 180 | 180 | user = User.objects.get(email='none@none.com') | |
181 | 181 | |||
self.assertFalse(user.check_password('1234')) | 182 | 182 | self.assertFalse(user.check_password('1234')) | |
self.assertTrue(user.check_password('4321')) | 183 | 183 | self.assertTrue(user.check_password('4321')) | |
184 | 184 | |||
185 | 185 | |||
class DeleteUserTest(APITestCase): | 186 | 186 | class DeleteUserTest(APITestCase): | |
fixtures = ['testusers'] | 187 | 187 | fixtures = ['testusers'] | |
188 | 188 | |||
def test_delete_user(self): | 189 | 189 | def test_delete_user(self): | |
url = '/api/me/' | 190 | 190 | url = '/api/me/' | |
user = User.objects.get(email='none@none.com') | 191 | 191 | user = User.objects.get(email='none@none.com') | |
192 | 192 | |||
self.client.login(email='none@none.com', password='1234') | 193 | 193 | self.client.login(email='none@none.com', password='1234') | |
self.client.delete(url) | 194 | 194 | self.client.delete(url) | |
self.assertFalse(User.objects.filter(email='none@none.com').exists()) | 195 | 195 | self.assertFalse(User.objects.filter(email='none@none.com').exists()) | |
196 | 196 | |||
197 | 197 | |||
class FlashcardDetailTest(APITestCase): | 198 | 198 | class FlashcardDetailTest(APITestCase): | |
fixtures = ['testusers', 'testsections'] | 199 | 199 | fixtures = ['testusers', 'testsections'] | |
200 | 200 | |||
def setUp(self): | 201 | 201 | def setUp(self): | |
self.section = Section.objects.get(pk=1) | 202 | 202 | self.section = Section.objects.get(pk=1) | |
self.user = User.objects.get(email='none@none.com') | 203 | 203 | self.user = User.objects.get(email='none@none.com') | |
self.section.enroll(self.user) | 204 | 204 | self.section.enroll(self.user) | |
self.inaccessible_flashcard = Flashcard(text="can't touch this!", section=Section.objects.get(pk=2), | 205 | 205 | self.inaccessible_flashcard = Flashcard(text="can't touch this!", section=Section.objects.get(pk=2), | |
author=self.user) | 206 | 206 | author=self.user) | |
self.inaccessible_flashcard.save() | 207 | 207 | self.inaccessible_flashcard.save() | |
self.flashcard = Flashcard(text="jason", section=self.section, author=self.user) | 208 | 208 | self.flashcard = Flashcard(text="jason", section=self.section, author=self.user) | |
self.flashcard.save() | 209 | 209 | self.flashcard.save() | |
#self.flashcard.add_to_deck(self.user) | 210 | 210 | #self.flashcard.add_to_deck(self.user) | |
self.client.login(email='none@none.com', password='1234') | 211 | 211 | self.client.login(email='none@none.com', password='1234') | |
212 | 212 | |||
def test_edit_flashcard(self): | 213 | 213 | def test_edit_flashcard(self): | |
user = self.user | 214 | 214 | user = self.user | |
flashcard = self.flashcard | 215 | 215 | flashcard = self.flashcard | |
url = "/api/flashcards/{}/".format(flashcard.pk) | 216 | 216 | url = "/api/flashcards/{}/".format(flashcard.pk) | |
data = {'text': 'new wow for the flashcard', | 217 | 217 | data = {'text': 'new wow for the flashcard', | |
'mask': '[[0,4]]'} | 218 | 218 | 'mask': '[[0,4]]'} | |
self.assertNotEqual(flashcard.text, data['text']) | 219 | 219 | self.assertNotEqual(flashcard.text, data['text']) | |
response = self.client.patch(url, data, format='json') | 220 | 220 | response = self.client.patch(url, data, format='json') | |
self.assertEqual(response.status_code, HTTP_200_OK) | 221 | 221 | self.assertEqual(response.status_code, HTTP_200_OK) | |
self.assertEqual(response.data['text'], data['text']) | 222 | 222 | self.assertEqual(response.data['text'], data['text']) | |
data = {'material_date': datetime.datetime(2015, 4, 12, 2, 2, 2), | 223 | 223 | data = {'material_date': datetime.datetime(2015, 4, 12, 2, 2, 2), | |
'mask': '[[1, 3]]'} | 224 | 224 | 'mask': '[[1, 3]]'} | |
user2 = User.objects.create(email='wow@wow.wow', password='wow') | 225 | 225 | user2 = User.objects.create(email='wow@wow.wow', password='wow') | |
user2.sections.add(self.section) | 226 | 226 | user2.sections.add(self.section) | |
user2.save() | 227 | 227 | user2.save() | |
UserFlashcard.objects.create(user=user2, flashcard=flashcard).save() | 228 | 228 | UserFlashcard.objects.create(user=user2, flashcard=flashcard).save() | |
response = self.client.patch(url, data, format='json') | 229 | 229 | response = self.client.patch(url, data, format='json') | |
serializer = FlashcardSerializer(data=response.data) | 230 | 230 | serializer = FlashcardSerializer(data=response.data) | |
serializer.is_valid(raise_exception=True) | 231 | 231 | serializer.is_valid(raise_exception=True) | |
self.assertEqual(response.status_code, HTTP_200_OK) | 232 | 232 | self.assertEqual(response.status_code, HTTP_200_OK) | |
# self.assertEqual(serializer.validated_data['material_date'], utc.localize(data['material_date'])) | 233 | 233 | # self.assertEqual(serializer.validated_data['material_date'], utc.localize(data['material_date'])) | |
self.assertEqual(serializer.validated_data['mask'], FlashcardMask([[1, 3]])) | 234 | 234 | self.assertEqual(serializer.validated_data['mask'], FlashcardMask([[1, 3]])) | |
data = {'mask': '[[3,6]]'} | 235 | 235 | data = {'mask': '[[3,6]]'} | |
response = self.client.patch(url, data, format='json') | 236 | 236 | response = self.client.patch(url, data, format='json') | |
user_flashcard = UserFlashcard.objects.get(user=user, flashcard=flashcard) | 237 | 237 | user_flashcard = UserFlashcard.objects.get(user=user, flashcard=flashcard) | |
self.assertEqual(response.status_code, HTTP_200_OK) | 238 | 238 | self.assertEqual(response.status_code, HTTP_200_OK) | |
self.assertEqual(user_flashcard.mask, FlashcardMask([[3, 6]])) | 239 | 239 | self.assertEqual(user_flashcard.mask, FlashcardMask([[3, 6]])) | |
240 | 240 | |||
def test_create_flashcard(self): | 241 | 241 | def test_create_flashcard(self): | |
data = {'text': 'this is a flashcard', | 242 | 242 | data = {'text': 'this is a flashcard', | |
'material_date': now(), | 243 | 243 | 'material_date': now(), | |
'mask': '[]', | 244 | 244 | 'mask': '[]', | |
'section': '1', | 245 | 245 | 'section': '1', | |
'previous': None} | 246 | 246 | 'previous': None} | |
response = self.client.post("/api/flashcards/", data, format="json") | 247 | 247 | response = self.client.post("/api/flashcards/", data, format="json") | |
self.assertEqual(response.status_code, HTTP_201_CREATED) | 248 | 248 | self.assertEqual(response.status_code, HTTP_201_CREATED) | |
self.assertEqual(response.data['text'], data['text']) | 249 | 249 | self.assertEqual(response.data['text'], data['text']) | |
self.assertTrue(Flashcard.objects.filter(section__pk=1, text=data['text']).exists()) | 250 | 250 | self.assertTrue(Flashcard.objects.filter(section__pk=1, text=data['text']).exists()) | |
251 | 251 | |||
def test_get_flashcard(self): | 252 | 252 | def test_get_flashcard(self): | |
response = self.client.get("/api/flashcards/%d/" % self.flashcard.id, format="json") | 253 | 253 | response = self.client.get("/api/flashcards/%d/" % self.flashcard.id, format="json") | |
self.assertEqual(response.status_code, HTTP_200_OK) | 254 | 254 | self.assertEqual(response.status_code, HTTP_200_OK) | |
self.assertEqual(response.data["text"], "jason") | 255 | 255 | self.assertEqual(response.data["text"], "jason") | |
256 | 256 | |||
def test_hide_flashcard(self): | 257 | 257 | def test_hide_flashcard(self): | |
response = self.client.post('/api/flashcards/%d/hide/' % self.flashcard.id, format='json') | 258 | 258 | response = self.client.post('/api/flashcards/%d/hide/' % self.flashcard.id, format='json') | |
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | 259 | 259 | self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | |
self.assertTrue(self.flashcard.is_hidden_from(self.user)) | 260 | 260 | self.assertTrue(self.flashcard.is_hidden_from(self.user)) | |
261 | 261 | |||
response = self.client.post('/api/flashcards/%d/hide/' % self.inaccessible_flashcard.pk, format='json') | 262 | 262 | 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 | 263 | 263 | # This should fail because the user is not enrolled in section id 2 | |
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN) | 264 | 264 | self.assertEqual(response.status_code, HTTP_403_FORBIDDEN) | |
265 | 265 | |||
def test_unhide_flashcard(self): | 266 | 266 | def test_unhide_flashcard(self): | |
self.flashcard.hide_from(self.user) | 267 | 267 | self.flashcard.hide_from(self.user) | |
268 | 268 | |||
response = self.client.post('/api/flashcards/%d/unhide/' % self.flashcard.id, format='json') | 269 | 269 | response = self.client.post('/api/flashcards/%d/unhide/' % self.flashcard.id, format='json') | |
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | 270 | 270 | self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | |
271 | 271 | |||
response = self.client.post('/api/flashcards/%d/unhide/' % self.inaccessible_flashcard.pk, format='json') | 272 | 272 | response = self.client.post('/api/flashcards/%d/unhide/' % self.inaccessible_flashcard.pk, format='json') | |
273 | 273 | |||
# This should fail because the user is not enrolled in section id 2 | 274 | 274 | # This should fail because the user is not enrolled in section id 2 | |
self.assertEqual(response.status_code, HTTP_403_FORBIDDEN) | 275 | 275 | self.assertEqual(response.status_code, HTTP_403_FORBIDDEN) | |
276 | 276 | |||
277 | 277 | |||
class SectionViewSetTest(APITestCase): | 278 | 278 | class SectionViewSetTest(APITestCase): | |
fixtures = ['testusers', 'testsections'] | 279 | 279 | fixtures = ['testusers', 'testsections'] | |
280 | 280 | |||
def setUp(self): | 281 | 281 | def setUp(self): | |
self.client.login(email='none@none.com', password='1234') | 282 | 282 | self.client.login(email='none@none.com', password='1234') | |
self.user = User.objects.get(email='none@none.com') | 283 | 283 | self.user = User.objects.get(email='none@none.com') | |
self.flashcard = Flashcard(text="jason", section=Section.objects.get(pk=1), material_date=now(), | 284 | 284 | self.flashcard = Flashcard(text="jason", section=Section.objects.get(pk=1), material_date=now(), | |
author=self.user) | 285 | 285 | author=self.user) | |
self.flashcard.save() | 286 | 286 | self.flashcard.save() | |
self.section = Section.objects.get(pk=1) | 287 | 287 | self.section = Section.objects.get(pk=1) | |
288 | 288 | |||
def test_list_sections(self): | 289 | 289 | def test_list_sections(self): | |
response = self.client.get("/api/sections/", format="json") | 290 | 290 | response = self.client.get("/api/sections/", format="json") | |
self.assertEqual(response.status_code, HTTP_200_OK) | 291 | 291 | self.assertEqual(response.status_code, HTTP_200_OK) | |
292 | 292 | |||
def test_section_enroll(self): | 293 | 293 | def test_section_enroll(self): | |
section = self.section | 294 | 294 | section = self.section | |
self.assertFalse(self.user.sections.filter(pk=section.pk)) | 295 | 295 | self.assertFalse(self.user.sections.filter(pk=section.pk)) | |
296 | 296 | |||
# test enrolling in a section without a whitelist | 297 | 297 | # test enrolling in a section without a whitelist | |
response = self.client.post('/api/sections/%d/enroll/' % section.pk) | 298 | 298 | response = self.client.post('/api/sections/%d/enroll/' % section.pk) | |
self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | 299 | 299 | self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) | |
self.assertTrue(self.user.sections.filter(pk=section.pk).exists()) | 300 | 300 | self.assertTrue(self.user.sections.filter(pk=section.pk).exists()) | |
301 | 301 | |||
section = Section.objects.get(pk=2) | 302 | 302 | section = Section.objects.get(pk=2) | |
WhitelistedAddress.objects.create(email='bad@none.com', section=section) | 303 | 303 | WhitelistedAddress.objects.create(email='bad@none.com', section=section) |