Skip to content

Commit

Permalink
Merge pull request #1052 from OneCommunityGlobal/parthj_unit_tests_an…
Browse files Browse the repository at this point in the history
…d_integration_test_for_timeZoneAPIController.js

Parthj unit tests and integration test for time zone api controller.js
  • Loading branch information
one-community authored Sep 21, 2024
2 parents 2e737d6 + e618eaf commit 9eae76d
Show file tree
Hide file tree
Showing 7 changed files with 833 additions and 298 deletions.
524 changes: 243 additions & 281 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions requirements/timeZoneAPIController/getTImeZone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Check mark: ✅
Cross Mark: ❌

# Get Time Zone

> ## Positive case
1. ✅ Returns status code 200 and response data as follows:
i. current location
ii. timezone

> ## Negative case
1. ✅ Returns status code 403, if the user is not authorised.
2. ✅ Returns status code 401, if the API key is missing.
3. ✅ Returns status code 400, if the location is missing.
4. ✅ Returns status code 404, if geocodeAPIEndpoint returns no results.
5. ✅ Returns status code 500, if any other error occurs.

> ## Edge case
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Check mark: ✅
Cross Mark: ❌

# Get Time Zone

> ## Positive case
1. ✅ Returns status code 200 and response data as follows:
i. current location
ii. timezone

> ## Negative case
1. ✅ Returns status code 400, if the token is missing in the request body.
2. ✅ Returns status code 403, if the no document exists in ProfileInitialSetupToken database with requested token.
3. ✅ Returns status code 400, if the location is missing.
4. ✅ Returns status code 404, if geocodeAPIEndpoint returns no results.
5. ✅ Returns status code 500, if any other error occurs.

> ## Edge case
13 changes: 7 additions & 6 deletions src/controllers/timeZoneAPIController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const fetch = require('node-fetch');
const dotenv = require('dotenv');

dotenv.config();
const ProfileInitialSetupToken = require('../models/profileInitialSetupToken');
const { hasPermission } = require('../utilities/permissions');

const premiumKey = process.env.TIMEZONE_PREMIUM_KEY;
const commonKey = process.env.TIMEZONE_COMMON_KEY;

const performTimeZoneRequest = async (req, res, apiKey) => {
const { location } = req.params;

Expand All @@ -17,7 +17,6 @@ const performTimeZoneRequest = async (req, res, apiKey) => {
try {
const geocodeAPIEndpoint = 'https://api.opencagedata.com/geocode/v1/json';
const url = `${geocodeAPIEndpoint}?key=${apiKey}&q=${location}&pretty=1&limit=1`;

const response = await fetch(url);
const data = await response.json();

Expand Down Expand Up @@ -53,16 +52,17 @@ const performTimeZoneRequest = async (req, res, apiKey) => {

const timeZoneAPIController = function () {
const getTimeZone = async (req, res) => {
const premiumKey = process.env.TIMEZONE_PREMIUM_KEY;
const commonKey = process.env.TIMEZONE_COMMON_KEY;
const { requestor } = req.body;

if (!requestor.role) {
res.status(403).send('Unauthorized Request');
return;
}

const userAPIKey = (await hasPermission(requestor, 'getTimeZoneAPIKey'))
? premiumKey
: commonKey;

if (!userAPIKey) {
res.status(401).send('API Key Missing');
return;
Expand All @@ -72,6 +72,7 @@ const timeZoneAPIController = function () {
};

const getTimeZoneProfileInitialSetup = async (req, res) => {
const commonKey = process.env.TIMEZONE_COMMON_KEY;
const { token } = req.body;
if (!token) {
res.status(400).send('Missing token');
Expand Down
Loading

0 comments on commit 9eae76d

Please sign in to comment.