def test_create_course_attachment(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) tmp_file = attachmenthelper.given_file_exists() # WHEN with open(tmp_file.name) as fp: data = {'course': course.pk, 'file[]': [fp]} response = self.client.post(reverse('planner_attachments_list'), data) # THEN self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]['title'], os.path.basename(tmp_file.name)) self.assertEqual(response.data[0]['size'], os.path.getsize(tmp_file.name)) self.assertEqual(response.data[0]['course'], data['course']) self.assertEqual(Attachment.objects.count(), 1) attachment = Attachment.objects.get(pk=response.data[0]['id']) attachmenthelper.verify_attachment_matches_data( self, attachment, response.data[0])
def test_not_found(self): user = userhelper.given_a_user_exists_and_is_authenticated(self.client) course_group = coursegrouphelper.given_course_group_exists(user) coursehelper.given_course_exists(course_group) tmp_file = attachmenthelper.given_file_exists() # WHEN with open(tmp_file.name) as fp: data = {'course': '9999', 'file[]': [fp]} responses = [ self.client.post(reverse('planner_attachments_list'), data), self.client.get( reverse('planner_attachments_detail', kwargs={'pk': '9999'})), self.client.delete( reverse('planner_attachments_detail', kwargs={'pk': '9999'})) ] # THEN for response in responses: if isinstance(response.data, list): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 0) else: self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertIn('not found', response.data['detail'].lower())
def test_import_invalid_json(self): # GIVEN userhelper.given_a_user_exists_and_is_authenticated(self.client) tmp_file = attachmenthelper.given_file_exists(ext='.json') # WHEN with open(tmp_file.name) as fp: data = { 'file[]': [fp] } response = self.client.post( reverse('importexport_resource_import'), data) # THEN self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn('detail', response.data) self.assertEqual(ExternalCalendar.objects.count(), 0) self.assertEqual(CourseGroup.objects.count(), 0) self.assertEqual(Course.objects.count(), 0) self.assertEqual(CourseSchedule.objects.count(), 0) self.assertEqual(Category.objects.count(), 0) self.assertEqual(MaterialGroup.objects.count(), 0) self.assertEqual(Material.objects.count(), 0) self.assertEqual(Event.objects.count(), 0) self.assertEqual(Homework.objects.count(), 0) self.assertEqual(Reminder.objects.count(), 0)
def test_create_orphaned_attachment_fails(self): # GIVEN userhelper.given_a_user_exists_and_is_authenticated(self.client) tmp_file = attachmenthelper.given_file_exists() # WHEN with open(tmp_file.name) as fp: data = {'file[]': [fp]} response = self.client.post(reverse('planner_attachments_list'), data) # THEN self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertIn('One of', response.data[0]['non_field_errors'][0])
def test_related_field_owned_by_another_user_forbidden(self): # GIVEN user1 = userhelper.given_a_user_exists() user2 = userhelper.given_a_user_exists_and_is_authenticated( self.client, username='******', email='*****@*****.**') 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) tmp_file = attachmenthelper.given_file_exists() # WHEN with open(tmp_file.name): response = self.client.post(reverse('planner_attachments_list'), {'course': course1.pk}) # THEN self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)