Skip to content

Commit 10c6244

Browse files
committed
feat: add tests for authenticate routes
1 parent 13d3886 commit 10c6244

6 files changed

Lines changed: 161 additions & 20 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export const testFlags = {
2+
disableFacebook: false,
3+
disableGoogle: false,
4+
disableGithub: false,
5+
disableTwitter: false,
6+
};
7+
8+
// Test Data
9+
const testSettings = () => {
10+
return {
11+
facebookAuthProvider: {
12+
appID: testFlags.disableFacebook
13+
? null
14+
: process.env.FACEBOOK_APP_ID || "629016041331396",
15+
appSecret: testFlags.disableFacebook
16+
? null
17+
: process.env.FACEBOOK_APP_SECRET || "0ea9fb7b258f79eb77062ef8fe695860",
18+
},
19+
googleAuthProvider: {
20+
clientID: testFlags.disableGoogle
21+
? null
22+
: process.env.GOOGLE_CLIENT_ID ||
23+
"890926583937-5rv8t479let7iq2mofp3iumv4k9q83sc.apps.googleusercontent.com",
24+
clientSecret: testFlags.disableGoogle
25+
? null
26+
: process.env.GOOGLE_CLIENT_SECRET || "UcATfElogefVlYPHUGJydVef",
27+
},
28+
githubAuthProvider: {
29+
clientID: testFlags.disableGithub
30+
? null
31+
: process.env.GITHUB_CLIENT_ID || "457470f0d7bc6d16fe22",
32+
clientSecret: testFlags.disableGithub
33+
? null
34+
: process.env.GITHUB_CLIENT_SECRET ||
35+
"a1448a4e4bd6fdad7772e30d1c1e5fedfcb9694a",
36+
},
37+
twitterAuthProvider: {
38+
consumerKey: testFlags.disableTwitter
39+
? null
40+
: process.env.TWITTER_CONSUMER_KEY || "xfZYJR3PWKjeWUavEIIjWFoAR",
41+
consumerSecret: testFlags.disableTwitter
42+
? null
43+
: process.env.TWITTER_CONSUMER_SECRET ||
44+
"iDwdmGAm7SYhfGLoISAvLl7io4X1Eg1WoOkWNwaDv5ShqMzuvz",
45+
},
46+
socialCallback: `${process.env.HOST}/social/authCallback`,
47+
};
48+
};
49+
50+
export default testSettings;

src/middleware/authProviderMW.js

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import CustomError from "../utils/customError";
2+
import testSettings from "./__test__/testSettings";
23

34
// Supported federated authentication providers.
45
export const FACEBOOK_PROVIDER = "Facebook";
@@ -21,26 +22,8 @@ const authProvider = (providerType) => {
2122
let provider;
2223
let providerEnabled = false;
2324

24-
// Test Data ===================================================
25-
req.settings = {};
26-
req.settings.facebookAuthProvider = {
27-
appID: process.env.FACEBOOK_APP_ID,
28-
appSecret: process.env.FACEBOOK_APP_SECRET,
29-
};
30-
req.settings.googleAuthProvider = {
31-
clientID: process.env.GOOGLE_CLIENT_ID,
32-
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
33-
};
34-
req.settings.githubAuthProvider = {
35-
clientID: process.env.GITHUB_CLIENT_ID,
36-
clientSecret: process.env.GITHUB_CLIENT_SECRET,
37-
};
38-
req.settings.twitterAuthProvider = {
39-
consumerKey: process.env.TWITTER_CONSUMER_KEY,
40-
consumerSecret: process.env.TWITTER_CONSUMER_SECRET,
41-
};
42-
req.settings.socialCallback = `${process.env.HOST}/social/authCallback`;
43-
// End Test Data ===============================================
25+
// Test Data
26+
req.settings = testSettings();
4427

4528
switch (providerType) {
4629
case FACEBOOK_PROVIDER:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import app from "../../..";
2+
import request from "supertest";
3+
import { testFlags } from "../../../middleware/__test__/testSettings";
4+
5+
describe("Facebook authentication", () => {
6+
beforeEach(() => {
7+
testFlags.disableFacebook = false;
8+
testFlags.disableGoogle = false;
9+
testFlags.disableGithub = false;
10+
testFlags.disableTwitter = false;
11+
});
12+
13+
it("should return a successful response", async () => {
14+
const res = await request(app).get("/social/facebook");
15+
expect(res.status).toBe(200);
16+
expect(res.body.data.authenticationUrl).toMatch(
17+
"https://www.facebook.com/v4.0/dialog/oauth"
18+
);
19+
});
20+
21+
it("should return a 401 response", async () => {
22+
testFlags.disableFacebook = true;
23+
24+
const res = await request(app).get("/social/facebook");
25+
expect(res.status).toBe(401);
26+
});
27+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import app from "../../..";
2+
import request from "supertest";
3+
import { testFlags } from "../../../middleware/__test__/testSettings";
4+
5+
describe("GitHub authentication", () => {
6+
beforeEach(() => {
7+
testFlags.disableFacebook = false;
8+
testFlags.disableGoogle = false;
9+
testFlags.disableGithub = false;
10+
testFlags.disableTwitter = false;
11+
});
12+
13+
it("should return a successful response", async () => {
14+
const res = await request(app).get("/social/github");
15+
expect(res.status).toBe(200);
16+
expect(res.body.data.authenticationUrl).toMatch(
17+
"https://github.com/login/oauth/authorize"
18+
);
19+
});
20+
21+
it("should return a 401 response", async () => {
22+
testFlags.disableGithub = true;
23+
24+
const res = await request(app).get("/social/github");
25+
expect(res.status).toBe(401);
26+
});
27+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import app from "../../..";
2+
import request from "supertest";
3+
import { testFlags } from "../../../middleware/__test__/testSettings";
4+
5+
describe("Google authentication", () => {
6+
beforeEach(() => {
7+
testFlags.disableFacebook = false;
8+
testFlags.disableGoogle = false;
9+
testFlags.disableGithub = false;
10+
testFlags.disableTwitter = false;
11+
});
12+
13+
it("should return a successful response", async () => {
14+
const res = await request(app).get("/social/google");
15+
expect(res.status).toBe(200);
16+
expect(res.body.data.authenticationUrl).toMatch(
17+
"https://accounts.google.com/o/oauth2/v2/auth"
18+
);
19+
});
20+
21+
it("should return a 401 response", async () => {
22+
testFlags.disableGoogle = true;
23+
24+
const res = await request(app).get("/social/google");
25+
expect(res.status).toBe(401);
26+
});
27+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import app from "../../..";
2+
import request from "supertest";
3+
import { testFlags } from "../../../middleware/__test__/testSettings";
4+
5+
describe("Twitter authentication", () => {
6+
beforeEach(() => {
7+
testFlags.disableFacebook = false;
8+
testFlags.disableGoogle = false;
9+
testFlags.disableGithub = false;
10+
testFlags.disableTwitter = false;
11+
});
12+
13+
it("should return a successful response", async () => {
14+
const res = await request(app).get("/social/twitter");
15+
expect(res.status).toBe(200);
16+
expect(res.body.data.authenticationUrl).toMatch(
17+
"https://api.twitter.com/oauth/authenticate"
18+
);
19+
});
20+
21+
it("should return a 401 response", async () => {
22+
testFlags.disableTwitter = true;
23+
24+
const res = await request(app).get("/social/twitter");
25+
expect(res.status).toBe(401);
26+
});
27+
});

0 commit comments

Comments
 (0)