Skip to content

Commit

Permalink
test: ✅ add e2e and api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nutfdt committed Oct 15, 2024
1 parent 4089ac7 commit f878059
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 15 deletions.
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PyJWT==2.8.0
cryptography==43.0.1
# ML
basegun-ml==2.0.1
numpy<2
# Dev
pytest==7.4.3
coverage==7.6.1
Expand Down
7 changes: 5 additions & 2 deletions backend/src/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from basegun_ml.classification import get_typology
from basegun_ml.measure import get_lengths
from basegun_ml.ocr import is_alarm_weapon, LowQuality, MissingText
from basegun_ml.ocr import LowQuality, MissingText, is_alarm_weapon
from fastapi import (
APIRouter,
BackgroundTasks,
Expand Down Expand Up @@ -37,6 +37,7 @@ def home():
def version():
return APP_VERSION


@router.post("/upload")
async def imageupload(
request: Request,
Expand Down Expand Up @@ -78,7 +79,7 @@ async def imageupload(

except Exception as e:
extras_logging["bg_error_type"] = e.__class__.__name__
logging.exception(e, extra=extras_logging)
logging.exception(e, extra=extras_logging)
# Temporary fix while ML package send 0 instead of None
# https://github.com/dnum-mi/basegun-ml/issues/14
gun_length = None if gun_length == 0 else gun_length
Expand Down Expand Up @@ -109,6 +110,7 @@ async def imageupload(
logging.exception(e, extra=extras_logging)
raise HTTPException(status_code=500, detail=str(e))


@router.post("/identification-blank-gun")
async def imageblankgun(
image: UploadFile = File(...),
Expand Down Expand Up @@ -138,6 +140,7 @@ async def imageblankgun(
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))


@router.post("/identification-feedback")
async def log_feedback(request: Request, user_id: Union[str, None] = Cookie(None)):
res = await request.json()
Expand Down
Binary file added backend/tests/blank_gun.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/tests/low_quality.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/tests/no_text.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions backend/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,41 @@ def test_403(self):
response = client.post("/api/expert-contact")
response.data = response.json()
assert response.status_code == 403


class TestBlankGunUpload:
def test_blank_gun(self):
with open("./tests/blank_gun.jpg", "rb") as f:
response = client.post(
"/api/identification-blank-gun",
files={"image": f},
)
response.data = response.json()
assert response.status_code == 200
response.data["alarm_model"] == "Alarm_model"
not response.data["missing_text"]
not response.data["low_quality"]

def test_bad_quality(self):
with open("./tests/low_quality.jpg", "rb") as f:
response = client.post(
"/api/identification-blank-gun",
files={"image": f},
)
response.data = response.json()
assert response.status_code == 200
response.data["alarm_model"] is None
not response.data["missing_text"]
response.data["missing_text"]

def test_missing_text(self):
with open("./tests/no_text.jpg", "rb") as f:
response = client.post(
"/api/identification-blank-gun",
files={"image": f},
)
response.data = response.json()
assert response.status_code == 200
response.data["alarm_model"] is None
response.data["missing_text"]
not response.data["low_quality"]
53 changes: 53 additions & 0 deletions frontend/cypress/e2e/blank-gun-detection.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
describe("Blank Gun Detection", () => {
it("should identificate real blank gun", () => {
cy.Identification();

cy.getByDataTestid("select-file").as("fileInput");
cy.intercept("POST", "/api/upload").as("upload");
cy.get("@fileInput").selectFile("./cypress/images/blank-gun.jpg", {
force: true,
});
cy.wait("@upload").then(({ response }) => {
expect(response.statusCode).to.eq(200);
});
cy.getByDataTestid("next-step").click();
cy.IdentificationPistoletSemiAuto();
cy.wait(5000);
cy.url().should("contain", "/guide-identification/resultat-final");
cy.getByDataTestid("arm-category").should("contain", "Catégorie C");
});

it("should identificate firearm with missing text", () => {
cy.Identification();

cy.getByDataTestid("select-file").as("fileInput");
cy.intercept("POST", "/api/upload").as("upload");
cy.get("@fileInput").selectFile("./cypress/images/no-text.jpg", {
force: true,
});
cy.wait("@upload").then(({ response }) => {
expect(response.statusCode).to.eq(200);
});
cy.getByDataTestid("next-step").click();
cy.IdentificationBlankGunMissingText();
cy.url().should("contain", "/guide-identification/resultat-final");
cy.getByDataTestid("arm-category").should("contain", "Catégorie B");
});

it("should identificate firearm with low quality", () => {
cy.Identification();

cy.getByDataTestid("select-file").as("fileInput");
cy.intercept("POST", "/api/upload").as("upload");
cy.get("@fileInput").selectFile("./cypress/images/low-quality.jpg", {
force: true,
});
cy.wait("@upload").then(({ response }) => {
expect(response.statusCode).to.eq(200);
});
cy.getByDataTestid("next-step").click();
cy.IdentificationBlankGunLowQuality();
cy.url().should("contain", "/guide-identification/resultat-final");
cy.getByDataTestid("arm-category").should("contain", "Catégorie B");
});
});
1 change: 1 addition & 0 deletions frontend/cypress/e2e/firearm-confidence.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe("Firearm Confidence", () => {
});
cy.getByDataTestid("next-step").click();
cy.IdentificationPistoletSemiAuto();
cy.wait(5000);
cy.url().should("contain", "/guide-identification/resultat-final");
cy.getByDataTestid("arm-category").should("contain", "Catégorie B");
});
Expand Down
1 change: 1 addition & 0 deletions frontend/cypress/e2e/firearm-identification.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("Firearm Identification", () => {
});
cy.getByDataTestid("next-step").click();
cy.IdentificationPistoletSemiAuto();
cy.wait(5000);
cy.url().should("contain", "/guide-identification/resultat-final");
cy.getByDataTestid("arm-category").should("contain", "Catégorie B");
cy.getByDataTestid("return-to-home-end").click();
Expand Down
1 change: 1 addition & 0 deletions frontend/cypress/e2e/firearm-securing.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe("Securing Firearm and Identification", () => {
cy.contains("p", "Basegun a identifié votre arme");
cy.getByDataTestid("next-step").click();
cy.IdentificationPistoletSemiAuto();
cy.wait(5000);
cy.url().should("contain", "/guide-identification/resultat-final");
cy.getByDataTestid("arm-category").should("contain", "Catégorie B");
cy.getByDataTestid("return-to-home-end").click();
Expand Down
Binary file added frontend/cypress/images/blank-gun.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/cypress/images/low-quality.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/cypress/images/no-text.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 43 additions & 10 deletions frontend/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ Cypress.Commands.add("IdentificationPistoletSemiAuto", () => {
cy.contains("Cartouches").first().click();
cy.getByDataTestid("next-step").should("not.have.attr", "disabled");
cy.getByDataTestid("next-step").click();
cy.url().should("contain", "/guide-identification/armes-alarme");
cy.getByDataTestid("instruction-armeAlarme").should("contain", "Votre arme");
cy.getByDataTestid("next-step").click();
cy.getByDataTestid("aucune-correspondance").click();
cy.getByDataTestid("next-step").click();
cy.url().should("contain", "/guide-identification/qualite-image");
cy.getByDataTestid("title-page").should(
"contain",
"Identification d'une arme d'alarme",
);
});

Cypress.Commands.add("IdentificationRevolver", () => {
Expand All @@ -133,11 +133,9 @@ Cypress.Commands.add("IdentificationRevolver", () => {
cy.contains("Balles").first().click();
cy.getByDataTestid("next-step").should("not.have.attr", "disabled");
cy.getByDataTestid("next-step").click();
cy.url().should("contain", "/guide-identification/armes-alarme");
cy.getByDataTestid("instruction-armeAlarme").should("contain", "Votre arme");
cy.getByDataTestid("next-step").click();
cy.getByDataTestid("aucune-correspondance").click();
cy.getByDataTestid("next-step").click();
cy.url().should("contain", "/guide-identification/qualite-image");
cy.getByDataTestid("title-page").should("contain", "Marquages non détéctés");
cy.get('.fr-col-12 > [data-testid="next-step"]').click();
});

Cypress.Commands.add("arrierePlatRevolver", () => {
Expand Down Expand Up @@ -198,3 +196,38 @@ Cypress.Commands.add("pasDeGuide", () => {
cy.getByDataTestid("go-to-identification").click();
cy.url().should("contain", "/guide-identification/resultat-typologie");
});

Cypress.Commands.add("IdentificationBlankGunMissingText", () => {
cy.url().should(
"contain",
"guide-identification/informations-complementaires",
);
cy.getByDataTestid("next-step").click();
cy.url().should("contain", "/guide-identification/munition-type");
cy.getByDataTestid("next-step").should("have.attr", "disabled");
cy.contains("Cartouches").first().click();
cy.getByDataTestid("next-step").should("not.have.attr", "disabled");
cy.getByDataTestid("next-step").click();
cy.url().should("contain", "/guide-identification/qualite-image");
cy.getByDataTestid("title-page").should("contain", "Marquages non détéctés");
cy.get('.fr-col-12 > [data-testid="next-step"]').click();
});

Cypress.Commands.add("IdentificationBlankGunLowQuality", () => {
cy.url().should(
"contain",
"guide-identification/informations-complementaires",
);
cy.getByDataTestid("next-step").click();
cy.url().should("contain", "/guide-identification/munition-type");
cy.getByDataTestid("next-step").should("have.attr", "disabled");
cy.contains("Cartouches").first().click();
cy.getByDataTestid("next-step").should("not.have.attr", "disabled");
cy.getByDataTestid("next-step").click();
cy.url().should("contain", "/guide-identification/qualite-image");
cy.getByDataTestid("title-page").should(
"contain",
"Qualité d'image insuffisante",
);
cy.get('.fr-col-12 > [data-testid="next-step"]').click();
});
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ watch(alarmModel, (newVal) => {
<DsfrButton
class="flex justify-center !w-full"
data-testid="next-step"
:label="footerValue.next"
:icon-right="true"
icon="ri-checkbox-circle-line"
Expand All @@ -305,6 +306,7 @@ watch(alarmModel, (newVal) => {
</div>
<div v-else>
<div class="fr-col-11 fr-col-lg-6 footer-actions mx-auto">
<VIcon name="ri:check-double-fill" animation="flash" />
<p>Redirection vers la page de résultat dans 5 secondes</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ async function uploadImage(base64: string, fileName: string) {
onMounted(async () => {
const base64String = img.value;
if (base64String) {
console.log("Chaîne Base64 récupérée pour l'envoi:", base64String);
await uploadImage(base64String, "image.jpg");
} else {
console.error("La chaîne Base64 est vide ou non définie.");
Expand Down Expand Up @@ -111,7 +110,7 @@ const alert = computed(() => {
</script>
<template>
<h2 class="fr-mt-3w flex justify-center titlePage">
<span> {{ title }}</span>
<span data-testid="title-page"> {{ title }}</span>
</h2>

<div class="fr-p-1w">
Expand Down Expand Up @@ -142,6 +141,6 @@ const alert = computed(() => {
.img-deco {
margin-top: auto;
margin-bottom: 1em;
width: 75%;
width: 65%;
}
</style>

0 comments on commit f878059

Please sign in to comment.