def test_get_mugshot_url_with_gravatar(self): """ Test if the correct mugshot is returned when the user makes use of gravatar. """ profile = Profile.objects.get(pk=1) gravatar_hash = hashlib.md5( profile.user.email.encode('utf-8')).hexdigest() # Test with the default settings mugshot_url = profile.get_mugshot_url() parsed = urlparse(mugshot_url) self.failUnlessEqual(parsed.netloc, 'www.gravatar.com') self.failUnlessEqual(parsed.path, '/avatar/' + gravatar_hash) self.failUnlessEqual( parse_qs(parsed.query), parse_qs( 's=%(size)s&d=%(default)s' % { 'size': userena_settings.USERENA_MUGSHOT_SIZE, 'default': userena_settings.USERENA_MUGSHOT_DEFAULT })) # Change userena settings userena_settings.USERENA_MUGSHOT_SIZE = 180 userena_settings.USERENA_MUGSHOT_DEFAULT = '404' # and test again mugshot_url = profile.get_mugshot_url() parsed = urlparse(mugshot_url) self.failUnlessEqual(parsed.netloc, 'www.gravatar.com') self.failUnlessEqual(parsed.path, '/avatar/' + gravatar_hash) self.failUnlessEqual( parse_qs(parsed.query), parse_qs( 's=%(size)s&d=%(default)s' % { 'size': userena_settings.USERENA_MUGSHOT_SIZE, 'default': userena_settings.USERENA_MUGSHOT_DEFAULT })) # Settings back to default userena_settings.USERENA_MUGSHOT_SIZE = 80 userena_settings.USERENA_MUGSHOT_DEFAULT = 'identicon'
def test_get_mugshot_url_with_gravatar(self): """ Test if the correct mugshot is returned when the user makes use of gravatar. """ profile = Profile.objects.get(pk=1) gravatar_hash = hashlib.md5(profile.user.email.encode('utf-8')).hexdigest() # Test with the default settings mugshot_url = profile.get_mugshot_url() parsed = urlparse(mugshot_url) self.failUnlessEqual(parsed.netloc, 'www.gravatar.com') self.failUnlessEqual(parsed.path, '/avatar/' + gravatar_hash) self.failUnlessEqual( parse_qs(parsed.query), parse_qs('s=%(size)s&d=%(default)s' % { 'size': userena_settings.USERENA_MUGSHOT_SIZE, 'default': userena_settings.USERENA_MUGSHOT_DEFAULT }) ) # Change userena settings userena_settings.USERENA_MUGSHOT_SIZE = 180 userena_settings.USERENA_MUGSHOT_DEFAULT = '404' # and test again mugshot_url = profile.get_mugshot_url() parsed = urlparse(mugshot_url) self.failUnlessEqual(parsed.netloc, 'www.gravatar.com') self.failUnlessEqual(parsed.path, '/avatar/' + gravatar_hash) self.failUnlessEqual( parse_qs(parsed.query), parse_qs('s=%(size)s&d=%(default)s' % { 'size': userena_settings.USERENA_MUGSHOT_SIZE, 'default': userena_settings.USERENA_MUGSHOT_DEFAULT }) ) # Settings back to default userena_settings.USERENA_MUGSHOT_SIZE = 80 userena_settings.USERENA_MUGSHOT_DEFAULT = 'identicon'
def test_get_gravatar(self): template = 's=%(size)s&d=%(type)s' # Check the defaults. parsed = urlparse(get_gravatar('*****@*****.**')) self.failUnlessEqual( parse_qs(parsed.query), parse_qs(template % { 'size': 80, 'type': 'identicon' })) # Check different size parsed = urlparse(get_gravatar('*****@*****.**', size=200)) self.failUnlessEqual( parse_qs(parsed.query), parse_qs(template % { 'size': 200, 'type': 'identicon' })) # Check different default parsed = urlparse(get_gravatar('*****@*****.**', default='404')) self.failUnlessEqual(parse_qs(parsed.query), parse_qs(template % { 'size': 80, 'type': '404' }))
def github_login( self, token_data=None, profile_data=None, email_data=None, process='login'): """ Mock a login to GitHub and return the response. Keyword Arguments: token_data - OAuth token data, or None for default profile_data - GitHub profile data, or None for default email_data - GitHub email data, or None for default process - 'login', 'connect', or 'redirect' """ login_url = reverse('github_login', locale=settings.WIKI_DEFAULT_LANGUAGE) callback_url = reverse('github_callback', unprefixed=True) # Ensure GitHub is setup as an auth provider self.ensure_github_app() # Start the login process # Store state in the session, and redirect the user to GitHub login_response = self.client.get(login_url, {'process': process}) assert login_response.status_code == 302 location = urlparse(login_response['location']) query = parse_qs(location.query) assert callback_url in query['redirect_uri'][0] state = query['state'][0] # Callback from GitHub, mock follow-on GitHub responses with requests_mock.Mocker() as mock_requests: # The callback view will make requests back to Github: # The OAuth2 authentication token (or error) mock_requests.post( GitHubOAuth2Adapter.access_token_url, json=token_data or self.github_token_data, headers={'content-type': 'application/json'}) # The authenticated user's profile data mock_requests.get( GitHubOAuth2Adapter.profile_url, json=profile_data or self.github_profile_data) # The user's emails, which could be an empty list if email_data is None: email_data = self.github_email_data mock_requests.get(GitHubOAuth2Adapter.emails_url, json=email_data) # Simulate the callback from Github data = {'code': 'github_code', 'state': state} response = self.client.get(callback_url, data, follow=True) return response
def github_login(self, token_data=None, profile_data=None, email_data=None, process='login'): """ Mock a login to GitHub and return the response. Keyword Arguments: token_data - OAuth token data, or None for default profile_data - GitHub profile data, or None for default email_data - GitHub email data, or None for default process - 'login', 'connect', or 'redirect' """ login_url = reverse('github_login', locale=settings.WIKI_DEFAULT_LANGUAGE) callback_url = reverse('github_callback', unprefixed=True) # Ensure GitHub is setup as an auth provider self.ensure_github_app() # Start the login process # Store state in the session, and redirect the user to GitHub login_response = self.client.get(login_url, {'process': process}) assert login_response.status_code == 302 location = urlparse(login_response['location']) query = parse_qs(location.query) assert callback_url in query['redirect_uri'][0] state = query['state'][0] # Callback from GitHub, mock follow-on GitHub responses with requests_mock.Mocker() as mock_requests: # The callback view will make requests back to Github: # The OAuth2 authentication token (or error) mock_requests.post(GitHubOAuth2Adapter.access_token_url, json=token_data or self.github_token_data, headers={'content-type': 'application/json'}) # The authenticated user's profile data mock_requests.get(GitHubOAuth2Adapter.profile_url, json=profile_data or self.github_profile_data) # The user's emails, which could be an empty list if email_data is None: email_data = self.github_email_data mock_requests.get(GitHubOAuth2Adapter.emails_url, json=email_data) # Simulate the callback from Github data = {'code': 'github_code', 'state': state} response = self.client.get(callback_url, data, follow=True) return response
def _form_urlencoded_data_deserializer(data): # param ``data`` is a URL encoded string (identical to what appears on a # querystring) try: # dic is in the form ``key: [value]`` dic = parse_qs(smart_text(data), strict_parsing=True) except ValueError: raise # For every ``key`` in ``dic``, that only has one item in the ``value`` # list, I flatted ``value`` return { k: (v if len(v) > 1 else v[0]) for k, v in dic.items() }
def handle(self, template, language=None, extra_request=None, **options): request = {} language_code = options.get('language_code', language) language = language_code or language or get_language() if not extra_request: extra_request = options.get('extra_request', []) if extra_request: request.update(parse_qs(extra_request, strict_parsing=True)) output = make_static(template, language, request) if options.get('output', False): with codecs.open(options.get('output'), 'w', 'utf-8') as output_file: output_file.write(output) else: self.stdout.write(output)
def test_l10n_updates_include_campaign(trans_doc, create_revision, edit_revision, client): """Translation URLs include GA campaign data.""" feed_url = reverse('wiki.feeds.l10n_updates', locale=trans_doc.locale, kwargs={'format': 'rss'}) resp = client.get(feed_url) assert resp.status_code == 200 feed = pq(resp.content) items = feed.find('item') assert len(items) == 1 desc_text = pq(items).find('description').text() desc_html = pq(desc_text) # Description is encoded HTML links = desc_html.find('a') assert len(links) == 3 for link in links: href = link.attrib['href'] querystring = parse_qs(urlparse(href).query) assert querystring['utm_campaign'] == ['feed'] assert querystring['utm_medium'] == ['rss'] assert querystring['utm_source'] == ['developer.mozilla.org']
def test_get_gravatar(self): template = 's=%(size)s&d=%(type)s' # Check the defaults. parsed = urlparse(get_gravatar('*****@*****.**')) self.failUnlessEqual( parse_qs(parsed.query), parse_qs(template % {'size': 80, 'type': 'identicon'}) ) # Check different size parsed = urlparse(get_gravatar('*****@*****.**', size=200)) self.failUnlessEqual( parse_qs(parsed.query), parse_qs(template % {'size': 200, 'type': 'identicon'}) ) # Check different default parsed = urlparse(get_gravatar('*****@*****.**', default='404')) self.failUnlessEqual( parse_qs(parsed.query), parse_qs(template % {'size': 80, 'type': '404'}) )
def page_from_url(url): page_values = parse_qs(urlsplit(url).query).get('page') if page_values: return int(page_values[0]) return 1