def _reset_avatar_services(): avatar_services.reset() gravatar_service = avatar_services.get_avatar_service( GravatarService.avatar_service_id) avatar_services.enable_service(gravatar_service.avatar_service_id, save=False) avatar_services.set_default_service(gravatar_service, save=False)
def test_put_with_render_avatars_at(self): """Testing the PUT <URL> API with render_avatars_at=...""" avatar_services.register(SimpleRenderAvatarService) avatar_services.enable_service(SimpleRenderAvatarService, save=False) avatar_services.set_default_service(SimpleRenderAvatarService) user = self.create_user(username='******', password='******') self.assertTrue(self.client.login(username='******', password='******')) rsp = self.api_put( get_user_item_url(user.username), { 'first_name': 'new-name', 'render_avatars_at': '24,abc,48,,128', }, expected_mimetype=user_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.assertEqual( rsp['user']['avatar_html'], { '24': '<div class="avatar" data-size="24">test-user</div>', '48': '<div class="avatar" data-size="48">test-user</div>', '128': '<div class="avatar" data-size="128">test-user</div>', })
def test_username_unsafe(self): """Testing avatar template tag rendering with an unsafe username""" t = Template('{% load avatars %}' '{% avatar target_user 32 avatar_service_id %}') self.request.user = User.objects.get(username='******') user = User.objects.create_user(first_name='<b>Bad', last_name='User</b>', username='******', email='*****@*****.**') Profile.objects.create(user=user) avatar_services.register(DummyAvatarService) avatar_services.enable_service(DummyAvatarService) self.assertHTMLEqual( t.render( RequestContext( self.request, { 'target_user': user, 'avatar_service_id': DummyAvatarService.avatar_service_id, })), '<img src="http://example.com/avatar.png" alt="%s" width="32"' ' height="32" srcset="http://example.com/avatar.png 1x"' ' class="avatar">\n' % '<b>Bad User</b>')
def test_username_unsafe(self): """Testing avatar template tag rendering with an unsafe username""" t = Template('{% load avatars %}' '{% avatar target_user 32 avatar_service_id %}') user = User.objects.create( first_name='<b>Bad', last_name='User</b>', username='******') Profile.objects.create(user=user) escaped_user = escape(user.get_full_name()) avatar_services.register(DummyAvatarService) avatar_services.enable_service(DummyAvatarService) self.assertHTMLEqual( t.render(RequestContext(self.request, { 'target_user': user, 'avatar_service_id': DummyAvatarService.avatar_service_id, })), '<img src="http://example.com/avatar.png" alt="%s" width="32"' ' height="32" srcset="http://example.com/avatar.png 1x"' ' class="avatar">\n' % escaped_user)
def test_post_with_render_avatars_at(self): """Testing the POST <URL> API with render_avatars_at=...""" self.client.login(username='******', password='******') avatar_services.register(SimpleRenderAvatarService) avatar_services.enable_service(SimpleRenderAvatarService, save=False) avatar_services.set_default_service(SimpleRenderAvatarService) rsp = self.api_post( get_user_list_url(), { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'render_avatars_at': '24,abc,48,,128', }, expected_mimetype=user_item_mimetype) self.assertEqual(rsp['stat'], 'ok') self.assertEqual( rsp['user']['avatar_html'], { '24': '<div class="avatar" data-size="24">myuser</div>', '48': '<div class="avatar" data-size="48">myuser</div>', '128': '<div class="avatar" data-size="128">myuser</div>', })
def test_username_unsafe(self): """Testing avatar template tag rendering with an unsafe username""" t = Template('{% load avatars %}' '{% avatar user 32 avatar_service_id %}') user = User.objects.create(first_name='<b>Bad', last_name='User</b>', username='******') Profile.objects.create(user=user) escaped_user = escape(user.get_full_name()) avatar_services.register(DummyAvatarService) avatar_services.enable_service(DummyAvatarService) self.assertHTMLEqual( t.render( Context({ 'user': user, 'request': self.request, 'avatar_service_id': DummyAvatarService.avatar_service_id, })), '<img src="http://example.com/avatar.png" alt="%s" width="32"' ' height="32" srcset="http://example.com/avatar.png 1x"' ' class="avatar">\n' % escaped_user)
def setUp(self): super(TemplateTagTests, self).setUp() avatar_services.enable_service(GravatarService, save=False) avatar_services.set_default_service(GravatarService, save=False) self.user = User.objects.get(username='******') self.request = RequestFactory().get('/') self.request.user = AnonymousUser()
def test_register(self): """Testing AvatarServiceHook registers services""" self.assertNotIn(DummyAvatarService, avatar_services) AvatarServiceHook(self.extension, DummyAvatarService, start_enabled=True) self.assertIn(DummyAvatarService, avatar_services) avatar_services.enable_service(DummyAvatarService, save=False) self.assertTrue(avatar_services.is_enabled(DummyAvatarService))
def setUp(self): super(TemplateTagTests, self).setUp() gravatar_service = avatar_services.get_avatar_service( GravatarService.avatar_service_id) avatar_services.enable_service(gravatar_service.avatar_service_id, save=False) avatar_services.set_default_service(gravatar_service, save=False) self.user = User.objects.get(username='******') self.request = HttpRequest() self.request.user = AnonymousUser()
def test_avatar_with_string_size_invalid(self): """Testing {% avatar %} template tag with invalid string size""" avatar_services.register(DummyAvatarService) avatar_services.enable_service(DummyAvatarService) template = Template('{% load avatars %}' '{% avatar target_user "ABC" avatar_service_id %}') self.assertEqual( template.render(RequestContext(self.request, { 'target_user': self.user, 'avatar_service_id': DummyAvatarService.avatar_service_id, })), '')
def test_avatar_with_string_size_invalid(self): """Testing {% avatar %} template tag with invalid string size""" avatar_services.register(DummyAvatarService) avatar_services.enable_service(DummyAvatarService) template = Template('{% load avatars %}' '{% avatar target_user "ABC" avatar_service_id %}') self.assertEqual( template.render( RequestContext( self.request, { 'target_user': self.user, 'avatar_service_id': DummyAvatarService.avatar_service_id, })), '')
def test_avatar_urls_with_service(self): """Testing {% avatar_urls %} template tag with avatar_service_id""" service = DummyAvatarService(use_2x=True) avatar_services.register(service) avatar_services.enable_service(service.avatar_service_id) t = Template('{% load avatars %}' '{% avatar_urls u 32 service_id %}') self.assertEqual( t.render( RequestContext(self.request, { 'u': self.user, 'service_id': service.avatar_service_id, })), '{' '"1x": "%(1x)s", ' '"2x": "%(2x)s"' '}' % service.get_avatar_urls_uncached(self.user, 32))
def test_avatar_with_string_size_valid(self): """Testing {% avatar %} template tag with string-encoded int size""" avatar_services.register(DummyAvatarService) avatar_services.enable_service(DummyAvatarService) template = Template('{% load avatars %}' '{% avatar target_user "32" avatar_service_id %}') self.assertHTMLEqual( template.render(RequestContext(self.request, { 'target_user': self.user, 'avatar_service_id': DummyAvatarService.avatar_service_id, })), ('<img src="http://example.com/avatar.png" alt="%s" width="32"' ' height="32" srcset="http://example.com/avatar.png 1x"' ' class="avatar djblets-o-avatar">\n' % self.user.username))
def test_get_with_render_avatars_at(self): """Testing the GET <URL> API with ?render-avatars-at=...""" avatar_services.register(SimpleRenderAvatarService) avatar_services.enable_service(SimpleRenderAvatarService, save=False) avatar_services.set_default_service(SimpleRenderAvatarService) rsp = self.api_get( get_user_list_url(), { 'render-avatars-at': '24,abc,48,,128', }, expected_mimetype=user_list_mimetype) self.assertEqual(rsp['stat'], 'ok') self.assertEqual(len(rsp['users']), 4) self.assertEqual( rsp['users'][0]['avatar_html'], { '24': '<div class="avatar" data-size="24">admin</div>', '48': '<div class="avatar" data-size="48">admin</div>', '128': '<div class="avatar" data-size="128">admin</div>', }) self.assertEqual( rsp['users'][1]['avatar_html'], { '24': '<div class="avatar" data-size="24">doc</div>', '48': '<div class="avatar" data-size="48">doc</div>', '128': '<div class="avatar" data-size="128">doc</div>', }) self.assertEqual( rsp['users'][2]['avatar_html'], { '24': '<div class="avatar" data-size="24">dopey</div>', '48': '<div class="avatar" data-size="48">dopey</div>', '128': '<div class="avatar" data-size="128">dopey</div>', }) self.assertEqual( rsp['users'][3]['avatar_html'], { '24': '<div class="avatar" data-size="24">grumpy</div>', '48': '<div class="avatar" data-size="48">grumpy</div>', '128': '<div class="avatar" data-size="128">grumpy</div>', })
def test_custom_avatar_service(self): """Test avatar template tag rendering a specific avatar service""" avatar_services.register(DummyAvatarService()) avatar_services.enable_service(DummyAvatarService.id) t = Template('{% load avatars %}' '{% avatar user 32 avatar_service_id %}') self.assertEqual( t.render(Context({ 'user': self.user, 'avatar_service_id': DummyAvatarService.id, 'request': self.request, })), '<img src="http://example.com/avatar.png" alt="%s" width="32"' ' height="32" srcset="http://example.com/avatar.png 1x">\n' % self.user.get_full_name() or self.user.username )
def test_avatar_specific_service(self): """Testing {% avatar %} template tag using a specific avatar service""" avatar_services.register(DummyAvatarService) avatar_services.enable_service(DummyAvatarService) t = Template('{% load avatars %}' '{% avatar target_user 32 avatar_service_id %}') self.assertHTMLEqual( t.render(RequestContext(self.request, { 'target_user': self.user, 'avatar_service_id': DummyAvatarService.avatar_service_id, })), ('<img src="http://example.com/avatar.png" alt="%s" width="32"' ' height="32" srcset="http://example.com/avatar.png 1x"' ' class="avatar djblets-o-avatar">\n' % self.user.username) )
def test_avatar_specific_service(self): """Testing {% avatar %} template tag using a specific avatar service""" avatar_services.register(DummyAvatarService) avatar_services.enable_service(DummyAvatarService) t = Template('{% load avatars %}' '{% avatar target_user 32 avatar_service_id %}') self.assertHTMLEqual( t.render( RequestContext( self.request, { 'target_user': self.user, 'avatar_service_id': DummyAvatarService.avatar_service_id, })), ('<img src="http://example.com/avatar.png" alt="%s" width="32"' ' height="32" srcset="http://example.com/avatar.png 1x"' ' class="avatar">\n' % self.user.username))
def test_custom_avatar_service(self): """Test avatar template tag rendering a specific avatar service""" avatar_services.register(DummyAvatarService) avatar_services.enable_service(DummyAvatarService) t = Template('{% load avatars %}' '{% avatar user 32 avatar_service_id %}') self.assertHTMLEqual( t.render( Context({ 'user': self.user, 'avatar_service_id': DummyAvatarService.avatar_service_id, 'request': self.request, })), '<img src="http://example.com/avatar.png" alt="%s" width="32"' ' height="32" srcset="http://example.com/avatar.png 1x"' ' class="avatar">\n' % self.user.get_full_name() or self.user.username)
def test_avatar_with_string_size_valid(self): """Testing {% avatar %} template tag with string-encoded int size""" avatar_services.register(DummyAvatarService) avatar_services.enable_service(DummyAvatarService) template = Template('{% load avatars %}' '{% avatar target_user "32" avatar_service_id %}') self.assertHTMLEqual( template.render( RequestContext( self.request, { 'target_user': self.user, 'avatar_service_id': DummyAvatarService.avatar_service_id, })), ('<img src="http://example.com/avatar.png" alt="%s" width="32"' ' height="32" srcset="http://example.com/avatar.png 1x"' ' class="avatar djblets-o-avatar">\n' % self.user.username))
def test_get_avatar_service_no_urls(self): """Testing the GET <URL> API when the avatar service returns no URLs """ avatar_services.register(NoURLAvatarService) avatar_services.enable_service(NoURLAvatarService, save=False) dopey = User.objects.get(username='******') settings_mgr = avatar_services.settings_manager_class(dopey) settings_mgr.avatar_service_id = NoURLAvatarService.avatar_service_id settings_mgr.save() rsp = self.api_get(get_user_item_url('dopey'), expected_mimetype=user_item_mimetype) self.assertEqual(rsp['stat'], 'ok') user_rsp = rsp['user'] self.assertIn('avatar_url', user_rsp) self.assertIsNone(user_rsp['avatar_url']) self.assertIn('avatar_urls', user_rsp) self.assertEqual(user_rsp['avatar_urls'], {})
def test_avatar_urls_with_service(self): """Testing {% avatar_urls %} template tag with avatar_service_id""" service = DummyAvatarService(use_2x=True) avatar_services.register(service) avatar_services.enable_service(service.avatar_service_id) t = Template( '{% load avatars %}' '{% avatar_urls u 32 service_id %}' ) self.assertEqual( t.render(RequestContext(self.request, { 'u': self.user, 'service_id': service.avatar_service_id, })), '{' '"1x": "%(1x)s", ' '"2x": "%(2x)s"' '}' % service.get_avatar_urls_uncached(self.user, 32) )
def setUpClass(cls): super(TemplateTagTests, cls).setUpClass() avatar_services.enable_service(GravatarService.avatar_service_id, save=False)
def setUp(self): super(ResourceItemTests, self).setUp() avatar_services.enable_service(GravatarService, save=False)