Skip to content

Commit 4ade0b2

Browse files
authored
FEATURE: add timestamp to user model collection (#1346)
* FEATURE: add timestamp to user model * TEST: add test for the changes in user model collection * TEST(FIX): fix test and add new data for test * FIX(DUMMY DATA): add new dummy data for test * TEST(FIX): fix fixture data
1 parent b4f4889 commit 4ade0b2

File tree

8 files changed

+70
-3
lines changed

8 files changed

+70
-3
lines changed

controllers/auth.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const githubAuthCallback = (req, res, next) => {
5353
userData = {
5454
github_id: user.username,
5555
github_display_name: user.displayName,
56+
// github_account_created_at: user.created_at,
57+
created_at: Date.now(),
58+
updated_at: Date.now(),
5659
};
5760

5861
const { userId, incompleteUserDetails } = await users.addOrUpdate(userData);

models/users.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const addOrUpdate = async (userData, userId = null) => {
3838
await userModel.doc(userId).set({
3939
...user.data(),
4040
...userData,
41+
updated_at: Date.now(),
4142
});
4243
}
4344

@@ -53,6 +54,7 @@ const addOrUpdate = async (userData, userId = null) => {
5354
isNewUser: false,
5455
userId: user.docs[0].id,
5556
incompleteUserDetails: user.docs[0].data().incompleteUserDetails,
57+
updated_at: Date.now(),
5658
};
5759
}
5860

@@ -65,7 +67,7 @@ const addOrUpdate = async (userData, userId = null) => {
6567
userData.roles = { archived: false, in_discord: false };
6668
userData.incompleteUserDetails = true;
6769
const userInfo = await userModel.add(userData);
68-
return { isNewUser: true, userId: userInfo.id, incompleteUserDetails: true };
70+
return { isNewUser: true, userId: userInfo.id, incompleteUserDetails: true, updated_at: Date.now() };
6971
} catch (err) {
7072
logger.error("Error in adding or updating user", err);
7173
throw err;

services/discordService.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const setInDiscordFalseScript = async () => {
3434
...user.roles,
3535
in_discord: false,
3636
},
37+
updated_at: Date.now(),
3738
};
3839
updateUsersPromises.push(userModel.doc(id).update(userData));
3940
});

test/fixtures/auth/githubUserInfo.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,39 @@ module.exports = () => {
6767
},
6868
},
6969
},
70+
{
71+
login: "sahsisunny",
72+
id: 70854507,
73+
node_id: "MDQ6VXNlcjcwODU0NTA3",
74+
avatar_url: "https://avatars.githubusercontent.com/u/70854507?v=4",
75+
gravatar_id: "",
76+
url: "https://api.github.com/users/sahsisunny",
77+
html_url: "https://github.com/sahsisunny",
78+
followers_url: "https://api.github.com/users/sahsisunny/followers",
79+
following_url: "https://api.github.com/users/sahsisunny/following{/other_user}",
80+
gists_url: "https://api.github.com/users/sahsisunny/gists{/gist_id}",
81+
starred_url: "https://api.github.com/users/sahsisunny/starred{/owner}{/repo}",
82+
subscriptions_url: "https://api.github.com/users/sahsisunny/subscriptions",
83+
organizations_url: "https://api.github.com/users/sahsisunny/orgs",
84+
repos_url: "https://api.github.com/users/sahsisunny/repos",
85+
events_url: "https://api.github.com/users/sahsisunny/events{/privacy}",
86+
received_events_url: "https://api.github.com/users/sahsisunny/received_events",
87+
type: "User",
88+
site_admin: false,
89+
name: "Sunny Sahsi",
90+
company: "@Real-Dev-Squad ",
91+
blog: "https://sahsisunny.netlify.app/",
92+
location: "India",
93+
email: null,
94+
hireable: true,
95+
bio: null,
96+
twitter_username: "sahsisunny",
97+
public_repos: 38,
98+
public_gists: 1,
99+
followers: 11,
100+
following: 4,
101+
created_at: "2020-09-06T16:21:38Z",
102+
updated_at: "2023-07-26T09:29:37Z",
103+
},
70104
];
71105
};

test/fixtures/user/user.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,5 +329,19 @@ module.exports = () => {
329329
twitter_id: "ramsingh123",
330330
linkedin_id: "ramsingh123",
331331
},
332+
{
333+
username: "sahsisunny",
334+
first_name: "sunny",
335+
last_name: "sahsi",
336+
github_id: githubUserInfo[1].login,
337+
github_display_name: githubUserInfo[1].name,
338+
roles: {
339+
member: true,
340+
in_discord: true,
341+
},
342+
incompleteUserDetails: false,
343+
updated_at: Date.now(),
344+
created_at: Date.now(),
345+
},
332346
];
333347
};

test/integration/auth.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("auth", function () {
4040
expect(res.headers.location).to.equal(githubOauthURL);
4141
});
4242

43-
it("should redirect the user to new sign up flow if they are have incomplte user details true", async function () {
43+
it("should redirect the user to new sign up flow if they are have incomplete user details true", async function () {
4444
const redirectURL = "https://my.realdevsquad.com/new-signup";
4545
sinon.stub(passport, "authenticate").callsFake((strategy, options, callback) => {
4646
callback(null, "accessToken", githubUserInfo[0]);
@@ -55,6 +55,7 @@ describe("auth", function () {
5555
expect(res).to.have.status(302);
5656
expect(res.headers.location).to.equal(redirectURL);
5757
});
58+
5859
// same data should be return from github and same data should be added there
5960
it("should redirect the request to the goto page on successful login, if user has incomplete user details false", async function () {
6061
await addUserToDBForTest(userData[0]);
@@ -90,7 +91,7 @@ describe("auth", function () {
9091
expect(res.headers.location).to.equal(rdsUrl);
9192
});
9293

93-
it("should redirect the realdevsquad.com if non RDS URL provided, any url that is other than *.realdevsqud.com is invalid", async function () {
94+
it("should redirect the realdevsquad.com if non RDS URL provided, any url that is other than *.realdevsquad.com is invalid", async function () {
9495
await addUserToDBForTest(userData[0]);
9596
const invalidRedirectUrl = new URL("https://google.com").href;
9697
const rdsUiUrl = new URL(config.get("services.rdsUi.baseUrl")).href;

test/unit/models/users.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,18 @@ describe("users", function () {
9292
expect(user.last_name).to.equal(userData.last_name);
9393
expect(userExists).to.equal(true);
9494
});
95+
96+
it("It should have created_At and updated_At fields", async function () {
97+
const userData = userDataArray[14];
98+
await users.addOrUpdate(userData);
99+
const githubUsername = "sahsisunny";
100+
const { user, userExists } = await users.fetchUser({ githubUsername });
101+
expect(user).to.haveOwnProperty("created_at");
102+
expect(user).to.haveOwnProperty("updated_at");
103+
expect(userExists).to.equal(true);
104+
});
95105
});
106+
96107
describe("user image verification", function () {
97108
let userId, discordId, profileImageUrl, discordImageUrl;
98109
beforeEach(async function () {

test/unit/services/discordService.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe("Discord services", function () {
2626
const updatedUsers = await fetchAllUsers();
2727
updatedUsers.forEach((user) => {
2828
expect(user.roles.in_discord).to.be.equal(false);
29+
expect(user.updated_at).to.be.a("number");
2930
});
3031
});
3132
});

0 commit comments

Comments
 (0)