예제 #1
0
async def test_token_invalid_data(data_payload, client):
    password_hash = pwd_context.hash("abc123")
    await UserFactory().create(name="username", password=password_hash)
    response = client.post("/auth/login", data=data_payload)

    assert response.status_code == status.HTTP_401_UNAUTHORIZED
    assert response.json() == {"detail": "Incorrect username or password"}
async def test_authenticate_user(client):
    password_hash = pwd_context.hash("abc123")
    new_user = await UserFactory().create(name="username",
                                          password=password_hash)

    RequestFormMock = namedtuple("OAuth2PasswordRequestForm",
                                 ["username", "password"])
    form_data = RequestFormMock("username", "abc123")
    user = await authenticate_user(form_data)
    assert user.id == new_user.id
async def test_authenticate_user_ivalid_username(client):
    password_hash = pwd_context.hash("abc123")
    await UserFactory().create(name="name", password=password_hash)

    RequestFormMock = namedtuple("OAuth2PasswordRequestForm",
                                 ["username", "password"])
    form_data = RequestFormMock("username", "abc123")
    with pytest.raises(HTTPException) as exinfo:
        await authenticate_user(form_data)
    assert "Incorrect username or password" == exinfo.value.detail
async def test_token_creation(mock_encode, client):
    mock_encode.return_value = "mock_hash"
    password_hash = pwd_context.hash("abc123")
    new_user = await UserFactory().create(name="username",
                                          password=password_hash)
    token = Token(new_user)
    assert token.create() == {
        "access_token": "mock_hash",
        "token_type": "bearer"
    }
예제 #5
0
async def test_token_success_creation(client):
    password_hash = pwd_context.hash("abc123")
    await UserFactory().create(name="name", password=password_hash)
    response = client.post("/auth/login",
                           data={
                               "username": "******",
                               "password": "******"
                           })

    assert response.status_code == status.HTTP_200_OK
    assert "access_token" in response.json().keys()