async def test_server_token_scopes(
    app, username, create_user_with_scopes, have_scopes, request_scopes, expected_scopes
):
    """Token provided by spawner is not in the db

    Insert token into db as a user-provided token.
    """
    db = app.db

    # apply templating
    def _format_scopes(scopes):
        if callable(scopes):

            async def get_scopes(*args):
                return _format_scopes(await maybe_future(scopes(*args)))

            return get_scopes

        return [s.replace("USER", username) for s in scopes]

    have_scopes = _format_scopes(have_scopes)
    request_scopes = _format_scopes(request_scopes)
    expected_scopes = _format_scopes(expected_scopes)

    user = create_user_with_scopes(*have_scopes, name=username)
    spawner = user.spawner
    spawner.server_token_scopes = request_scopes

    await user.spawn()
    orm_token = orm.APIToken.find(db, spawner.api_token)
    assert orm_token
    assert set(orm_token.scopes) == set(expected_scopes)
    await user.stop()
Token provided by spawner is not in the db

Insert token into db as a user-provided token.
jupyterhub/tests/test_spawner.py