From 572fe22d50b926ea337ab7319fb6bc263f0901c4 Mon Sep 17 00:00:00 2001 From: Mihajlo Medjedovic Date: Tue, 30 Aug 2022 17:27:37 +0200 Subject: [PATCH] chore: mocksas9 controller --- api/src/controllers/mock-sas9.ts | 190 +++++++++++++++++++++++++++++++ api/src/routes/api/mock-sas9.ts | 105 +++-------------- 2 files changed, 208 insertions(+), 87 deletions(-) create mode 100644 api/src/controllers/mock-sas9.ts diff --git a/api/src/controllers/mock-sas9.ts b/api/src/controllers/mock-sas9.ts new file mode 100644 index 0000000..39eabf5 --- /dev/null +++ b/api/src/controllers/mock-sas9.ts @@ -0,0 +1,190 @@ +import { readFile } from '@sasjs/utils' +import express from 'express' +import path from 'path' +import { Request, Post, Get } from 'tsoa' + +export interface Sas9Response { + content: string + redirect?: string + error?: boolean +} + +export interface MockFileRead { + content: string + error?: boolean +} + +export class MockSas9Controller { + private loggedIn: boolean = false + + @Get('/SASStoredProcess') + public async sasStoredProcess(): Promise { + if (!this.loggedIn) { + return { + content: '', + redirect: '/SASLogon/login' + } + } + + const content = await getMockResponseFromFile([ + process.cwd(), + 'mocks', + 'generic', + 'sas9', + 'sas-stored-process' + ]) + + if (content.error) { + return { + content: content.content, + error: true + } + } else { + return { + content: content.content + } + } + } + + @Post('/SASStoredProcess/do/') + public async sasStoredProcessDo( + @Request() req: express.Request + ): Promise { + if (!this.loggedIn) { + return { + content: '', + redirect: '/SASLogon/login' + } + } + + let program = req.query._program?.toString() || '' + program = program.replace('/', '') + + const content = await getMockResponseFromFile([ + process.cwd(), + 'mocks', + ...program.split('/') + ]) + + if (content.error) { + return { + content: content.content, + error: true + } + } + + const parsedContent = parseJsonIfValid(content.content) + + return { + content: parsedContent + } + } + + @Get('/SASLogon/login') + public async loginGet(): Promise { + const content = await getMockResponseFromFile([ + process.cwd(), + 'mocks', + 'generic', + 'sas9', + 'login' + ]) + + if (content.error) { + return { + content: content.content, + error: true + } + } else { + return { + content: content.content + } + } + } + + @Post('/SASLogon/login') + public async loginPost(): Promise { + this.loggedIn = true + + const content = await getMockResponseFromFile([ + process.cwd(), + 'mocks', + 'generic', + 'sas9', + 'logged-in' + ]) + + if (content.error) { + return { + content: content.content, + error: true + } + } else { + return { + content: content.content + } + } + } + + @Get('/SASLogon/logout') + public async logout(): Promise { + this.loggedIn = false + + const content = await getMockResponseFromFile([ + process.cwd(), + 'mocks', + 'generic', + 'sas9', + 'logged-out' + ]) + + if (content.error) { + return { + content: content.content, + error: true + } + } else { + return { + content: content.content + } + } + } +} + +/** + * If JSON is valid it will be parsed otherwise will return text unaltered + * @param content string to be parsed + * @returns JSON or string + */ +const parseJsonIfValid = (content: string) => { + let fileContent = '' + + try { + fileContent = JSON.parse(content) + } catch (err: any) { + fileContent = content + } + + return fileContent +} + +const getMockResponseFromFile = async ( + filePath: string[] +): Promise => { + const filePathParsed = path.join(...filePath) + let error: boolean = false + + let file = await readFile(filePathParsed).catch((err: any) => { + const errMsg = `Error reading mocked file on path: ${filePathParsed}\nError: ${err}` + console.error(errMsg) + + error = true + + return errMsg + }) + + return { + content: file, + error: error + } +} diff --git a/api/src/routes/api/mock-sas9.ts b/api/src/routes/api/mock-sas9.ts index 4281d1b..3421eba 100644 --- a/api/src/routes/api/mock-sas9.ts +++ b/api/src/routes/api/mock-sas9.ts @@ -1,82 +1,41 @@ import { readFile } from '@sasjs/utils' import express from 'express' import path from 'path' +import { MockSas9Controller } from '../../controllers/mock-sas9' const mockSas9Router = express.Router() const { MOCK_SERVERTYPE } = process.env -let loggedIn: boolean = false +// Mock controller must be singleton because it keeps the states +// for example `isLoggedIn` and potentially more in future mocks +const mockSas9Controller = new MockSas9Controller() mockSas9Router.get('/SASStoredProcess', async (req, res) => { - if (!loggedIn) { - res.redirect('/SASLogon/login') - return - } + const response = await mockSas9Controller.sasStoredProcess() - const filePath = path.join( - process.cwd(), - 'mocks', - 'generic', - 'sas9', - 'sas-stored-process' - ) - - let file - - try { - file = await readFile(filePath) - } catch (err: any) { - console.error(`Mocked file on path: ${filePath} is not found.`) - res.status(403).send(err.toString()) + if (response.redirect) { + res.redirect(response.redirect) return } try { - res.send(file) + res.send(response.content) } catch (err: any) { res.status(403).send(err.toString()) } }) mockSas9Router.post('/SASStoredProcess/do/', async (req, res) => { - let program = req.query._program?.toString() || '' - program = program.replace('/', '') - const filePath = path.join(process.cwd(), 'mocks', ...program.split('/')) + const response = await mockSas9Controller.sasStoredProcessDo(req) - let file - - try { - file = await readFile(filePath) - } catch (err: any) { - let err = `Mocked file on path: ${filePath} is not found.` - console.error(err) - res.status(403).send(err) + if (response.redirect) { + res.redirect(response.redirect) return } - if (!file) { - let err = `Mocked file on path: ${filePath} is not found.` - console.error(err) - res.status(403).send(err) - return - } - - if (!loggedIn) { - res.redirect('/SASLogon/login') - return - } - - let fileContent = '' - try { - fileContent = JSON.parse(file) - } catch (err: any) { - fileContent = file - } - - try { - res.send(fileContent) + res.send(response.content) } catch (err: any) { res.status(403).send(err.toString()) } @@ -84,58 +43,30 @@ mockSas9Router.post('/SASStoredProcess/do/', async (req, res) => { if (MOCK_SERVERTYPE !== undefined) { mockSas9Router.get('/SASLogon/login', async (req, res) => { - const filePath = path.join( - process.cwd(), - 'mocks', - 'generic', - 'sas9', - 'login' - ) + const response = await mockSas9Controller.loginGet() try { - const file = await readFile(filePath) - - res.send(file) + res.send(response.content) } catch (err: any) { res.status(403).send(err.toString()) } }) mockSas9Router.post('/SASLogon/login', async (req, res) => { - loggedIn = true - - const filePath = path.join( - process.cwd(), - 'mocks', - 'generic', - 'sas9', - 'logged-in' - ) + const response = await mockSas9Controller.loginPost() try { - const file = await readFile(filePath) - - res.send(file) + res.send(response.content) } catch (err: any) { res.status(403).send(err.toString()) } }) mockSas9Router.get('/SASLogon/logout', async (req, res) => { - loggedIn = false - - const filePath = path.join( - process.cwd(), - 'mocks', - 'generic', - 'sas9', - 'logged-out' - ) + const response = await mockSas9Controller.logout() try { - const file = await readFile(filePath) - - res.send(file) + res.send(response.content) } catch (err: any) { res.status(403).send(err.toString()) }