Skip to content

Commit 2ac202e

Browse files
RahulGoyal-techprakashchoudhary07Achintya-Chatterjee
authored
Tests for fetchProfileDiffUnobfuscated and filtering out id in addOrUpdate (#2256)
* Added test for id filterin in addOrUpdate * Added Tests for Unobfuscated details and filtered id * Updated test to match fixture instead of Regex * Added test for id filterin in addOrUpdate * Added Tests for Unobfuscated details and filtered id * Updated test to match fixture instead of Regex * Removed comments * Test Cases Added * Added nested profileDiff --------- Co-authored-by: Prakash Choudhary <[email protected]> Co-authored-by: Achintya Chatterjee <[email protected]>
1 parent 7409ecd commit 2ac202e

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const chai = require("chai");
2+
const { expect } = chai;
3+
const cleanDb = require("../../utils/cleanDb");
4+
const profileDiffsQuery = require("../../../models/profileDiffs");
5+
const getProfileDiffs = require("../../fixtures/profileDiffs/profileDiffs");
6+
7+
describe("profileDiffs", function () {
8+
afterEach(async function () {
9+
await cleanDb();
10+
});
11+
12+
describe("fetchProfileDiffUnobfuscated", function () {
13+
it("should successfully return profileDiffs", async function () {
14+
const profileDiffs = getProfileDiffs();
15+
const profileDiffId = await profileDiffsQuery.add(profileDiffs[1]);
16+
const diff = await profileDiffsQuery.fetchProfileDiffUnobfuscated(profileDiffId);
17+
expect(diff).haveOwnProperty("id");
18+
expect(diff.profileDiffExists).to.equal(true);
19+
});
20+
21+
it("should not return profileDiffs for invalid id", async function () {
22+
const profileDiffs = getProfileDiffs();
23+
await profileDiffsQuery.add(profileDiffs[0]);
24+
const profileDiffId = "invalid-id";
25+
const diff = await profileDiffsQuery.fetchProfileDiffUnobfuscated(profileDiffId);
26+
expect(diff.profileDiffExists).to.equal(false);
27+
});
28+
29+
it("should return profileDiffs with unobfuscated email and phone", async function () {
30+
const profileDiffs = getProfileDiffs();
31+
const profileDiffId = await profileDiffsQuery.add(profileDiffs[0]);
32+
const diff = await profileDiffsQuery.fetchProfileDiffUnobfuscated(profileDiffId);
33+
expect(diff.phone).to.equal(profileDiffs[0].phone);
34+
expect(diff.email).to.equal(profileDiffs[0].email);
35+
});
36+
37+
it("it should throw an error on undefined profileDiff Id", async function () {
38+
const profileDiffs = getProfileDiffs();
39+
await profileDiffsQuery.add(profileDiffs[0]);
40+
let error = "";
41+
try {
42+
await profileDiffsQuery.fetchProfileDiffUnobfuscated(undefined);
43+
} catch (err) {
44+
error = err;
45+
}
46+
Object.freeze(error);
47+
expect(error).to.be.an("Error");
48+
expect(error.message).to.be.equal(
49+
'Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.'
50+
);
51+
});
52+
53+
it("it should throw an error on null profileDiff Id", async function () {
54+
const profileDiffs = getProfileDiffs();
55+
await profileDiffsQuery.add(profileDiffs[0]);
56+
let error = "";
57+
try {
58+
await profileDiffsQuery.fetchProfileDiffUnobfuscated(null);
59+
} catch (err) {
60+
error = err;
61+
}
62+
Object.freeze(error);
63+
expect(error).to.be.an("Error");
64+
expect(error.message).to.be.equal(
65+
'Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.'
66+
);
67+
});
68+
69+
it("it should throw an error on passing profileDiff Id as empty string", async function () {
70+
const profileDiffs = getProfileDiffs();
71+
await profileDiffsQuery.add(profileDiffs[0]);
72+
let error = "";
73+
try {
74+
await profileDiffsQuery.fetchProfileDiffUnobfuscated("");
75+
} catch (err) {
76+
error = err;
77+
}
78+
Object.freeze(error);
79+
expect(error).to.be.an("Error");
80+
expect(error.message).to.be.equal(
81+
'Value for argument "documentPath" is not a valid resource path. Path must be a non-empty string.'
82+
);
83+
});
84+
85+
it("it should correctly fetch deeply nested profileDiff", async function () {
86+
const profileDiff = {
87+
level1: {
88+
level2: {
89+
level3: {
90+
level4: {
91+
level5: {
92+
level6: {
93+
level7: {
94+
level8: {
95+
level9: {
96+
level10: "nested-random-diff",
97+
},
98+
},
99+
},
100+
},
101+
},
102+
},
103+
},
104+
},
105+
},
106+
};
107+
const profileDiffId = await profileDiffsQuery.add(profileDiff);
108+
const diff = await profileDiffsQuery.fetchProfileDiffUnobfuscated(profileDiffId);
109+
expect(diff)
110+
.to.have.nested.property("level1.level2.level3.level4.level5.level6.level7.level8.level9.level10")
111+
.that.equals("nested-random-diff");
112+
});
113+
});
114+
});

test/unit/models/users.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,47 @@ describe("users", function () {
115115
const { user } = await users.fetchUser({ githubUsername });
116116
expect(user).to.haveOwnProperty("github_created_at");
117117
});
118+
119+
it("it should filter out the id field while updating profileDiff", async function () {
120+
const userData = userDataArray[0];
121+
const { userId } = await users.addOrUpdate(userData);
122+
const profileDiff = { id: "random-id", diff: "random-diff" };
123+
await users.addOrUpdate(profileDiff, userId);
124+
const data = (await userModel.doc(userId).get()).data();
125+
expect(data).to.haveOwnProperty("diff");
126+
expect(data.id).not.equal("random-id");
127+
});
128+
129+
it("it should update profileDiff even if it is deeply nested", async function () {
130+
const userData = userDataArray[0];
131+
const { userId } = await users.addOrUpdate(userData);
132+
const profileDiffs = {
133+
level1: {
134+
level2: {
135+
level3: {
136+
level4: {
137+
level5: {
138+
level6: {
139+
level7: {
140+
level8: {
141+
level9: {
142+
level10: "nested-random-diff",
143+
},
144+
},
145+
},
146+
},
147+
},
148+
},
149+
},
150+
},
151+
},
152+
};
153+
await users.addOrUpdate(profileDiffs, userId);
154+
const data = (await userModel.doc(userId).get()).data();
155+
expect(data)
156+
.to.have.nested.property("level1.level2.level3.level4.level5.level6.level7.level8.level9.level10")
157+
.that.equals("nested-random-diff");
158+
});
118159
});
119160

120161
describe("addOrUpdate-Dev Feature Flag", function () {

0 commit comments

Comments
 (0)