Skip to content

Commit

Permalink
Merge pull request #112 from bcgov/feat/create-cif-user-upon-login
Browse files Browse the repository at this point in the history
Feat: create cif user upon login
  • Loading branch information
matthieu-foucault committed Nov 25, 2021
2 parents 5d8ee50 + 8e87a8e commit 6512517
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 19 deletions.
14 changes: 0 additions & 14 deletions app/mutations/User/createUserFromSession.ts

This file was deleted.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"wait-on": "^6.0.0"
},
"dependencies": {
"@bcgov-cas/sso-express": "^2.0.1",
"@bcgov-cas/sso-express": "^2.0.2",
"@bcgov-cas/sso-react": "^1.0.0",
"@bcgov/bc-sans": "^1.0.1",
"@button-inc/bcgov-theme": "^1.0.1",
Expand Down
3 changes: 3 additions & 0 deletions app/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import headersMiddleware from "./middleware/headers";
import graphQlMiddleware from "./middleware/graphql";
import { pgPool } from "./db";
import ssoMiddleware from "./middleware/sso";
import createUserMiddleware from "./middleware/createUser";

const port = Number.parseInt(process.env.PORT, 10) || 3004;
const dev = process.env.NODE_ENV !== "production";
Expand Down Expand Up @@ -46,6 +47,8 @@ app.prepare().then(async () => {

server.use(await ssoMiddleware());

server.get("/auth-callback", createUserMiddleware("localhost", port));

server.use(cookieParser());

server.use(graphQlMiddleware());
Expand Down
44 changes: 44 additions & 0 deletions app/server/middleware/createUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Request, Response, NextFunction } from "express";

// This middleware calls the createUserFromSession mutation.
// The request to that mutation is made with the current session
// cookies to ensure authentication.

const createUserMutation = `
mutation {
createUserFromSession(input: {}) {
__typename
}
}
`;

const createUserMiddleware = (host: string, port: number) => {
return async (req: Request, _res: Response, next: NextFunction) => {
const fetchOptions = {
method: "POST",
body: JSON.stringify({
query: createUserMutation,
variables: null,
}),
headers: {
"Content-Type": "application/json",
cookie: req.headers.cookie,
},
};

const response = await fetch(
`http://${host}:${port}/graphql`,
fetchOptions
);

if (!response?.ok) {
throw new Error(
`Failed to create user from session: ${response.statusText}`
);
}

next();
};
};

export default createUserMiddleware;
87 changes: 87 additions & 0 deletions app/tests/unit/server/middleware/createUser.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import createUserMiddleware from "server/middleware/createUser";

global.fetch = jest
.fn()
.mockImplementation(() => Promise.resolve({ ok: true }));

beforeEach(() => {
fetch.mockClear();
});

describe("The create user middleware", () => {
it("calls the local graphql endpoint with a createUserFromSession mutation and the request cookies", async () => {
const middlewareUnderTest = createUserMiddleware(
"somehost.example.com",
123987
);

await middlewareUnderTest(
{ headers: { cookie: "testsessioncookie" } } as any,
null,
jest.fn()
);

expect(global.fetch).toHaveBeenCalledWith(
"http://somehost.example.com:123987/graphql",
{
body: '{"query":"\\nmutation {\\n createUserFromSession(input: {}) {\\n __typename\\n }\\n}\\n","variables":null}',
headers: {
"Content-Type": "application/json",
cookie: "testsessioncookie",
},
method: "POST",
}
);
});

it("does not break the middleware chain", async () => {
const middlewareUnderTest = createUserMiddleware(
"somehost.example.com",
123987
);

const next = jest.fn();

await middlewareUnderTest({ headers: { cookie: "" } } as any, null, next);

expect(next).toHaveBeenCalled();
});

it("throws on network error", async () => {
global.fetch = jest.fn().mockImplementation(() => {
throw new Error("Network Error");
});

const middlewareUnderTest = createUserMiddleware("a", 1);

await expect(async () => {
await middlewareUnderTest(
{ headers: { cookie: "acookie" } } as any,
null,
jest.fn()
);
}).rejects.toThrow("Network Error");
});

it("throws on http error", async () => {
fetch.mockImplementation(() =>
Promise.resolve({
ok: false,
status: 599,
statusText: "Bad Test Server Error",
})
);

const middlewareUnderTest = createUserMiddleware("a", 1);

await expect(async () => {
await middlewareUnderTest(
{ headers: { cookie: "acookie" } } as any,
null,
jest.fn()
);
}).rejects.toThrow(
"Failed to create user from session: Bad Test Server Error"
);
});
});
8 changes: 4 additions & 4 deletions app/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -573,10 +573,10 @@
"@babel/helper-validator-identifier" "^7.14.9"
to-fast-properties "^2.0.0"

"@bcgov-cas/sso-express@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@bcgov-cas/sso-express/-/sso-express-2.0.1.tgz#18f87de06ca78d54748b144bb1740534b9680041"
integrity sha512-EdiFDKo+1pgmHwgk9NRUgWrhj/yZ9Ng12NIRyiT3jmkZnUuAO+DfUk/BeiY/yGGCcf0p2J4tN2AorhwBwXMZRw==
"@bcgov-cas/sso-express@^2.0.2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@bcgov-cas/sso-express/-/sso-express-2.0.2.tgz#20c87cca3fd2107c4a56862f1178744a4109c4fe"
integrity sha512-MjxxLdqAHxpkYtSYNOara4fHJUR0VO3VpeF6izwGBDwIHlmsEDttNn29qtkGG7cUrFdTepl6gd+mZicVpl+LaA==

"@bcgov-cas/sso-react@^1.0.0":
version "1.0.0"
Expand Down

0 comments on commit 6512517

Please sign in to comment.