|
| 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