예제 #1
0
def test_hash_returns_hashed_value():
    value = "some value"
    result = hash_service.hash_(value)
    second_result = hash_service.hash_(value)

    assert result != value
    assert result != second_result
async def register(repo: UserRepo, credentials: Credentials) -> UserRegistry:
    email = credentials.email.lower()

    user = await repo.fetch_by_email(email)
    if user:
        raise EmailNotUniqueError(email)

    password_hash = hash_service.hash_(credentials.password)

    user = await repo.persist(email, password_hash)
    return UserRegistry(**user.dict())
예제 #3
0
async def _populate_user(db: Database) -> None:
    values = [
        {
            "email": "*****@*****.**",
            "password_hash": hash_("dev@1234")
        },
        {
            "email": "*****@*****.**",
            "password_hash": hash_("dev2@1234")
        },
        {
            "email": "*****@*****.**",
            "password_hash": hash_("dev3@1234")
        },
        {
            "email": "*****@*****.**",
            "password_hash": hash_("dev4@1234")
        },
    ]
    await _populate_table(db, User, values)
    for index, _ in enumerate(values):
        await _populate_todo_item(db, index + 1)
예제 #4
0
 def test_success(self, test_client, credentials):
     register_user({
         "email":
         credentials.email,
         "password_hash":
         hash_service.hash_(credentials.password),
     })
     with test_client as client:
         response = client.post(oauth2_token_url,
                                data=build_form_data(credentials))
         body = response.json()
         assert body["access_token"]
         assert body["expire"]
         assert body["token_type"] == "bearer"
         assert response.status_code == 200
    async def test_invalid_credentials(self, user_repo, credentials, user):
        # Setup
        email = credentials.email
        password_hash = hash_service.hash_("other password")

        user_repo.fetch_by_email.return_value = User(
            **{**user.dict(), "email": email, "password_hash": password_hash}
        )

        # Test
        result = await user_service.get_by_credentials(user_repo, credentials)

        # Assertions
        user_repo.fetch_by_email.assert_called_once_with(email)
        assert not result
    async def test_register_not_unique(self, user_repo, credentials, user):
        # Setup
        email = credentials.email
        password_hash = hash_service.hash_(credentials.password)

        user_repo.fetch_by_email.return_value = User(
            **{**user.dict(), "email": email, "password_hash": password_hash}
        )

        # Test
        with pytest.raises(EmailNotUniqueError) as excinfo:
            await user_service.register(user_repo, credentials)

        # Assertions
        error = excinfo.value
        assert error.msg == "email already registered"
        assert error.email == email
    async def test_register_unique(self, user_repo, credentials, user):
        # Setup
        email = credentials.email
        password_hash = hash_service.hash_(credentials.password)

        user_repo.fetch_by_email.return_value = None
        user_repo.persist.return_value = User(
            **{**user.dict(), "email": email, "password_hash": password_hash}
        )

        # Test
        result = await user_service.register(user_repo, credentials)

        # Assertions
        user_repo.fetch_by_email.assert_called_once()
        user_repo.persist.assert_called_once()
        assert result.email == credentials.email
예제 #8
0
def logged_user(test_client, credentials):
    id_ = 1
    email = credentials.email
    password_hash = hash_service.hash_(credentials.password)

    register_user({
        "id": id_,
        "email": credentials.email,
        "password_hash": password_hash
    })
    with test_client as client:
        response = client.post(oauth2_token_url,
                               data=build_form_data(credentials))
        body = response.json()
        return LoggedUser(
            User(id=id_, email=email, password_hash=password_hash),
            body["access_token"],
        )
예제 #9
0
def test_verify_invalid_value():
    value = "some value"
    result = hash_service.hash_("other value")

    assert not hash_service.verify(value, result)
예제 #10
0
def test_verify_valid_value():
    value = "some value"
    result = hash_service.hash_(value)

    assert hash_service.verify(value, result)