test: 💍 Refactoring and tests
This commit is contained in:
parent
c40fe8e073
commit
6bbbfa6a89
|
@ -2,6 +2,8 @@ const OpenAPIBackend = require('openapi-backend').default
|
|||
const express = require('express')
|
||||
const jwt = require('jsonwebtoken')
|
||||
const backend = require('./lib/backend')
|
||||
const { deconstruct } = require('./lib/credentials')
|
||||
const { inspect } = require('util')
|
||||
|
||||
const app = express()
|
||||
app.use(express.json())
|
||||
|
@ -21,30 +23,38 @@ api.register({
|
|||
|
||||
// register security handler for jwt auth
|
||||
api.registerSecurityHandler('bearerAuth', (c, req, res) => {
|
||||
const authHeader = c.request.headers['authorization']
|
||||
if (!authHeader) {
|
||||
const { authorization } = deconstruct(c)
|
||||
if (!authorization) {
|
||||
throw new Error('Missing authorization header')
|
||||
}
|
||||
const token = authHeader.replace('Bearer ', '')
|
||||
return jwt.verify(token, process.env.JWT_SECRET || 'secret')
|
||||
return jwt.verify(authorization, process.env.JWT_SECRET || 'secret')
|
||||
})
|
||||
|
||||
// register operation handlers
|
||||
api.register({
|
||||
login: async (c, req, res) => {
|
||||
console.log('login initiated')
|
||||
const token = await backend.login(c.request.query.socialSecurityNumber)
|
||||
return res.status(200).json(token)
|
||||
try {
|
||||
console.log('login initiated')
|
||||
const { socialSecurityNumber } = deconstruct(c)
|
||||
const token = await backend.login(socialSecurityNumber)
|
||||
return res.status(200).json(token)
|
||||
} catch (err) {
|
||||
return res.status(500).json({ message: err.message, stack: err.stack })
|
||||
}
|
||||
},
|
||||
waitForToken: async (c, req, res) => {
|
||||
const order = c.request.params.order
|
||||
console.log('wait for token')
|
||||
const { order } = deconstruct(c)
|
||||
|
||||
const cookie = await backend.waitForToken({order})
|
||||
const jwtToken = jwt.sign(cookie, process.env.JWT_SECRET || 'secret')
|
||||
console.log('login succeeded')
|
||||
return res.status(200).json(jwtToken)
|
||||
},
|
||||
getChildren: async (c, req, res) => {
|
||||
const cookie = c.security.bearerAuth
|
||||
console.log('get children')
|
||||
const { cookie } = deconstruct(c)
|
||||
|
||||
try {
|
||||
const children = await backend.getChildren(cookie)
|
||||
return res.status(200).json(children)
|
||||
|
@ -53,8 +63,7 @@ api.register({
|
|||
}
|
||||
},
|
||||
getChildById: async (c, req, res) => {
|
||||
const cookie = c.security.bearerAuth
|
||||
const childId = c.request.params.childId
|
||||
const { cookie, childId } = deconstruct(c)
|
||||
try {
|
||||
const child = await backend.getChildById(childId, cookie)
|
||||
return res.status(200).json(child)
|
||||
|
@ -63,44 +72,37 @@ api.register({
|
|||
}
|
||||
},
|
||||
getNews: async (c, req, res) => {
|
||||
const cookie = c.security.bearerAuth
|
||||
const childId = c.request.params.childId
|
||||
const { cookie, childId } = deconstruct(c)
|
||||
const news = await backend.getNews(childId, cookie)
|
||||
return res.status(200).json(news)
|
||||
},
|
||||
getCalendar: async (c, req, res) => {
|
||||
const cookie = c.security.bearerAuth
|
||||
const childId = c.request.params.childId
|
||||
const { cookie, childId } = deconstruct(c)
|
||||
const calendar = await backend.getCalendar(childId, cookie)
|
||||
return res.status(200).json(calendar)
|
||||
},
|
||||
getNotifications: async (c, req, res) => {
|
||||
const cookie = c.security.bearerAuth
|
||||
const childId = c.request.params.childSdsId
|
||||
const notifications = await backend.getNotifications(childId, cookie)
|
||||
const { cookie, childSdsId } = deconstruct(c)
|
||||
const notifications = await backend.getNotifications(childSdsId, cookie)
|
||||
return res.status(200).json(notifications)
|
||||
},
|
||||
getMenu: async (c, req, res) => {
|
||||
const cookie = c.security.bearerAuth
|
||||
const childId = c.request.params.childId
|
||||
const { cookie, childId } = deconstruct(c)
|
||||
const menu = await backend.getMenu(childId, cookie)
|
||||
return res.status(200).json(menu)
|
||||
},
|
||||
getSchedule: async (c, req, res) => {
|
||||
const cookie = c.security.bearerAuth
|
||||
const childId = c.request.params.childSdsId
|
||||
const schedule = await backend.getSchedule(childId, cookie)
|
||||
const { cookie, childSdsId } = deconstruct(c)
|
||||
const schedule = await backend.getSchedule(childSdsId, cookie)
|
||||
return res.status(200).json(schedule)
|
||||
},
|
||||
getClassmates: async (c, req, res) => {
|
||||
const cookie = c.security.bearerAuth
|
||||
const childId = c.request.params.childSdsId
|
||||
const classmates = await backend.getClassmates(childId, cookie)
|
||||
const { cookie, childSdsId } = deconstruct(c)
|
||||
const classmates = await backend.getClassmates(childSdsId, cookie)
|
||||
return res.status(200).json(classmates)
|
||||
},
|
||||
download: async (c, req, res) => {
|
||||
const cookie = c.security.bearerAuth
|
||||
const url = c.request.query.url
|
||||
const { cookie, url } = deconstruct(c)
|
||||
const stream = await backend.download(url, cookie)
|
||||
stream.body.pipe(res.body)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
const deconstruct = (c) => {
|
||||
const result = {}
|
||||
if (c.security) {
|
||||
result.cookie = c.security.bearerAuth
|
||||
}
|
||||
if (c.request.headers) {
|
||||
if (c.request.headers.authorization) {
|
||||
result.authorization = c.request.headers.authorization.replace('Bearer ', '')
|
||||
}
|
||||
}
|
||||
if (c.request.params) {
|
||||
result.order = c.request.params.order
|
||||
result.childId = c.request.params.childId
|
||||
result.childSdsId = c.request.params.childSdsId
|
||||
}
|
||||
if (c.request.query) {
|
||||
result.socialSecurityNumber = c.request.query.socialSecurityNumber
|
||||
result.url = c.request.query.url
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
deconstruct
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
const { deconstruct } = require('./credentials')
|
||||
|
||||
describe('credentials', () => {
|
||||
describe('deconstruct', () => {
|
||||
let c
|
||||
beforeEach(() => {
|
||||
c = {
|
||||
request: {
|
||||
headers: {
|
||||
authorization: 'Bearer authorization'
|
||||
},
|
||||
params: {
|
||||
order: 'abc-123',
|
||||
childId: 'childId',
|
||||
childSdsId: 'childSdsId'
|
||||
},
|
||||
query: {
|
||||
socialSecurityNumber: '200001019999',
|
||||
url: 'https://google.com'
|
||||
},
|
||||
},
|
||||
security: {
|
||||
bearerAuth: 'bearerAuth'
|
||||
}
|
||||
}
|
||||
})
|
||||
it('returns all expected values', () => {
|
||||
expect(deconstruct(c)).toEqual({
|
||||
authorization: 'authorization',
|
||||
order: 'abc-123',
|
||||
childId: 'childId',
|
||||
childSdsId: 'childSdsId',
|
||||
socialSecurityNumber: '200001019999',
|
||||
cookie: 'bearerAuth',
|
||||
url: 'https://google.com'
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
File diff suppressed because it is too large
Load Diff
|
@ -17,14 +17,18 @@
|
|||
"openapi-backend": "^3.6.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^26.0.19",
|
||||
"eslint": "^7.14.0",
|
||||
"eslint-config-standard": "^16.0.2",
|
||||
"eslint-plugin-import": "^2.22.1",
|
||||
"eslint-plugin-node": "^11.1.0",
|
||||
"eslint-plugin-promise": "^4.2.1"
|
||||
"eslint-plugin-promise": "^4.2.1",
|
||||
"jest": "^26.6.3",
|
||||
"nodemon": "^2.0.6"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"dev": "nodemon",
|
||||
"test": "jest"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC"
|
||||
|
|
Loading…
Reference in New Issue