def test_cache_improperly_configured_works(self): with improperly_configured_cache_patch(): instance1 = factories.LatexImageFactory() instance2 = factories.LatexImageErrorFactory() self.assertEqual(LatexImage.objects.all().count(), 2) instance1.delete() instance2.delete() self.assertEqual(LatexImage.objects.all().count(), 0)
def test_delete_image_file_deleted(self): instance = factories.LatexImageFactory() import os upload_to, file_name = os.path.split(str(instance.image)) file_path = os.path.join(settings.MEDIA_ROOT, upload_to, file_name) self.assertTrue(os.path.isfile(file_path)) instance.delete() self.assertFalse(os.path.isfile(file_path))
def setUp(self): super().setUp() tex_key = "what_ever_key" self._obj = factories.LatexImageFactory(tex_key=tex_key, creator=self.test_user) self.assertEqual(LatexImage.objects.all().count(), 1) self.tex_key = tex_key # Here we assume all caches are lost for some reason self.test_cache.clear()
def test_image_create_image_url_not_cached(self): instance = factories.LatexImageFactory() self.assertIsNotNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "data_url"), instance.data_url)) self.assertIsNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "compile_error")))
def test_get_success_filter_fields(self): instance = factories.LatexImageFactory(creator=self.test_user) filter_fields = ["data_url", "creator"] resp = self.client.get( self.get_detail_url(instance.tex_key, fields=filter_fields)) self.assertEqual(resp.status_code, 200) response_dict = json.loads(resp.content.decode()) self.assertEqual(sorted(filter_fields), sorted(list(response_dict.keys())))
def test_post_different_code_no_convert_if_same_tex_key_exists(self): tex_key = "what_ever_key" _obj = factories.LatexImageFactory(tex_key=tex_key) self.assertEqual(LatexImage.objects.all().count(), 1) with mock.patch("latex.converter.Tex2ImgBase.get_converted_data_url" ) as mock_convert: resp = self.post_latex_form_view(data=self.get_post_data( tex_key=tex_key)) mock_convert.assert_not_called() self.assertEqual(resp.status_code, 200) self.assertEqual(LatexImage.objects.all().count(), 1) self.assertEqual(LatexImage.objects.all()[0].data_url, _obj.data_url)
def test_get_may_not_visit_others_image_except_superuser(self): another_user = factories.UserFactory() instance = factories.LatexImageFactory(creator=another_user) self.client.force_authenticate(user=another_user) resp = self.client.get(self.get_detail_url(instance.tex_key)) self.assertEqual(resp.status_code, 200) self.client.force_authenticate(user=self.test_user) resp = self.client.get(self.get_detail_url(instance.tex_key)) self.assertEqual(resp.status_code, 404) self.client.force_authenticate(user=self.superuser) resp = self.client.get(self.get_detail_url(instance.tex_key)) self.assertEqual(resp.status_code, 200)
def test_image_create_image_relative_path_cached(self): instance = factories.LatexImageFactory() self.assertIsNotNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "image"))) self.assertEqual(self.test_cache.get( get_field_cache_key(instance.tex_key, "image")), str(instance.image)) self.assertIsNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "data_url"))) self.assertIsNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "compile_error")))
def test_delete_image_cache_deleted(self): instance = factories.LatexImageFactory() serializer = LatexImageSerializer(instance) data = serializer.to_representation(instance) creator_cache_key = get_field_cache_key(instance.tex_key, "creator") self.test_cache.add(creator_cache_key, data["creator"]) self.assertEqual(self.test_cache.get(creator_cache_key), data["creator"]) creation_time_cache_key = get_field_cache_key( instance.tex_key, "creation_time") self.test_cache.add(creation_time_cache_key, data["creation_time"]) self.assertEqual( self.test_cache.get(creation_time_cache_key), data["creation_time"]) instance.delete() self.assertIsNone(self.test_cache.get(creation_time_cache_key)) self.assertIsNone( self.test_cache.get( get_field_cache_key(instance.tex_key, "compile_error")))
def test_get_success(self): instance = factories.LatexImageFactory(creator=self.test_user) resp = self.client.get(self.get_detail_url(instance.tex_key)) self.assertEqual(resp.status_code, 200)
def test_get_not_authenticated(self): instance = factories.LatexImageFactory() self.client.force_authenticate(user=None) resp = self.client.get(self.get_detail_url(instance.tex_key)) self.assertEqual(resp.status_code, 401)