def test_get_user_group_users(db: Session, normal_user: User) -> None: node = create_random_node(db, created_by_id=normal_user.id, node_type="test_get_user_group_users") name = random_lower_string() user_group_in = UserGroupCreate(name=name, node_id=node.id) user_group = crud.user_group.create(db=db, obj_in=user_group_in, created_by_id=normal_user.id) user1 = create_random_user(db) user2 = create_random_user(db) user3 = create_random_user(db) crud.user_group.add_user(db=db, user_group=user_group, user_id=user1.id) crud.user_group.add_user(db=db, user_group=user_group, user_id=user2.id) group_users = crud.user_group.get_users(db, user_group=user_group) for user in group_users: assert user.id in [user1.id, user2.id] assert user.id != user3.id
def test_create_item(): title = random_lower_string() description = random_lower_string() item_in = ItemCreate(title=title, description=description) user = create_random_user() item = crud.item.create_with_owner(db_session=db_session, obj_in=item_in, owner_id=user.id) assert item.title == title assert item.description == description assert item.owner_id == user.id
def test_get_existing_user(client: TestClient, superuser_token_headers: dict, db: Session) -> None: user = create_random_user(db=db, type="superuser") assert user user_id = user.id r = client.get( f"{settings.API_V1_STR}/users/{user_id}", headers=superuser_token_headers, ) assert r.status_code == 200 api_user = r.json() compare_api_and_db_query_results(api_result=api_user, db_dict=to_json(user))
def create_random_tag(owner_id: int = None): if owner_id is None: user = create_random_user() owner_id = user.id title = random_lower_string() description = random_lower_string() text = random_lower_string() tag_in = TagCreate(title=title, description=description, text=text, id=id) return crud.tag.create_with_owner(db_session=db_session, obj_in=tag_in, owner_id=owner_id)
def test_get_professors_admin_with_perms(client: TestClient, db: Session) -> None: create_random_user(db, type="professor") create_random_user(db, type="professor") admin_perms = AdminPermissions(0) admin_perms["professor"] = True admin_with_perms = create_random_user(db, type="admin", permissions=admin_perms.permissions) r = client.get( f"{settings.API_V1_STR}/professors/", headers=authentication_token_from_email(client=client, email=admin_with_perms.email, db=db), ) assert r.status_code == 200 all_professors = r.json() assert len(all_professors) > 2 for professor in all_professors: assert "user_id" in professor
def test_get_course_admin(client: TestClient, db: Session) -> None: admin_perms = AdminPermissions(0) admin_perms["course"] = True admin = create_random_user(db=db, type="admin", permissions=admin_perms.permissions) admin_user_token_headers = authentication_token_from_email( client=client, db=db, email=admin.email, user_type="admin") r = client.get(f"{settings.API_V1_STR}/courses/", headers=admin_user_token_headers) assert r.status_code == 200
def test_add_term_students(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None: term = create_random_term(db) students = [ create_random_user(db, type="student", school_id=term.year.school_id), create_random_user(db, type="student", school_id=term.year.school_id), ] data = [user.id for user in students] r = client.post(f"{settings.API_V1_STR}/terms/{term.id}/students", headers=superuser_token_headers, json=data) assert r.status_code == 207 assert r.json() fetched_students = [student_id for student_id in r.json()["success"]] for user in students: assert user.id in fetched_students student = crud.student.get(db, id=user.id) assert student assert student.term_id == term.id
def test_create_superuser_by_normal_admin_with_user_perms(client: TestClient, db: Session) -> None: admin_user = create_random_user(db=db, type="admin", is_admin=True, permissions=1) username = random_email() password = random_password() data = {"email": username, "password": password, "type": "superuser"} r = client.post( f"{settings.API_V1_STR}/users/", headers=authentication_token_from_email(client=client, email=admin_user.email, db=db), json=data, ) assert r.status_code == 403
def test_get_school_admin(client: TestClient, db: Session) -> None: school = create_random_school(db) admin = create_random_user(db, "admin", permissions=4) admin_user_token_headers = authentication_token_from_email( client=client, db=db, email=admin.email, user_type="admin") r = client.get(f"{settings.API_V1_STR}/schools/{school.id}", headers=admin_user_token_headers) assert r.status_code == 200 fetched_school = r.json() assert fetched_school compare_api_and_db_query_results(api_result=fetched_school, db_dict=to_json(school))
def test_remove_term_student(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None: term = create_random_term(db) students = [ create_random_user(db, type="student", school_id=term.year.school_id), create_random_user(db, type="student", school_id=term.year.school_id), ] for user in students: student = crud.student.get(db, user.id) assert student crud.student.update(db, db_obj=student, obj_in=StudentUpdate(term_id=term.id)) r = client.delete( f"{settings.API_V1_STR}/terms/{term.id}/students/{students[0].id}", headers=superuser_token_headers) assert r.status_code == 200 updated_student = r.json() assert updated_student["user_id"] == students[0].id assert updated_student["term_id"] is None
def test_get_item(db: Session) -> None: user = create_random_user(db) collection = create_random_collection(user) item = create_random_item(user, collection) stored_item = Item.from_dto( services.item.get_item(InternalUserDTO(**user.dict()), item.uuid)) assert stored_item assert item.uuid == stored_item.uuid assert item.geometry == stored_item.geometry assert item.properties == stored_item.properties assert item.collection_uuid == stored_item.collection_uuid
def test_get_item(db: Session) -> None: title = random_lower_string() description = random_lower_string() item_in = ItemCreate(title=title, description=description) user = create_random_user(db) item = crud.item.create_with_owner(db=db, obj_in=item_in, owner_id=user.id) stored_item = crud.item.get(db=db, id=item.id) assert stored_item assert item.id == stored_item.id assert item.title == stored_item.title assert item.description == stored_item.description assert item.owner_id == stored_item.owner_id
def test_create_quote(): title = random_lower_string() description = random_lower_string() text = random_lower_string() quote_in = QuoteCreate(title=title, description=description, text=text) user = create_random_user() quote = crud.quote.create_with_owner( db_session=db_session, obj_in=quote_in, owner_id=user.id ) assert quote.title == title assert quote.description == description assert quote.owner_id == user.id
def test_update_user_fail_normal_user(client: TestClient, db: Session) -> None: user = create_random_user(db) user_token_headers = authentication_token_from_email( client=client, email=user.email, db=db ) data = {"email": random_email()} r = client.put( f"{settings.API_V1_STR}/users/{user.id}", headers=user_token_headers, json=data ) content = r.json() assert r.status_code == 400 assert content["detail"] == "The user is not a superuser"
def test_update_contract_attach_nanny_by_user(client: TestClient, normal_user_token_headers: dict, db: Session) -> None: r = client.get(f"{settings.API_V1_STR}/users/me", headers=normal_user_token_headers) user_id = r.json()["id"] contract = create_random_contract(db, user_id=user_id, has_nanny=False) assert contract.user_id == user_id assert contract.nanny_id == None assert not contract.nanny_id nanny = create_random_user(db) response = client.put( f"{settings.API_V1_STR}/contracts/{contract.id}/nanny", headers=normal_user_token_headers, json=f"{nanny.id}") assert response.status_code == 200 content = response.json() assert "id" in content assert "user_id" in content assert "nanny_id" in content assert content["user_id"] == user_id assert content["nanny_id"] == nanny.id assert content["weekdays"] == contract.weekdays assert content["weeks"] == contract.weeks assert content["hours"] == contract.hours assert content["price_hour_standard"] == contract.price_hour_standard assert content["price_hour_extra"] == contract.price_hour_extra assert content["price_fees"] == contract.price_fees assert content["price_meals"] == contract.price_meals assert content["start"] == str(contract.start) assert content["end"] == str(contract.end) response = client.get( f"{settings.API_V1_STR}/contracts/{contract.id}", headers=normal_user_token_headers, ) assert response.status_code == 200 content = response.json() assert "id" in content assert "user_id" in content assert "nanny_id" in content assert content["user_id"] == user_id assert content["nanny_id"] == nanny.id assert content["weekdays"] == contract.weekdays assert content["weeks"] == contract.weeks assert content["hours"] == contract.hours assert content["price_hour_standard"] == contract.price_hour_standard assert content["price_hour_extra"] == contract.price_hour_extra assert content["price_fees"] == contract.price_fees assert content["price_meals"] == contract.price_meals assert content["start"] == str(contract.start) assert content["end"] == str(contract.end)
def test_update_automatic_debit(db: Session): debit_in = DebitCreate() user = create_random_user(db) debit = crud.debit.create_with_owner(db=db, obj_in=debit_in, owner_id=user.id) status = "approved" debit_update = DebitUpdate(status=status) debit2 = crud.debit.update_status(db, db_obj=debit, obj_in=debit_update) assert debit_update.status == status assert debit.id == debit2.id assert debit.owner_id == debit2.owner_id
def test_get_professor_me_normal_professor(client: TestClient, db: Session) -> None: professor = create_random_user(db, type="professor") r = client.get( f"{settings.API_V1_STR}/professors/me", headers=authentication_token_from_email(client=client, email=professor.email, db=db), ) assert r.status_code == 200 fetched_professor = r.json() assert fetched_professor["user_id"] == professor.id
def create_random_note(db: Session, *, author_id: Optional[int] = None) -> models.Note: if author_id is None: user = create_random_user(db) author_id = user.id title = random_lower_string() description = random_lower_string() note_in = NoteCreate(title=title, description=description, id=id) return crud.note.create_with_author(db=db, obj_in=note_in, author_id=author_id)
def test_delete_quote(): user = create_random_user() quote = create_random_quote(user.id) title = quote.title description = quote.description quote2 = crud.quote.remove(db_session=db_session, id=quote.id) quote3 = crud.quote.get(db_session=db_session, id=quote.id) assert quote3 is None assert quote2.id == quote.id assert quote2.title == title assert quote2.description == description assert quote2.owner_id == user.id
def test_create_item(db: Session) -> None: geometry = Point(0.0, 0.0) properties = {"name": "test-name1"} item_in = ItemCreate(geometry=geometry, properties=properties) user = create_random_user(db) collection = create_random_collection(user) item = services.item.create_collection_item(InternalUserDTO(**user.dict()), collection.uuid, item_in.to_dto()) assert to_shape(item.geometry) == geometry assert item.properties == properties assert item.collection_uuid == collection.uuid
def test_read_professor_by_id_superuser(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None: professor = create_random_user(db, type="professor") r = client.get( f"{settings.API_V1_STR}/professors/{professor.id}", headers=superuser_token_headers, ) assert r.status_code == 200 fetched_professor = r.json() assert fetched_professor["user_id"] == professor.id
def test_promote_professor(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None: user = create_random_user(db, type="professor") data = {"is_admin": True} r = client.put( f"{settings.API_V1_STR}/users/{user.id}", headers=superuser_token_headers, json=data, ) assert r.status_code == 200 admin = crud.admin.get(db, id=user.id) assert admin assert admin.permissions == 0
def create_random_student(db: Session, term_id: Optional[str] = None, school_id: Optional[str] = None) -> Student: user = create_random_user(db, type="student", school_id=school_id) if term_id and crud.term.get(db, id=term_id): student_in = StudentUpdate(user_id=user.id, term_id=term_id) else: term = create_random_term(db) student_in = StudentUpdate(user_id=user.id, term_id=term.id) student = crud.student.get(db, id=user.id) assert student return crud.student.update(db, db_obj=student, obj_in=student_in)
def test_create_item(db: Session, test_itemtype, test_category, test_payment) -> None: user = create_random_user(db) item = _create_item(db, test_itemtype.id, test_category.id, test_payment.id, user.id) assert item.description == DESCRIPTION assert item.amount == AMOUNT assert item.date == datetime.date(2020, 12, 1) assert item.itemtype_id == test_itemtype.id assert item.payment_id == test_payment.id assert item.category_id == test_category.id assert item.owner_id == user.id
def test_update_user_normal_user(client: TestClient, normal_user_token_headers: dict[str, str], db: Session) -> None: user = create_random_user(db, type="student") full_name = random_lower_string() email = random_email() password = random_password() data = {"full_name": full_name, "email": email, "password": password} r = client.put( f"{settings.API_V1_STR}/users/{user.id}", headers=normal_user_token_headers, json=data, ) assert r.status_code == 403
def test_update_user_superuser(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None: user = create_random_user(db, type="student") full_name = random_lower_string() data = {"full_name": full_name} r = client.put( f"{settings.API_V1_STR}/users/{user.id}", headers=superuser_token_headers, json=data, ) assert r.status_code == 200 updated_user = r.json() db.refresh(user) compare_api_and_db_query_results(api_result=updated_user, db_dict=to_json(user))
def test_update_user( client: TestClient, superuser_token_headers: dict, db: Session ) -> None: user = create_random_user(db) data = {"email": random_email()} r = client.put( f"{settings.API_V1_STR}/users/{user.id}", headers=superuser_token_headers, json=data, ) content = r.json() assert r.status_code == 200 assert content["email"] == data["email"]
def test_add_term_students_not_a_user(client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None: term = create_random_term(db) students = [ create_random_user(db, type="student", school_id=term.year.school_id), create_random_user(db, type="student", school_id=term.year.school_id), ] data = [user.id for user in students] new_user_id = generate_uuid() data.append(new_user_id) r = client.post(f"{settings.API_V1_STR}/terms/{term.id}/students", headers=superuser_token_headers, json=data) assert r.status_code == 207 response = r.json() assert response.get("errors").get("not a user")[0] == new_user_id for user in students: student = crud.student.get(db, id=user.id) assert student assert student.user_id in response.get("success") assert student.term_id == term.id
def test_read_professor_divisions_by_id_superuser( client: TestClient, superuser_token_headers: dict[str, str], db: Session) -> None: professor = create_random_user(db, type="professor") division = create_random_division(db, professor_id=professor.id) r = client.get( f"{settings.API_V1_STR}/professors/{professor.id}/divisions", headers=superuser_token_headers, ) assert r.status_code == 200 divisions = r.json() compare_api_and_db_query_results(api_result=divisions[-1], db_dict=to_json(division))
def test_update_item(db: Session) -> None: title = random_lower_string() description = random_lower_string() item_in = ItemCreate(title=title, description=description) user = create_random_user(db) item = crud.item.create_with_owner(db=db, obj_in=item_in, owner_id=user.id) description2 = random_lower_string() item_update = ItemUpdate(description=description2) item2 = crud.item.update(db=db, db_obj=item, obj_in=item_update) assert item.id == item2.id assert item.title == item2.title assert item2.description == description2 assert item.owner_id == item2.owner_id