Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to pass down the header to bot #1848

Merged
merged 14 commits into from
Jan 21, 2024
Prev Previous commit
Next Next commit
Added integration test for discord remove group
  • Loading branch information
Shubham Sharma committed Jan 16, 2024
commit 4151cf07ffff33f12ef1825fff06d7601bc0c861
41 changes: 30 additions & 11 deletions test/integration/discordactions.test.js
skv93-coder marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ chai.use(chaiHttp);
const { userStatusDataForOooState } = require("../fixtures/userStatus/userStatus");
const { generateCronJobToken } = require("../utils/generateBotToken");
const { CRON_JOB_HANDLER } = require("../../constants/bot");
const { genrateCloudFlareHeaders } = require("../../utils/discord-actions");

// describe("reason", function () {});
describe("Discord actions", function () {
Expand Down Expand Up @@ -239,27 +238,27 @@ describe("Discord actions", function () {
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("Role added successfully!");
});
it("should allow role to be added if reson passed", async function () {
it("should create reson and pass it down to the bot", async function () {
satyam73 marked this conversation as resolved.
Show resolved Hide resolved
fetchStub.returns(
Promise.resolve({
status: 200,
json: () => Promise.resolve({}),
})
);
const DISCORD_BASE_URL = config.get("services.discordBot.baseUrl");

await chai
const body = { roleid, userid: userData[0].discordId };
satyam73 marked this conversation as resolved.
Show resolved Hide resolved
const res = await chai
.request(app)
.post("/discord-actions/roles")
.set("cookie", `${cookieName}=${jwt}`)
.send({ roleid, userid: userData[0].discordId });
.send(body);

const headers = genrateCloudFlareHeaders({ id: userId, userName: "ankur" });
sinon.assert.calledWith(fetchStub, `${DISCORD_BASE_URL}/roles/add`, {
method: "PUT",
body: JSON.stringify({ roleid, userid: userData[0].discordId }),
headers,
});
expect(res).to.have.status(201);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("Role added successfully!");
expect(fetchStub.getCall(0).args[1].headers["X-Audit-Log-Reason"]).to.equal(
`Action initiator's username=>ankur and id=${userId}`
);
});
it("should not allow unknown role to be added to user", async function () {
const res = await chai
Expand Down Expand Up @@ -328,6 +327,26 @@ describe("Discord actions", function () {
});
});

it("should create reson and pass it down to the bot", async function () {
skv93-coder marked this conversation as resolved.
Show resolved Hide resolved
fetchStub.returns(
Promise.resolve({
status: 200,
json: () => Promise.resolve({ roleId: "1234", wasSuccess: true }),
})
);
const res = await chai
.request(app)
.delete("/discord-actions/roles")
.set("cookie", `${cookieName}=${jwt}`)
.send({ roleid, userid: userData[0].discordId });

expect(res).to.have.status(200);
expect(res.body).to.be.an("object");
expect(res.body.message).to.equal("Role deleted successfully");
expect(fetchStub.getCall(0).args[1].headers["X-Audit-Log-Reason"]).to.equal(
`Action initiator's username=>ankur and id=${userId}`
);
});
it("should not allow unknown role to be deleted from user", async function () {
const res = await chai
.request(app)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/utils/genrateCloudFlareHeaders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("genrateCloudFlareHeaders", function () {
expect(data.Authorization).to.include("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9");
});
it("generates headers with prop Content-Type and Authorization and X-Audit-Log-Reason when id and userName is passed", function () {
const data = genrateCloudFlareHeaders({ id: "id", userName: "userName" });
const data = genrateCloudFlareHeaders({ id: "id", username: "userName" });
expect(data["Content-Type"]).to.be.eq("application/json");
expect(data.Authorization).to.include("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9");
expect(data["X-Audit-Log-Reason"]).to.be.eq("Action initiator's username=>userName and id=id");
Expand Down
6 changes: 3 additions & 3 deletions utils/discord-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const generateAuthTokenForCloudflare = () => {
});
return authToken;
};
const genrateCloudFlareHeaders = ({ userName, id } = {}) => {
const genrateCloudFlareHeaders = ({ username, id } = {}) => {
const authToken = generateAuthTokenForCloudflare();
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
};
if (userName && id) {
headers["X-Audit-Log-Reason"] = `Action initiator's username=>${userName} and id=${id}`;
if (username && id) {
headers["X-Audit-Log-Reason"] = `Action initiator's username=>${username} and id=${id}`;
}
return headers;
};
Expand Down
Loading