def test_can_see(self): now = timezone.now() # Released yesterday yesterday = Exhibition( title='New Exhibition', description='description goes here', released_at=now + timedelta(hours=-24)) self.assertTrue(yesterday.can_see(self.user)) self.assertTrue(yesterday.can_see(self.staff_user)) self.assertTrue(yesterday.can_see(self.super_user)) # Released today today = Exhibition( title='New Exhibition', description='description goes here', released_at=now) self.assertTrue(today.can_see(self.user)) self.assertTrue(today.can_see(self.staff_user)) self.assertTrue(today.can_see(self.super_user)) # Released today tomorrow = Exhibition( title='New Exhibition', description='description goes here', released_at=now + timedelta(hours=24)) self.assertFalse(tomorrow.can_see(self.user)) self.assertTrue(tomorrow.can_see(self.staff_user)) self.assertTrue(tomorrow.can_see(self.super_user))
def test_can_save(self): # Students cannot save exhibitions student = Exhibition( author=self.user, title='New Exhibition', description='description goes here', released_at=timezone.now()) self.assertFalse(student.can_save(self.user)) self.assertTrue(student.can_save(self.staff_user)) self.assertTrue(student.can_save(self.super_user)) staff = Exhibition( author=self.staff_user, title='New Exhibition', description='description goes here', released_at=timezone.now()) self.assertFalse(staff.can_save(self.user)) self.assertTrue(staff.can_save(self.staff_user)) self.assertTrue(staff.can_save(self.super_user)) superuser = Exhibition( author=self.super_user, title='New Exhibition', description='description goes here', released_at=timezone.now()) self.assertFalse(superuser.can_save(self.user)) self.assertTrue(superuser.can_save(self.staff_user)) self.assertTrue(superuser.can_save(self.super_user))
def test_can_see_default_cohort(self): # Only students in cohort1 (or with no cohort, i.e. the default cohort) # can see exhibitions for the default cohort1 exhibition_cohort1 = Exhibition(title='New Exhibition', description='description goes here', cohort=self.cohort1) self.assertTrue(exhibition_cohort1.can_see(self.no_cohort_user)) self.assertTrue(exhibition_cohort1.can_see(self.cohort1_user)) self.assertFalse(exhibition_cohort1.can_see(self.cohort2_user)) # Staff can see all exhibitions self.no_cohort_user.is_staff = True self.cohort1_user.is_staff = True self.cohort2_user.is_staff = True self.assertTrue(exhibition_cohort1.can_see(self.no_cohort_user)) self.assertTrue(exhibition_cohort1.can_see(self.cohort1_user)) self.assertTrue(exhibition_cohort1.can_see(self.cohort2_user)) # Super users can see all exhibitions self.no_cohort_user.is_staff = False self.cohort1_user.is_staff = False self.cohort2_user.is_staff = False self.no_cohort_user.is_superuser = True self.cohort1_user.is_superuser = True self.cohort2_user.is_superuser = True self.assertTrue(exhibition_cohort1.can_see(self.no_cohort_user)) self.assertTrue(exhibition_cohort1.can_see(self.cohort1_user)) self.assertTrue(exhibition_cohort1.can_see(self.cohort2_user))
def test_can_see_non_default_cohort(self): # Only users in cohort2 can see exhibitions for cohort2 exhibition_cohort2 = Exhibition(title='New Exhibition', description='description goes here', cohort=self.cohort2) self.assertFalse(exhibition_cohort2.can_see(self.no_cohort_user)) self.assertFalse(exhibition_cohort2.can_see(self.cohort1_user)) self.assertTrue(exhibition_cohort2.can_see(self.cohort2_user)) # Staff can see all exhibitions self.no_cohort_user.is_staff = True self.cohort1_user.is_staff = True self.cohort2_user.is_staff = True self.assertTrue(exhibition_cohort2.can_see(self.no_cohort_user)) self.assertTrue(exhibition_cohort2.can_see(self.cohort1_user)) self.assertTrue(exhibition_cohort2.can_see(self.cohort2_user)) # Super users can see all exhibitions self.no_cohort_user.is_staff = False self.cohort1_user.is_staff = False self.cohort2_user.is_staff = False self.no_cohort_user.is_superuser = True self.cohort1_user.is_superuser = True self.cohort2_user.is_superuser = True self.assertTrue(exhibition_cohort2.can_see(self.no_cohort_user)) self.assertTrue(exhibition_cohort2.can_see(self.cohort1_user)) self.assertTrue(exhibition_cohort2.can_see(self.cohort2_user))
def test_can_see_queryset(self): exhibition_all = Exhibition.objects.create( title='New Exhibition', description='description goes here', author=self.staff_user, cohort=None, ) # All students can see the no-cohort exhibition no_cohort_user_qs = Exhibition.can_see_queryset( user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 1) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 1) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 1) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all) # Staff access doesn't change cohort queryset (even though staff can # see all exhibitions) self.no_cohort_user.is_staff = True no_cohort_user_qs = Exhibition.can_see_queryset( user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 1) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) self.cohort1_user.is_staff = True cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 1) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) self.cohort2_user.is_staff = True cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 1) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all) # And neither does superuser access self.no_cohort_user.is_staff = False self.no_cohort_user.is_superuser = True no_cohort_user_qs = Exhibition.can_see_queryset( user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 1) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) self.cohort1_user.is_staff = False self.cohort1_user.is_superuser = True cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 1) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) self.cohort2_user.is_staff = False self.cohort2_user.is_superuser = True cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 1) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all)
def test_can_see_default_cohort(self): # Only students in cohort1 (or with no cohort, i.e. the default cohort) # can see exhibitions for the default cohort1 exhibition_cohort1 = Exhibition( title='New Exhibition', description='description goes here', cohort=self.cohort1) self.assertTrue(exhibition_cohort1.can_see(self.no_cohort_user)) self.assertTrue(exhibition_cohort1.can_see(self.cohort1_user)) self.assertFalse(exhibition_cohort1.can_see(self.cohort2_user)) # Staff can see all exhibitions self.no_cohort_user.is_staff = True self.cohort1_user.is_staff = True self.cohort2_user.is_staff = True self.assertTrue(exhibition_cohort1.can_see(self.no_cohort_user)) self.assertTrue(exhibition_cohort1.can_see(self.cohort1_user)) self.assertTrue(exhibition_cohort1.can_see(self.cohort2_user)) # Super users can see all exhibitions self.no_cohort_user.is_staff = False self.cohort1_user.is_staff = False self.cohort2_user.is_staff = False self.no_cohort_user.is_superuser = True self.cohort1_user.is_superuser = True self.cohort2_user.is_superuser = True self.assertTrue(exhibition_cohort1.can_see(self.no_cohort_user)) self.assertTrue(exhibition_cohort1.can_see(self.cohort1_user)) self.assertTrue(exhibition_cohort1.can_see(self.cohort2_user))
def test_can_see_queryset(self): exhibition_all = Exhibition.objects.create( title='New Exhibition', description='description goes here', author=self.staff_user, cohort=None, ) # All students can see the no-cohort exhibition no_cohort_user_qs = Exhibition.can_see_queryset(user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 1) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 1) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 1) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all) # Staff access doesn't change cohort queryset (even though staff can # see all exhibitions) self.no_cohort_user.is_staff = True no_cohort_user_qs = Exhibition.can_see_queryset(user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 1) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) self.cohort1_user.is_staff = True cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 1) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) self.cohort2_user.is_staff = True cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 1) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all) # And neither does superuser access self.no_cohort_user.is_staff = False self.no_cohort_user.is_superuser = True no_cohort_user_qs = Exhibition.can_see_queryset(user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 1) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) self.cohort1_user.is_staff = False self.cohort1_user.is_superuser = True cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 1) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) self.cohort2_user.is_staff = False self.cohort2_user.is_superuser = True cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 1) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all)
def test_can_save(self): # Staff & super users can save all exhibitions exhibition_all = Exhibition( title='New Exhibition', description='description goes here', ) self.assertFalse(exhibition_all.can_save(self.no_cohort_user)) self.assertFalse(exhibition_all.can_save(self.cohort1_user)) self.assertFalse(exhibition_all.can_save(self.cohort2_user)) self.no_cohort_user.is_staff = True self.cohort1_user.is_staff = True self.cohort2_user.is_staff = True self.assertTrue(exhibition_all.can_save(self.no_cohort_user)) self.assertTrue(exhibition_all.can_save(self.cohort1_user)) self.assertTrue(exhibition_all.can_save(self.cohort2_user)) self.no_cohort_user.is_staff = False self.cohort1_user.is_staff = False self.cohort2_user.is_staff = False self.no_cohort_user.is_superuser = True self.cohort1_user.is_superuser = True self.cohort2_user.is_superuser = True self.assertTrue(exhibition_all.can_save(self.no_cohort_user)) self.assertTrue(exhibition_all.can_save(self.cohort1_user)) self.assertTrue(exhibition_all.can_save(self.cohort2_user))
def test_can_see_null_cohorts(self): # All users can see exhibitions with null cohorts exhibition_all = Exhibition( title='New Exhibition', description='description goes here', ) self.assertTrue(exhibition_all.can_see(self.no_cohort_user)) self.assertTrue(exhibition_all.can_see(self.cohort1_user)) self.assertTrue(exhibition_all.can_see(self.cohort2_user)) self.no_cohort_user.is_staff = True self.cohort1_user.is_staff = True self.cohort2_user.is_staff = True self.assertTrue(exhibition_all.can_see(self.no_cohort_user)) self.assertTrue(exhibition_all.can_see(self.cohort1_user)) self.assertTrue(exhibition_all.can_see(self.cohort2_user)) self.no_cohort_user.is_staff = False self.cohort1_user.is_staff = False self.cohort2_user.is_staff = False self.no_cohort_user.is_superuser = True self.cohort1_user.is_superuser = True self.cohort2_user.is_superuser = True self.assertTrue(exhibition_all.can_see(self.no_cohort_user)) self.assertTrue(exhibition_all.can_see(self.cohort1_user)) self.assertTrue(exhibition_all.can_see(self.cohort2_user))
def test_can_save_non_default_cohort(self): # Students cannot save exhibitions exhibition_cohort2 = Exhibition(title='New Exhibition', description='description goes here', cohort=self.cohort2) self.assertFalse(exhibition_cohort2.can_save(self.no_cohort_user)) self.assertFalse(exhibition_cohort2.can_save(self.cohort1_user)) self.assertFalse(exhibition_cohort2.can_save(self.cohort2_user)) # Staff members can save all exhibitions self.no_cohort_user.is_staff = True self.cohort1_user.is_staff = True self.cohort2_user.is_staff = True self.assertTrue(exhibition_cohort2.can_save(self.no_cohort_user)) self.assertTrue(exhibition_cohort2.can_save(self.cohort1_user)) self.assertTrue(exhibition_cohort2.can_save(self.cohort2_user)) self.no_cohort_user.is_staff = False self.cohort1_user.is_staff = False self.cohort2_user.is_staff = False self.no_cohort_user.is_superuser = True self.cohort1_user.is_superuser = True self.cohort2_user.is_superuser = True self.assertTrue(exhibition_cohort2.can_save(self.no_cohort_user)) self.assertTrue(exhibition_cohort2.can_save(self.cohort1_user)) self.assertTrue(exhibition_cohort2.can_save(self.cohort2_user))
def test_can_see_non_default_cohort(self): # Only users in cohort2 can see exhibitions for cohort2 exhibition_cohort2 = Exhibition( title='New Exhibition', description='description goes here', cohort=self.cohort2) self.assertFalse(exhibition_cohort2.can_see(self.no_cohort_user)) self.assertFalse(exhibition_cohort2.can_see(self.cohort1_user)) self.assertTrue(exhibition_cohort2.can_see(self.cohort2_user)) # Staff can see all exhibitions self.no_cohort_user.is_staff = True self.cohort1_user.is_staff = True self.cohort2_user.is_staff = True self.assertTrue(exhibition_cohort2.can_see(self.no_cohort_user)) self.assertTrue(exhibition_cohort2.can_see(self.cohort1_user)) self.assertTrue(exhibition_cohort2.can_see(self.cohort2_user)) # Super users can see all exhibitions self.no_cohort_user.is_staff = False self.cohort1_user.is_staff = False self.cohort2_user.is_staff = False self.no_cohort_user.is_superuser = True self.cohort1_user.is_superuser = True self.cohort2_user.is_superuser = True self.assertTrue(exhibition_cohort2.can_see(self.no_cohort_user)) self.assertTrue(exhibition_cohort2.can_see(self.cohort1_user)) self.assertTrue(exhibition_cohort2.can_see(self.cohort2_user))
def get_context_data(self, **kwargs): context = super(UpdateArtworkView, self).get_context_data(**kwargs) context['action'] = reverse('artwork-edit', kwargs={'pk': self.get_object().id}) artwork = context['object'] if artwork: context['share_url'] = ShareView.get_share_url( artwork.get_absolute_url()) submissions_qs = Submission.objects.filter( artwork__exact=artwork.id).order_by('-created_at').all() submissions = {int(x.exhibition_id): x for x in submissions_qs} exhibitions_qs = Exhibition.can_see_queryset( Exhibition.objects, self.request.user).order_by('-released_at', 'created_at') exhibitions = exhibitions_qs.all() # Collect the exhibitions we've already submitted this artwork to, # and the ones that can still be submitted to context['exhibitions_submitted'] = [] context['exhibitions_to_submit'] = [] for exh in exhibitions: submission = submissions.get(exh.id) if submission: exh.submitted = submission context['exhibitions_submitted'].append(exh) else: context['exhibitions_to_submit'].append(exh) return context
def get_context_data(self, **kwargs): context = super(CreateSubmissionView, self).get_context_data(**kwargs) # Restrict list of artwork the current user can submit context['form'].fields['artwork'].queryset = Artwork.can_save_queryset( context['form'].fields['artwork'].queryset, self.request.user) # Restrict to specific artwork if given artwork_id = self.kwargs.get('artwork') exclude_exhibitions = [] if artwork_id: context['form'].fields['artwork'].queryset =\ context['form'].fields['artwork'].queryset.filter(id=artwork_id) # Fetch the exhibitions this artwork has already been submitted to exclude_exhibitions = Submission.objects.filter( artwork__exact=artwork_id).order_by('-created_at').values_list( 'exhibition', flat=True) # Restrict list of exhibitions the current user can see, # and exclude exhibitions this artwork has already been submitted to context['form'].fields[ 'exhibition'].queryset = Exhibition.can_see_queryset( context['form'].fields['exhibition'].queryset, self.request.user).exclude(id__in=exclude_exhibitions) return context
def get_context_data(self, **kwargs): context = super(UpdateArtworkView, self).get_context_data(**kwargs) context['action'] = reverse('artwork-edit', kwargs={'pk': self.get_object().id}) artwork = context['object'] if artwork: context['share_url'] = ShareView.get_share_url(artwork.get_absolute_url()) submissions_qs = Submission.objects.filter( artwork__exact=artwork.id).order_by('-created_at').all() submissions = { int(x.exhibition_id) : x for x in submissions_qs } exhibitions_qs = Exhibition.can_see_queryset( Exhibition.objects, self.request.user).order_by('-released_at', 'created_at') exhibitions = exhibitions_qs.all() # Collect the exhibitions we've already submitted this artwork to, # and the ones that can still be submitted to context['exhibitions_submitted'] = [] context['exhibitions_to_submit'] = [] for exh in exhibitions: submission = submissions.get(exh.id) if submission: exh.submitted = submission context['exhibitions_submitted'].append(exh) else: context['exhibitions_to_submit'].append(exh) return context
def get_context_data(self, **kwargs): context = super(CreateSubmissionView, self).get_context_data(**kwargs) # Restrict list of artwork the current user can submit context['form'].fields['artwork'].queryset = Artwork.can_save_queryset( context['form'].fields['artwork'].queryset, self.request.user) # Restrict to specific artwork if given artwork_id = self.kwargs.get('artwork') exclude_exhibitions = [] if artwork_id: context['form'].fields['artwork'].queryset =\ context['form'].fields['artwork'].queryset.filter(id=artwork_id) # Fetch the exhibitions this artwork has already been submitted to exclude_exhibitions = Submission.objects.filter( artwork__exact=artwork_id).order_by('-created_at').values_list('exhibition', flat=True) # Restrict list of exhibitions the current user can see, # and exclude exhibitions this artwork has already been submitted to context['form'].fields['exhibition'].queryset = Exhibition.can_see_queryset( context['form'].fields['exhibition'].queryset, self.request.user).exclude(id__in=exclude_exhibitions) return context
def test_can_save_non_default_cohort(self): # Students cannot save exhibitions exhibition_cohort2 = Exhibition( title='New Exhibition', description='description goes here', cohort=self.cohort2) self.assertFalse(exhibition_cohort2.can_save(self.no_cohort_user)) self.assertFalse(exhibition_cohort2.can_save(self.cohort1_user)) self.assertFalse(exhibition_cohort2.can_save(self.cohort2_user)) # Staff members can save all exhibitions self.no_cohort_user.is_staff = True self.cohort1_user.is_staff = True self.cohort2_user.is_staff = True self.assertTrue(exhibition_cohort2.can_save(self.no_cohort_user)) self.assertTrue(exhibition_cohort2.can_save(self.cohort1_user)) self.assertTrue(exhibition_cohort2.can_save(self.cohort2_user)) self.no_cohort_user.is_staff = False self.cohort1_user.is_staff = False self.cohort2_user.is_staff = False self.no_cohort_user.is_superuser = True self.cohort1_user.is_superuser = True self.cohort2_user.is_superuser = True self.assertTrue(exhibition_cohort2.can_save(self.no_cohort_user)) self.assertTrue(exhibition_cohort2.can_save(self.cohort1_user)) self.assertTrue(exhibition_cohort2.can_save(self.cohort2_user))
def test_save_unique(self): exhibition = Exhibition( title='New Exhibition', description='description goes here', released_at=timezone.now(), author=self.user) exhibition.save() artwork = Artwork(title='New Artwork', code='// code goes here', author=self.user) artwork.save() submission1 = Submission(exhibition=exhibition, artwork=artwork, submitted_by=self.user) submission2 = Submission(exhibition=exhibition, artwork=artwork, submitted_by=self.user) # submissions must be unique self.assertEqual(submission1.save(), None) self.assertRaisesRegexp( IntegrityError, 'columns exhibition_id, artwork_id are not unique', submission2.save)
def test_released_yet(self): now = timezone.now() # Released yesterday yesterday = Exhibition(title='New Exhibition', description='description goes here', released_at=now + timedelta(hours=-24)) self.assertTrue(yesterday.released_yet) # Released today today = Exhibition(title='New Exhibition', description='description goes here', released_at=now) self.assertTrue(today.released_yet) # Released tomorrow tomorrow = Exhibition(title='New Exhibition', description='description goes here', released_at=now + timedelta(hours=24)) self.assertFalse(tomorrow.released_yet)
def test_can_see(self): # Everyone can see submissions exhibition = Exhibition(title='New Exhibition', description='description goes here', released_at=timezone.now()) student_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.user) staff_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.staff_user) submission = Submission(exhibition=exhibition, artwork=student_artwork) self.assertTrue(submission.can_see()) self.assertTrue(submission.can_see(self.user)) self.assertTrue(submission.can_see(self.staff_user)) self.assertTrue(submission.can_see(self.super_user))
def test_str(self): exhibition = Exhibition(title='New Exhibition', description='description goes here') artwork = Artwork(title='New Artwork', code='// code goes here') submission = Submission(exhibition=exhibition, artwork=artwork) self.assertEquals( str(submission), 'New Exhibition :: New Artwork' )
def test_can_save_released_exhibition(self): exhibition = Exhibition(title='New Exhibition', description='description goes here', released_at=timezone.now()) self.assertTrue(exhibition.released_yet) student_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.user) staff_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.staff_user) # only authors can submit artwork submission = Submission(exhibition=exhibition, artwork=student_artwork) self.assertTrue(submission.can_save(self.user)) self.assertFalse(submission.can_save(self.staff_user)) self.assertFalse(submission.can_save(self.super_user)) submission = Submission(exhibition=exhibition, artwork=staff_artwork) self.assertFalse(submission.can_save(self.user)) self.assertTrue(submission.can_save(self.staff_user)) self.assertFalse(submission.can_save(self.super_user))
def test_can_save_unreleased_exhibition(self): exhibition = Exhibition( title='New Exhibition', description='description goes here', released_at=timezone.now() + timedelta(hours=24)) self.assertFalse(exhibition.released_yet) student_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.user) staff_artwork = Artwork(title='New Artwork', code='// code goes here', author=self.staff_user) # students cannot submit to unreleased exhibitions submission = Submission(exhibition=exhibition, artwork=student_artwork) self.assertFalse(submission.can_save(self.user)) self.assertFalse(submission.can_save(self.staff_user)) self.assertFalse(submission.can_save(self.super_user)) # but staff can submission = Submission(exhibition=exhibition, artwork=staff_artwork) self.assertFalse(submission.can_save(self.user)) self.assertTrue(submission.can_save(self.staff_user)) self.assertFalse(submission.can_save(self.super_user))
def test_can_see_queryset(self): now = timezone.now() # Released yesterday yesterday = Exhibition(author=self.user, title='Yesterday Exhibition', description='description goes here', released_at=now + timedelta(hours=-24)) yesterday.save() # Released today today = Exhibition(author=self.user, title='Today Exhibition', description='description goes here', released_at=now) today.save() # Released tomorrow tomorrow = Exhibition(author=self.user, title='Tomorrow Exhibition', description='description goes here', released_at=now + timedelta(hours=24)) tomorrow.save() public_qs = Exhibition.can_see_queryset(Exhibition.objects) self.assertEqual(len(public_qs.all()), 2) self.assertEqual(public_qs.all()[0].id, yesterday.id) self.assertEqual(public_qs.all()[1].id, today.id) student_qs = Exhibition.can_see_queryset(Exhibition.objects, self.user) self.assertEqual(len(student_qs.all()), 2) self.assertEqual(student_qs.all()[0].id, yesterday.id) self.assertEqual(student_qs.all()[1].id, today.id) staff_qs = Exhibition.can_see_queryset(Exhibition.objects, self.staff_user) self.assertEqual(len(staff_qs.all()), 3) self.assertEqual(staff_qs.all()[0].id, yesterday.id) self.assertEqual(staff_qs.all()[1].id, today.id) self.assertEqual(staff_qs.all()[2].id, tomorrow.id) super_qs = Exhibition.can_see_queryset(Exhibition.objects, self.super_user) self.assertEqual(len(super_qs.all()), 3) self.assertEqual(super_qs.all()[0].id, yesterday.id) self.assertEqual(super_qs.all()[1].id, today.id) self.assertEqual(super_qs.all()[2].id, tomorrow.id)
def test_can_see_queryset(self): exhibition_all = Exhibition.objects.create( title='New Exhibition', description='description goes here', author=self.staff_user, cohort=None, ) exhibition_cohort1 = Exhibition.objects.create( title='Exhibition 1', description='description goes here', author=self.staff_user, cohort=self.cohort1, ) exhibition_cohort2 = Exhibition.objects.create( title='Exhibition 2', description='description goes here', author=self.staff_user, cohort=self.cohort2, ) # No cohort student can see the no cohort exhibition, and the default cohort no_cohort_user_qs = Exhibition.can_see_queryset(user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 2) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) self.assertEqual(no_cohort_user_qs.all()[1], exhibition_cohort1) # Cohort1 student gets the no cohort + cohort1 exhibitions cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 2) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) self.assertEqual(cohort1_user_qs.all()[1], exhibition_cohort1) # Cohort2 student gets the no cohort + cohort2 exhibitions cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 2) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all) self.assertEqual(cohort2_user_qs.all()[1], exhibition_cohort2) # Staff access doesn't change cohort queryset (even though staff can # see all exhibitions) self.no_cohort_user.is_staff = True no_cohort_user_qs = Exhibition.can_see_queryset(user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 2) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) self.assertEqual(no_cohort_user_qs.all()[1], exhibition_cohort1) self.cohort1_user.is_staff = True cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 2) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) self.assertEqual(cohort1_user_qs.all()[1], exhibition_cohort1) self.cohort2_user.is_staff = True cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 2) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all) self.assertEqual(cohort2_user_qs.all()[1], exhibition_cohort2) # But superusers see all cohorts in the queryset self.no_cohort_user.is_staff = False self.no_cohort_user.is_superuser = True no_cohort_user_qs = Exhibition.can_see_queryset(user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 3) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) self.assertEqual(no_cohort_user_qs.all()[1], exhibition_cohort1) self.assertEqual(no_cohort_user_qs.all()[2], exhibition_cohort2) self.cohort1_user.is_staff = False self.cohort1_user.is_superuser = True cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 3) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) self.assertEqual(cohort1_user_qs.all()[1], exhibition_cohort1) self.assertEqual(cohort1_user_qs.all()[2], exhibition_cohort2) self.cohort2_user.is_staff = False self.cohort2_user.is_superuser = True cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 3) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all) self.assertEqual(cohort2_user_qs.all()[1], exhibition_cohort1) self.assertEqual(cohort2_user_qs.all()[2], exhibition_cohort2)
def test_can_save(self): # Students cannot save exhibitions student = Exhibition(author=self.user, title='New Exhibition', description='description goes here', released_at=timezone.now()) self.assertFalse(student.can_save(self.user)) self.assertTrue(student.can_save(self.staff_user)) self.assertTrue(student.can_save(self.super_user)) staff = Exhibition(author=self.staff_user, title='New Exhibition', description='description goes here', released_at=timezone.now()) self.assertFalse(staff.can_save(self.user)) self.assertTrue(staff.can_save(self.staff_user)) self.assertTrue(staff.can_save(self.super_user)) superuser = Exhibition(author=self.super_user, title='New Exhibition', description='description goes here', released_at=timezone.now()) self.assertFalse(superuser.can_save(self.user)) self.assertTrue(superuser.can_save(self.staff_user)) self.assertTrue(superuser.can_save(self.super_user))
def test_can_see_queryset(self): exhibition_all = Exhibition.objects.create( title='New Exhibition', description='description goes here', author=self.staff_user, cohort=None, ) exhibition_cohort1 = Exhibition.objects.create( title='Exhibition 1', description='description goes here', author=self.staff_user, cohort=self.cohort1, ) exhibition_cohort2 = Exhibition.objects.create( title='Exhibition 2', description='description goes here', author=self.staff_user, cohort=self.cohort2, ) # No cohort student can see the no cohort exhibition, and the default cohort no_cohort_user_qs = Exhibition.can_see_queryset( user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 2) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) self.assertEqual(no_cohort_user_qs.all()[1], exhibition_cohort1) # Cohort1 student gets the no cohort + cohort1 exhibitions cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 2) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) self.assertEqual(cohort1_user_qs.all()[1], exhibition_cohort1) # Cohort2 student gets the no cohort + cohort2 exhibitions cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 2) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all) self.assertEqual(cohort2_user_qs.all()[1], exhibition_cohort2) # Staff access doesn't change cohort queryset (even though staff can # see all exhibitions) self.no_cohort_user.is_staff = True no_cohort_user_qs = Exhibition.can_see_queryset( user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 2) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) self.assertEqual(no_cohort_user_qs.all()[1], exhibition_cohort1) self.cohort1_user.is_staff = True cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 2) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) self.assertEqual(cohort1_user_qs.all()[1], exhibition_cohort1) self.cohort2_user.is_staff = True cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 2) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all) self.assertEqual(cohort2_user_qs.all()[1], exhibition_cohort2) # But superusers see all cohorts in the queryset self.no_cohort_user.is_staff = False self.no_cohort_user.is_superuser = True no_cohort_user_qs = Exhibition.can_see_queryset( user=self.no_cohort_user) self.assertEqual(no_cohort_user_qs.count(), 3) self.assertEqual(no_cohort_user_qs.all()[0], exhibition_all) self.assertEqual(no_cohort_user_qs.all()[1], exhibition_cohort1) self.assertEqual(no_cohort_user_qs.all()[2], exhibition_cohort2) self.cohort1_user.is_staff = False self.cohort1_user.is_superuser = True cohort1_user_qs = Exhibition.can_see_queryset(user=self.cohort1_user) self.assertEqual(cohort1_user_qs.count(), 3) self.assertEqual(cohort1_user_qs.all()[0], exhibition_all) self.assertEqual(cohort1_user_qs.all()[1], exhibition_cohort1) self.assertEqual(cohort1_user_qs.all()[2], exhibition_cohort2) self.cohort2_user.is_staff = False self.cohort2_user.is_superuser = True cohort2_user_qs = Exhibition.can_see_queryset(user=self.cohort2_user) self.assertEqual(cohort2_user_qs.count(), 3) self.assertEqual(cohort2_user_qs.all()[0], exhibition_all) self.assertEqual(cohort2_user_qs.all()[1], exhibition_cohort1) self.assertEqual(cohort2_user_qs.all()[2], exhibition_cohort2)
def test_str(self): exhibition = Exhibition(title='New Exhibition', description='description goes here') self.assertEquals(str(exhibition), 'New Exhibition')
def test_can_see(self): now = timezone.now() # Released yesterday yesterday = Exhibition(title='New Exhibition', description='description goes here', released_at=now + timedelta(hours=-24)) self.assertTrue(yesterday.can_see(self.user)) self.assertTrue(yesterday.can_see(self.staff_user)) self.assertTrue(yesterday.can_see(self.super_user)) # Released today today = Exhibition(title='New Exhibition', description='description goes here', released_at=now) self.assertTrue(today.can_see(self.user)) self.assertTrue(today.can_see(self.staff_user)) self.assertTrue(today.can_see(self.super_user)) # Released today tomorrow = Exhibition(title='New Exhibition', description='description goes here', released_at=now + timedelta(hours=24)) self.assertFalse(tomorrow.can_see(self.user)) self.assertTrue(tomorrow.can_see(self.staff_user)) self.assertTrue(tomorrow.can_see(self.super_user))
def test_can_see_queryset(self): now = timezone.now() # Released yesterday yesterday = Exhibition( author=self.user, title='Yesterday Exhibition', description='description goes here', released_at=now + timedelta(hours=-24)) yesterday.save() # Released today today = Exhibition( author=self.user, title='Today Exhibition', description='description goes here', released_at=now) today.save() # Released tomorrow tomorrow = Exhibition( author=self.user, title='Tomorrow Exhibition', description='description goes here', released_at=now + timedelta(hours=24)) tomorrow.save() public_qs = Exhibition.can_see_queryset(Exhibition.objects) self.assertEqual(len(public_qs.all()), 2) self.assertEqual(public_qs.all()[0].id, yesterday.id) self.assertEqual(public_qs.all()[1].id, today.id) student_qs = Exhibition.can_see_queryset(Exhibition.objects, self.user) self.assertEqual(len(student_qs.all()), 2) self.assertEqual(student_qs.all()[0].id, yesterday.id) self.assertEqual(student_qs.all()[1].id, today.id) staff_qs = Exhibition.can_see_queryset(Exhibition.objects, self.staff_user) self.assertEqual(len(staff_qs.all()), 3) self.assertEqual(staff_qs.all()[0].id, yesterday.id) self.assertEqual(staff_qs.all()[1].id, today.id) self.assertEqual(staff_qs.all()[2].id, tomorrow.id) super_qs = Exhibition.can_see_queryset(Exhibition.objects, self.super_user) self.assertEqual(len(super_qs.all()), 3) self.assertEqual(super_qs.all()[0].id, yesterday.id) self.assertEqual(super_qs.all()[1].id, today.id) self.assertEqual(super_qs.all()[2].id, tomorrow.id)