Пример #1
0
    def test_grant(self):
        user = self.create_user()
        client = self.create_client(user)
        print user
        print client
        data = {
            'user_id': user.id,
            'user': user,
            'client': client,
            'client_id': client.client_id,
            'code': '101112',
            'redirect_uri': 'http://example.com',
            '_scopes': 'email user',
            'expires': datetime.utcnow() + timedelta(seconds=10),
        }

        grant = Grant(**data)
        db.session.add(grant)
        db.session.commit()
        grant = Grant.get_for_oauth2(client.client_id, '101112')
        assert grant.user.id == user.id
        assert grant.client_id == client.client_id
        assert grant.scopes == ['email', 'user']
        assert Grant.get_for_oauth2(client.client_id, '101112').id == grant.id

        class Request(object):
            scopes = ['email', 'user']
            redirect_uri = 'http://example.com'

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

        request = Request(user=user)
        current_user = lambda: request.user
        grant = Grant.set_for_oauth2(current_user, 'abcdef', {'code': '123'}, request)

        assert grant.scopes == ['email', 'user']
        assert grant.client_id == 'abcdef'
        assert grant.code == '123'