Exemplo n.º 1
0
def test_create_access_token(mocker):
    access_token_expires = timedelta(minutes=30)
    token = create_access_token({"sub": "username"}, access_token_expires)
    get_user = mocker.Mock()
    get_current_user = get_current_user_func(get_user)
    get_current_user(token)

    assert get_user.mock_calls == [mocker.call(username="******")]
Exemplo n.º 2
0
def test_create_access_token_no_user(mocker):
    access_token_expires = timedelta(minutes=30)
    token = create_access_token({"sub": "username"}, access_token_expires)
    get_user = mocker.Mock(return_value=None)
    get_current_user = get_current_user_func(get_user)
    with pytest.raises(HTTPException) as http_error:
        get_current_user(token)

    assert http_error.value.status_code == 401
    assert http_error.value.detail == "Could not validate credentials - no such user"
Exemplo n.º 3
0
def test_create_access_token_bad_jwt(mocker):
    access_token_expires = timedelta(minutes=30)
    token = create_access_token({"sub": None}, access_token_expires)
    get_user = mocker.Mock()
    get_current_user = get_current_user_func(get_user)
    with pytest.raises(HTTPException) as http_error:
        get_current_user(token)

    assert http_error.value.status_code == 401
    assert http_error.value.detail == "Could not validate credentials - bad token"
Exemplo n.º 4
0
def new_jwt_token(user):
    access_token_expires = timedelta(seconds=settings.SESSION_COOKIE_AGE)
    return create_access_token(data={"sub": user.username},
                               expires_delta=access_token_expires)