Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
    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)
Esempio n. 4
0
    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)
Esempio n. 5
0
 def setUp(self):
     self.auth = OAuthAuthentication(
         redirect_url='https://example.test/login/oauth/',
         client_id='test_client',
         client_secret='test_secret',
     )
Esempio n. 6
0
 def setUp(self):
     self.auth = OAuthAuthentication(
         redirect_url='https://example.test/login/oauth/',
         client_id='test_client',
         client_secret='test_secret',
     )