Skip to content

Commit

Permalink
Merge pull request #623 from Travelport-Ukraine/master
Browse files Browse the repository at this point in the history
1.14.9 Terminal errors handling
  • Loading branch information
dchertousov authored Mar 31, 2023
2 parents 91cc8cb + 304fbb7 commit 866c409
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uapi-json",
"version": "1.14.8",
"version": "1.14.9",
"description": "Travelport Universal API",
"main": "src/",
"files": [
Expand Down
20 changes: 16 additions & 4 deletions src/Services/Terminal/Terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const RETRY_CODES_LIST = ['14058'];

const DEFAULT_RETRY_TIMES = 3;

const UNEXPECTED_TERMINAL_ERRORS = [
'NO PREVIOUS OR ORIGINAL REQUEST',
];

// Adding event handler on beforeExit and exit process events to process open terminals
process.on('beforeExit', () => {
Promise.all(
Expand Down Expand Up @@ -188,6 +192,10 @@ module.exports = function (settings) {
sessionToken: tokenData.sessionToken,
command: `SEM/${emulatePcc}/AG`,
}).then((response) => {
if (response[0].match(/duty code not authorised/i)) {
return Promise.reject(new TerminalRuntimeError.TerminalAuthIssue(response));
}

if (!response[0].match(/^PROCEED/)) {
return Promise.reject(new TerminalRuntimeError.TerminalEmulationFailed(response));
}
Expand All @@ -202,7 +210,7 @@ module.exports = function (settings) {

const terminal = {
getToken: getSessionToken,
executeCommand: (rawCommand, stopMD = defaultStopMD) => new Promise(async (resolve, reject) => {
executeCommand: async (rawCommand, stopMD = defaultStopMD) => {
try {
const sessionToken = await getSessionToken();
const terminalId = getTerminalId(sessionToken);
Expand All @@ -223,14 +231,18 @@ module.exports = function (settings) {
terminalState: TERMINAL_STATE_READY,
});

resolve(response);
if (UNEXPECTED_TERMINAL_ERRORS.some(e => response.includes(e))) {
throw new TerminalRuntimeError.TerminalUnexpectedError({ screen: response });
}

return response;
} catch (err) {
Object.assign(state, {
terminalState: TERMINAL_STATE_ERROR,
});
reject(err);
throw err;
}
}),
},
closeSession: () => getSessionToken()
.then(
sessionToken => service.closeSession({
Expand Down
2 changes: 2 additions & 0 deletions src/Services/Terminal/TerminalErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Object.assign(TerminalRuntimeError, createErrorsList({
TerminalIsClosed: 'Terminal is closed',
ErrorClosingSession: 'Error closing session',
NoAgreement: ['There is no agreement between current pcc and you trying to reach', errorCodes.Validation],
TerminalAuthIssue: ['Temporary terminal auth issue. Please, try again later', errorCodes.Validation],
TerminalUnexpectedError: ['Terminal unexpected error. Please, try again later', errorCodes.Validation],
}, TerminalRuntimeError));

module.exports = {
Expand Down
41 changes: 34 additions & 7 deletions test/Terminal/Terminal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ const executeCommandEmulationFailed = sinon.spy((params) => {
case 'I':
return getTerminalResponse('I');
default:
if (params.command.match(/SEM\/6K66\/AG/)) {
return getTerminalResponse('NOT_AUTHORISED');
}

if (params.command.match(/^SEM/)) {
return getTerminalResponse('RESTRICTED');
}
Expand Down Expand Up @@ -184,6 +188,16 @@ const terminalEmulationFailed = proxyquire(terminalPath, {
'./TerminalService': terminalServiceEmulationFailed,
});

const createUapiTerminal = (emulatePcc, terminalFunc) => {
const emulateConfig = Object.assign({}, config, {
emulatePcc,
});
return terminalFunc({
auth: emulateConfig,
emulatePcc,
});
};

// Tests
describe('#Terminal', function terminalTest() {
this.timeout(5000);
Expand Down Expand Up @@ -444,13 +458,7 @@ describe('#Terminal', function terminalTest() {
executeCommandEmulationFailed.resetHistory();

const emulatePcc = '7j8i';
const emulateConfig = Object.assign({}, config, {
emulatePcc,
});
const uAPITerminal = terminalEmulationFailed({
auth: emulateConfig,
emulatePcc,
});
const uAPITerminal = createUapiTerminal(emulatePcc, terminalEmulationFailed);

return uAPITerminal
.executeCommand('I')
Expand All @@ -466,6 +474,25 @@ describe('#Terminal', function terminalTest() {
return uAPITerminal.closeSession();
});
});
it('Should fail if not authorised by galileo', async () => {
// Resetting spies
getSessionToken.resetHistory();
executeCommandEmulationFailed.resetHistory();

const emulatePcc = '6K66';
const uAPITerminal = createUapiTerminal(emulatePcc, terminalEmulationFailed);

try {
await uAPITerminal.executeCommand('I');
throw new Error('Emulation has not failed');
} catch (e) {
expect(e).to.be.an.instanceof(
TerminalRuntimeError.TerminalAuthIssue
);
}

await uAPITerminal.closeSession();
});
it('Should emulate pcc', () => {
// Resetting spies
getSessionToken.resetHistory();
Expand Down
2 changes: 2 additions & 0 deletions test/Terminal/TerminalResponses/NOT_AUTHORISED.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AG - DUTY CODE NOT AUTHORISED FOR TERMINAL - GALILEO
><

0 comments on commit 866c409

Please sign in to comment.