Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/unit tests #70

Open
wants to merge 27 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a11f830
fixed old tests
kostysh Jul 16, 2020
d239719
add tests
kostysh Jul 17, 2020
14d0911
fixed wrong GLIDER_ORGID env
kostysh Jul 19, 2020
e2e811a
added tests for parsers (used in hotels search)
kostysh Jul 19, 2020
111bdf3
added code coverage tool and configuration
kostysh Jul 20, 2020
4d1da92
added test for GliderError class
kostysh Jul 20, 2020
ba9dc90
added "code" case for the test for GliderError class
kostysh Jul 20, 2020
c49eb43
updated tests runner configuration
kostysh Jul 20, 2020
5269492
added test for searchHorel resolver
kostysh Jul 20, 2020
f8bad8f
added test for orderCreateWithOffer hotel order resolver
kostysh Jul 20, 2020
b2180f1
added test for mapFromOffer
kostysh Jul 20, 2020
eb21f7a
disabled mongo calls in testing mode
kostysh Jul 20, 2020
8e7ddca
fix lintig errors
kostysh Jul 20, 2020
edea38c
Linting enabled in CI configuration
kostysh Jul 20, 2020
cb0a918
added test for reduceContactInformation
kostysh Jul 22, 2020
647ec80
added tests for parsers: useDictionary, mergeHourAndDate
kostysh Jul 23, 2020
c303ca4
added test for parser: convertDateToAirportTime
kostysh Jul 23, 2020
4a325d4
added test for parser: reduceToProperty
kostysh Jul 23, 2020
2d003f0
added tests for parsers: roundCommissionDecimals & deepMerge
kostysh Jul 23, 2020
3ed02d9
fixed type in config; minor update for tests
kostysh Jul 23, 2020
467d9bc
added tests for utils: uniqueObjectsList & flatOneDepth
kostysh Jul 24, 2020
c5cb04d
tests structure changed
kostysh Jul 24, 2020
0929e89
tests structure update and new tests
kostysh Jul 24, 2020
5ce8f41
more tests for transformInputData
kostysh Aug 3, 2020
85f9faf
tests for transforInputData utils and soapTemplates
kostysh Aug 4, 2020
4a38881
tests for soapTemplates
kostysh Aug 18, 2020
ea08bad
more tests for soapTemplates
kostysh Aug 19, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
- name: Install Dependencies
run: npm ci

# - name: Code Linting
# run: npm run lint
- name: Code Linting
run: npm run lint

- name: Start Redis
uses: superchargejs/redis-github-action@master
Expand All @@ -22,7 +22,7 @@ jobs:
- name: Unit testing
run: npm test
env:
GLIDER_ORGID: '0x71cd1781a3082f33d2521ac8290c9d4b3b3b116e4e8548a4914b71a1f7201da0'
GLIDER_ORGID: '0x94bf5a57b850a35b4d1d7b59f663ce3a8a76fd9928ef2067cc772fc97fb0ad75'
INFURA_ENDPOINT: wss://ropsten.infura.io/ws/v3
INFURA_PROJECT_ID: ${{ secrets.INFURA_PROJECT_ID }}
REDIS_URL: redis://localhost:6379
10 changes: 10 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"all": true,
"extension": [
".js"
],
"include": [
"api/**",
"helpers/**"
]
}
10 changes: 5 additions & 5 deletions admin/v1/config/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const { basicDecorator } = require('../../../decorators/basic');
const { debugInfo } = require('../../../config');

module.exports = basicDecorator(async (req, res) => {
res.json({
code: 200,
message: "OK",
data: debugInfo(),
});
res.json({
code: 200,
message: 'OK',
data: debugInfo(),
});
}, true); // true - means administrative route
1 change: 1 addition & 0 deletions api/v1/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
const { name, description, version } = require('../package.json');

module.exports = async (req, res) => {
Expand Down
1 change: 1 addition & 0 deletions api/v1/offers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
const GliderError = require('../../helpers/error');
const { basicDecorator } = require('../../decorators/basic');
const { offerPriceRQ } = require('../../helpers/resolvers/flight/offerPrice');
Expand Down
15 changes: 10 additions & 5 deletions api/v1/orders/createWithOffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const {
} = require('../../../helpers/guarantee');
const hotelResolver = require('../../../helpers/resolvers/hotel/orderCreateWithOffer');
const flightResolver = require('../../../helpers/resolvers/flight/orderCreateWithOffer');
const { setOrderStatus, assertOrgerStatus } = require('../../../helpers/resolvers/utils/offers');
const { setOrderStatus, assertOrderStatus } = require('../../../helpers/resolvers/utils/offers');

module.exports = basicDecorator(async (req, res) => {
const requestBody = req.body;
Expand All @@ -28,8 +28,13 @@ module.exports = basicDecorator(async (req, res) => {
const storedOffer = await offerManager.getOffer(requestBody.offerId);

let originOffers = [];

if (storedOffer instanceof FlightOffer) {

// in case of not priced offer
// there possible situation when storedOffer.extraData.originOffers is undefined
if (storedOffer instanceof FlightOffer &&
storedOffer.extraData &&
storedOffer.extraData.originOffers) {

originOffers = await Promise.all(
storedOffer.extraData.originOffers.map(
offerId => offerManager.getOffer(offerId)
Expand All @@ -42,7 +47,7 @@ module.exports = basicDecorator(async (req, res) => {
...originOffers
];

assertOrgerStatus(allOffers);
assertOrderStatus(allOffers);

try {
await setOrderStatus(allOffers, 'CREATING');
Expand All @@ -59,7 +64,7 @@ module.exports = basicDecorator(async (req, res) => {
guaranteeClaim = await claimGuaranteeWithCard(requestBody.guaranteeId);
}

// Handle an Accomodation offer
// Handle an Accommodation offer
if (storedOffer instanceof AccommodationOffer) {

if (!guaranteeClaim) {
Expand Down
1 change: 1 addition & 0 deletions api/v1/searchOffers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
const { basicDecorator } = require('../../decorators/basic');
const GliderError = require('../../helpers/error');
const { searchHotel } = require('../../helpers/resolvers/searchHotel');
Expand Down
1 change: 1 addition & 0 deletions api/v1/seatmap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
const GliderError = require('../../helpers/error');
const { basicDecorator } = require('../../decorators/basic');
const { seatMapRQ } = require('../../helpers/resolvers/flight/seatAvailability');
Expand Down
21 changes: 12 additions & 9 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
* - The Github's branch, if the deployment is made using the Vercel/Github integration
* - Defaults to 'staging', including local
*/
var buildEnv = require('./env.json');
let buildEnv;
try {
buildEnv = require('./env.json');
} catch (err) {}

// Define the current enviroment
// Define the current environment
const determineEnviroment = () => {
// If defined, use the Glider environment variable
if(process.env.GLIDER_ENV) {
Expand All @@ -35,12 +38,12 @@ const determineEnviroment = () => {
}
}

const enviroment = determineEnviroment();
const environment = determineEnviroment();

// Get an an environment variable
const getConfigKey = (key) => {
// Return environment specific variable if any
const envKey = `${enviroment.toUpperCase()}_${key}`;
const envKey = `${environment.toUpperCase()}_${key}`;
if(process.env.hasOwnProperty(envKey)) {
return process.env[envKey];
}
Expand Down Expand Up @@ -91,8 +94,8 @@ const airFranceConfig = {
const airCanadaConfig = {
apiKey: getConfigKey('AC_API_KEY'),
commission: getConfigKey('AC_COMISSION') || '0',
baseUrl: getConfigKey('AC_BASEURL') || (enviroment === 'production' ? 'https://ndcexchange.mconnect.aero/messaging/v2/ndc-exchange/' : 'https://ndchub.mconnect.aero/messaging/v2/ndc-exchange/'),
baseUrlPci: getConfigKey('AC_BASEURL_PCI') || (enviroment === 'production' ? 'https://pci.ndcexchange.mconnect.aero/messaging/v2/ndc-exchange/' : 'https://pci.ndchub.mconnect.aero/messaging/v2/ndc-exchange/'),
baseUrl: getConfigKey('AC_BASEURL') || (environment === 'production' ? 'https://ndcexchange.mconnect.aero/messaging/v2/ndc-exchange/' : 'https://ndchub.mconnect.aero/messaging/v2/ndc-exchange/'),
baseUrlPci: getConfigKey('AC_BASEURL_PCI') || (environment === 'production' ? 'https://pci.ndcexchange.mconnect.aero/messaging/v2/ndc-exchange/' : 'https://pci.ndchub.mconnect.aero/messaging/v2/ndc-exchange/'),
AirlineID: getConfigKey('AC_PARTICIPANT_RECIPENT_AIRLINE_ID') || 'AC',
PointOfSale: {
Location: {
Expand Down Expand Up @@ -136,7 +139,7 @@ const erevmax = {

module.exports.debugInfo = () => {
return {
enviroment: enviroment,
environment: environment,
erevmax: erevmax,
env: process.env,
buildEnv: buildEnv,
Expand All @@ -152,10 +155,10 @@ module.exports.elasticUrl = getConfigKey('ELASTIC_URL') || 'http://localhost:920
module.exports.INFURA_URI = `${getConfigKey('INFURA_ENDPOINT')}/${getConfigKey('INFURA_PROJECT_ID')}`;
module.exports.GLIDER_DID = `did:orgid:${getConfigKey('GLIDER_ORGID') || '0x71cd1781a3082f33d2521ac8290c9d4b3b3b116e4e8548a4914b71a1f7201da0'}`;
module.exports.GLIDER_ADMIN_DID = `did:orgid:${getConfigKey('GLIDER_ORGID') || '0x71cd1781a3082f33d2521ac8290c9d4b3b3b116e4e8548a4914b71a1f7201da0'}#${getConfigKey('GLIDER_ADMIN_KEY') || ''}`;
module.exports.SIMARD_URL = getConfigKey('SIMARD_URL') || `https://${enviroment}.api.simard.io/api/v1`;
module.exports.SIMARD_URL = getConfigKey('SIMARD_URL') || `https://${environment}.api.simard.io/api/v1`;
module.exports.SIMARD_JWT = getConfigKey('SIMARD_JWT') || getConfigKey('JWT');
module.exports.LIF_MIN_DEPOSIT = getConfigKey('LIF_MIN_DEPOSIT') || '0';
module.exports.expirationTime = 30 * 60; // 30 min in seconds
module.exports.expirationLong = 60 * 60 * 24 * 365 * 7; // 7 years in seconds
module.exports.ETHEREUM_NETWORK = getConfigKey('ETHEREUM_NETWORK') || 'ropsten';
module.exports.enviroment = enviroment;
module.exports.environment = environment;
5 changes: 4 additions & 1 deletion helpers/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ module.exports.verifyJWT = async (type, jwt, isAdmin = false) => {
let didResult;
const cachedDidResult = JSON.parse(await redisClient.asyncGet(`didResult_${did}`));

if (cachedDidResult && typeof cachedDidResult.didDocument === 'object') {
if (!process.env.TESTING &&
cachedDidResult &&
typeof cachedDidResult.didDocument === 'object') {
/* istanbul ignore next */
didResult = cachedDidResult;
} else {
didResult = await orgIdResolver.resolve(did);
Expand Down
16 changes: 16 additions & 0 deletions helpers/models/mongo/hotels.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ class HotelsManager {

// Search for hotel by the given location
searchByLocation (location, skip = 0, limit = null) {
if (process.env.TESTING) {
/* istanbul ignore next */
const mockHotelsEmpty = require('../../../test/mocks/hotelsEmpty.json');
const mockHotels = require('../../../test/mocks/hotels.json');
return process.env.TESTING_EMPTY_RESULT === '1'
? mockHotelsEmpty
: mockHotels;
}
return this.get(
{
location: {
Expand All @@ -219,6 +227,14 @@ class HotelsManager {

// Search for hotel within the given polygon of coordinates
searchWithin (polygon, skip = 0, limit = null) {
if (process.env.TESTING) {
/* istanbul ignore next */
const mockHotelsEmpty = require('../../../test/mocks/hotelsEmpty.json');
const mockHotels = require('../../../test/mocks/hotels.json');
return process.env.TESTING_EMPTY_RESULT === '1'
? mockHotelsEmpty
: mockHotels;
}
return this.get(
{
location: {
Expand Down
1 change: 1 addition & 0 deletions helpers/mongo/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
const mongoose = require('mongoose');
const config = require('../../config');

Expand Down
8 changes: 5 additions & 3 deletions helpers/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const { zonedTimeToUtc } = require('date-fns-tz');
const { airports } = require('./timeZoneByAirportCode');

module.exports.reduceObjectToProperty = (object, property) => Object.entries(object)
module.exports.reduceObjectToProperty = (array, property) => Object.entries(array)
.reduce(
(result, [key, value])=> ({
...result,
Expand Down Expand Up @@ -68,7 +68,7 @@ module.exports.mergeHourAndDate = array => array
})
);

module.exports.convertDateToIrportTime = (date, time, iataCode) => zonedTimeToUtc(
module.exports.convertDateToAirportTime = (date, time, iataCode) => zonedTimeToUtc(
`${date} ${time}:00.000`,
airports[iataCode]
);
Expand All @@ -80,6 +80,7 @@ module.exports.reduceToProperty = (object, property) => Object.keys(object)
};
});

/* istanbul ignore next */
module.exports.splitSegments = (combinations) => combinations
.map(
({ _items_, ...others }) => ({
Expand Down Expand Up @@ -108,7 +109,7 @@ module.exports.roundCommissionDecimals = (offers) => offers
})
);

module.exports.reduceAcomodation = (accommodation) => accommodation
module.exports.reduceAccommodation = (accommodation) => accommodation
.reduce(
(ac, { _provider_, _id_, ...others }) => {
const key = `${_provider_}.${_id_}`;
Expand All @@ -120,6 +121,7 @@ module.exports.reduceAcomodation = (accommodation) => accommodation
{}
);

/* istanbul ignore next */
module.exports.reduceRoomStays = (_roomStays_ => {
// The offer dicts will contain all offers
let offers = {};
Expand Down
1 change: 1 addition & 0 deletions helpers/parsers/responseKeys.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
const parseKeys = obj => {
Object.keys(obj).forEach((v) => {

Expand Down
1 change: 1 addition & 0 deletions helpers/redis/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* istanbul ignore file */
const redis = require('redis');
const config = require('../../config');

Expand Down
10 changes: 5 additions & 5 deletions helpers/resolvers/flight/offerPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {
FaultsTransformTemplate_AC,
ErrorsTransformTemplate_AC
} = require('../../camaroTemplates/provideOfferPrice');
const { setOrderStatus, assertOrgerStatus } = require('../utils/offers');
const { setOrderStatus, assertOrderStatus } = require('../utils/offers');

// Convert response data to the object form
const processResponse = async (data, template) => {
Expand Down Expand Up @@ -167,7 +167,7 @@ module.exports.offerPriceRQ = async (
const offers = await fetchFlightsOffersByIds(offerIds);

// Assert order status in offers
assertOrgerStatus(offers);
assertOrderStatus(offers);

try {
await setOrderStatus(offers, 'CREATING');
Expand Down Expand Up @@ -255,7 +255,7 @@ module.exports.offerPriceRQ = async (
}
);

// Update serments Ids to initially obtained with original offers
// Update segments Ids to initially obtained with original offers
const newSegmentsChanged = Object.entries(offerResult.offer.itinerary.segments)
.reduce(
(a, v) => {
Expand All @@ -276,7 +276,7 @@ module.exports.offerPriceRQ = async (

offerResult.offer.itinerary.segments = newSegmentsChanged.segments;

// Update serments refs to initially obtained with original offers
// Update segments refs to initially obtained with original offers
const newDestinationsChanged = offerResult.offer.destinations
.map(d => ({
...d,
Expand All @@ -287,7 +287,7 @@ module.exports.offerPriceRQ = async (
}));
delete offerResult.offer.destinations;

// Update passengers Ids to initally assigned during offers search
// Update passengers Ids to initially assigned during offers search
const newPassengersChanged = Object.entries(offerResult.offer.passengers)
.reduce(
(a, v) => {
Expand Down
2 changes: 1 addition & 1 deletion helpers/resolvers/flight/seatAvailability.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const processResponse = async (data, offers, template) => {
prices
};
}

return a;
}, {});

Expand Down
50 changes: 34 additions & 16 deletions helpers/resolvers/hotel/orderCreateWithOffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,48 @@ const GliderError = require('../../error');
const responseTemplate = require('../../camaroTemplates/hotelResNotifRS').otaHotelResNotifRSTemplate;

const hotelResNotif = require('../../transformInputData/hotelResNotif');
const mapOTAHotelResNotifSoap = require('../../soapTemplates/ota/otaHotelResNotifRQ');
const {
mapHotelResNotifSoap
} = require('../../soapTemplates/ota/otaHotelResNotifRQ');
const { v4: uuidv4 } = require('uuid');

module.exports = async (offer, passengers, card) => {
// Build the request
const otaHotelResNotifRQData = hotelResNotif.mapFromOffer(offer, passengers, card);
const otaRequestBody = mapOTAHotelResNotifSoap(otaHotelResNotifRQData);

const response = await axios({
method: 'post',
url: config.erevmax.reservationUrl,
headers: {
'Content-Type': 'text/xml;charset=UTF-8',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'SOAPAction': 'http://www.opentravel.org/OTA/2003/05/getOTAHotelAvailability',
},
data: otaRequestBody
});
const otaRequestBody = mapHotelResNotifSoap(otaHotelResNotifRQData);

let response;

if (!process.env.TESTING) {
/* istanbul ignore next */
response = await axios({
method: 'post',
url: config.erevmax.reservationUrl,
headers: {
'Content-Type': 'text/xml;charset=UTF-8',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'SOAPAction': 'http://www.opentravel.org/OTA/2003/05/getOTAHotelAvailability',
},
data: otaRequestBody
});
} else {
response = process.env.TESTING_PROVIDER_ERRORS === '1'
? {
status: 502,
data: {}
}
: require('../../../test/mocks/erevmaxOrder.json');
}
// console.log('@@@', require('../../json').stringifyCircular(response));

// Handle error from reservation
if(response.status !== 200 || !response.data) {
console.log(JSON.stringify(otaRequestBody));
response.data && console.log(JSON.stringify(response.data));
/* istanbul ignore if */
if (!process.env.TESTING) {
console.log(JSON.stringify(otaRequestBody));
response.data && console.log(JSON.stringify(response.data));
}
throw new GliderError(
`[erevmax:${response.status}] Booking creation failed`,
502
Expand Down
Loading