Commit ec4e278d0171869e99f8ca8ed850188cca693f7d

Authored by Andrew Buss
1 parent 1519ec887e
Exists in master

Added tests for section enroll + drop

Showing 4 changed files with 93 additions and 15 deletions Side-by-side Diff

flashcards/fixtures/testsections.json View file @ ec4e278
  1 +[
  2 + {
  3 + "fields": {
  4 + "instructor": "Goldstein, Paul S",
  5 + "course_title": "Arch Workshop: Adv. Labwork",
  6 + "course_num": "180",
  7 + "department": "Anthropological Archeology",
  8 + "quarter": "SP15"
  9 + },
  10 + "model": "flashcards.section",
  11 + "pk": 1
  12 + },
  13 + {
  14 + "fields": {
  15 + "instructor": "Kramer, Elise A.",
  16 + "course_title": "Debating Multiculturalism",
  17 + "course_num": "23",
  18 + "department": "Anthropology",
  19 + "quarter": "SP15"
  20 + },
  21 + "model": "flashcards.section",
  22 + "pk": 2
  23 + },
  24 + {
  25 + "fields": {
  26 + "instructor": "Pedersen, David",
  27 + "course_title": "Debating Multiculturalism",
  28 + "course_num": "23",
  29 + "department": "Anthropology",
  30 + "quarter": "SP15"
  31 + },
  32 + "model": "flashcards.section",
  33 + "pk": 3
  34 + }
  35 +]
flashcards/models.py View file @ ec4e278
... ... @@ -169,7 +169,6 @@
169 169 """
170 170 :return: whether the user is on the waitlist for this section
171 171 """
172   - assert user is User
173 172 return self.whitelist.filter(email=user.email).exists()
174 173  
175 174 class Meta:
flashcards/tests/test_api.py View file @ ec4e278
1 1 from django.core import mail
2   -from flashcards.models import User, Section, Flashcard
3   -from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK, HTTP_401_UNAUTHORIZED, \
4   - HTTP_403_FORBIDDEN
  2 +from flashcards.models import User, Section, Flashcard, WhitelistedAddress
  3 +from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK, HTTP_403_FORBIDDEN
5 4 from rest_framework.test import APITestCase
6 5 from re import search
7 6 from django.utils.timezone import now
8 7  
9 8  
10 9  
11 10  
12 11  
... ... @@ -179,23 +178,69 @@
179 178  
180 179  
181 180 class FlashcardDetailTest(APITestCase):
182   - fixtures = ['testusers']
  181 + fixtures = ['testusers', 'testsections']
183 182  
184 183 def setUp(self):
185   - section = Section(department="cse", course_num="5", course_title="cool course", instructor="gary",
186   - quarter="fa15")
187   - section.save()
  184 + section = Section.objects.get(pk=1)
  185 + user = User.objects.get(email='none@none.com')
188 186  
189   - user = User.objects.create_user(email="jacob@aaa.com", password="top_secret")
190   - user.save()
191   -
192 187 self.flashcard = Flashcard(text="jason", section=section, material_date=now(), author=user)
193 188 self.flashcard.save()
194 189  
195   -
196 190 def test_get_flashcard(self):
197   - assert self.client.login(email='none@none.com', password='1234')
  191 + self.client.login(email='none@none.com', password='1234')
198 192 response = self.client.get("/api/flashcards/%d/" % self.flashcard.id, format="json")
199 193 self.assertEqual(response.status_code, HTTP_200_OK)
200 194 self.assertEqual(response.data["text"], "jason")
  195 +
  196 +
  197 +class SectionViewSetTest(APITestCase):
  198 + fixtures = ['testusers', 'testsections']
  199 +
  200 + def setUp(self):
  201 + self.client.login(email='none@none.com', password='1234')
  202 + self.user = User.objects.get(email='none@none.com')
  203 +
  204 + def test_list_sections(self):
  205 + response = self.client.get("/api/sections/", format="json")
  206 + self.assertEqual(response.status_code, HTTP_200_OK)
  207 +
  208 + def test_section_enroll(self):
  209 + section = Section.objects.get(pk=1)
  210 + self.assertFalse(self.user.sections.filter(pk=section.pk))
  211 +
  212 + # test enrolling in a section without a whitelist
  213 + response = self.client.post('/api/sections/%d/enroll/' % section.pk)
  214 + self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
  215 + self.assertTrue(self.user.sections.filter(pk=section.pk).exists())
  216 +
  217 + section = Section.objects.get(pk=2)
  218 + WhitelistedAddress.objects.create(email='bad@none.com', section=section)
  219 +
  220 + # test enrolling in a section when not on the whitelist
  221 + response = self.client.post('/api/sections/%d/enroll/' % section.pk)
  222 + self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
  223 + self.assertFalse(self.user.sections.filter(pk=section.pk).exists())
  224 +
  225 + WhitelistedAddress.objects.create(email=self.user.email, section=section)
  226 +
  227 + # test enrolling in a section when on the whitelist
  228 + response = self.client.post('/api/sections/%d/enroll/' % section.pk)
  229 + self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
  230 + self.assertTrue(self.user.sections.filter(pk=section.pk).exists())
  231 +
  232 + def test_section_drop(self):
  233 + section = Section.objects.get(pk=3)
  234 +
  235 + # test dropping a section that the user isn't in
  236 + response = self.client.post('/api/sections/%d/drop/' % section.pk)
  237 + self.assertEqual(response.status_code, 400)
  238 +
  239 + self.user.sections.add(section)
  240 + self.assertTrue(self.user.sections.filter(pk=section.pk).exists())
  241 +
  242 + # test dropping a section that the user is in
  243 + response = self.client.post('/api/sections/%d/drop/' % section.pk)
  244 + self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
  245 + self.assertFalse(self.user.sections.filter(pk=section.pk).exists())
flashcards/views.py View file @ ec4e278
... ... @@ -37,9 +37,9 @@
37 37 """
38 38 section = self.get_object()
39 39 if request.user.sections.filter(pk=section.pk).exists():
40   - return ValidationError("You are already in this section.")
  40 + raise ValidationError("You are already in this section.")
41 41 if section.is_whitelisted() and not section.is_user_on_whitelist(request.user):
42   - return PermissionDenied("You must be on the whitelist to add this section.")
  42 + raise PermissionDenied("You must be on the whitelist to add this section.")
43 43 request.user.sections.add(section)
44 44 return Response(status=HTTP_204_NO_CONTENT)
45 45