예제 #1
0
def test_create_user_new_email(
    client: TestClient,
    superuser_token_headers: dict,
    uow: SqlAlchemyUnitOfWork = get_sqlalchemy_uow()
) -> None:
    email = random_email()
    name = random_lower_string()
    password = random_lower_string()
    data = {"email": email, "password": password, "name": name}
    r = client.post(
        f"{settings.API_V1_STR}/users/",
        headers=superuser_token_headers,
        json=data,
    )
    assert 200 <= r.status_code < 300
    created_user = r.json()
    user = user_services.get_user_by_email(email, uow)
    assert user
    assert user.email == created_user["email"]
    assert user.id == created_user["id"]
    assert user.id is not None

    # Ensure we don't expose passwords
    assert created_user.get("hashed_password") is None
    assert created_user.get("password") is None
예제 #2
0
    def post(self):
        user = user_services.get_user_by_email()

        if user:
            return 'email already registered!', 400
        else:
            user_services.create_user()

        return 'user created success', 201
예제 #3
0
    def post(self):
        user = user_services.get_user_by_email()

        if user and user.verify_password(g.args.get('password')):

            token = user.generate_auth_token()

            return {
                'token': token,
                'expiration': 3600 * 24,
                'username': user.email
            }
        else:
            return 'user does not exist or email and password does not match, please check!', 400
예제 #4
0
def test_get_existing_user(
    client: TestClient,
    superuser_token_headers: dict,
    uow: SqlAlchemyUnitOfWork = get_sqlalchemy_uow()
) -> None:
    email = random_email()
    name = random_lower_string()
    password = random_lower_string()
    user_in = UserCreate(email=email, name=name, password=password)
    user = user_services.create_user(user_in, uow)
    user_id = user.id
    r = client.get(
        f"{settings.API_V1_STR}/users/{user_id}",
        headers=superuser_token_headers,
    )
    assert 200 <= r.status_code < 300
    api_user = r.json()
    existing_user = user_services.get_user_by_email(email, uow)
    assert existing_user
    assert existing_user.email == api_user["email"]
예제 #5
0
def authentication_token_from_email(
        *, client: TestClient, email: EmailStr, uow: SqlAlchemyUnitOfWork = get_sqlalchemy_uow()
) -> Dict[str, str]:
    """
    Return a valid token for the user with given email.

    If the user doesn't exist it is created first.
    """
    password = random_lower_string()
    name = random_lower_string()
    with uow:
        user = user_services.get_user_by_email(email, uow)
        if user:
            uow.users.delete_by_email(email)
            uow.commit()
        user_in = UserCreate(name=name, email=email, password=password)
        user = user_services.create_user(user_in, uow)

        uow.commit()
    return user_authentication_headers(
        client=client, username=email, password=password
    )