def assertURLEqual(self, url1, url2): parts1 = urlparse.urlparse(url1) parts2 = urlparse.urlparse(url2) self.assertEqual(parts1.scheme, parts2.scheme) self.assertEqual(parts1.hostname, parts2.hostname) self.assertEqual(parts1.netloc, parts2.netloc) self.assertEqual(parts1.params, parts2.params) self.assertEqual(parts1.path, parts2.path) self.assertEqual(parts1.port, parts2.port) self.assertEqual(urlparse.parse_qs(parts1.query), urlparse.parse_qs(parts2.query))
def test_oauth2_step1(self): with patch('uuid.uuid4') as fake: fake.return_value = 'random-string' request = DummyRequest() request.params = {'next_url': 'http://localhost/'} request.session = {} response = oauth2_step1( request=request, auth_uri='http://example.com/oauth2/auth', client_id='1234', redirect_url='http://localhost/oauth2/callback', scope='scope1 scope2') self.assertEqual(response.status, '302 Found') url = urlparse.urlparse(response.location) self.assertEqual(url.netloc, 'example.com') self.assertEqual(url.path, '/oauth2/auth') query = urlparse.parse_qs(url.query) self.assertEqual( query, { 'scope': ['scope1 scope2'], 'state': ['random-string'], 'redirect_uri': ['http://localhost/oauth2/callback'], 'response_type': ['code'], 'client_id': ['1234'], }) self.assertEqual(request.session['next_url'], 'http://localhost/')
def test_oauth2_step1(self): with patch('uuid.uuid4') as fake: fake.return_value = 'random-string' request = DummyRequest() request.params = {'next_url': 'http://localhost/'} request.session = {} response = oauth2_step1( request=request, auth_uri='http://example.com/oauth2/auth', client_id='1234', redirect_url='http://localhost/oauth2/callback', scope='scope1 scope2' ) self.assertEqual(response.status, '302 Found') url = urlparse.urlparse(response.location) self.assertEqual(url.netloc, 'example.com') self.assertEqual(url.path, '/oauth2/auth') query = urlparse.parse_qs(url.query) self.assertEqual(query, { 'scope': ['scope1 scope2'], 'state': ['random-string'], 'redirect_uri': ['http://localhost/oauth2/callback'], 'response_type': ['code'], 'client_id': ['1234'], }) self.assertEqual(request.session['next_url'], 'http://localhost/')
def _assert_error(self, url, error, description=None): query = urlparse.parse_qs(urlparse.urlparse(url).query) expected = {'error': [error]} if description: expected['error_description'] = [description] self.assertEqual(query, expected)
def test_facebook_callback(self): # call the login to fill the session res = self.testapp.get('/facebook/login', { 'next_url': 'https://localhost/foo/bar', }) self.assertEqual(res.status, '302 Found') url = urlparse.urlparse(res.location) query = urlparse.parse_qs(url.query) state = query['state'][0] with patch('requests.post') as fake_post: fake_post.return_value.status_code = 200 fake_post.return_value.json = { 'access_token': '1234', } with patch('requests.get') as fake_get: fake_get.return_value.status_code = 200 fake_get.return_value.json = { 'id': '789', 'username': '******', 'first_name': 'John', 'last_name': 'Doe', 'name': 'John Doe', 'email': '*****@*****.**', } res = self.testapp.get('/facebook/callback', { 'code': '1234', 'state': state, }) self.assertEqual(res.status, '302 Found') self.assertEqual(res.location, 'http://localhost/register')
def __init__(self, db_uri=DEFAULT_MONGODB_URI, connection_factory=pymongo.Connection): self.db_uri = urlparse.urlparse(db_uri) self.connection = connection_factory( host=self.db_uri.hostname or DEFAULT_MONGODB_HOST, port=self.db_uri.port or DEFAULT_MONGODB_PORT, tz_aware=True) if self.db_uri.path: self.database_name = self.db_uri.path[1:] else: self.database_name = DEFAULT_MONGODB_NAME
def get_audience(public_url_root): parts = urlparse.urlparse(public_url_root) if parts.port is None: if parts.scheme == 'http': port = 80 elif parts.scheme == 'https': port = 443 else: raise ValueError('Error geting the port from %s' % public_url_root) else: port = parts.port return '%s://%s:%d' % (parts.scheme, parts.hostname, port)
def test_google_login(self): res = self.testapp.get("/google/login", {"next_url": "https://localhost/foo/bar"}) self.assertEqual(res.status, "302 Found") url = urlparse.urlparse(res.location) self.assertEqual(url.netloc, "accounts.google.com") self.assertEqual(url.path, "/o/oauth2/auth") query = urlparse.parse_qs(url.query) self.assertEqual(sorted(query.keys()), ["client_id", "redirect_uri", "response_type", "scope", "state"]) scope = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile" self.assertEqual(query["scope"], [scope]) self.assertEqual(query["redirect_uri"], ["http://localhost/google/callback"]) self.assertEqual(query["client_id"], ["id"])
def test_facebook_login(self): res = self.testapp.get('/facebook/login', { 'next_url': 'https://localhost/foo/bar', }) self.assertEqual(res.status, '302 Found') url = urlparse.urlparse(res.location) self.assertEqual(url.netloc, 'www.facebook.com') self.assertEqual(url.path, '/dialog/oauth/') query = urlparse.parse_qs(url.query) self.assertEqual(sorted(query.keys()), [ 'client_id', 'redirect_uri', 'response_type', 'scope', 'state', ]) self.assertEqual(query['scope'], ['email']) self.assertEqual(query['redirect_uri'], ['http://localhost/facebook/callback']) self.assertEqual(query['client_id'], ['id'])
def test_google_login(self): res = self.testapp.get('/google/login', { 'next_url': 'https://localhost/foo/bar', }) self.assertEqual(res.status, '302 Found') url = urlparse.urlparse(res.location) self.assertEqual(url.netloc, 'accounts.google.com') self.assertEqual(url.path, '/o/oauth2/auth') query = urlparse.parse_qs(url.query) self.assertEqual(sorted(query.keys()), [ 'client_id', 'redirect_uri', 'response_type', 'scope', 'state', ]) scope = 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile' self.assertEqual(query['scope'], [scope]) self.assertEqual(query['redirect_uri'], ['http://localhost/google/callback']) self.assertEqual(query['client_id'], ['id'])
def test_google_callback(self): # call the login to fill the session res = self.testapp.get("/google/login", {"next_url": "https://localhost/foo/bar"}) self.assertEqual(res.status, "302 Found") url = urlparse.urlparse(res.location) query = urlparse.parse_qs(url.query) state = query["state"][0] with patch("requests.post") as fake_post: fake_post.return_value.status_code = 200 fake_post.return_value.json = {"access_token": "1234"} with patch("requests.get") as fake_get: fake_get.return_value.status_code = 200 fake_get.return_value.json = { "id": "789", "name": "John Doe", "given_name": "John", "family_name": "Doe", "email": "*****@*****.**", } res = self.testapp.get("/google/callback", {"code": "1234", "state": state}) self.assertEqual(res.status, "302 Found") self.assertEqual(res.location, "http://localhost/register")