class OAuthAuthenticationTest(TestCase): """ Tests the behaviour of the OAuthAuthentication implementation. """ def setUp(self): self.auth = OAuthAuthentication( redirect_url='https://example.test/login/oauth/', client_id='test_client', client_secret='test_secret', ) def test_initial_state(self): self.assertEqual( self.auth.real_auth.auth_token, '', "The auth token should be the empty string when not explicitly set.", ) self.assertFalse( self.auth.is_ready(), "Initially the authentication backend should not be ready.") def test_authorize_url(self): url, state = self.auth.authorize_url(['one', 'two'], 'random_string') path, params = url.split('?', 1) param_dict = { item.split('=', 1)[0]: unquote(item.split('=', 1)[1]) for item in params.split('&') } self.assertEqual(state, 'random_string', "The given state was changed by the implementation.") self.assertEqual(path, 'https://moneybird.com/oauth/authorize/', "The OAuth URL is incorrect.") self.assertDictEqual( param_dict, { 'response_type': 'code', 'client_id': 'test_client', 'redirect_uri': 'https://example.test/login/oauth/', 'scope': 'one+two', 'state': 'random_string', }, "The generated URL parameters for authorization are incorrect for the given input." ) def test_generate_state(self): url, state = self.auth.authorize_url(['one', 'two', 'three']) self.assertGreater(len(state), 16, "The generated state string is too short.") states = [] for i in range(10000): state = OAuthAuthentication._generate_state() self.assertGreater(len(state), 16, "The generated state string is too short.") self.assertNotIn( state, states, "The randomization of the state is not random enough (%d)." % i) states.append(state)
class OAuthAuthenticationTest(TestCase): """ Tests the behaviour of the OAuthAuthentication implementation. """ def setUp(self): self.auth = OAuthAuthentication( redirect_url='https://example.test/login/oauth/', client_id='test_client', client_secret='test_secret', ) def test_initial_state(self): self.assertEqual( self.auth.real_auth.auth_token, '', "The auth token should be the empty string when not explicitly set.", ) self.assertFalse(self.auth.is_ready(), "Initially the authentication backend should not be ready.") def test_authorize_url(self): url, state = self.auth.authorize_url(['one', 'two'], 'random_string') path, params = url.split('?', 1) param_dict = {item.split('=', 1)[0]: unquote(item.split('=', 1)[1]) for item in params.split('&')} self.assertEqual(state, 'random_string', "The given state was changed by the implementation.") self.assertEqual(path, 'https://moneybird.com/oauth/authorize/', "The OAuth URL is incorrect.") self.assertDictEqual(param_dict, { 'response_type': 'code', 'client_id': 'test_client', 'redirect_uri': 'https://example.test/login/oauth/', 'scope': 'one+two', 'state': 'random_string', }, "The generated URL parameters for authorization are incorrect for the given input.") def test_generate_state(self): url, state = self.auth.authorize_url(['one', 'two', 'three']) self.assertGreater(len(state), 16, "The generated state string is too short.") states = [] for i in range(10000): state = OAuthAuthentication._generate_state() self.assertGreater(len(state), 16, "The generated state string is too short.") self.assertNotIn(state, states, "The randomization of the state is not random enough (%d)." % i) states.append(state)
def test_generate_state(self): url, state = self.auth.authorize_url(['one', 'two', 'three']) self.assertGreater(len(state), 16, "The generated state string is too short.") states = [] for i in range(10000): state = OAuthAuthentication._generate_state() self.assertGreater(len(state), 16, "The generated state string is too short.") self.assertNotIn(state, states, "The randomization of the state is not random enough (%d)." % i) states.append(state)
def test_generate_state(self): url, state = self.auth.authorize_url(['one', 'two', 'three']) self.assertGreater(len(state), 16, "The generated state string is too short.") states = [] for i in range(10000): state = OAuthAuthentication._generate_state() self.assertGreater(len(state), 16, "The generated state string is too short.") self.assertNotIn( state, states, "The randomization of the state is not random enough (%d)." % i) states.append(state)
def setUp(self): self.auth = OAuthAuthentication( redirect_url='https://example.test/login/oauth/', client_id='test_client', client_secret='test_secret', )