Skip to content

Commit f497bdd

Browse files
committed
added test for fetch call to github api
1 parent 4090f3c commit f497bdd

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

controllers/auth.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const googleAuthCallback = (req, res, next) => {
5656

5757
if (userDataFromDB.userExists) {
5858
if (userDataFromDB.user.roles.developer === true) {
59-
// console.log("hi");
59+
// Waiting for sameer-supe to update what he requires on frontend.
6060
return res.boom.unauthorized("User is not allowed to login via Google");
6161
}
6262
}
@@ -164,6 +164,7 @@ const githubAuthCallback = (req, res, next) => {
164164
// console.log(userData);
165165

166166
if (userData.email === null) {
167+
// console.log("old", userData);
167168
const res = await fetch("https://api.github.com/user/emails", {
168169
headers: {
169170
Authorization: `token ${accessToken}`,
@@ -177,10 +178,9 @@ const githubAuthCallback = (req, res, next) => {
177178
// Get the first primary email, if it exists
178179
if (primaryEmails.length > 0) {
179180
userData.email = primaryEmails[0].email;
180-
} else {
181-
userData.email = null;
182-
// console.log("userData.email", userData.email);
181+
// console.log("userData.email setting up", userData.email);
183182
}
183+
// console.log(userData);
184184
}
185185

186186
const { userId, incompleteUserDetails, role } = await users.addOrUpdate(userData);
@@ -213,6 +213,7 @@ const githubAuthCallback = (req, res, next) => {
213213
newUrl.searchParams.set("token", token);
214214
authRedirectionUrl = newUrl.toString();
215215
}
216+
// console.log(userData);
216217
return res.redirect(authRedirectionUrl);
217218
})(req, res, next);
218219
} catch (err) {

test/integration/auth.test.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,43 @@ describe("auth", function () {
257257
expect(res.headers["set-cookie"][1]).to.include("SameSite=Lax");
258258
});
259259

260+
it("should redirect to the correct URL and update user email when GitHub API returns primary email", async function () {
261+
// Define a fake GitHub API response for user emails (primary email)
262+
const rdsUrl = new URL(config.get("services.rdsUi.baseUrl")).href;
263+
const fakeEmails = [
264+
{ primary: true, email: "[email protected]" },
265+
{ primary: false, email: "[email protected]" },
266+
];
267+
268+
// Stub fetch to resolve with the fake email response
269+
const fetchStub = sinon.stub(global, "fetch").resolves(new Response(JSON.stringify(fakeEmails)));
270+
271+
// Stub passport.authenticate to simulate a successful authentication
272+
sinon.stub(passport, "authenticate").callsFake((strategy, options, callback) => {
273+
callback(null, "accessToken", {
274+
username: "github-user",
275+
displayName: "GitHub User",
276+
_json: { email: null, created_at: "2022-01-01" },
277+
id: 12345,
278+
});
279+
return (req, res, next) => {};
280+
});
281+
282+
const res = await chai
283+
.request(app)
284+
.get(`/auth/github/callback`)
285+
.query({ code: "codeReturnedByGithub", state: rdsUrl })
286+
.redirects(0);
287+
expect(res).to.have.status(302);
288+
289+
// Verify that the fetch function was called with the correct GitHub API URL
290+
const fetchArgs = fetchStub.getCall(0).args;
291+
expect(fetchArgs[0]).to.equal("https://api.github.com/user/emails");
292+
expect(fetchArgs[1].headers.Authorization).to.equal("token accessToken"); // Ensure the token is passed correctly
293+
// Check if the user data was updated with the primary email returned by GitHub API
294+
// expect(userData.email).to.equal('[email protected]'); // Make sure the email was updated from the API response
295+
});
296+
260297
it("should return google call back URL", async function () {
261298
const googleOauthURL = generateGoogleAuthRedirectUrl({});
262299
const res = await chai.request(app).get("/auth/google/login").redirects(0);
@@ -408,11 +445,7 @@ describe("auth", function () {
408445

409446
expect(res).to.have.status(401);
410447
expect(res.body).to.be.an("object");
411-
expect(res.body).to.eql({
412-
statusCode: 401,
413-
error: "Unauthorized",
414-
message: "User cannot be authenticated",
415-
});
448+
expect(res.body.message).to.equal("User cannot be authenticated");
416449

417450
return done();
418451
});

0 commit comments

Comments
 (0)