From ec4e278d0171869e99f8ca8ed850188cca693f7d Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Sun, 10 May 2015 15:56:37 -0700 Subject: [PATCH] Added tests for section enroll + drop --- flashcards/fixtures/testsections.json | 35 ++++++++++++++++++ flashcards/models.py | 1 - flashcards/tests/test_api.py | 68 ++++++++++++++++++++++++++++------- flashcards/views.py | 4 +-- 4 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 flashcards/fixtures/testsections.json diff --git a/flashcards/fixtures/testsections.json b/flashcards/fixtures/testsections.json new file mode 100644 index 0000000..edb9be3 --- /dev/null +++ b/flashcards/fixtures/testsections.json @@ -0,0 +1,35 @@ +[ + { + "fields": { + "instructor": "Goldstein, Paul S", + "course_title": "Arch Workshop: Adv. Labwork", + "course_num": "180", + "department": "Anthropological Archeology", + "quarter": "SP15" + }, + "model": "flashcards.section", + "pk": 1 + }, + { + "fields": { + "instructor": "Kramer, Elise A.", + "course_title": "Debating Multiculturalism", + "course_num": "23", + "department": "Anthropology", + "quarter": "SP15" + }, + "model": "flashcards.section", + "pk": 2 + }, + { + "fields": { + "instructor": "Pedersen, David", + "course_title": "Debating Multiculturalism", + "course_num": "23", + "department": "Anthropology", + "quarter": "SP15" + }, + "model": "flashcards.section", + "pk": 3 + } +] \ No newline at end of file diff --git a/flashcards/models.py b/flashcards/models.py index 3911424..89ef439 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -169,7 +169,6 @@ class Section(Model): """ :return: whether the user is on the waitlist for this section """ - assert user is User return self.whitelist.filter(email=user.email).exists() class Meta: diff --git a/flashcards/tests/test_api.py b/flashcards/tests/test_api.py index 0bd9f21..fa82a86 100644 --- a/flashcards/tests/test_api.py +++ b/flashcards/tests/test_api.py @@ -1,7 +1,6 @@ from django.core import mail -from flashcards.models import User, Section, Flashcard -from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK, HTTP_401_UNAUTHORIZED, \ - HTTP_403_FORBIDDEN +from flashcards.models import User, Section, Flashcard, WhitelistedAddress +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 @@ -179,23 +178,68 @@ class DeleteUserTest(APITestCase): class FlashcardDetailTest(APITestCase): - fixtures = ['testusers'] + fixtures = ['testusers', 'testsections'] def setUp(self): - section = Section(department="cse", course_num="5", course_title="cool course", instructor="gary", - quarter="fa15") - section.save() - - user = User.objects.create_user(email="jacob@aaa.com", password="top_secret") - user.save() + section = Section.objects.get(pk=1) + user = User.objects.get(email='none@none.com') self.flashcard = Flashcard(text="jason", section=section, material_date=now(), author=user) self.flashcard.save() - def test_get_flashcard(self): - assert self.client.login(email='none@none.com', password='1234') + 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') + + 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 = Section.objects.get(pk=1) + 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 = Section.objects.get(pk=3) + + # 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()) \ No newline at end of file diff --git a/flashcards/views.py b/flashcards/views.py index d163689..1c8fe0c 100644 --- a/flashcards/views.py +++ b/flashcards/views.py @@ -37,9 +37,9 @@ class SectionViewSet(ReadOnlyModelViewSet): """ section = self.get_object() if request.user.sections.filter(pk=section.pk).exists(): - return ValidationError("You are already in this section.") + raise ValidationError("You are already in this section.") if section.is_whitelisted() and not section.is_user_on_whitelist(request.user): - return PermissionDenied("You must be on the whitelist to add this section.") + raise PermissionDenied("You must be on the whitelist to add this section.") request.user.sections.add(section) return Response(status=HTTP_204_NO_CONTENT) -- 1.9.1