Commit 1c546f804757a9ccbef30c441591b15456f76752

Authored by Andrew Buss
1 parent 7031375881
Exists in master

cleaned up some tests

Showing 3 changed files with 41 additions and 57 deletions Side-by-side Diff

flashcards/models.py View file @ 1c546f8
... ... @@ -143,6 +143,17 @@
143 143 if self.is_hidden or self.flashcardhide_set.filter(user=user).exists(): return True
144 144 return False
145 145  
  146 +
  147 + def hide_from(self, user):
  148 + if self.is_in_deck(user): user.unpull(self)
  149 + obj, created = FlashcardHide.objects.get_or_create(user=user, flashcard=self)
  150 + if not created:
  151 + raise ValidationError("The card has already been hidden.")
  152 + obj.save()
  153 +
  154 + def is_in_deck(self, user):
  155 + return self.userflashcard_set.filter(user=user).exists()
  156 +
146 157 def add_to_deck(self, user):
147 158 if not user.is_in_section(self.section):
148 159 raise PermissionDenied("You don't have the permission to add this card")
flashcards/tests/test_api.py View file @ 1c546f8
1   -from datetime import datetime
2   -
3 1 from django.core import mail
4 2 from flashcards.models import *
5 3 from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_201_CREATED, HTTP_200_OK, HTTP_403_FORBIDDEN
6 4 from rest_framework.test import APITestCase
7 5 from re import search
  6 +import datetime
8 7 from django.utils.timezone import now
9 8 from flashcards.validators import FlashcardMask
10 9 from flashcards.serializers import FlashcardSerializer
11 10  
12 11  
13 12  
... ... @@ -203,15 +202,15 @@
203 202 self.section = Section.objects.get(pk=1)
204 203 self.user = User.objects.get(email='none@none.com')
205 204 self.section.enroll(self.user)
206   - self.inaccessible_flashcard = Flashcard(text="you can't see me!", section=Section.objects.get(pk=2),
207   - material_date=now(), author=self.user)
  205 + self.inaccessible_flashcard = Flashcard(text="can't touch this!", section=Section.objects.get(pk=2),
  206 + author=self.user)
208 207 self.inaccessible_flashcard.save()
209   - self.flashcard = Flashcard(text="jason", section=self.section, material_date=now(), author=self.user)
  208 + self.flashcard = Flashcard(text="jason", section=self.section, author=self.user)
210 209 self.flashcard.save()
211 210 self.flashcard.add_to_deck(self.user)
  211 + self.client.login(email='none@none.com', password='1234')
212 212  
213 213 def test_edit_flashcard(self):
214   - self.client.login(email='none@none.com', password='1234')
215 214 user = self.user
216 215 flashcard = self.flashcard
217 216 url = "/api/flashcards/{}/".format(flashcard.pk)
... ... @@ -221,7 +220,7 @@
221 220 response = self.client.patch(url, data, format='json')
222 221 self.assertEqual(response.status_code, HTTP_200_OK)
223 222 self.assertEqual(response.data['text'], data['text'])
224   - data = {'material_date': datetime(2015, 4, 12, 2, 2, 2),
  223 + data = {'material_date': datetime.datetime(2015, 4, 12, 2, 2, 2),
225 224 'mask': '[[1, 3]]'}
226 225 user2 = User.objects.create(email='wow@wow.wow', password='wow')
227 226 user2.sections.add(self.section)
... ... @@ -240,10 +239,6 @@
240 239 self.assertEqual(user_flashcard.mask, FlashcardMask([[3, 6]]))
241 240  
242 241 def test_create_flashcard(self):
243   - self.client.login(email='none@none.com', password='1234')
244   - user = User.objects.get(email='none@none.com')
245   - user.sections.add(Section.objects.get(pk=1))
246   - user.save()
247 242 data = {'text': 'this is a flashcard',
248 243 'material_date': now(),
249 244 'mask': '[]',
250 245  
251 246  
... ... @@ -255,12 +250,31 @@
255 250 self.assertTrue(Flashcard.objects.filter(section__pk=1, text=data['text']).exists())
256 251  
257 252 def test_get_flashcard(self):
258   - self.client.login(email='none@none.com', password='1234')
259 253 response = self.client.get("/api/flashcards/%d/" % self.flashcard.id, format="json")
260 254 self.assertEqual(response.status_code, HTTP_200_OK)
261 255 self.assertEqual(response.data["text"], "jason")
262 256  
  257 + def test_hide_flashcard(self):
  258 + response = self.client.post('/api/flashcards/%d/hide/' % self.flashcard.id, format='json')
  259 + self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
  260 + self.assertTrue(self.flashcard.is_hidden_from(self.user))
263 261  
  262 + response = self.client.post('/api/flashcards/%d/hide/' % self.inaccessible_flashcard.pk, format='json')
  263 + # This should fail because the user is not enrolled in section id 2
  264 + self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
  265 +
  266 + def test_unhide_flashcard(self):
  267 + self.flashcard.hide_from(self.user)
  268 +
  269 + response = self.client.post('/api/flashcards/%d/unhide/' % self.flashcard.id, format='json')
  270 + self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
  271 +
  272 + response = self.client.post('/api/flashcards/%d/unhide/' % self.inaccessible_flashcard.pk, format='json')
  273 +
  274 + # This should fail because the user is not enrolled in section id 2
  275 + self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
  276 +
  277 +
264 278 class SectionViewSetTest(APITestCase):
265 279 fixtures = ['testusers', 'testsections']
266 280  
... ... @@ -338,7 +352,7 @@
338 352 self.assertEqual(response.status_code, HTTP_200_OK)
339 353  
340 354 def test_section_feed(self):
341   - Flashcard.objects.create(author=self.user, material_date=datetime.now(),
  355 + Flashcard.objects.create(author=self.user, material_date=now(),
342 356 text='wow', section=self.section,
343 357 mask=None).save()
344 358 response = self.client.get('/api/sections/{}/feed/'.format(self.section.pk))
... ... @@ -350,44 +364,4 @@
350 364 self.user.save()
351 365 response = self.client.get('/api/sections/1/ordered_deck/')
352 366 self.assertEqual(response.status_code, HTTP_200_OK)
353   -
354   -
355   -class FlashcardViewSetTest(APITestCase):
356   - fixtures = ['testusers', 'testsections']
357   -
358   - def setUp(self):
359   - self.client.login(email='none@none.com', password='1234')
360   - self.user = User.objects.get(email='none@none.com')
361   - self.section = Section.objects.get(pk=1)
362   - self.section.enroll(self.user)
363   - self.flashcard = Flashcard(text="jason", section=self.section, material_date=now(),
364   - author=self.user)
365   - self.flashcard.save()
366   - self.inaccessible_flashcard = Flashcard(text="can't touch this", section=Section.objects.get(pk=2),
367   - material_date=now(), author=self.user)
368   - self.inaccessible_flashcard.save()
369   -
370   - def test_edit_flashcard(self):
371   - return
372   -
373   - def test_hide_flashcard(self):
374   - response = self.client.post('/api/flashcards/1/hide/', format='json')
375   - self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
376   - self.assertTrue(self.flashcard.is_hidden_from(self.user))
377   -
378   - response = self.client.post('/api/flashcards/%d/hide/' % self.inaccessible_flashcard.pk, format='json')
379   - # This should fail because the user is not enrolled in section id 2
380   - self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
381   -
382   - def test_unhide_flashcard(self):
383   - flashcard_hide = FlashcardHide(user=self.user, flashcard=self.flashcard)
384   - flashcard_hide.save()
385   -
386   - response = self.client.post('/api/flashcards/1/unhide/', format='json')
387   - self.assertEqual(response.status_code, HTTP_204_NO_CONTENT)
388   -
389   - response = self.client.post('/api/flashcards/%d/unhide/' % self.inaccessible_flashcard.pk, format='json')
390   -
391   - # This should fail because the user is not enrolled in section id 2
392   - self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
flashcards/views.py View file @ 1c546f8
... ... @@ -305,11 +305,10 @@
305 305 parameters_strategy:
306 306 form: replace
307 307 """
308   - obj, created = FlashcardHide.objects.get_or_create(user=request.user, flashcard=self.get_object())
309   - if not created:
  308 + try:
  309 + self.get_object().hide_from(request.user)
  310 + except django.core.exceptions.ValidationError:
310 311 raise ValidationError("The card has already been hidden.")
311   -
312   - obj.save()
313 312 return Response(status=HTTP_204_NO_CONTENT)
314 313  
315 314 @detail_route(methods=['post'])