1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-10 19:34:34 +00:00

chore: mocksas9 controller

This commit is contained in:
2022-08-30 17:27:37 +02:00
parent 091268bf58
commit 572fe22d50
2 changed files with 208 additions and 87 deletions

View File

@@ -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<Sas9Response> {
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<Sas9Response> {
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<Sas9Response> {
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<Sas9Response> {
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<Sas9Response> {
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<MockFileRead> => {
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
}
}

View File

@@ -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())
}