def test_user_infos_is_saved_on_token_call( client: TestClient, event_loop: asyncio.AbstractEventLoop): assert AUTH_DISABLED_DUMMY_USER token_route_dummy_params = dict(code="some_code", redirect_uri="redirect_uri") # Fred connects connection_api.set_user_name(nom="Fred") # type: ignore response = client.get( token_path, params=token_route_dummy_params, headers=auth_headers(), ) assert response.status_code == 200 query = event_loop.run_until_complete( AdemeUtilisateur.filter(ademe_user_id="dummy", )) assert query[0].nom == "Fred" # Fred connects with new name Frederic connection_api.set_user_name(nom="Frederic") # type: ignore response = client.get(token_path, params=token_route_dummy_params, headers=auth_headers()) query = event_loop.run_until_complete( AdemeUtilisateur.filter(ademe_user_id="dummy", )) assert query[0].nom == "Frederic"
def test_crud_item(client: TestClient, event_loop: asyncio.AbstractEventLoop): add_ecriture_droit(client, epci_id=fiche_action["epci_id"]) # POST /v2/fiche_action/epci_id response = client.post(post_path, json=fiche_action, headers=auth_headers()) assert response.status_code == 200 assert response.json()["uid"] == fiche_action["uid"] # GET /v2/fiche_action/epci_id/all response = client.get(list_path) assert response.status_code == 200 assert len(response.json()) == 1 assert response.json()[0]["uid"] == fiche_action["uid"] # GET /v2/fiche_action/epci_id/uid response = client.get(item_path) assert response.status_code == 200 assert response.json()["uid"] == fiche_action["uid"] # DELETE /v2/fiche_action/epci_id/uid response = client.delete(item_path, headers=auth_headers()) assert response.status_code == 200 # 404 # GET /v2/fiche_action/epci_id/uid response = client.get(item_path) assert response.status_code == 404 # DELETE /v2/fiche_action/epci_id/uid response = client.delete(item_path, headers=auth_headers()) assert response.status_code == 404
def test_crud_item(client: TestClient, event_loop: asyncio.AbstractEventLoop): add_ecriture_droit(client, epci_id=indicateur_personnalise["epci_id"]) # POST /epci_id/v2/indicateur_personnalise response = client.post(post_path, json=indicateur_personnalise, headers=auth_headers()) assert response.status_code == 200 assert response.json()["uid"] == indicateur_personnalise["uid"] # GET /epci_id/v2/indicateur_personnalise/all response = client.get(list_path) assert response.status_code == 200 assert len(response.json()) == 1 assert response.json()[0]["uid"] == indicateur_personnalise["uid"] # GET /epci_id/v2/indicateur_personnalise/uid response = client.get(item_path) assert response.status_code == 200 assert response.json()["uid"] == indicateur_personnalise["uid"] assert response.json()["meta"] == indicateur_personnalise["meta"] # DELETE /epci_id/v2/indicateur_personnalise/uid response = client.delete(item_path, headers=auth_headers()) assert response.status_code == 200 # 404 # GET /epci_id/v2/indicateur_personnalise/uid response = client.get(item_path) assert response.status_code == 404 # DELETE /epci_id/v2/indicateur_personnalise/uid response = client.delete(item_path, headers=auth_headers()) assert response.status_code == 404
def test_droits(client: TestClient, event_loop: asyncio.AbstractEventLoop): # 401 # POST /v2/fiche_action/epci_id response = client.post(post_path, json=fiche_action, headers=auth_headers()) assert response.status_code == 401
def test_update_fiche_action(client: TestClient): new_data = { "avancement": "faite", "indicateur_personnalise_ids": ["3"], } existing_fiche_action = {**fiche_action, **new_data} post_path = f"{path}/{existing_fiche_action['epci_id']}" response = client.post(post_path, json=existing_fiche_action, headers=auth_headers()) assert response.status_code == 200 assert response.json()["uid"] == existing_fiche_action["uid"] assert response.json()["avancement"] == existing_fiche_action["avancement"] assert (response.json()["indicateur_personnalise_ids"] == existing_fiche_action["indicateur_personnalise_ids"]) response = client.get(list_path) assert response.status_code == 200 assert len(response.json()) == 1 assert response.json()[0]["uid"] == existing_fiche_action["uid"] assert response.json( )[0]["avancement"] == existing_fiche_action["avancement"] assert (response.json()[0]["indicateur_personnalise_ids"] == existing_fiche_action["indicateur_personnalise_ids"])
def test_update_indicateur_referentiel_commentaire(client: TestClient): new_data = { "value": "And what is the use of a book,' thought Alice, 'without pictures or conversation?", } existing_indicateur_referentiel_commentaire = { **indicateur_referentiel_commentaire, **new_data, } post_path = f"{path}/{existing_indicateur_referentiel_commentaire['epci_id']}" response = client.post( post_path, json=existing_indicateur_referentiel_commentaire, headers=auth_headers(), ) assert response.status_code == 200 assert (response.json()["indicateur_id"] == existing_indicateur_referentiel_commentaire["indicateur_id"]) assert (response.json()["value"] == existing_indicateur_referentiel_commentaire["value"]) response = client.get(list_path) assert response.status_code == 200 assert len(response.json()) == 1 assert (response.json()[0]["indicateur_id"] == existing_indicateur_referentiel_commentaire["indicateur_id"]) assert (response.json()[0]["value"] == existing_indicateur_referentiel_commentaire["value"])
def test_validation_error(self, test_client, logged_user): with test_client as client: response = client.post("/hero", json={}, headers=auth_headers( logged_user.access_token)) assert response.status_code == 422 assert response.json() == { "detail": [ { "loc": ["body", "dto", "nickname"], "msg": "field required", "type": "value_error.missing", }, { "loc": ["body", "dto", "power_class"], "msg": "field required", "type": "value_error.missing", }, { "loc": ["body", "dto", "location"], "msg": "field required", "type": "value_error.missing", }, ] }
def post_with_droits( client: TestClient, epci_id: str, post_json: dict, post_path: str, ) -> Response: add_ecriture_droit(client, epci_id=epci_id) return client.post(post_path, json=post_json, headers=auth_headers())
def test_not_found(self, test_client, logged_user, make_update_hero_dto): dto = make_update_hero_dto() with test_client as client: response = client.put( "/hero/1", headers=auth_headers(logged_user.access_token), json=dto.dict(), ) assert response.status_code == 404
def test_droits(client: TestClient, event_loop: asyncio.AbstractEventLoop): # 401 # POST /v2/indicateur_referentiel_commentaire/epci_id response = client.post( post_path, headers=auth_headers(), json=indicateur_referentiel_commentaire, ) assert response.status_code == 401
def test_droits(client: TestClient): # 401 # POST /v2/indicateur_personnalise_value/epci_id response = client.post( post_path, headers=auth_headers(), json=indicateur_X_2019, ) assert response.status_code == 401
def test_found(self, test_client, logged_user, make_hero): id_ = 1 hero = make_hero(id=id_, user_id=logged_user.user.id) insert_hero(hero.dict()) with test_client as client: response = client.delete("/hero/1", headers=auth_headers( logged_user.access_token)) assert response.status_code == 204
def test_unauthorized(self, test_client): with test_client as client: token = str(jwt.encode({"sub": "userid:1"}, secret_key)) response = client.get( oauth2_instrospect_url, headers=auth_headers(token), ) assert response.json().get("detail") == "invalid token" assert response.status_code == 401
def test_success(self, test_client, logged_user, make_hero): id_ = 1 hero = make_hero(id=id_, user_id=logged_user.user.id) insert_hero(hero.dict()) with test_client as client: response = client.get(f"/hero/{id_}", headers=auth_headers( logged_user.access_token)) assert response.status_code == 200 assert response.json() == hero.dict()
def test_create_mismatched_fiche_action(client: TestClient): mismatched_data = { "epci_id": "mismatch-epci-id", } mismatched_fiche_action = {**fiche_action, **mismatched_data} post_path = f"{path}/{fiche_action['epci_id']}" response = client.post(post_path, json=mismatched_fiche_action, headers=auth_headers()) assert response.status_code == 400
def test_success(self, test_client, logged_user): user, access_token = logged_user with test_client as client: response = client.get( oauth2_instrospect_url, headers=auth_headers(logged_user.access_token), ) body = response.json() assert body == { "id": user.id, "email": user.email, } assert response.status_code == 200
def test_create_mismatched_status(client: TestClient): add_ecriture_droit(client, epci_id=status["epci_id"]) mismatched_data = { "epci_id": "mismatch-epci-id", } mismatched_status = {**status, **mismatched_data} post_path = f"{path}/{status['epci_id']}" response = client.post(post_path, headers=auth_headers(), json=mismatched_status) assert response.status_code == 400
def test_from_other_user(self, test_client, logged_user, make_user, make_hero): other_user = make_user() insert_user(other_user.dict()) id_ = 1 hero = make_hero(id=id_, user_id=other_user.id) insert_hero(hero.dict()) with test_client as client: response = client.get(f"/hero/{id_}", headers=auth_headers( logged_user.access_token)) assert response.status_code == 404
def test_success(self, test_client, logged_user, make_hero, make_update_hero_dto): id_ = 1 hero = make_hero(id=id_, user_id=logged_user.user.id) insert_hero(hero.dict()) dto = make_update_hero_dto() with test_client as client: response = client.put( f"/hero/{id_}", headers=auth_headers(logged_user.access_token), json=dto.dict(), ) assert response.status_code == 200 assert response.json() == {**hero.dict(), **dto.dict()}
def test_success(self, test_client, logged_user, make_create_hero_dto): dto = make_create_hero_dto() with test_client as client: response = client.post("/hero", json=dto.dict(), headers=auth_headers( logged_user.access_token)) assert response.status_code == 201 getter = itemgetter("user_id", "name", "nickname", "power_class", "location") assert getter(response.json()) == getter({ **dto.dict(), "user_id": logged_user.user.id })
def test_create_mismatched_indicateur_personnalise(client: TestClient): mismatched_data = { "epci_id": "mismatch-epci-id", } mismatched_indicateur_personnalise = { **indicateur_personnalise, **mismatched_data } post_path = f"{path}/{indicateur_personnalise['epci_id']}" response = client.post(post_path, json=mismatched_indicateur_personnalise, headers=auth_headers()) assert response.status_code == 400
def test_add(client: TestClient, event_loop: asyncio.AbstractEventLoop): # POST /v2/action_status/epci_id response = client.post(post_path, json=epci, headers=auth_headers()) assert response.status_code == 200 assert response.json()["uid"] == epci["uid"] # GET /v2/action_status/epci_id/all response = client.get(list_path) assert response.status_code == 200 assert len(response.json()) == 1 assert response.json()[0]["uid"] == epci["uid"] # GET /v2/action_status/epci_id/uid response = client.get(item_path) assert response.status_code == 200 assert response.json()["uid"] == epci["uid"]
def test_crud_item(client: TestClient, event_loop: asyncio.AbstractEventLoop): add_ecriture_droit(client, epci_id=status["epci_id"]) # POST /v2/action_status/epci_id response = client.post(post_path, json=status, headers=auth_headers()) assert response.status_code == 200 assert response.json()["action_id"] == status["action_id"] # GET /v2/action_status/epci_id/all response = client.get(list_path) assert response.status_code == 200 assert len(response.json()) == 1 assert response.json()[0]["action_id"] == status["action_id"] # GET /v2/action_status/epci_id/action_id response = client.get(item_path) assert response.status_code == 200 assert response.json()["action_id"] == status["action_id"]
def test_create_mismatched_indicateur_referentiel_commentaire( client: TestClient): mismatched_data = { "epci_id": "mismatch-epci-id", } mismatched_indicateur_referentiel_commentaire = { **indicateur_referentiel_commentaire, **mismatched_data, } post_path = f"{path}/{indicateur_referentiel_commentaire['epci_id']}" response = client.post( post_path, json=mismatched_indicateur_referentiel_commentaire, headers=auth_headers(), ) assert response.status_code == 400
def test_conflict(self, test_client, logged_user, make_hero, make_create_hero_dto): hero = make_hero(user_id=logged_user.user.id) insert_hero(hero.dict()) dto = make_create_hero_dto(name=hero.name, nickname=hero.nickname) with test_client as client: response = client.post("/hero", json=dto.dict(), headers=auth_headers( logged_user.access_token)) assert response.status_code == 409 assert response.json() == { "detail": { "msg": "hero already exists", "type": "conflict_error.not_unique", } }
def test_validation_error(self, test_client, logged_user): with test_client as client: response = client.put( "/hero/1", headers=auth_headers(logged_user.access_token), json={ "name": [], "nickname": [], "power_class": "over 9000", "location": "brazil", }, ) assert response.status_code == 422 assert response.json() == { "detail": [ { "loc": ["body", "dto", "name"], "msg": "str type expected", "type": "type_error.str", }, { "loc": ["body", "dto", "nickname"], "msg": "str type expected", "type": "type_error.str", }, { "ctx": { "enum_values": ["S", "A", "B", "C"] }, "loc": ["body", "dto", "power_class"], "msg": "value is not a valid enumeration member; permitted: 'S', " "'A', 'B', 'C'", "type": "type_error.enum", }, { "loc": ["body", "dto", "location"], "msg": "value is not a valid dict", "type": "type_error.dict", }, ] }
def test_success(self, test_client, logged_user, make_user, make_heroes): other_user = make_user() insert_user(other_user.dict()) heroes = [ hero.dict() for hero in [ *make_heroes(10, user_id=logged_user.user.id), *make_heroes(10, user_id=other_user.id), ] ] insert_hero(heroes) with test_client as client: response = client.get("/hero", headers=auth_headers( logged_user.access_token)) assert response.status_code == 200 response_body = response.json() assert len(response_body) == len(heroes) for hero in response_body: assert hero in heroes
def test_update_fiche_action_categorie(client: TestClient): new_data = { "nom": "Alice", } existing_fiche_action_categorie = {**plan, **new_data} post_path = f"{path}/{existing_fiche_action_categorie['epci_id']}" response = client.post(post_path, json=existing_fiche_action_categorie, headers=auth_headers()) assert response.status_code == 200 assert response.json()["uid"] == existing_fiche_action_categorie["uid"] assert response.json()["nom"] == existing_fiche_action_categorie["nom"] response = client.get(list_path) assert response.status_code == 200 assert len(response.json()) == 2 assert response.json()[1]["uid"] == existing_fiche_action_categorie["uid"] assert response.json()[1]["nom"] == existing_fiche_action_categorie["nom"]
def test_update_status(client: TestClient): add_ecriture_droit(client, epci_id=status["epci_id"]) new_data = { "avancement": "programmee", } new_status = {**status, **new_data} post_path = f"{path}/{new_status['epci_id']}" response = client.post(post_path, headers=auth_headers(), json=new_status) assert response.status_code == 200 assert response.json()["action_id"] == status["action_id"] assert response.json()["avancement"] == new_status["avancement"] response = client.get(list_path) assert response.status_code == 200 assert len(response.json()) == 1 assert response.json()[0]["action_id"] == status["action_id"] assert response.json()[0]["avancement"] == new_status["avancement"]
def test_update(client: TestClient): add_ecriture_droit(client, epci_id=epci["uid"]) new_data = { "nom": "Blue Kingdom", } updated_epci = {**epci, **new_data} response = client.post(post_path, headers=auth_headers(), json=updated_epci) assert response.status_code == 200 assert response.json()["uid"] == epci["uid"] assert response.json()["nom"] == new_data["nom"] response = client.get(list_path) assert response.status_code == 200 assert len(response.json()) == 1 assert response.json()[0]["uid"] == epci["uid"] assert response.json()[0]["nom"] == new_data["nom"]