def test_access_object_owned_by_another_user(self): # GIVEN user1 = userhelper.given_a_user_exists() user2 = userhelper.given_a_user_exists_and_is_authenticated( self.client, username='******', email='*****@*****.**') event = eventhelper.given_event_exists(user1) course_group1 = coursegrouphelper.given_course_group_exists(user1) course_group2 = coursegrouphelper.given_course_group_exists(user2) course1 = coursehelper.given_course_exists(course_group1) coursehelper.given_course_exists(course_group2) homework = homeworkhelper.given_homework_exists(course1) attachment1 = attachmenthelper.given_attachment_exists(user1, course=course1) attachmenthelper.given_attachment_exists(user1, homework=homework) attachmenthelper.given_attachment_exists(user1, event=event) # WHEN responses = [ self.client.get( reverse('planner_attachments_list') + '?course={}'.format(course1.pk)), self.client.get( reverse('planner_attachments_list') + '?event={}'.format(event.pk)), self.client.get( reverse('planner_attachments_list') + '?homework={}'.format(homework.pk)), self.client.delete( reverse('planner_attachments_detail', kwargs={'pk': attachment1.pk})) ] # THEN for response in responses: if isinstance(response.data, list): self.assertEqual(len(response.data), 0) else: self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_delete_attachment_by_id(self): # GIVEN user = userhelper.given_a_user_exists_and_is_authenticated(self.client) course_group = coursegrouphelper.given_course_group_exists(user) course = coursehelper.given_course_exists(course_group) attachment = attachmenthelper.given_attachment_exists(user, course) # WHEN response = self.client.delete( reverse('planner_attachments_detail', kwargs={'pk': attachment.pk})) # THEN self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertFalse(Attachment.objects.filter(pk=attachment.pk).exists()) self.assertEqual(Attachment.objects.count(), 0)
def test_get_attachment_by_id(self): # GIVEN user = userhelper.given_a_user_exists_and_is_authenticated(self.client) course_group = coursegrouphelper.given_course_group_exists(user) course = coursehelper.given_course_exists(course_group) attachment = attachmenthelper.given_attachment_exists(user, course) # WHEN response = self.client.get( reverse('planner_attachments_detail', kwargs={'pk': attachment.pk})) # THEN self.assertEqual(response.status_code, status.HTTP_200_OK) attachmenthelper.verify_attachment_matches_data( self, attachment, response.data)
def test_get_attachments(self): # GIVEN user1 = userhelper.given_a_user_exists() user2 = userhelper.given_a_user_exists_and_is_authenticated( self.client, username='******', email='*****@*****.**') event1 = eventhelper.given_event_exists(user1) event2 = eventhelper.given_event_exists(user2) course_group1 = coursegrouphelper.given_course_group_exists(user1) course_group2 = coursegrouphelper.given_course_group_exists(user2) course1 = coursehelper.given_course_exists(course_group1) course2 = coursehelper.given_course_exists(course_group2) course3 = coursehelper.given_course_exists(course_group2) homework1 = homeworkhelper.given_homework_exists(course1) homework2 = homeworkhelper.given_homework_exists(course2) attachmenthelper.given_attachment_exists(user1, course=course1) attachmenthelper.given_attachment_exists(user2, course=course2) attachmenthelper.given_attachment_exists(user2, course=course3) attachmenthelper.given_attachment_exists(user2, course=course3) attachmenthelper.given_attachment_exists(user1, event=event1) attachmenthelper.given_attachment_exists(user2, event=event2) attachmenthelper.given_attachment_exists(user2, event=event2) attachmenthelper.given_attachment_exists(user1, homework=homework1) attachmenthelper.given_attachment_exists(user2, homework=homework2) attachmenthelper.given_attachment_exists(user2, homework=homework2) # WHEN response1 = self.client.get(reverse('planner_attachments_list')) response2 = self.client.get( reverse('planner_attachments_list') + f'?course={course3.pk}') response3 = self.client.get( reverse('planner_attachments_list') + f'?event={event2.pk}') response4 = self.client.get( reverse('planner_attachments_list') + f'?homework={homework2.pk}') # THEN self.assertEqual(response1.status_code, status.HTTP_200_OK) self.assertEqual(response2.status_code, status.HTTP_200_OK) self.assertEqual(response3.status_code, status.HTTP_200_OK) self.assertEqual(response4.status_code, status.HTTP_200_OK) self.assertEqual(Attachment.objects.count(), 10) self.assertEqual(len(response1.data), 7) self.assertEqual(len(response2.data), 2) self.assertEqual(len(response3.data), 2) self.assertEqual(len(response4.data), 2)