mirror of
https://github.com/sasjs/server.git
synced 2026-01-07 14:40:05 +00:00
chore: mocksas9 controller
This commit is contained in:
190
api/src/controllers/mock-sas9.ts
Normal file
190
api/src/controllers/mock-sas9.ts
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,82 +1,41 @@
|
|||||||
import { readFile } from '@sasjs/utils'
|
import { readFile } from '@sasjs/utils'
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
import { MockSas9Controller } from '../../controllers/mock-sas9'
|
||||||
|
|
||||||
const mockSas9Router = express.Router()
|
const mockSas9Router = express.Router()
|
||||||
|
|
||||||
const { MOCK_SERVERTYPE } = process.env
|
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) => {
|
mockSas9Router.get('/SASStoredProcess', async (req, res) => {
|
||||||
if (!loggedIn) {
|
const response = await mockSas9Controller.sasStoredProcess()
|
||||||
res.redirect('/SASLogon/login')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const filePath = path.join(
|
if (response.redirect) {
|
||||||
process.cwd(),
|
res.redirect(response.redirect)
|
||||||
'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())
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
res.send(file)
|
res.send(response.content)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
mockSas9Router.post('/SASStoredProcess/do/', async (req, res) => {
|
mockSas9Router.post('/SASStoredProcess/do/', async (req, res) => {
|
||||||
let program = req.query._program?.toString() || ''
|
const response = await mockSas9Controller.sasStoredProcessDo(req)
|
||||||
program = program.replace('/', '')
|
|
||||||
const filePath = path.join(process.cwd(), 'mocks', ...program.split('/'))
|
|
||||||
|
|
||||||
let file
|
if (response.redirect) {
|
||||||
|
res.redirect(response.redirect)
|
||||||
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)
|
|
||||||
return
|
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 {
|
try {
|
||||||
fileContent = JSON.parse(file)
|
res.send(response.content)
|
||||||
} catch (err: any) {
|
|
||||||
fileContent = file
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
res.send(fileContent)
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
}
|
}
|
||||||
@@ -84,58 +43,30 @@ mockSas9Router.post('/SASStoredProcess/do/', async (req, res) => {
|
|||||||
|
|
||||||
if (MOCK_SERVERTYPE !== undefined) {
|
if (MOCK_SERVERTYPE !== undefined) {
|
||||||
mockSas9Router.get('/SASLogon/login', async (req, res) => {
|
mockSas9Router.get('/SASLogon/login', async (req, res) => {
|
||||||
const filePath = path.join(
|
const response = await mockSas9Controller.loginGet()
|
||||||
process.cwd(),
|
|
||||||
'mocks',
|
|
||||||
'generic',
|
|
||||||
'sas9',
|
|
||||||
'login'
|
|
||||||
)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const file = await readFile(filePath)
|
res.send(response.content)
|
||||||
|
|
||||||
res.send(file)
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
mockSas9Router.post('/SASLogon/login', async (req, res) => {
|
mockSas9Router.post('/SASLogon/login', async (req, res) => {
|
||||||
loggedIn = true
|
const response = await mockSas9Controller.loginPost()
|
||||||
|
|
||||||
const filePath = path.join(
|
|
||||||
process.cwd(),
|
|
||||||
'mocks',
|
|
||||||
'generic',
|
|
||||||
'sas9',
|
|
||||||
'logged-in'
|
|
||||||
)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const file = await readFile(filePath)
|
res.send(response.content)
|
||||||
|
|
||||||
res.send(file)
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
mockSas9Router.get('/SASLogon/logout', async (req, res) => {
|
mockSas9Router.get('/SASLogon/logout', async (req, res) => {
|
||||||
loggedIn = false
|
const response = await mockSas9Controller.logout()
|
||||||
|
|
||||||
const filePath = path.join(
|
|
||||||
process.cwd(),
|
|
||||||
'mocks',
|
|
||||||
'generic',
|
|
||||||
'sas9',
|
|
||||||
'logged-out'
|
|
||||||
)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const file = await readFile(filePath)
|
res.send(response.content)
|
||||||
|
|
||||||
res.send(file)
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
res.status(403).send(err.toString())
|
res.status(403).send(err.toString())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user