From 1c546f804757a9ccbef30c441591b15456f76752 Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Sun, 17 May 2015 07:06:26 -0700 Subject: [PATCH] cleaned up some tests --- flashcards/models.py | 11 ++++++ flashcards/tests/test_api.py | 80 +++++++++++++++----------------------------- flashcards/views.py | 7 ++-- 3 files changed, 41 insertions(+), 57 deletions(-) diff --git a/flashcards/models.py b/flashcards/models.py index 4259451..41ce37d 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -143,6 +143,17 @@ class Flashcard(Model): if self.is_hidden or self.flashcardhide_set.filter(user=user).exists(): return True return False + + def hide_from(self, user): + if self.is_in_deck(user): user.unpull(self) + obj, created = FlashcardHide.objects.get_or_create(user=user, flashcard=self) + if not created: + raise ValidationError("The card has already been hidden.") + obj.save() + + def is_in_deck(self, user): + return self.userflashcard_set.filter(user=user).exists() + def add_to_deck(self, user): if not user.is_in_section(self.section): raise PermissionDenied("You don't have the permission to add this card") diff --git a/flashcards/tests/test_api.py b/flashcards/tests/test_api.py index 1d634e1..1b3c36a 100644 --- a/flashcards/tests/test_api.py +++ b/flashcards/tests/test_api.py @@ -1,10 +1,9 @@ -from datetime import datetime - 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 +import datetime from django.utils.timezone import now from flashcards.validators import FlashcardMask from flashcards.serializers import FlashcardSerializer @@ -203,15 +202,15 @@ class FlashcardDetailTest(APITestCase): self.section = Section.objects.get(pk=1) self.user = User.objects.get(email='none@none.com') self.section.enroll(self.user) - self.inaccessible_flashcard = Flashcard(text="you can't see me!", section=Section.objects.get(pk=2), - material_date=now(), author=self.user) + self.inaccessible_flashcard = Flashcard(text="can't touch this!", section=Section.objects.get(pk=2), + author=self.user) self.inaccessible_flashcard.save() - self.flashcard = Flashcard(text="jason", section=self.section, material_date=now(), author=self.user) + self.flashcard = Flashcard(text="jason", section=self.section, author=self.user) self.flashcard.save() self.flashcard.add_to_deck(self.user) + self.client.login(email='none@none.com', password='1234') def test_edit_flashcard(self): - self.client.login(email='none@none.com', password='1234') user = self.user flashcard = self.flashcard url = "/api/flashcards/{}/".format(flashcard.pk) @@ -221,7 +220,7 @@ class FlashcardDetailTest(APITestCase): response = self.client.patch(url, data, format='json') self.assertEqual(response.status_code, HTTP_200_OK) self.assertEqual(response.data['text'], data['text']) - data = {'material_date': datetime(2015, 4, 12, 2, 2, 2), + data = {'material_date': datetime.datetime(2015, 4, 12, 2, 2, 2), 'mask': '[[1, 3]]'} user2 = User.objects.create(email='wow@wow.wow', password='wow') user2.sections.add(self.section) @@ -240,10 +239,6 @@ class FlashcardDetailTest(APITestCase): self.assertEqual(user_flashcard.mask, FlashcardMask([[3, 6]])) 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': '[]', @@ -255,11 +250,30 @@ class FlashcardDetailTest(APITestCase): 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") + def test_hide_flashcard(self): + response = self.client.post('/api/flashcards/%d/hide/' % self.flashcard.id, format='json') + self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) + self.assertTrue(self.flashcard.is_hidden_from(self.user)) + + 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): + self.flashcard.hide_from(self.user) + + response = self.client.post('/api/flashcards/%d/unhide/' % self.flashcard.id, 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) + class SectionViewSetTest(APITestCase): fixtures = ['testusers', 'testsections'] @@ -338,7 +352,7 @@ class SectionViewSetTest(APITestCase): self.assertEqual(response.status_code, HTTP_200_OK) def test_section_feed(self): - Flashcard.objects.create(author=self.user, material_date=datetime.now(), + Flashcard.objects.create(author=self.user, material_date=now(), text='wow', section=self.section, mask=None).save() response = self.client.get('/api/sections/{}/feed/'.format(self.section.pk)) @@ -350,43 +364,3 @@ class SectionViewSetTest(APITestCase): 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_edit_flashcard(self): - return - - def test_hide_flashcard(self): - response = self.client.post('/api/flashcards/1/hide/', format='json') - self.assertEqual(response.status_code, HTTP_204_NO_CONTENT) - self.assertTrue(self.flashcard.is_hidden_from(self.user)) - - 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): - flashcard_hide = FlashcardHide(user=self.user, flashcard=self.flashcard) - flashcard_hide.save() - - response = self.client.post('/api/flashcards/1/unhide/', 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) \ No newline at end of file diff --git a/flashcards/views.py b/flashcards/views.py index e186d46..a6c0ea4 100644 --- a/flashcards/views.py +++ b/flashcards/views.py @@ -305,11 +305,10 @@ class FlashcardViewSet(GenericViewSet, UpdateModelMixin, CreateModelMixin, Retri parameters_strategy: form: replace """ - obj, created = FlashcardHide.objects.get_or_create(user=request.user, flashcard=self.get_object()) - if not created: + try: + self.get_object().hide_from(request.user) + except django.core.exceptions.ValidationError: raise ValidationError("The card has already been hidden.") - - obj.save() return Response(status=HTTP_204_NO_CONTENT) @detail_route(methods=['post']) -- 1.9.1