Example #1
0
    def test_token(self):
        user = self.create_user()
        client = self.create_client(user)

        token_data = {
            'user': user,
            'client': client,
            'token_type': 'bearer',
            'access_token': '123',
            'refresh_token': 'abc',
            'expires': datetime.utcnow() + timedelta(seconds=3600),
            '_scopes': 'email user',
        }

        token = Token(**token_data)
        db.session.add(token)
        db.session.commit()

        token = Token.get_for_oauth2(access_token='123')

        assert token.user.id == user.id
        assert token.client.client_id == client.client_id
        assert token.scopes == ['email', 'user']

        class Request(object):
            scopes = ['email', 'user']

            def __init__(self, user, client):
                self.user = user
                self.client = client

        request = Request(user=user, client=client)

        token_data = {
            'expires_in': 3600,
            'access_token': 'abc',
            'refresh_token': '123',
            'token_type': 'Bearer',
            'scope': ['email', 'user'],
        }

        token = Token.set_for_oauth2(token_data, request)
        assert token.user.id == user.id
        assert token.client.client_id == client.client_id
        assert token.scopes == ['email', 'user']

        token = Token.get_for_oauth2(access_token='abc')
        assert token.user.id == user.id
        assert token.client.client_id == client.client_id
        assert token.scopes == ['email', 'user']

        token = Token.get_for_oauth2(refresh_token='123')
        assert token.user.id == user.id
        assert token.client.client_id == client.client_id
        assert token.scopes == ['email', 'user']
Example #2
0
 def save_bearer_token(self, token, request):
     print token
     return Token.set_for_oauth2(token, request)