Skip to content

Commit 63c7396

Browse files
Updated: archived users data structure and test cases
1 parent af3b069 commit 63c7396

File tree

4 files changed

+64
-41
lines changed

4 files changed

+64
-41
lines changed

controllers/members.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,10 @@ const archiveMembers = async (req, res) => {
8585
const { username } = req.params;
8686
const user = await dataAccess.retrieveUsers({ username });
8787
const superUserId = req.userData.id;
88-
const body = req.body;
89-
const meta = {
90-
userId: user?.user?.id,
91-
superUserId: superUserId,
92-
username: username,
93-
};
94-
const isReasonNullOrUndefined = !body?.reason;
95-
const isReasonEmptyOrWhitespace = /^\s*$/.test(body?.reason);
88+
const { reason } = req.body;
89+
const roles = req?.userData?.roles;
90+
const isReasonNullOrUndefined = !reason;
91+
const isReasonEmptyOrWhitespace = /^\s*$/.test(reason);
9692
if (isReasonNullOrUndefined || isReasonEmptyOrWhitespace) {
9793
return res.boom.badRequest("Reason is required");
9894
}
@@ -101,7 +97,19 @@ const archiveMembers = async (req, res) => {
10197
if (successObject.isArchived) {
10298
return res.boom.badRequest("User is already archived");
10399
}
104-
addLog("archive-details", meta, { body: body?.reason });
100+
const body = {
101+
reason: reason,
102+
archived_user: {
103+
user_id: user.user.id,
104+
username: user.user.username,
105+
},
106+
archived_by: {
107+
user_id: superUserId,
108+
roles: roles,
109+
},
110+
};
111+
112+
addLog("archived-details", {}, body);
105113
return res.status(204).send();
106114
}
107115
return res.boom.notFound("User doesn't exist");

models/logs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const fetchLogs = async (query, param) => {
5353
if (userId) {
5454
const logsSnapshot = await logsModel
5555
.where("type", "==", param)
56-
.where("meta.userId", "==", userId)
56+
.where("body.archived_user.user_id", "==", userId)
5757
.orderBy("timestamp", "desc")
5858
.get();
5959
const logs = [];

test/fixtures/logs/archievedUsers.js

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
11
const archivedUserDetailsModal = [
22
{
3-
type: "archive-details",
4-
meta: {
5-
userId: "TEST_USER_ID_1",
6-
superUserId: "TEST_SUPER_USER_ID",
7-
username: "TEST_USERNAME_1",
8-
},
3+
type: "archived-details",
4+
meta: {},
95
body: {
10-
reason: "TEST_REASON_1",
6+
reason: "test reason",
7+
archived_user: { user_id: "R5kljdsleH4Gr2t7tvr0Z", username: "testUser1" },
8+
archived_by: {
9+
user_id: "ReMyuklislajwooncVL",
10+
roles: {
11+
in_discord: true,
12+
super_user: false,
13+
member: true,
14+
archived: false,
15+
},
16+
},
1117
},
1218
timestamp: {
1319
_seconds: 1657193216,
1420
_nanoseconds: 912000000,
1521
},
1622
},
1723
{
18-
type: "archive-details",
19-
meta: {
20-
userId: "TEST_USER_ID_2",
21-
superUserId: "TEST_SUPER_USER_ID",
22-
username: "TEST_USERNAME_2",
23-
},
24+
type: "archived-details",
25+
meta: {},
2426
body: {
25-
reason: "TEST_REASON_2",
27+
reason: "test reason",
28+
archived_user: { user_id: "R5kljdsleH4Gr2t7tvr0Z", username: "testUser1" },
29+
archived_by: {
30+
user_id: "ReMyuklislajwooncVL",
31+
roles: {
32+
in_discord: true,
33+
super_user: false,
34+
member: true,
35+
archived: false,
36+
},
37+
},
2638
},
2739
timestamp: {
2840
_seconds: 1657193216,
2941
_nanoseconds: 912000000,
3042
},
3143
},
3244
{
33-
type: "archive-details",
34-
meta: {
35-
userId: "TEST_USER_ID_1",
36-
superUserId: "TEST_SUPER_USER_ID",
37-
username: "TEST_USERNAME_1",
38-
},
45+
type: "archived-details",
46+
meta: {},
3947
body: {
40-
reason: "TEST_REASON_1",
48+
reason: "test reason",
49+
archived_user: { user_id: "Efskee4Gr2t7tvr0Z", username: "testUser2" },
50+
archived_by: {
51+
user_id: "ReMyuklislajwooncVL",
52+
roles: {
53+
in_discord: true,
54+
super_user: false,
55+
member: true,
56+
archived: false,
57+
},
58+
},
4159
},
4260
timestamp: {
4361
_seconds: 1657193216,

test/unit/models/logs.test.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("Logs", function () {
4949
});
5050
});
5151

52-
describe("GET /logs/archive-details", function () {
52+
describe("GET /logs/archived-details", function () {
5353
let addLogsStub;
5454
let jwt;
5555
beforeEach(async function () {
@@ -61,15 +61,13 @@ describe("Logs", function () {
6161
Sinon.restore();
6262
});
6363

64-
it("Should return an object with status 500 and an error message", async function () {
64+
it("Should return an Internal server error message", async function () {
6565
addLogsStub = Sinon.stub(logsQuery, "fetchLogs");
6666
addLogsStub.throws(new Error(INTERNAL_SERVER_ERROR));
6767

6868
addUser(userToBeMadeMember).then(() => {
69-
const res = chai.request(app).get("/logs/archive-details").set("cookie", `${cookieName}=${jwt}`).send();
69+
const res = chai.request(app).get("/logs/archived-details").set("cookie", `${cookieName}=${jwt}`).send();
7070

71-
// expect(res).to.have.status(500);
72-
// expect(res.body).to.have.property("message").that.is.a("string");
7371
expect(res.body.message).to.equal(INTERNAL_SERVER_ERROR);
7472
});
7573
});
@@ -92,13 +90,13 @@ describe("Logs", function () {
9290
expect(data[0]).to.have.property("timestamp").that.is.an("object");
9391
expect(data[0].timestamp).to.have.property("_seconds").that.is.a("number");
9492
expect(data[0].timestamp).to.have.property("_nanoseconds").that.is.a("number");
95-
expect(data[0].meta).to.have.property("username").that.is.a("string");
93+
expect(data[0].body.archived_user).to.have.property("username").that.is.a("string");
9694
expect(data[0].body).to.have.property("reason").that.is.a("string");
9795
});
98-
it("Should fetch all archived logs for given username", async function () {
96+
it("Should fetch all archived logs for given user_id", async function () {
9997
const { type, meta, body } = logsData.archivedUserDetailsModal[0];
10098
const query = {
101-
userId: meta.userId,
99+
userId: body.archived_user.user_id,
102100
};
103101
await logsQuery.addLog(type, meta, body);
104102
const data = await logsQuery.fetchLogs(query, type);
@@ -107,7 +105,6 @@ describe("Logs", function () {
107105
expect(data[0]).to.have.property("timestamp").that.is.an("object");
108106
expect(data[0].timestamp).to.have.property("_seconds").that.is.a("number");
109107
expect(data[0].timestamp).to.have.property("_nanoseconds").that.is.a("number");
110-
expect(data[0].meta).to.have.property("username").that.is.a("string");
111108
expect(data[0].body).to.have.property("reason").that.is.a("string");
112109
});
113110
it("Should throw response status 404, if username is incorrect in the query", async function () {
@@ -117,7 +114,7 @@ describe("Logs", function () {
117114
};
118115
await logsQuery.addLog(type, meta, body);
119116
const data = await logsQuery.fetchLogs(query, type);
120-
const response = await chai.request(app).get(`/logs/${type}/${meta.username}`);
117+
const response = await chai.request(app).get(`/logs/${type}/${query}`);
121118

122119
expect(data).to.be.an("array").with.lengthOf(0);
123120
expect(response).to.have.status(404);

0 commit comments

Comments
 (0)