def test_create_user(db: Session) -> None: username = random_lower_string() password = random_lower_string() user_in = UserCreate(username=username, password=password) user = crud.user.create(db, user_in) assert user.username == username assert hasattr(user, "password_hashed")
def test_authenticate_user_invalid_password(db: Session) -> None: username = random_lower_string() password = random_lower_string() user_in = UserCreate(username=username, password=password) crud.user.create(db, user_in) authenticated_user = crud.user.authenticate(db, username=username, password=password + "1") assert authenticated_user is None
def test_create_user(client: TestClient) -> None: username = random_lower_string() password = random_lower_string() data = {"username": username, "password": password} response = client.post("/users", json=data) assert response.status_code == 200 new_user = response.json() assert username == new_user["username"] assert not new_user["is_superuser"]
def test_create_existing_user(client: TestClient, db: Session) -> None: username = random_lower_string() password = random_lower_string() data = {"username": username, "password": password} user_in = UserCreate(username=username, password=password) crud.user.create(db, user_in) response = client.post("/users", json=data) json = response.json() assert response.status_code == 400 assert json["detail"] == "User already exists."
def test_get_user(db: Session) -> None: username = random_lower_string() password = random_lower_string() user_in = UserCreate(username=username, password=password) user = crud.user.create(db, user_in) user2 = crud.user.get(db, user.id) assert user2 assert jsonable_encoder(user2) == jsonable_encoder(user) user3 = crud.user.get_by_username(db, username=user.username) assert user3 assert jsonable_encoder(user3) == jsonable_encoder(user)
def test_access_token(client: TestClient, db: Session) -> None: password = utils.random_lower_string() user = utils.create_random_user(db, password) # Wrong email response_1 = client.post('/sessions/access-token', json={ "email": utils.random_email(), "password": password, "host": "mobile" }) # Wrong password response_2 = client.post('/sessions/access-token', json={ "email": user.email, "password": utils.random_lower_string(), "host": "mobile" }) # Email not yet verified response_3 = client.post('/sessions/access-token', json={ "email": user.email, "password": password, "host": "mobile" }) # Wrong platform response_4 = client.post('/sessions/access-token', json={ "email": user.email, "password": password, "host": "web" }) # All right user = utils.activate_random_user(db, user) response_5 = client.post('/sessions/access-token', json={ "email": user.email, "password": password, "host": "mobile" }) assert response_1.status_code == 400 assert response_2.status_code == 400 assert response_3.status_code == 401 assert response_4.status_code == 401 data = response_5.json() assert response_5.status_code == 200 assert "user" in data assert "token" in data assert data["token"]
def test_request_reset_password(client: TestClient, db: Session) -> None: new_password = utils.random_lower_string() confirm_password = new_password user = utils.create_random_user(db) user = utils.activate_random_user(db, user) token_1 = security.generate_token(str(uuid4()), "reset", datetime.utcnow() + timedelta(hours=2)) token_2 = security.generate_token(str(user.id), "reset", datetime.utcnow() + timedelta(hours=2)) response_1 = client.post('/sessions/reset-password', json={ "new_password": new_password, "confirm_password": confirm_password, "token": token_1 }) response_2 = client.post('/sessions/reset-password', json={ "new_password": new_password, "confirm_password": confirm_password, "token": token_2 }) db.refresh(user) assert response_1.status_code == 404 assert response_2.status_code == 200 assert security.verify_password(new_password, user.hashed_password)
def test_update_users(client: TestClient, db: Session) -> None: name = utils.random_lower_string() surname = utils.random_lower_string() address = utils.random_lower_string() user_1 = utils.create_random_user(db) user_2 = utils.create_random_user(db) provider = utils.create_random_provider(db) token_1 = security.generate_token(str(user_1.id), 'access', datetime.utcnow() + timedelta(days=1)) token_2 = security.generate_token(str(provider.id), 'access', datetime.utcnow() + timedelta(days=1)) header_1 = {'Authorization': f'Bearer {token_1}'} header_2 = {'Authorization': f'Bearer {token_2}'} # Update user with existing email response_1 = client.put('/users', headers=header_1, json={ "name": name, "surname": surname, "email": user_2.email, }) # Update user normally response_2 = client.put('/users', headers=header_1, json={ "name": name, "surname": surname, "email": user_1.email, }) # Update provider normally response_3 = client.put('/users', headers=header_2, json={ "name": name, "surname": surname, "address": address, "email": provider.email, }) assert response_1.status_code == 400 data_1 = response_2.json() assert response_2.status_code == 200 assert data_1 assert data_1['name'] == name assert data_1['surname'] == surname data_2 = response_3.json() assert response_3.status_code == 200 assert data_2 assert data_2['name'] == name assert data_2['surname'] == surname assert data_2['address'] == address db.delete(provider) db.commit()
def test_create_link(db: Session) -> None: title = random_lower_string() url = "https://example.com" link_in = LinkCreate(title=title, url=url) link = crud.link.create(db, link_in) assert link.title == title assert link.url == url assert not link.liked assert not link.archived
def test_create_same_tag(db: Session) -> None: tag_in = TagCreate(name=random_lower_string()) tag = crud.tag.create(db, tag_in) assert tag assert tag.name == tag_in.name tag2 = crud.tag.create(db, tag_in) assert tag2 assert tag2 == tag
def test_invalidate_cache_prefix(rdc: RedisCache) -> None: suffix = utils.random_lower_string() prefix = "teste:5" key = f"{prefix}:{suffix}" value = 30 rdc.setex(key, timedelta(seconds=3600), value=pickle.dumps(value)) cache = rdc.get(key) assert cache rdc.invalidate_cache_prefix(prefix) cache = rdc.get(key) assert cache is None
def test_create_user_by_normal_user( client: TestClient, normal_user_token_headers: Dict[str, str]) -> None: username = random_email() password = random_lower_string() data = {"email": username, "password": password} r = client.post( f"{settings.API_V1_STR}/users/", headers=normal_user_token_headers, json=data, ) assert r.status_code == 400
def test_create_notification(db: Session) -> None: provider = utils.create_random_provider(db) message = utils.random_lower_string() document_id = crud_notification.create(str(provider.id), message) notification = Notification.objects(id=document_id).first() assert notification assert notification.content == message assert notification.recipient_id == str(provider.id) db.delete(provider) db.commit() Notification.delete(notification)
def test_update_link(db: Session) -> None: link = create_random_link(db) new_title = random_lower_string() new_url = "https://example.org" tag_name = random_lower_string() link_update = LinkUpdate( title=new_title, url=new_url, tags=[TagCreate(name=tag_name)], liked=not link.liked, archived=not link.archived, ) new_link = crud.link.update(db, object_db=link, object_update=link_update) assert new_link assert new_link.title == link_update.title assert new_link.url == link_update.url assert new_link.liked == link_update.liked assert new_link.archived == link_update.archived new_tags = list(new_link.tags) assert len(new_tags) == 1 assert new_tags[0].name == tag_name
def test_create_link(client: TestClient, db: Session) -> None: user, password = create_random_user(db) headers = get_user_auth_headers(user.username, password, client) title = random_lower_string() url = "https://example.com" data = {"title": title, "url": url} response = client.post("/links", json=data, headers=headers) assert response.status_code == 200 json = response.json() assert json["title"] == title assert json["url"] == url assert not json["liked"] assert not json["archived"]
def test_create_users(client: TestClient, db: Session) -> None: name = utils.random_lower_string() surname = utils.random_lower_string() email = utils.random_lower_string() password = utils.random_lower_string() user_2 = utils.create_random_user(db) response_1 = client.post('/users', json={ "name": name, "surname": surname, "email": user_2.email, "password": password, "confirm_password": password }) response_2 = client.post('/users', json={ "name": name, "surname": surname, "email": email, "password": password, "confirm_password": password }) assert response_1.status_code == 400 assert response_2.status_code == 201
def test_request_forgot_password(client: TestClient, db: Session) -> None: password = utils.random_lower_string() user = utils.create_random_user(db, password) user = utils.activate_random_user(db, user) response_1 = client.post('/sessions/forgot-password', json={ "email": user.email, }) response_2 = client.post('/sessions/forgot-password', json={ "email": utils.random_email(), }) assert response_1.status_code == 200 assert response_2.status_code == 404
def test_update_password(client: TestClient, db: Session) -> None: old_password = utils.random_lower_string() new_password = utils.random_lower_string() user = utils.create_random_user(db, old_password) user = utils.activate_random_user(db, user) token = security.generate_token(str(user.id), "access", datetime.utcnow() + timedelta(hours=2)) header = {'Authorization': f'Bearer {token}'} response_1 = client.put("/users/password", headers=header, json={ "old_password": utils.random_lower_string(), "new_password": new_password, "confirm_password": new_password, }) response_2 = client.put("/users/password", headers=header, json={ "old_password": old_password, "new_password": new_password, "confirm_password": new_password, }) assert response_1.status_code == 400 db.refresh(user) assert response_2.status_code == 204 assert security.verify_password(new_password, user.hashed_password) db.delete(user) db.commit()
def test_invalidate_cache_provider(db: Session, rdc: RedisCache) -> None: user = utils.create_random_user(db) provider = utils.create_random_provider(db) appointment = utils.create_random_appointment(db, provider, user) user.user_appointments.append(appointment) prefix = f"providers-appointments:{str(provider.id)}" suffix = utils.random_lower_string() key = f"{prefix}:{suffix}" value = 40 rdc.setex(key, timedelta(seconds=3600), value=pickle.dumps(value)) cache = rdc.get(key) assert cache rdc.invalidate_cache_provider(user) cache = rdc.get(key) assert cache is None
def authentication_token_from_email( *, client: TestClient, email: str, db: Session ) -> 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() user = crud.user.get_by_email(db, email=email) if not user: user_in_create = UserCreate(username=email, email=email, password=password) user = crud.user.create(db, obj_in=user_in_create) else: user_in_update = UserUpdate(password=password) user = crud.user.update(db, db_obj=user, obj_in=user_in_update) return user_authentication_headers(client=client, email=email, password=password)
def random_user_data(db: Session): email = random_email() password = random_lower_string() user_in = dict(username=email, email=email, password=password, full_name=random_lower_string()) return user_in
def create_random_user(db: Session) -> User: email = random_email() password = random_lower_string() user_in = UserCreate(username=email, email=email, password=password) user = crud.user.create(db=db, obj_in=user_in) return user