Skip to content

Commit 8b84e85

Browse files
committed
Added get route for fetching user device info
1 parent 632260a commit 8b84e85

File tree

7 files changed

+137
-2
lines changed

7 files changed

+137
-2
lines changed

constants/errorMessages.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module.exports = {
66
DATA_ADDED_SUCCESSFULLY: "User Device Info added successfully!",
77
USER_DATA_ALREADY_PRESENT: "The authentication document has already been created",
88
BAD_REQUEST: "BAD_REQUEST",
9+
INVALID_QUERY_PARAM: "Invalid Query Parameters Passed",
910
FILE_TOO_LARGE: (size) => `File too large, max accepted size is ${size} MB`,
1011
};

controllers/auth.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ const passport = require("passport");
22
const users = require("../models/users");
33
const QrCodeAuthModel = require("../models/qrCodeAuth");
44
const authService = require("../services/authService");
5-
const { SOMETHING_WENT_WRONG, DATA_ADDED_SUCCESSFULLY, BAD_REQUEST } = require("../constants/errorMessages");
5+
const {
6+
SOMETHING_WENT_WRONG,
7+
DATA_ADDED_SUCCESSFULLY,
8+
BAD_REQUEST,
9+
INVALID_QUERY_PARAM,
10+
} = require("../constants/errorMessages");
611

712
/**
813
* Fetches the user info from GitHub and authenticates User
@@ -122,9 +127,32 @@ const updateAuthStatus = async (req, res) => {
122127
}
123128
};
124129

130+
const fetchUserDeviceInfo = async (req, res) => {
131+
try {
132+
const queryParamArray = Object.keys(req.query);
133+
if (queryParamArray.length === 1 && queryParamArray[0] === "device_id") {
134+
const deviceId = req.query.device_id;
135+
const userDeviceInfoData = await QrCodeAuthModel.retrieveUserDeviceInfo(deviceId);
136+
if (!userDeviceInfoData.userExists) {
137+
return res.boom.notFound("No Authentication found!");
138+
}
139+
return res.json({
140+
message: "Authentication document retrieved successfully.",
141+
data: { ...userDeviceInfoData.data },
142+
});
143+
} else {
144+
return res.boom.badRequest(INVALID_QUERY_PARAM);
145+
}
146+
} catch (error) {
147+
logger.error(`Error while fetching user: ${error}`);
148+
return res.boom.badImplementation(SOMETHING_WENT_WRONG);
149+
}
150+
};
151+
125152
module.exports = {
126153
githubAuth,
127154
signout,
128155
storeUserDeviceInfo,
129156
updateAuthStatus,
157+
fetchUserDeviceInfo,
130158
};

models/levels.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ const addLevel = async (levelData) => {
2727
const deleteLevel = async (id) => {
2828
try {
2929
await levelModel.doc(id).delete();
30-
return;
3130
} catch (err) {
3231
logger.error("Error in deleting Level", err);
3332
throw err;

models/qrCodeAuth.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,28 @@ const storeUserDeviceInfo = async (userDeviceInfoData) => {
5454
}
5555
};
5656

57+
const retrieveUserDeviceInfo = async (deviceId) => {
58+
try {
59+
const queryDocument = await QrCodeAuthModel.where("device_id", "==", deviceId).get();
60+
const userData = queryDocument.docs[0];
61+
62+
if (!userData) {
63+
return {
64+
userExists: false,
65+
};
66+
}
67+
return {
68+
userExists: true,
69+
data: userData.data(),
70+
};
71+
} catch (err) {
72+
logger.error("Error in retrieving user device info", err);
73+
throw err;
74+
}
75+
};
76+
5777
module.exports = {
5878
updateStatus,
5979
storeUserDeviceInfo,
80+
retrieveUserDeviceInfo,
6081
};

routes/auth.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ router.get("/github/callback", auth.githubAuth);
1212

1313
router.get("/signout", auth.signout);
1414

15+
router.get("/qr-code-auth", auth.fetchUserDeviceInfo);
16+
1517
router.post("/qr-code-auth", userDeviceInfoValidator.storeUserDeviceInfo, auth.storeUserDeviceInfo);
18+
1619
router.patch(
1720
"/qr-code-auth/authorization_status/:authorization_status",
1821
authenticate,

test/integration/qrCodeAuth.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,51 @@ describe("QrCodeAuth", function () {
160160
});
161161
});
162162
});
163+
164+
describe("GET call for fetching user device info", function () {
165+
let userId = "";
166+
let userDeviceInfoData;
167+
beforeEach(async function () {
168+
userId = await addUser(user);
169+
userDeviceInfoData = { ...userDeviceInfoDataArray[0], user_id: userId, authorization_status: "NOT_INIT" };
170+
});
171+
afterEach(async function () {
172+
await cleanDb();
173+
});
174+
175+
it("should successfully fetch the user device info", function (done) {
176+
qrCodeAuthModel.storeUserDeviceInfo(userDeviceInfoData);
177+
chai
178+
.request(app)
179+
.get(`/auth/qr-code-auth?device_id=${userDeviceInfoData.device_id}`)
180+
.end((err, res) => {
181+
if (err) {
182+
return done(err);
183+
}
184+
expect(res).to.have.status(200);
185+
expect(res.body).to.be.a("object");
186+
expect(res.body.message).to.equal(`Authentication document retrieved successfully.`);
187+
188+
return done();
189+
});
190+
});
191+
192+
it("should fail with 404, when the document is not found", function (done) {
193+
chai
194+
.request(app)
195+
.get(`/auth/qr-code-auth?device_id=${userDeviceInfoData.device_id}`)
196+
.end((err, res) => {
197+
if (err) {
198+
return done(err);
199+
}
200+
201+
expect(res).to.have.status(404);
202+
expect(res.body).to.be.a("object");
203+
expect(res.body.message).to.equal("No Authentication found!");
204+
expect(res.body.error).to.equal("Not Found");
205+
206+
return done();
207+
});
208+
});
209+
});
163210
});

test/unit/models/qrCodeAuth.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,40 @@ describe("mobile auth", function () {
7878
expect(response.userExists).to.be.equal(false);
7979
});
8080
});
81+
82+
describe("retrieveUserDeviceInfo", function () {
83+
it("should fetch the user device info for mobile auth", async function () {
84+
const userData = userDataArray[0];
85+
const { userId } = await users.addOrUpdate(userData);
86+
87+
const userDeviceInfoData = {
88+
...userDeviceInfoDataArray[0],
89+
user_id: userId,
90+
authorization_status: "NOT_INIT",
91+
};
92+
93+
await qrCodeAuth.storeUserDeviceInfo(userDeviceInfoData);
94+
const response = await qrCodeAuth.retrieveUserDeviceInfo(userDeviceInfoData.device_id);
95+
96+
const userDeviceInfo = response.data;
97+
const {
98+
user_id: userID,
99+
device_info: deviceInfo,
100+
device_id: deviceId,
101+
authorization_status: authorizationStatus,
102+
} = userDeviceInfo;
103+
104+
const data = (await qrCodeAuthModel.doc(userId).get()).data();
105+
106+
Object.keys(userDeviceInfo).forEach((key) => {
107+
expect(userDeviceInfo[key]).to.deep.equal(data[key]);
108+
});
109+
110+
expect(response).to.be.an("object");
111+
expect(userID).to.be.a("string");
112+
expect(deviceInfo).to.be.a("string");
113+
expect(deviceId).to.be.a("string");
114+
expect(authorizationStatus).to.be.a("string");
115+
});
116+
});
81117
});

0 commit comments

Comments
 (0)