-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from bcgov/feat/create-cif-user-upon-login
Feat: create cif user upon login
- Loading branch information
Showing
6 changed files
with
139 additions
and
19 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters