async def test_create_record_authorized(): await create_superuser() async with client as c: response = await c.post( app.url_path_for("admin_create_record"), data=records[0].json(), headers=superuser_authorization, ) assert response.status_code == 201 assert await records_s.get_obj(id=1) json_resp = json.loads(response.content) for key in RecordOutAdmin.__fields__: assert key in json_resp
async def test_update_record_authorized(): await create_user() await create_records(id=1) async with client as c: response = await c.put( app.url_path_for("update_record", id=1), data=records[1].json(), headers=authorization, ) assert response.status_code == 200 json_resp = json.loads(response.content) for key in RecordOut.__fields__: assert key in json_resp for key in RecordIn.__fields__: assert json_resp[key] == records[1].dict()[key]
async def test_user_token_login(): await create_user() async with client as c: response = await c.post( app.url_path_for("user_token_login"), data={ "username": user_email, "password": user_password }, ) assert response.status_code == 200 json_resp = json.loads(response.content) assert json_resp["access_token"] assert json_resp["token_type"] == "bearer" assert response.cookies.get("refresh_token")
async def test_get_records_with_filter_params(): await create_superuser() await create_user() await create_records(id=1) await create_records(id=2) async with client as c: response = await c.get( app.url_path_for("admin_get_records"), params={"creator_id": 2}, headers=superuser_authorization, ) assert response.status_code == 200 json_resp = json.loads(response.content) for i in range(len(records)): assert json_resp["items"][i]["creator_id"] == 2
async def test_update_record_authorized(): await create_superuser() await create_user() await create_records(id=1) record_in = RecordInAdmin(**records[0].dict(), creator_id=2) async with client as c: response = await c.put( app.url_path_for("admin_update_record", id="1"), data=record_in.json(), headers=superuser_authorization, ) assert response.status_code == 200 assert await records_s.filter(creator_id=2) json_resp = json.loads(response.content) for key in RecordOutAdmin.__fields__: assert key in json_resp
async def test_reset_password(): await create_user() redis_instance = await Redis(host=settings.REDIS_HOST, password=settings.REDIS_PASSWORD, db=settings.REDIS_DB).get_instance() _, keys = await redis_instance.scan( match=f"{email_prefix}:{reset_prefix}:*") uuid = keys[0].decode("utf-8").split(":")[-1] async with client as c: response = await c.post( app.url_path_for("reset_password"), json={ "uuid": uuid, "password": "******" }, ) assert response.status_code == 200 user_obj = await User.get(id=1) assert user_obj.verify_password("New_password1")
async def test_get_records_with_filter_params(): await create_user() await create_records(id=1) """Testing filter with bad order query parameter""" async with client as c: response = await c.get( app.url_path_for("get_records"), headers=authorization, params={"order": "bad_order"}, ) assert response.status_code == 400 """Testing with -id order parameter""" async with client as c: response = await c.get( app.url_path_for("get_records"), headers=authorization, params={"order": "-id"}, ) assert response.status_code == 200 json_resp = json.loads(response.content) for i in range(len(records)): assert json_resp["items"][i]["id"] == len(records) - i """Testing with id order parameter""" async with client as c: response = await c.get( app.url_path_for("get_records"), headers=authorization, params={"order": "id"}, ) assert response.status_code == 200 json_resp = json.loads(response.content) for i in range(len(records)): assert json_resp["items"][i]["id"] == i + 1 """Testing with is_important = True parameter""" async with client as c: response = await c.get( app.url_path_for("get_records"), headers=authorization, params={"is_important": "1"}, ) json_resp = json.loads(response.content) assert response.status_code == 200 assert len(json_resp["items"]) == len(records) // 2 + 1 for i in range(len(json_resp["items"])): assert json_resp["items"][i]["is_important"] """Testing with is_important = False parameter""" async with client as c: response = await c.get( app.url_path_for("get_records"), headers=authorization, params={"is_important": "0"}, ) json_resp = json.loads(response.content) assert response.status_code == 200 assert len(json_resp["items"]) == len(records) // 2 for i in range(len(json_resp["items"])): assert json_resp["items"][i]["is_important"] is False """Testing with is_important + order parameters""" async with client as c: response = await c.get( app.url_path_for("get_records"), headers=authorization, params={"is_important": "1", "order": "id"}, ) assert response.status_code == 200 json_resp = json.loads(response.content) assert len(json_resp["items"]) == len(records) // 2 + 1 for i in range(len(json_resp["items"]) - 1): assert json_resp["items"][i]["id"] < json_resp["items"][i + 1]["id"] """Testing with date_from = now() => expecting 0 items""" async with client as c: response = await c.get( app.url_path_for("get_records"), headers=authorization, params={"date_from": datetime.now()}, ) assert response.status_code == 200 json_resp = json.loads(response.content) assert len(json_resp["items"]) == 0 """Testing with date_to = 3rd record creation time""" record = await records_s.get(id=3) time_created = record.create_date async with client as c: response = await c.get( app.url_path_for("get_records"), headers=authorization, params={"date_to": time_created}, ) assert response.status_code == 200 json_resp = json.loads(response.content) assert len(json_resp["items"]) == 3
async def test_get_records_unauthorized(): async with client as c: response = await c.get(app.url_path_for("get_records")) assert response.status_code == 401
async def test_post_record_unauthorized(): response = await client.post( app.url_path_for("create_record"), data=records[0].json() ) assert response.status_code == 401
async def test_delete_record_unauthorized(): async with client as c: response = await c.delete(app.url_path_for("delete_record", id=1)) assert response.status_code == 401
async def test_update_record_unauthorized(): async with client as c: response = await c.put( app.url_path_for("update_record", id=1), data=records[1].json() ) assert response.status_code == 401
async def test_user_recover_password_with_wrong_email(): async with client as c: response = await c.post(app.url_path_for("user_recover_password"), json={"email": "wrong_email"}) assert response.status_code == 404
async def test_user_token_refresh_without_authorization_header(): await create_user() async with client as c: response = await c.post(app.url_path_for("user_token_refresh"), ) assert response.status_code == 401
async def test_user_registration_confirm_with_bad_uuid(): async with client as c: response = await c.get( app.url_path_for("user_registration_confirm", uuid="bad_uuid")) assert response.status_code == 400
async def test_update_record_unauthorized(): async with client as c: response = await c.put(app.url_path_for("admin_update_record", id="1")) assert response.status_code == 401