class TestGetBrowserIDAssertion(unittest.TestCase): def test_account_server_url_is_mandatory(self): try: get_browserid_assertion("email", "password", "audience") except ValueError as e: self.assertEqual("%s" % e, 'Please define an account_server_url.') else: self.fail("ValueError not raised") @mock.patch('fxa.core.Client', return_value=mocked_core_client()) def test_duration_and_audience_are_used(self, core_client): get_browserid_assertion("email", "password", "audience", account_server_url="account_server_url", duration=3600 * 1000) core_client().login().get_identity_assertion.assert_called_with( audience='audience', duration=3600 * 1000)
class TestGetBearerToken(unittest.TestCase): def test_account_server_url_is_mandatory(self): try: get_bearer_token("email", "password", oauth_server_url="oauth_server_url", client_id="client_id") except ValueError as e: self.assertEqual("%s" % e, 'Please define an account_server_url.') else: self.fail("ValueError not raised") def test_oauth_server_url_is_mandatory(self): try: get_bearer_token("email", "password", account_server_url="account_server_url", client_id="client_id") except ValueError as e: self.assertEqual("%s" % e, 'Please define an oauth_server_url.') else: self.fail("ValueError not raised") def test_client_id_is_mandatory(self): try: get_bearer_token("email", "password", account_server_url="account_server_url", oauth_server_url="oauth_server_url") except ValueError as e: self.assertEqual("%s" % e, 'Please define a client_id.') else: self.fail("ValueError not raised") @mock.patch('fxa.core.Client', return_value=mocked_core_client()) @mock.patch('fxa.oauth.Client', return_value=mocked_oauth_client()) def test_scopes_default_to_profile(self, oauth_client, core_client): get_bearer_token("email", "password", client_id="543210789456", account_server_url="account_server_url", oauth_server_url="oauth_server_url") oauth_client().authorize_token.assert_called_with( 'abcd', 'profile', '543210789456')
class TestFxABearerTokenAuth(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestFxABearerTokenAuth, self).__init__(*args, **kwargs) self.auth = FxABearerTokenAuth( email="*****@*****.**", password="******", client_id="53210789456", account_server_url="https://accounts.com/auth/v1", oauth_server_url="https://oauth.com/oauth/v1") @mock.patch('fxa.core.Client', return_value=mocked_core_client()) @mock.patch('fxa.oauth.Client', return_value=mocked_oauth_client()) def test_header_are_set_to_request(self, oauth_client_patch, core_client_patch): r = self.auth(Request()) self.assertIn('Authorization', r.headers) self.assertTrue(r.headers['Authorization'].startswith("Bearer"), "Authorization headers does not start with Bearer") core_client_patch.assert_called_with( server_url="https://accounts.com/auth/v1") oauth_client_patch.assert_called_with( server_url="https://oauth.com/oauth/v1") core_client_patch.return_value.login.return_value. \ get_identity_assertion.assert_called_with( "https://oauth.com/") @mock.patch('fxa.core.Client', return_value=mocked_core_client()) @mock.patch('fxa.oauth.Client', return_value=mocked_oauth_client()) def test_memory_cache_is_set_by_default(self, oauth_client_patch, core_client_patch): assert type(self.auth.cache) is MemoryCache self.assertEqual(self.auth.cache.ttl, DEFAULT_CACHE_EXPIRY) @mock.patch('fxa.core.Client', return_value=mocked_core_client()) @mock.patch('fxa.oauth.Client', return_value=mocked_oauth_client()) def test_memory_cache_is_used(self, oauth_client_patch, core_client_patch): # First call should set the cache value self.auth(Request()) self.assertEquals(core_client_patch.call_count, 1) self.assertEquals(oauth_client_patch.call_count, 1) # Second call should use the cache value self.auth(Request()) self.assertEquals(core_client_patch.call_count, 1) self.assertEquals(oauth_client_patch.call_count, 1) @mock.patch('fxa.core.Client', return_value=mocked_core_client()) @mock.patch('fxa.oauth.Client', return_value=mocked_oauth_client()) def test_it_works_with_cache_deactivated(self, oauth_client_patch, core_client_patch): auth = FxABearerTokenAuth( email="*****@*****.**", password="******", client_id="53210789456", account_server_url="https://accounts.com/auth/v1", oauth_server_url="https://oauth.com/oauth/v1", cache=False) assert not auth.cache r = auth(Request()) self.assertIn('Authorization', r.headers)
class TestFxABrowserIDAuth(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestFxABrowserIDAuth, self).__init__(*args, **kwargs) self.auth = FxABrowserIDAuth(email="*****@*****.**", password="******", with_client_state=True, server_url="http://*****:*****@mock.patch('fxa.core.Client', return_value=mocked_core_client()) def test_audience_is_parsed(self, client_patch): self.auth(Request()) self.assertEquals(self.auth.audience, "http://www.example.com/") @mock.patch('fxa.core.Client', return_value=mocked_core_client()) def test_server_url_is_passed_to_client(self, client_patch): self.auth(Request()) client_patch.assert_called_with(server_url="http://*****:*****@mock.patch('fxa.core.Client', return_value=mocked_core_client()) def test_header_are_set_to_request(self, client_patch): r = self.auth(Request()) self.assertIn('Authorization', r.headers) self.assertTrue(r.headers['Authorization'].startswith("BrowserID"), "Authorization headers does not start with BrowserID") self.assertIn('X-Client-State', r.headers) client_patch.assert_called_with(server_url="http://*****:*****@restmail.com", "this is not a password", keys=True) client_patch.return_value.login.return_value. \ get_identity_assertion.assert_called_with( audience="http://www.example.com/", duration=3600) @mock.patch('fxa.core.Client', return_value=mocked_core_client()) def test_client_state_not_set_by_default(self, client_patch): auth = FxABrowserIDAuth(email="*****@*****.**", password="******", server_url="http://*****:*****@mock.patch('fxa.core.Client', return_value=mocked_core_client()) def test_memory_cache_is_set_by_default(self, client_patch): auth = FxABrowserIDAuth(email="*****@*****.**", password="******", server_url="http://*****:*****@mock.patch('fxa.core.Client', return_value=mocked_core_client()) def test_memory_cache_is_used(self, client_patch): auth = FxABrowserIDAuth(email="*****@*****.**", password="******", server_url="http://*****:*****@mock.patch('fxa.core.Client', return_value=mocked_core_client()) def test_it_works_with_cache_deactivated(self, client_patch): auth = FxABrowserIDAuth(email="*****@*****.**", password="******", server_url="http://localhost:5000", cache=False) assert not auth.cache r = auth(Request()) self.assertIn('Authorization', r.headers)