Exemplo n.º 1
0
    def test_clear(self):
        """When called on a temporary token, the token should be
        removed from the OS's secret store.
        """
        # Expected value.
        exp_before = '_remove'
        exp_after = None

        # Test data and state.
        service = '__TokenTestCase_test_remove'
        user = self.user
        temp = True
        if keyring.get_password(service, user):
            raise RuntimeError('Secret already existed.')
        token = cx.Token(service, user, temp)
        token.set(exp_before)
        act_before = keyring.get_password(service, user)

        # Run test.
        token.clear()

        # Determine test result.
        act_after = keyring.get_password(service, user)
        self.assertEqual(exp_before, act_before)
        self.assertEqual(exp_after, act_after)
Exemplo n.º 2
0
    def test_set_random_urlsafe(self, mock_secrets):
        """If the urlsafe parameter is true, generate a URL safe
        cryptographically random token.
        """
        # Expected value.
        exp_call = call(32)
        exp_token = 'spam'

        # Test data and state.
        mock_secrets.return_value = exp_token
        service = '__TokenTestCase_test_set_random_urlsafe'
        user = self.user
        temp = True
        if keyring.get_password(service, user):
            raise RuntimeError('Secret already existed.')
        token = cx.Token(service, user, temp)
        length = 32
        urlsafe = True

        # Run test.
        try:
            token.set_random(length, urlsafe)

            # Determine test result.
            act_call = mock_secrets.call_args
            act_token = keyring.get_password(service, user)
            self.assertEqual(exp_call, act_call)
            self.assertEqual(exp_token, act_token)

        # Clean up.
        finally:
            keyring.delete_password(service, user)
Exemplo n.º 3
0
    def test_set_random(self, mock_secrets):
        """Given a token length in bytes, set the secret as a
        cryptographically bytes of the given length.
        """
        # Expected value.
        exp_call = call(32)
        exp_token = b'spam'

        # Test data and state.
        mock_secrets.return_value = exp_token
        service = '__TokenTestCase_test_set_random'
        user = self.user
        temp = True
        if keyring.get_password(service, user):
            raise RuntimeError('Secret already existed.')
        token = cx.Token(service, user, temp)
        length = 32

        # Run test.
        try:
            token.set_random(length)

            # Determine test result.
            act_call = mock_secrets.call_args
            raw_token = keyring.get_password(service, user)
            act_token = bytes(raw_token, encoding='utf_8')
            self.assertEqual(exp_call, act_call)
            self.assertEqual(exp_token, act_token)

        # Clean up.
        finally:
            keyring.delete_password(service, user)
Exemplo n.º 4
0
    def test_get_access_token(self, mock_get):
        """Given the app ID, the original login URI, the app secret,
        and the login code, call the oauth/access_token endpoint and
        return the response from Facebook.
        """
        # Expected values.
        exp_call = call(
            'https://graph.facebook.com/v12.0/oauth/access_token',
            {
                'client_id': self.app_id,
                'redirect_uri': 'https://127.0.0.1:5002/facebook_login',
                'client_secret': self.app_secret,
                'code': 'eggs',
            },
        )
        exp_resp = {
            'access_token': 'bacon',
            'token_type': 'sausages',
            'expires_in': 1000,
        }

        # Test data and state.
        resp = MagicMock()
        resp.text = json.dumps(exp_resp)
        mock_get.return_value = resp
        app_id = cx.Token(self.app_id_loc, self.app_id_account)
        redirect_uri = 'https://127.0.0.1:5002/facebook_login'
        app_secret = cx.Token(self.app_secret_loc, self.app_secret_account)
        code = 'eggs'

        # Run test.
        act_resp = fb.get_access_token(app_id, redirect_uri, app_secret, code)

        # Determine test result.
        act_call = mock_get.call_args
        self.assertEqual(exp_call, act_call)
        self.assertEqual(exp_resp, act_resp)
Exemplo n.º 5
0
    def test_get(self):
        """When called, the get() method should return the value of the
        secret as a string.
        """
        # Expected values.
        exp = self.value

        # Create specific test data and state.
        token = cx.Token(self.service, self.user)

        # Run test.
        act = token.get()

        # Determine if test passed.
        self.assertEqual(exp, act)
Exemplo n.º 6
0
    def test_repr(self):
        """When needed, Token objects should return a representation of
        themselves useful for debugging.
        """
        # Expected value.
        exp = f"Token('{self.service}', '{self.user}')"

        # Set up specific test data and state.
        token = cx.Token(self.service, self.user)

        # Run test.
        act = repr(token)

        # Determine is test passed.
        self.assertEqual(exp, act)
Exemplo n.º 7
0
    def test_get_secret_does_not_exist(self):
        """If the secret doesn't exist in the OS's secret store, get()
        should raise a connect.SecretDoesNotExist exception.
        """
        # Expected value.
        exp = cx.SecretDoesNotExist
        exp_msg = 'Expected secret not in OS secret store.'

        # Set up specific test data and state.
        token = cx.Token(self.not_service, self.user)

        # Determine if test passed when block completes.
        with self.assertRaisesRegex(exp, exp_msg):

            # Run test.
            _ = token.get()
Exemplo n.º 8
0
    def test_do_not_clear_permanent_secrets(self):
        """When called on a permanent token, raise a PermanentSecret
        error.
        """
        # Expected value.
        exp_ex = cx.PermanentSecret
        exp_msg = 'Cannot clear a permanent secret.'
        exp_value = self.value

        # Test data and state.
        token = cx.Token(self.service, self.user, temp=False)

        # Run test and determine result.
        with self.assertRaisesRegex(exp_ex, exp_msg):
            token.clear()
        act_value = keyring.get_password(self.service, self.user)
        self.assertEqual(exp_value, act_value)
Exemplo n.º 9
0
    def test_do_not_set_if_token_not_temporary(self):
        """If the secret isn't temporary, raise a PermanentSecret
        exception."""
        # Expected values.
        exp_ex = cx.PermanentSecret
        exp_msg = 'Cannot create a permanent secret.'
        exp_value = self.value

        # Test data and state.
        value = 'spam'
        token = cx.Token(self.service, self.user, temp=False)

        # Run test and determine result.
        with self.assertRaisesRegex(exp_ex, exp_msg):
            token.set(value)
        act_value = keyring.get_password(self.service, self.user)
        self.assertEqual(exp_value, act_value)
Exemplo n.º 10
0
    def test_initialize(self):
        """Given a service and a username, the Token class should
        return an instance with those attributes set.
        """
        # Expected values.
        exp = {
            'service': self.service,
            'user': self.user,
        }

        # Run test.
        token = cx.Token(**exp)

        # Extract test result.
        act = {
            'service': token.service,
            'user': token.user,
        }

        # Determine if test passed.
        self.assertDictEqual(exp, act)
Exemplo n.º 11
0
    def test_set(self):
        """When given a value, the value should be stored as a secret."""
        # Expected value.
        exp = 'spam'

        # Test data and state.
        service = '__TokenTestCase_test_make'
        user = self.user
        token = cx.Token(service, user, temp=True)

        # Run test.
        token.set(exp)

        # Determine test result.
        try:
            act = keyring.get_password(service, user)
            self.assertEqual(exp, act)

        # Clean up if exception.
        finally:
            keyring.delete_password(service, user)
Exemplo n.º 12
0
    def test_login(self, mock_get, mock_secrets):
        """When given an app ID token, send a login request to
        Facebook.
        """
        # Expected values.
        exp_url = ('https://www.facebook.com/v12.0/dialog/oauth?'
                   f'client_id={self.app_id}&'
                   f'redirect_uri=https://127.0.0.1:5002/facebook_login&'
                   f'state=eggs')
        exp_return = '_facebook_login_response'

        # Test data and state.
        mock_secrets.return_value = 'eggs'
        mock_get.side_effect = get_facebook_redirect
        token = cx.Token(self.app_id_loc, self.app_id_account)

        # Run test.
        act_return = fb.login(token)

        # Determine test result.
        last_call = mock_get.call_args
        act_url = last_call[0][0]
        self.assertEqual(exp_return, act_return)
        self.assertEqual(exp_url, act_url)