def test_can_all_create_methods(self, generate_encrypted_key, set_title): """ The encrypted key should be a created automatically. """ ImageFactory.create() self.assertEquals(1, generate_encrypted_key.call_count) self.assertEquals(1, set_title.call_count)
def test_can_get_images_uploaded_by_a_user(self): """ Tests that the manager can get images specifically uploaded by a user. """ user_1 = UserFactory.create() user_2 = UserFactory.create() image_1a = ImageFactory.create(uploaded_by=user_1) image_1b = ImageFactory.create(uploaded_by=user_1) image_2a = ImageFactory.create(uploaded_by=user_2) self.assertEquals([image_1b, image_1a], list(Image.objects.filter_uploaded_by(user_1))) self.assertEquals([image_2a], list(Image.objects.filter_uploaded_by(user_2)))
def test_can_view_raw_image(self): """ If the image is requested with a file extension the raw image is sent. """ image = ImageFactory.create() response = self.client.get(image.get_raw_url()) self.assertEquals(response.get('content-type'), 'image/png')
def test_can_view_image(self): """ Tests an existing image can be viewed. """ image = ImageFactory.create() response = self.client.get(image.get_absolute_url()) self.assertEquals(200, response.status_code) self.assertIsInstance(response.context['view'], DetailView) self.assertTemplateUsed(response, 'images/image_detail.html')
def test_returns_true_for_uploaded(self): """ If the user uploaded the image it should return true. """ image = ImageFactory.create() adaptor_field = MagicMock() adaptor_field.obj = image adaptor_field.request.user = image.uploaded_by self.assertTrue(UploadedByEditInline.can_edit(adaptor_field))
def test_image_knows_raw_url(self): """ Tests an image knows its file extension. """ image = ImageFactory.create(pk=1) self.assertEquals( image.get_raw_url(), reverse('images:image_detail_raw', kwargs={'slug': image.encrypted_key, 'extension': image.file_extension}) )
def test_image_knows_raw_url(self): """ Tests an image knows its file extension. """ image = ImageFactory.create(pk=1) self.assertEquals( image.get_raw_url(), reverse('images:image_detail_raw', kwargs={ 'slug': image.encrypted_key, 'extension': image.file_extension }))
def test_image_knows_mime_type(self): """ Tests an image knows its file extension. """ image = ImageFactory.create() self.assertEquals(image.mime_type, 'image/png')
def test_image_knows_file_extension(self): """ Tests an image knows its file extension. """ image = ImageFactory.create() self.assertEquals(image.file_extension, 'png')
def test_returns_false_for_anonymous_users(self): image = ImageFactory.create() adaptor_field = MagicMock() adaptor_field.obj = image adaptor_field.request.user = AnonymousUser() self.assertFalse(UploadedByEditInline.can_edit(adaptor_field))
def test_image_knows_mime_type(self): """ Tests an image knows its file extension. """ image = ImageFactory.create() self.assertEquals(image.mime_type, b'image/png')
def test_can_get_latest_images(self, make_thumbnail): """ Tests the list of latest images can be returned. :return: """ make_thumbnail.return_value = 'thumbnail' ImageFactory.create(uploaded_by=self.user) image_1 = ImageFactory.create(uploaded_by=self.user) image_2 = ImageFactory.create(uploaded_by=self.user) image_3 = ImageFactory.create(uploaded_by=self.user) image_4 = ImageFactory.create(uploaded_by=self.user) image_5 = ImageFactory.create(uploaded_by=self.user) image_6 = ImageFactory.create(uploaded_by=self.user) image_7 = ImageFactory.create(uploaded_by=self.user) image_8 = ImageFactory.create(uploaded_by=self.user) image_9 = ImageFactory.create(uploaded_by=self.user) image_10 = ImageFactory.create(uploaded_by=self.user) response = self.client.get(reverse('images:latest_images')) expected_response_dict = { 'results': [ { 'title': image_10.title, 'thumbnail': 'thumbnail', 'url': image_10.get_absolute_url() }, { 'title': image_9.title, 'thumbnail': 'thumbnail', 'url': image_9.get_absolute_url() }, { 'title': image_8.title, 'thumbnail': 'thumbnail', 'url': image_8.get_absolute_url() }, { 'title': image_7.title, 'thumbnail': 'thumbnail', 'url': image_7.get_absolute_url() }, { 'title': image_6.title, 'thumbnail': 'thumbnail', 'url': image_6.get_absolute_url() }, { 'title': image_5.title, 'thumbnail': 'thumbnail', 'url': image_5.get_absolute_url() }, { 'title': image_4.title, 'thumbnail': 'thumbnail', 'url': image_4.get_absolute_url() }, { 'title': image_3.title, 'thumbnail': 'thumbnail', 'url': image_3.get_absolute_url() }, { 'title': image_2.title, 'thumbnail': 'thumbnail', 'url': image_2.get_absolute_url() }, { 'title': image_1.title, 'thumbnail': 'thumbnail', 'url': image_1.get_absolute_url() }, ] } self.assertJSONEqual(response.content.decode('utf-8'), json.dumps(expected_response_dict))
def test_can_get_latest_images(self, make_thumbnail): """ Tests the list of latest images can be returned. :return: """ make_thumbnail.return_value = 'thumbnail' ImageFactory.create(uploaded_by=self.user) image_1 = ImageFactory.create(uploaded_by=self.user) image_2 = ImageFactory.create(uploaded_by=self.user) image_3 = ImageFactory.create(uploaded_by=self.user) image_4 = ImageFactory.create(uploaded_by=self.user) image_5 = ImageFactory.create(uploaded_by=self.user) image_6 = ImageFactory.create(uploaded_by=self.user) image_7 = ImageFactory.create(uploaded_by=self.user) image_8 = ImageFactory.create(uploaded_by=self.user) image_9 = ImageFactory.create(uploaded_by=self.user) image_10 = ImageFactory.create(uploaded_by=self.user) response = self.client.get(reverse('images:latest_images')) expected_response_dict = { 'results': [ {'title': image_10.title, 'thumbnail': 'thumbnail', 'url': image_10.get_absolute_url()}, {'title': image_9.title, 'thumbnail': 'thumbnail', 'url': image_9.get_absolute_url()}, {'title': image_8.title, 'thumbnail': 'thumbnail', 'url': image_8.get_absolute_url()}, {'title': image_7.title, 'thumbnail': 'thumbnail', 'url': image_7.get_absolute_url()}, {'title': image_6.title, 'thumbnail': 'thumbnail', 'url': image_6.get_absolute_url()}, {'title': image_5.title, 'thumbnail': 'thumbnail', 'url': image_5.get_absolute_url()}, {'title': image_4.title, 'thumbnail': 'thumbnail', 'url': image_4.get_absolute_url()}, {'title': image_3.title, 'thumbnail': 'thumbnail', 'url': image_3.get_absolute_url()}, {'title': image_2.title, 'thumbnail': 'thumbnail', 'url': image_2.get_absolute_url()}, {'title': image_1.title, 'thumbnail': 'thumbnail', 'url': image_1.get_absolute_url()}, ] } self.assertJSONEqual(response.content.decode('utf-8'), json.dumps(expected_response_dict))