def test_update_client(db: Session) -> None: password = random_lower_string() email = random_email() client_in = ClientCreate(email=email, password=password) client = crud.client.create(db, obj_in=client_in) new_password = random_lower_string() client_in_update = ClientUpdate(password=new_password) crud.client.update(db, db_obj=client, obj_in=client_in_update) client_2 = crud.client.get(db, id=client.id) assert client_2 assert client.email == client_2.email assert verify_password(new_password, client_2.hashed_password)
def test_create_client(db: Session) -> None: email = random_email() password = random_lower_string() client_in = ClientCreate(email=email, password=password) client = crud.client.create(db, obj_in=client_in) assert client.email == email assert hasattr(client, "hashed_password")
def test_authenticate_client(db: Session) -> None: email = random_email() password = random_lower_string() client_in = ClientCreate(email=email, password=password) client = crud.client.create(db, obj_in=client_in) authenticated_client = crud.client.authenticate(db, email=email, password=password) assert authenticated_client assert client.email == authenticated_client.email
def test_get_access_token_incorrect_data(test_client: TestClient, db: Session) -> None: username = random_email() password = random_lower_string() login_data = { "username": username, "password": password, } r = test_client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) assert r.status_code == 400
def test_get_client(db: Session) -> None: password = random_lower_string() username = random_email() client_in = ClientCreate(email=username, password=password, is_superuser=True) client = crud.client.create(db, obj_in=client_in) client_2 = crud.client.get(db, id=client.id) assert client_2 assert client.email == client_2.email assert jsonable_encoder(client) == jsonable_encoder(client_2)
def test_create_client_by_normal_client( test_client: TestClient, normal_client_token_headers: Dict[str, str]) -> None: username = random_email() password = random_lower_string() data = {"email": username, "password": password} r = test_client.post( f"{settings.API_V1_STR}/clients/", headers=normal_client_token_headers, json=data, ) assert r.status_code == 200
def test_get_access_token(test_client: TestClient, db: Session) -> None: username = random_email() password = random_lower_string() client_in = ClientCreate(email=username, password=password) crud.client.create(db, obj_in=client_in) login_data = { "username": username, "password": password, } r = test_client.post(f"{settings.API_V1_STR}/login/access-token", data=login_data) tokens = r.json() assert r.status_code == 200 assert "access_token" in tokens assert tokens["access_token"]
def test_create_client_existing_username(test_client: TestClient, normal_client_token_headers: dict, db: Session) -> None: username = random_email() password = random_lower_string() user_in = ClientCreate(email=username, password=password) crud.client.create(db, obj_in=user_in) data = {"email": username, "password": password} r = test_client.post( f"{settings.API_V1_STR}/clients/", headers=normal_client_token_headers, json=data, ) created_user = r.json() assert r.status_code == 400 assert "_id" not in created_user
def test_create_user_new_email(test_client: TestClient, normal_client_token_headers: dict, db: Session) -> None: username = random_email() password = random_lower_string() data = {"email": username, "password": password} r = test_client.post( f"{settings.API_V1_STR}/clients/", headers=normal_client_token_headers, json=data, ) assert 200 <= r.status_code < 300 created_client = r.json() client = crud.client.get_by_email(db, email=username) assert client assert client.email == created_client["email"]
def authentication_token_from_email(*, test_client: TestClient, email: str, db: Session) -> Dict[str, str]: """ Return a valid token for the client with given email. If the client doesn't exist it is created first. """ password = random_lower_string() client_obj = crud.client.get_by_email(db, email=email) if not client_obj: client_in_create = ClientCreate(username=email, email=email, password=password) client_obj = crud.client.create(db, obj_in=client_in_create) else: client_in_update = ClientUpdate(password=password) client_obj = crud.client.update(db, db_obj=client_obj, obj_in=client_in_update) return client_authentication_headers(test_client=test_client, email=email, password=password)
def create_random_client(db: Session) -> Client: email = random_email() password = random_lower_string() client_in = ClientCreate(username=email, email=email, password=password) client = crud.client.create(db=db, obj_in=client_in) return client
def test_not_authenticate_client(db: Session) -> None: email = random_email() password = random_lower_string() client = crud.client.authenticate(db, email=email, password=password) assert client is None