Skip to content

Commit 46623d1

Browse files
ivinayakgpallabezskv93-coderShubham Sharma
authored
feat: goal site auth api (initial) (#1445)
* feat: goal site auth api (initial) * refact: changes done to the goals token api flow * refact: minor changes, goals token controller * Added test case for fixing nock test fail for get token API for goal… (#1602) * Added test case for fixing nock test fail for get token API for goal site * Modified how we pass argument in goal service --------- Co-authored-by: Shubham Sharma <[email protected]> --------- Co-authored-by: pallabez <[email protected]> Co-authored-by: Shubham Sharma <[email protected]> Co-authored-by: Shubham Sharma <[email protected]>
1 parent e765af7 commit 46623d1

File tree

10 files changed

+173
-0
lines changed

10 files changed

+173
-0
lines changed

config/default.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ module.exports = {
4747
routes: {
4848
authRedirection: "/goto",
4949
},
50+
goalAPI: {
51+
baseUrl: "",
52+
secretKey: "",
53+
cookieName: "",
54+
},
5055
},
5156
discordBot: {
5257
baseUrl: "<DISCORD_BOT_BASE_URL>",

config/development.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ module.exports = {
2222
authRedirection: "/healthcheck",
2323
},
2424
},
25+
26+
goalAPI: {
27+
baseUrl: "",
28+
secretKey: "",
29+
cookieName: "",
30+
},
2531
},
2632

2733
userToken: {

config/staging.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ module.exports = {
1717
rdsApi: {
1818
baseUrl: "https://staging-api.realdevsquad.com",
1919
},
20+
goalAPI: {
21+
baseUrl: "",
22+
secretKey: "",
23+
cookieName: "",
24+
},
2025
},
2126

2227
integrations: {

config/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ module.exports = {
3535
discordBot: {
3636
baseUrl: "DISCORD_BASE_URL",
3737
},
38+
goalAPI: {
39+
baseUrl: "https://goals-api.realdevsquad.com",
40+
secretKey: "123456789",
41+
cookieName: "goals_session",
42+
},
3843
},
3944

4045
cors: {

controllers/goals.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { SOMETHING_WENT_WRONG } = require("../constants/errorMessages");
2+
const goals = require("../services/goalService");
3+
4+
const getGoalSiteToken = async (req, res) => {
5+
try {
6+
const { roles, id: userId } = req.userData;
7+
8+
const goalApiResponse = await goals.getOrCreateGoalUser({ userId, roles });
9+
10+
if (goalApiResponse.status === 201) {
11+
let goalApiData = await goalApiResponse.json();
12+
goalApiData = goalApiData.data;
13+
const userData = goalApiData.attributes;
14+
return res.status(200).json({ message: "success", user: { ...userData, id: goalApiData.id } });
15+
}
16+
return res.status(goalApiResponse.status).json({ message: "error" });
17+
} catch {
18+
return res.boom.badImplementation(SOMETHING_WENT_WRONG);
19+
}
20+
};
21+
22+
module.exports = {
23+
getGoalSiteToken,
24+
};

routes/goals.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const express = require("express");
2+
const router = express.Router();
3+
const authenticate = require("../middlewares/authenticate");
4+
const goals = require("../controllers/goals");
5+
6+
router.get("/token", authenticate, goals.getGoalSiteToken);
7+
8+
module.exports = router;

routes/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ app.use("/issues", require("./issues.js"));
3131
app.use("/progresses", require("./progresses.js"));
3232
app.use("/monitor", require("./monitor.js"));
3333
app.use("/staging", require("./staging.js"));
34+
app.use("/goals", require("./goals.js"));
3435
module.exports = app;

services/goalService.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const config = require("config");
2+
3+
const getOrCreateGoalUser = async ({ userId, roles }) => {
4+
const body = JSON.stringify({
5+
data: {
6+
type: "User",
7+
attributes: {
8+
rds_id: userId,
9+
roles: roles,
10+
},
11+
},
12+
});
13+
const goalSiteConfig = config.services.goalAPI;
14+
return fetch(`${goalSiteConfig.baseUrl}/api/v1/user/`, {
15+
method: "POST",
16+
body,
17+
headers: { "Content-Type": "application/vnd.api+json", "Rest-Key": goalSiteConfig.secretKey },
18+
});
19+
};
20+
module.exports = { getOrCreateGoalUser };

test/fixtures/goals/Token.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const GET_OR_CREATE_GOAL_USER = {
2+
status: 201,
3+
json: () =>
4+
Promise.resolve({
5+
id: "123456",
6+
data: { attributes: { rds_id: "134556", token: "WHhHhWHu9ijHjkKhdbvFFhbnhCj" } },
7+
}),
8+
};
9+
module.exports = { GET_OR_CREATE_GOAL_USER };

test/integration/goals.test.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const chai = require("chai");
2+
const sinon = require("sinon");
3+
const { expect } = chai;
4+
const chaiHttp = require("chai-http");
5+
const nock = require("nock");
6+
7+
const app = require("../../server");
8+
const authService = require("../../services/authService");
9+
const addUser = require("../utils/addUser");
10+
const config = require("config");
11+
const cookieName = config.get("userToken.cookieName");
12+
const userData = require("../fixtures/user/user")();
13+
const cleanDb = require("../utils/cleanDb");
14+
const goals = require("../../services/goalService");
15+
const { GET_OR_CREATE_GOAL_USER } = require("../fixtures/goals/Token");
16+
17+
chai.use(chaiHttp);
18+
19+
const user = userData[6];
20+
let jwt;
21+
let goalSiteConfig;
22+
23+
describe("Goals Site", function () {
24+
before(async function () {
25+
const userId = await addUser(user);
26+
user.id = userId;
27+
const goalsBackendUserId = "test_1";
28+
jwt = authService.generateAuthToken({ userId: userId });
29+
goalSiteConfig = config.services.goalAPI;
30+
31+
nock(goalSiteConfig.baseUrl)
32+
.post("/api/v1/user/")
33+
.reply(function (uri, requestBody) {
34+
const headers = this.req.headers;
35+
36+
if (headers["Rest-Key"] === goalSiteConfig.secretKey) {
37+
return [
38+
201,
39+
{
40+
message: "success",
41+
user: {
42+
rds_id: userId,
43+
token: {
44+
exp: 1694625316,
45+
access: "access-token-goal-site-backend",
46+
},
47+
created_at: "2023-09-12T17:07:28.242030Z",
48+
modified_at: "2023-09-12T17:15:16.383069Z",
49+
roles: {
50+
restricted: false,
51+
},
52+
id: goalsBackendUserId,
53+
},
54+
},
55+
];
56+
}
57+
return [400, { data: "something went wrong" }];
58+
});
59+
});
60+
61+
after(async function () {
62+
await cleanDb();
63+
});
64+
65+
afterEach(async function () {
66+
sinon.restore();
67+
});
68+
69+
describe("GET /token - set goal site token as cookie", function () {
70+
it("Should set the cookie successfully on the request and return success", function (done) {
71+
sinon.stub(goals, "getOrCreateGoalUser").resolves(GET_OR_CREATE_GOAL_USER);
72+
73+
chai
74+
.request(app)
75+
.get("/goals/token/")
76+
.set("cookie", `${cookieName}=${jwt}`)
77+
.end((err, res) => {
78+
if (err) {
79+
return done(err);
80+
}
81+
expect(res).to.have.status(200);
82+
expect(res.body).to.have.property("user");
83+
const userResponseData = res.body.user;
84+
expect(userResponseData).to.have.property("rds_id");
85+
expect(userResponseData).to.have.property("token");
86+
return done();
87+
});
88+
});
89+
});
90+
});

0 commit comments

Comments
 (0)