def test_image_search(self): ImageFactory(title='fx2-quicktimeflash.png') ImageFactory(title='another-image.png') url = reverse('gallery.search', args=['image']) response = self.client.get(url, {'q': 'quicktime'}, follow=True) doc = pq(response.content) eq_(1, len(doc('#media-list li')))
def test_image_search(self): ImageFactory(title="fx2-quicktimeflash.png") ImageFactory(title="another-image.png") url = reverse("gallery.search", args=["image"]) response = self.client.get(url, {"q": "quicktime"}, follow=True) doc = pq(response.content) eq_(1, len(doc("#media-list li")))
def test_search_description(self): ImageFactory(description='This image was automatically migrated') ImageFactory(description='This image was automatically migrated') ImageFactory(description='This image was automatically') url = reverse('gallery.search', args=['image']) response = self.client.get(url, {'q': 'migrated'}, follow=True) doc = pq(response.content) eq_(2, len(doc('#media-list li')))
def test_search_description(self): ImageFactory(description="This image was automatically migrated") ImageFactory(description="This image was automatically migrated") ImageFactory(description="This image was automatically") url = reverse("gallery.search", args=["image"]) response = self.client.get(url, {"q": "migrated"}, follow=True) doc = pq(response.content) eq_(2, len(doc("#media-list li")))
def test_thumbnail_url_if_set(self): """thumbnail_url_if_set() returns self.thumbnail if set, or else returns self.file""" img = ImageFactory() eq_(img.file.url, img.thumbnail_url_if_set()) generate_thumbnail(img, 'file', 'thumbnail') eq_(img.thumbnail.url, img.thumbnail_url_if_set())
def test_image_media_page(self): """Test the media page.""" img = ImageFactory() response = self.client.get(img.get_absolute_url(), follow=True) eq_(200, response.status_code) doc = pq(response.content) eq_(img.title, doc('h1').text()) eq_(img.description, doc('#media-object div.description').text()) eq_(img.file.url, doc('#media-view img')[0].attrib['src'])
def test_image_media_page(self): """Test the media page.""" img = ImageFactory() response = self.client.get(img.get_absolute_url(), follow=True) eq_(200, response.status_code) doc = pq(response.content) eq_(img.title, doc("h1").text()) eq_(img.description, doc("#media-object div.description").text()) eq_(img.file.url, doc("#media-view img")[0].attrib["src"])
def test_thumbnail_url_if_set(self, create_thumbnail_mock): """thumbnail_url_if_set() returns self.thumbnail if set, or else returns self.file""" img = ImageFactory() eq_(img.file.url, img.thumbnail_url_if_set()) create_thumbnail_mock.return_value = ContentFile('the dude') generate_thumbnail(img, 'file', 'thumbnail') eq_(img.thumbnail.url, img.thumbnail_url_if_set())
def test_thumbnail_url_if_set(self, create_thumbnail_mock): """thumbnail_url_if_set() returns self.thumbnail if set, or else returns self.file""" img = ImageFactory() eq_(img.file.url, img.thumbnail_url_if_set()) create_thumbnail_mock.return_value = ContentFile("the dude") generate_thumbnail(img, "file", "thumbnail") eq_(img.thumbnail.url, img.thumbnail_url_if_set())
def test_gallery_image_list(self): """Test for ajax endpoint without search parameter.""" img = ImageFactory() url = urlparams(reverse('gallery.async'), type='image') response = self.client.get(url, follow=True) eq_(200, response.status_code) doc = pq(response.content) imgs = doc('#media-list li img') eq_(1, len(imgs)) eq_(img.thumbnail_url_if_set(), imgs[0].attrib['src'])
def test_gallery_image_list(self): """Test for ajax endpoint without search parameter.""" img = ImageFactory() url = urlparams(reverse("gallery.async"), type="image") response = self.client.get(url, follow=True) eq_(200, response.status_code) doc = pq(response.content) imgs = doc("#media-list li img") eq_(1, len(imgs)) eq_(img.thumbnail_url_if_set(), imgs[0].attrib["src"])
def test_gallery_images(self): """Test that all images show up on images gallery page. Also, Make sure they don't show up on videos page. """ img = ImageFactory() response = get(self.client, 'gallery.gallery', args=['image']) eq_(200, response.status_code) doc = pq(response.content) imgs = doc('#media-list li img') eq_(1, len(imgs)) eq_(img.thumbnail_url_if_set(), imgs[0].attrib['src'])
def test_gallery_images(self): """Test that all images show up on images gallery page. Also, Make sure they don't show up on videos page. """ img = ImageFactory() response = get(self.client, "gallery.gallery", args=["image"]) eq_(200, response.status_code) doc = pq(response.content) imgs = doc("#media-list li img") eq_(1, len(imgs)) eq_(img.thumbnail_url_if_set(), imgs[0].attrib["src"])
class TestLazyWikiImageTags(TestCase): def setUp(self): self.d, self.r, self.p = doc_rev_parser("Test content", "Installing Firefox") self.img = ImageFactory(title="test.jpg") def tearDown(self): self.img.delete() def test_simple(self): """Simple image tag markup.""" doc = pq(self.p.parse("[[Image:test.jpg]]", locale=settings.WIKI_DEFAULT_LANGUAGE)) img = doc("img") eq_("test.jpg", img.attr("alt")) eq_(self.img.file.url, img.attr("data-original-src")) assert "placeholder.gif" in img.attr("src")
def test_full_fallback(self): """Find current locale's image, not the English one.""" # first, pretend there is no English version self.img.locale = 'ja' self.img.save() img = pq_img(self.p, '[[Image:test.jpg]]', selector='img', locale='ja') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src')) # then, create an English version en_img = ImageFactory(title='test.jpg', locale='en-US') # Ensure they're not equal self.assertNotEquals(en_img.file.url, self.img.file.url) # make sure there is no fallback img = pq_img(self.p, '[[Image:test.jpg]]', selector='img', locale='ja') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src')) # now delete the English version self.img.delete() self.img = en_img # don't break tearDown img = pq_img(self.p, '[[Image:test.jpg]]', selector='img', locale='ja') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src'))
def test_full_fallback(self): """Find current locale's image, not the English one.""" # first, pretend there is no English version self.img.locale = "ja" self.img.save() img = pq_img(self.p, "[[Image:test.jpg]]", selector="img", locale="ja") eq_("test.jpg", img.attr("alt")) eq_(self.img.file.url, img.attr("src")) # then, create an English version en_img = ImageFactory(title="test.jpg", locale="en-US") # Ensure they're not equal self.assertNotEqual(en_img.file.url, self.img.file.url) # make sure there is no fallback img = pq_img(self.p, "[[Image:test.jpg]]", selector="img", locale="ja") eq_("test.jpg", img.attr("alt")) eq_(self.img.file.url, img.attr("src")) # now delete the English version self.img.delete() self.img = en_img # don't break tearDown img = pq_img(self.p, "[[Image:test.jpg]]", selector="img", locale="ja") eq_("test.jpg", img.attr("alt")) eq_(self.img.file.url, img.attr("src"))
def test_images(self): img = ImageFactory(title='image-file.png') d1, _, _ = doc_rev_parser('[[Image:image-file.png]]', title='D1') eq_(len(d1.images), 1) eq_(d1.images[0], img) eq_(len(img.documents), 1) eq_(img.documents[0], d1)
def test_gallery_image_search(self): """Test for ajax endpoint with search parameter.""" img = ImageFactory() url = urlparams(reverse('gallery.async'), type='image', q='foobar') response = self.client.get(url, follow=True) eq_(200, response.status_code) doc = pq(response.content) imgs = doc('#media-list li img') eq_(0, len(imgs)) url = urlparams(reverse('gallery.async'), type='image', q=img.title) response = self.client.get(url, follow=True) eq_(200, response.status_code) doc = pq(response.content) imgs = doc('#media-list li img') eq_(1, len(imgs)) eq_(img.thumbnail_url_if_set(), imgs[0].attrib['src'])
class TestLazyWikiImageTags(TestCase): def setUp(self): self.d, self.r, self.p = doc_rev_parser( 'Test content', 'Installing Firefox') self.img = ImageFactory(title='test.jpg') def tearDown(self): self.img.delete() def test_simple(self): """Simple image tag markup.""" doc = pq(self.p.parse('[[Image:test.jpg]]', locale=settings.WIKI_DEFAULT_LANGUAGE)) img = doc('img') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('data-original-src')) assert 'placeholder.gif' in img.attr('src')
def test_images(self): img = ImageFactory(title="image-file.png") d1, _, _ = doc_rev_parser("[[Image:image-file.png]]", title="D1") eq_(len(d1.images), 1) eq_(d1.images[0], img) eq_(len(img.documents), 1) eq_(img.documents[0], d1)
def test_image_draft_shows(self): """The image draft is loaded for this user.""" img = ImageFactory(is_draft=True, creator=self.u) response = get(self.client, "gallery.gallery", args=["image"]) eq_(200, response.status_code) doc = pq(response.content) assert doc(".file.preview img").attr("src").endswith(img.file.name) eq_(1, doc(".file.preview img").length)
def test_delete_image_without_permissions(self): """Can't delete an image I didn't create.""" img = ImageFactory() u = UserFactory() self.client.login(username=u.username, password='******') r = post(self.client, 'gallery.delete_media', args=['image', img.id]) eq_(403, r.status_code) eq_(1, Image.objects.count())
def test_button_image_for_nesting(self): """You can nest [[Image:]] inside {for} inside {button}.""" ImageFactory(title='image-file.png') text = '{button {for mac}[[Image:image-file.png]]{/for} text}' p = WikiParser() doc = pq(p.parse(text)) assert 'frameless' in doc('img').attr('class') eq_(0, doc('div.caption').length) eq_(0, doc('div.img').length)
def test_delete_own_image(self): """Can delete an image I created.""" u = UserFactory() self.client.login(username=u.username, password='******') img = ImageFactory(creator=u) r = post(self.client, 'gallery.delete_media', args=['image', img.id]) eq_(200, r.status_code) eq_(0, Image.objects.count())
def test_button_image_for_nesting(self): """You can nest [[Image:]] inside {for} inside {button}.""" ImageFactory(title="image-file.png") text = "{button {for mac}[[Image:image-file.png]]{/for} text}" p = WikiParser() doc = pq(p.parse(text)) assert "frameless" in doc("img").attr("class") eq_(0, doc("div.caption").length) eq_(0, doc("div.img").length)
class TestLazyWikiImageTags(TestCase): def setUp(self): self.d, self.r, self.p = doc_rev_parser('Test content', 'Installing Firefox') self.img = ImageFactory(title='test.jpg') def tearDown(self): self.img.delete() def test_simple(self): """Simple image tag markup.""" doc = pq( self.p.parse('[[Image:test.jpg]]', locale=settings.WIKI_DEFAULT_LANGUAGE)) img = doc('img') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('data-original-src')) assert 'placeholder.gif' in img.attr('src')
def test_image_draft_post(self): """Posting to the page saves the field values for the image draft.""" ImageFactory(is_draft=True, creator=self.u) response = post(self.client, 'gallery.gallery', {'description': '??', 'title': 'test'}, args=['image']) eq_(200, response.status_code) doc = pq(response.content) # Preview for all 3 video formats: flv, ogv, webm eq_('??', doc('#gallery-upload-modal textarea').html().strip()) eq_('test', doc('#gallery-upload-modal input[name="title"]').val())
def test_delete_image(self): """Deleting an uploaded image works.""" im = ImageFactory() u = UserFactory() add_permission(u, Image, 'delete_image') self.client.login(username=u.username, password='******') r = post(self.client, 'gallery.delete_media', args=['image', im.id]) eq_(200, r.status_code) eq_(0, Image.objects.count())
def test_edit_image_without_permissions(self): """Can't edit an image I didn't create.""" img = ImageFactory() u = UserFactory() self.client.login(username=u.username, password='******') r = post(self.client, 'gallery.edit_media', {'description': 'arrr'}, args=['image', img.id]) eq_(403, r.status_code)
def test_edit_image_without_permissions(self): """Can't edit an image I didn't create.""" img = ImageFactory() u = UserFactory() self.client.login(username=u.username, password="******") r = post( self.client, "gallery.edit_media", {"description": "arrr"}, args=["image", img.id] ) eq_(403, r.status_code)
def test_edit_own_image(self): """Can edit an image I created.""" u = UserFactory() img = ImageFactory(creator=u) self.client.login(username=u.username, password='******') r = post(self.client, 'gallery.edit_media', {'description': 'arrr'}, args=['image', img.id]) eq_(200, r.status_code) eq_('arrr', Image.objects.get().description)
def test_schedule_rebuild_kb_on_delete(self, schedule_rebuild_kb): """KB rebuild scheduled on delete""" im = ImageFactory() u = UserFactory() add_permission(u, Image, 'delete_image') self.client.login(username=u.username, password='******') r = post(self.client, 'gallery.delete_media', args=['image', im.id]) eq_(200, r.status_code) eq_(0, Image.objects.count()) assert schedule_rebuild_kb.called
def test_edit_own_image(self): """Can edit an image I created.""" u = UserFactory() img = ImageFactory(creator=u) self.client.login(username=u.username, password="******") r = post( self.client, "gallery.edit_media", {"description": "arrr"}, args=["image", img.id] ) eq_(200, r.status_code) eq_("arrr", Image.objects.get().description)
def test_edit_image_with_permissions(self): """Editing image sets the updated_by field.""" img = ImageFactory() u = UserFactory() add_permission(u, Image, 'change_image') self.client.login(username=u.username, password='******') r = post(self.client, 'gallery.edit_media', {'description': 'arrr'}, args=['image', img.id]) eq_(200, r.status_code) eq_(u.username, Image.objects.get().updated_by.username)
def test_edit_image_with_permissions(self): """Editing image sets the updated_by field.""" img = ImageFactory() u = UserFactory() add_permission(u, Image, "change_image") self.client.login(username=u.username, password="******") r = post( self.client, "gallery.edit_media", {"description": "arrr"}, args=["image", img.id] ) eq_(200, r.status_code) eq_(u.username, Image.objects.get().updated_by.username)
def setUp(self): self.d, self.r, self.p = doc_rev_parser( 'Test content', 'Installing Firefox') self.img = ImageFactory(title='test.jpg')
class TestWikiImageTags(TestCase): def setUp(self): self.d, self.r, self.p = doc_rev_parser( 'Test content', 'Installing Firefox') self.img = ImageFactory(title='test.jpg') def tearDown(self): self.img.delete() def test_empty(self): """Empty image tag markup does not change.""" img = pq_img(self.p, '[[Image:]]', 'p') eq_('The image "" does not exist.', img.text()) def test_simple(self): """Simple image tag markup.""" img = pq_img(self.p, '[[Image:test.jpg]]', 'img') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src')) def test_simple_fallback(self): """Fallback to English if current locale doesn't have the image.""" img = pq_img(self.p, '[[Image:test.jpg]]', selector='img', locale='ja') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src')) def test_full_fallback(self): """Find current locale's image, not the English one.""" # first, pretend there is no English version self.img.locale = 'ja' self.img.save() img = pq_img(self.p, '[[Image:test.jpg]]', selector='img', locale='ja') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src')) # then, create an English version en_img = ImageFactory(title='test.jpg', locale='en-US') # Ensure they're not equal self.assertNotEquals(en_img.file.url, self.img.file.url) # make sure there is no fallback img = pq_img(self.p, '[[Image:test.jpg]]', selector='img', locale='ja') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src')) # now delete the English version self.img.delete() self.img = en_img # don't break tearDown img = pq_img(self.p, '[[Image:test.jpg]]', selector='img', locale='ja') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src')) def test_caption(self): """Give the image a caption.""" self.img.title = 'img test.jpg' self.img.save() img_div = pq_img(self.p, '[[Image:img test.jpg|frame|my caption]]', 'div.img') img = img_div('img') caption = img_div.text() eq_(self.img.file.url, img.attr('src')) eq_('my caption', img.attr('alt')) eq_('my caption', caption) def test_page_link(self): """Link to a wiki page.""" img_a = pq_img(self.p, '[[Image:test.jpg|page=Installing Firefox]]', 'a') img = img_a('img') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src')) eq_('/en-US/kb/installing-firefox', img_a.attr('href')) def test_page_link_edit(self): """Link to a nonexistent wiki page.""" img_a = pq_img(self.p, '[[Image:test.jpg|page=Article List]]', 'a') img = img_a('img') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src')) assert img_a.hasClass('new') eq_('/en-US/kb/new?title=Article+List', img_a.attr('href')) def test_page_link_caption(self): """Link to a wiki page with caption and frame.""" img_div = pq_img(self.p, '[[Image:test.jpg|frame|page=A page|my caption]]', 'div.img') img_a = img_div('a') img = img_a('img') caption = img_div.text() eq_('my caption', img.attr('alt')) eq_('my caption', caption) eq_(self.img.file.url, img.attr('src')) assert img_a.hasClass('new') eq_('/en-US/kb/new?title=A+page', img_a.attr('href')) def test_link(self): """Link to an external page.""" img_a = pq_img(self.p, '[[Image:test.jpg|link=http://test.com]]', 'a') img = img_a('img') eq_('test.jpg', img.attr('alt')) eq_(self.img.file.url, img.attr('src')) eq_('http://test.com', img_a.attr('href')) def test_link_caption(self): """Link to an external page with caption.""" img_div = pq_img(self.p, '[[Image:test.jpg|link=http://ab.us|frame|caption]]', 'div.img') img = img_div('img') img_a = img_div('a') eq_(self.img.file.url, img.attr('src')) eq_('http://ab.us', img_a.attr('href')) def test_link_align(self): """Link with align.""" img_div = pq_img(self.p, '[[Image:test.jpg|link=http://site.com|align=left]]', 'div.img') eq_('img align-left', img_div.attr('class')) def test_link_align_invalid(self): """Link with invalid align.""" img = pq_img(self.p, '[[Image:test.jpg|link=http://example.ro|align=inv]]') assert 'frameless' in img.attr('class') def test_link_valign(self): """Link with valign.""" img = pq_img(self.p, '[[Image:test.jpg|link=http://example.com|valign=top]]') eq_('vertical-align: top;', img.attr('style')) def test_link_valign_invalid(self): """Link with invalid valign.""" img = pq_img(self.p, '[[Image:test.jpg|link=http://example.com|valign=off]]') eq_(None, img.attr('style')) def test_alt(self): """Image alt attribute is overriden but caption is not.""" img_div = pq_img(self.p, '[[Image:test.jpg|alt=my alt|frame|my caption]]', 'div.img') img = img_div('img') caption = img_div.text() eq_('my alt', img.attr('alt')) eq_('my caption', caption) def test_alt_empty(self): """Image alt attribute can be empty.""" img = pq_img(self.p, '[[Image:test.jpg|alt=|my caption]]') eq_('', img.attr('alt')) def test_alt_unsafe(self): """Potentially unsafe alt content is escaped.""" unsafe_vals = ( ('an"<script>alert()</script>', 'an"&lt;script&gt;alert()&lt;/script&gt;'), ("an'<script>alert()</script>", "an'&lt;script&gt;alert()&lt;/script&gt;"), ('single\'"double', "single'"double"), ) for alt_sent, alt_expected in unsafe_vals: img = pq_img(self.p, '[[Image:test.jpg|alt=' + alt_sent + ']]') is_true = str(img).startswith('<img alt="' + alt_expected + '"') assert is_true, ('Expected "%s", sent "%s"' % (alt_expected, alt_sent)) def test_width(self): """Image width attribute set.""" img = pq_img(self.p, '[[Image:test.jpg|width=10]]') eq_('10', img.attr('width')) def test_width_invalid(self): """Invalid image width attribute set to auto.""" img = pq_img(self.p, '[[Image:test.jpg|width=invalid]]') eq_(None, img.attr('width')) def test_height(self): """Image height attribute set.""" img = pq_img(self.p, '[[Image:test.jpg|height=10]]') eq_('10', img.attr('height')) def test_height_invalid(self): """Invalid image height attribute set to auto.""" img = pq_img(self.p, '[[Image:test.jpg|height=invalid]]') eq_(None, img.attr('height')) def test_frame(self): """Image has frame if specified.""" img_div = pq_img(self.p, '[[Image:test.jpg|frame|caption]]', 'div.img') assert not img_div('img').hasClass('frameless') eq_('caption', img_div('img').attr('alt')) eq_('caption', img_div.text()) eq_(self.img.file.url, img_div('img').attr('src')) def test_frameless_link(self): """Image has frameless class and link if specified.""" img_a = pq_img(self.p, '[[Image:test.jpg|page=Installing Firefox]]', 'a') img = img_a('img') assert 'frameless' in img.attr('class') eq_('/en-US/kb/installing-firefox', img_a.attr('href'))