Skip to content

Commit

Permalink
Merge pull request #2678 from MTES-MCT/master
Browse files Browse the repository at this point in the history
Master -> dev
  • Loading branch information
Riron authored Sep 1, 2023
2 parents df19258 + 78a2929 commit 7ca2047
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
37 changes: 37 additions & 0 deletions back/src/common/middlewares/__tests__/graphqlBodyParser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import express, { json } from "express";
import supertest from "supertest";
import { graphqlBodyParser } from "../graphqlBodyParser";

describe("graphqlBodyParser", () => {
const app = express();
const graphQLPath = "/";
app.use(json());
app.use(graphQLPath, graphqlBodyParser);

app.get("/hello", (req, res) => {
res.status(200).send("world");
});
app.post(graphQLPath, (req, res) => {
// Return body
res.status(200).send({ reqBody: req.body });
});

const request = supertest(app);

it("should convert JSON encoded variables to JSON", async () => {
const response = await request
.post(graphQLPath)
.send({ query: "{ __typename }", variables: '{ "foo": 1 }' });

expect(response.body.reqBody.variables.foo).toBe(1);
});

it("should convert application/graphql requests", async () => {
const response = await request
.post(graphQLPath)
.set("Content-Type", "application/graphql")
.send("{ __typename }");

expect(response.body.reqBody.query).toBe("{ __typename }");
});
});
14 changes: 12 additions & 2 deletions back/src/common/middlewares/graphqlBodyParser.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { Request, Response, NextFunction, text } from "express";

/**
* GraphQL server middleware to support application/graphql requests
* Taken from https://github.com/graphql-middleware/body-parser-graphql
* GraphQL server middleware to:
* - accept JSON-encoded strings for gql variables
* - support application/graphql requests (taken from https://github.com/graphql-middleware/body-parser-graphql)
*/
export function graphqlBodyParser(
req: Request,
res: Response,
next: NextFunction
) {
if (typeof req.body?.variables === "string") {
try {
req.body.variables = JSON.parse(req.body.variables);
} catch (e) {
// https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#json-parsing-failure
res.status(400).send(e instanceof Error ? e.message : e);
}
}

if (req.is("application/graphql")) {
text({ type: "application/graphql" })(req, res, () => {
req.headers["content-type"] = "application/json";
Expand Down
7 changes: 6 additions & 1 deletion back/src/companies/sirenify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ export default function buildSirenify<T>(
);
}

if (companySearchResult.statutDiffusionEtablissement === "O") {
if (
companySearchResult.statutDiffusionEtablissement === "O" ||
// on auto-complète également nom et adresse si l'établissement est non diffusible
// mais inscrit sur Trackdéchets
companySearchResult.isRegistered
) {
const { setter, getter } = accessors[idx];

sirenifiedInput = setter(sirenifiedInput, {
Expand Down
47 changes: 47 additions & 0 deletions back/src/forms/__tests__/sirenify.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,53 @@ describe("sirenifyFormInput", () => {
searchResults[intermediary2.company.siret!].address
);
});

it("should overwrite `name` and `address` if company is not diffusible but registered in Trackdéchets", async () => {
const emitter = await userWithCompanyFactory("MEMBER", {
siret: "90398556200011",
orgId: "90398556200011"
});

function searchResult(companyName: string) {
return {
name: companyName,
address: `Adresse ${companyName}`,
statutDiffusionEtablissement: "N",
isRegistered: true
} as CompanySearchResult;
}

const searchResults = {
[emitter.company.siret!]: searchResult("émetteur")
};

searchCompanySpy.mockImplementation((clue: string) => {
return Promise.resolve(searchResults[clue]);
});

const formInput: CreateFormInput = {
emitter: {
company: {
siret: "90398556200011"
}
}
};

const sirenified = await sirenifyFormInput(formInput, {
email: "[email protected]",
auth: AuthType.Bearer
} as Express.User);

expect(sirenified).toMatchObject({
emitter: {
company: {
name: "émetteur",
address: "Adresse émetteur",
siret: "90398556200011"
}
}
});
});
});

describe("sirenifyResealedFormInput", () => {
Expand Down

0 comments on commit 7ca2047

Please sign in to comment.