Skip to content

Commit

Permalink
Merge pull request #50 from web-tech-tw/ipgeo
Browse files Browse the repository at this point in the history
feat: add ipgeo query for checking applications
  • Loading branch information
supersonictw authored Aug 19, 2024
2 parents d09ea0a + 293a861 commit 330e7c3
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-validator": "^6.14.3",
"geoip-lite": "^1.4.10",
"http-status-codes": "^2.2.0",
"js-sha256": "^0.9.0",
"mongoose": "^6.8.4",
Expand Down
4 changes: 4 additions & 0 deletions src/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const middlewareAccess = require("../middleware/access");
const middlewareInspector = require("../middleware/inspector");
const middlewareValidator = require("express-validator");

const ipGeoQuery = require("geoip-lite");

// Create router
const {Router: newRouter} = express;
const router = newRouter();
Expand Down Expand Up @@ -58,6 +60,7 @@ router.post(

const userAgent = utilVisitor.getUserAgent(req);
const ipAddress = utilVisitor.getIPAddress(req);
const ipGeolocation = ipGeoQuery.lookup(ipAddress);
const codeData = `${roomId}_${ipAddress}|${userAgent}`;
const applicationId = utilCode.computeHash(codeData, 24);

Expand All @@ -78,6 +81,7 @@ router.post(
user_agent: userAgent,
created_at: utilNative.getPosixTimestamp(),
ip_address: ipAddress,
ip_geolocation: ipGeolocation,
code,
});

Expand Down
1 change: 1 addition & 0 deletions src/schemas/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = new Schema({
room_id: String,
user_agent: String,
ip_address: String,
ip_geolocation: Object,
created_at: Number,
approval_by: String | null,
approval_at: Number,
Expand Down
6 changes: 6 additions & 0 deletions src/utils/visitor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"use strict";
// The simple toolbox for fetch visitor information from HTTP request.

// Import isProduction
const {isProduction} = require("../config");

/**
* Get IP Address.
* @module ip_address
Expand All @@ -9,6 +12,9 @@
* @return {string} the IP Address
*/
function getIPAddress(req) {
if (!isProduction()) {
return "8.8.8.8";
}
return req?.clientIp || req.ip;
}

Expand Down
1 change: 1 addition & 0 deletions test/admin_room.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const to = utils.urlGlue("/admin-room");
describe("/", function() {
const method = (roleName, expectedCode, done) => request(app)
.get(to("/"))
.set("User-Agent", process.env.TEST_USER_AGENT)
.set("Accept", "application/json")
.set("Authorization", utils.getUserTestToken(roleName))
.expect(expectedCode)
Expand Down
6 changes: 6 additions & 0 deletions test/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe("/application", function() {
.post("/room")
.send({slug: "test-room"})
.type("form")
.set("User-Agent", process.env.TEST_USER_AGENT)
.set("Accept", "application/json")
.set("Authorization", utils.getUserTestToken("admin"))
.then(() => done())
Expand All @@ -41,6 +42,7 @@ describe("/application", function() {
.post(to("/"))
.send({slug: "test-room"})
.type("form")
.set("User-Agent", process.env.TEST_USER_AGENT)
.set("Accept", "application/json")
.expect(StatusCodes.CREATED)
.then((res) => {
Expand All @@ -63,6 +65,7 @@ describe("/application", function() {
.post(to("/"))
.send({slug: "test-room"})
.type("form")
.set("User-Agent", process.env.TEST_USER_AGENT)
.set("Accept", "application/json")
.expect(StatusCodes.CREATED)
.then((res) => {
Expand All @@ -80,6 +83,7 @@ describe("/application", function() {
request(app)
.get(to("/"))
.query({code})
.set("User-Agent", process.env.TEST_USER_AGENT)
.set("Accept", "application/json")
.set("Authorization", utils.getUserTestToken("manager"))
.expect(StatusCodes.OK)
Expand All @@ -97,6 +101,7 @@ describe("/application", function() {
request(app)
.patch(to("/"))
.query({code})
.set("User-Agent", process.env.TEST_USER_AGENT)
.set("Accept", "application/json")
.set("Authorization", utils.getUserTestToken("manager"))
.expect(StatusCodes.NO_CONTENT)
Expand All @@ -114,6 +119,7 @@ describe("/application", function() {
request(app)
.delete(to("/"))
.query({code})
.set("User-Agent", process.env.TEST_USER_AGENT)
.set("Accept", "application/json")
.set("Authorization", utils.getUserTestToken("manager"))
.expect(StatusCodes.NO_CONTENT)
Expand Down
4 changes: 3 additions & 1 deletion test/kernel/init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";

const {runLoader} = require("../../src/config");
runLoader();

process.env.NODE_ENV = "testing";
runLoader();
process.env.TEST_USER_AGENT =
"Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14";
1 change: 1 addition & 0 deletions test/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("/room", function() {
.post(to("/"))
.send({slug: `test-room-${roleName}`})
.type("form")
.set("User-Agent", process.env.TEST_USER_AGENT)
.set("Accept", "application/json")
.set("Authorization", utils.getUserTestToken(roleName))
.expect(expectedCode)
Expand Down

0 comments on commit 330e7c3

Please sign in to comment.