mirror of
https://github.com/sasjs/server.git
synced 2026-01-07 06:30:06 +00:00
test(user): added specs for admin action to create user
This commit is contained in:
3
.github/workflows/build.yml
vendored
3
.github/workflows/build.yml
vendored
@@ -28,3 +28,6 @@ jobs:
|
|||||||
run: npm run package:lib
|
run: npm run package:lib
|
||||||
env:
|
env:
|
||||||
CI: true
|
CI: true
|
||||||
|
ACCESS_TOKEN_SECRET: ${{secrets.ACCESS_TOKEN_SECRET}}
|
||||||
|
REFRESH_TOKEN_SECRET: ${{secrets.REFRESH_TOKEN_SECRET}}
|
||||||
|
AUTH_CODE_SECRET: ${{secrets.AUTH_CODE_SECRET}}
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ authRouter.delete('/logout', (req, res) => {
|
|||||||
res.sendStatus(204)
|
res.sendStatus(204)
|
||||||
})
|
})
|
||||||
|
|
||||||
const generateAccessToken = (data: InfoJWT) =>
|
export const generateAccessToken = (data: InfoJWT) =>
|
||||||
jwt.sign(data, process.env.ACCESS_TOKEN_SECRET as string, {
|
jwt.sign(data, process.env.ACCESS_TOKEN_SECRET as string, {
|
||||||
expiresIn: '1day'
|
expiresIn: '1day'
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -11,13 +11,6 @@ const client = {
|
|||||||
clientid: 'someclientID',
|
clientid: 'someclientID',
|
||||||
clientsecret: 'someclientSecret'
|
clientsecret: 'someclientSecret'
|
||||||
}
|
}
|
||||||
// const adminUser = {
|
|
||||||
// displayname: 'Test Admin',
|
|
||||||
// username: 'testAdminUsername',
|
|
||||||
// password: '12345678',
|
|
||||||
// isadmin: true,
|
|
||||||
// isactive: true
|
|
||||||
// }
|
|
||||||
const user = {
|
const user = {
|
||||||
displayname: 'Test User',
|
displayname: 'Test User',
|
||||||
username: 'testUsername',
|
username: 'testUsername',
|
||||||
@@ -38,13 +31,9 @@ describe('auth', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
if (con) {
|
|
||||||
await con.connection.dropDatabase()
|
await con.connection.dropDatabase()
|
||||||
await con.connection.close()
|
await con.connection.close()
|
||||||
}
|
|
||||||
if (mongoServer) {
|
|
||||||
await mongoServer.stop()
|
await mongoServer.stop()
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('authorize', () => {
|
describe('authorize', () => {
|
||||||
|
|||||||
@@ -4,12 +4,20 @@ import { getTreeExample } from '../../../controllers/deploy'
|
|||||||
import { getTmpFilesFolderPath } from '../../../utils/file'
|
import { getTmpFilesFolderPath } from '../../../utils/file'
|
||||||
import { folderExists, fileExists, readFile, deleteFolder } from '@sasjs/utils'
|
import { folderExists, fileExists, readFile, deleteFolder } from '@sasjs/utils'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
|
import { generateAccessToken } from '../auth'
|
||||||
|
|
||||||
describe('files', () => {
|
describe('files', () => {
|
||||||
|
const accessToken = generateAccessToken({
|
||||||
|
client_id: 'someClientID',
|
||||||
|
username: 'username',
|
||||||
|
isadmin: false,
|
||||||
|
isactive: true
|
||||||
|
})
|
||||||
describe('deploy', () => {
|
describe('deploy', () => {
|
||||||
const shouldFailAssertion = async (payload: any) => {
|
const shouldFailAssertion = async (payload: any) => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.post('/SASjsApi/drive/deploy')
|
.post('/SASjsApi/drive/deploy')
|
||||||
|
.auth(accessToken, { type: 'bearer' })
|
||||||
.send(payload)
|
.send(payload)
|
||||||
|
|
||||||
expect(res.statusCode).toEqual(400)
|
expect(res.statusCode).toEqual(400)
|
||||||
@@ -79,6 +87,7 @@ describe('files', () => {
|
|||||||
it('should respond with payload example if valid payload was not provided', async () => {
|
it('should respond with payload example if valid payload was not provided', async () => {
|
||||||
const res = await request(app)
|
const res = await request(app)
|
||||||
.post('/SASjsApi/drive/deploy')
|
.post('/SASjsApi/drive/deploy')
|
||||||
|
.auth(accessToken, { type: 'bearer' })
|
||||||
.send({ fileTree: getTreeExample() })
|
.send({ fileTree: getTreeExample() })
|
||||||
|
|
||||||
expect(res.statusCode).toEqual(200)
|
expect(res.statusCode).toEqual(200)
|
||||||
|
|||||||
154
src/routes/api/spec/user.spec.ts
Normal file
154
src/routes/api/spec/user.spec.ts
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
import mongoose, { Mongoose } from 'mongoose'
|
||||||
|
import { MongoMemoryServer } from 'mongodb-memory-server'
|
||||||
|
import request from 'supertest'
|
||||||
|
import app from '../../../app'
|
||||||
|
import { createUser } from '../../../controllers/createUser'
|
||||||
|
import { createClient } from '../../../controllers/createClient'
|
||||||
|
import { generateAccessToken, populateClients } from '../auth'
|
||||||
|
import { InfoJWT } from '../../../types'
|
||||||
|
|
||||||
|
const client = {
|
||||||
|
clientid: 'someclientID',
|
||||||
|
clientsecret: 'someclientSecret'
|
||||||
|
}
|
||||||
|
const adminUser = {
|
||||||
|
displayname: 'Test Admin',
|
||||||
|
username: 'testAdminUsername',
|
||||||
|
password: '12345678',
|
||||||
|
isadmin: true,
|
||||||
|
isactive: true
|
||||||
|
}
|
||||||
|
const user = {
|
||||||
|
displayname: 'Test User',
|
||||||
|
username: 'testUsername',
|
||||||
|
password: '87654321',
|
||||||
|
isadmin: false,
|
||||||
|
isactive: true
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('user', () => {
|
||||||
|
let con: Mongoose
|
||||||
|
let mongoServer: MongoMemoryServer
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
mongoServer = await MongoMemoryServer.create()
|
||||||
|
con = await mongoose.connect(mongoServer.getUri())
|
||||||
|
})
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await con.connection.dropDatabase()
|
||||||
|
await con.connection.close()
|
||||||
|
await mongoServer.stop()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('create', () => {
|
||||||
|
const adminAccessToken = generateAccessToken({
|
||||||
|
client_id: client.clientid,
|
||||||
|
username: adminUser.username,
|
||||||
|
isadmin: adminUser.isadmin,
|
||||||
|
isactive: adminUser.isactive
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
const collections = mongoose.connection.collections
|
||||||
|
const collection = collections['users']
|
||||||
|
await collection.deleteMany({})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respond with new user', async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/SASjsApi/user')
|
||||||
|
.auth(adminAccessToken, { type: 'bearer' })
|
||||||
|
.send(user)
|
||||||
|
.expect(200)
|
||||||
|
|
||||||
|
expect(res.body.username).toEqual(user.username)
|
||||||
|
expect(res.body.displayname).toEqual(user.displayname)
|
||||||
|
expect(res.body.isadmin).toEqual(user.isadmin)
|
||||||
|
expect(res.body.isactive).toEqual(user.isactive)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respond with Unauthorized if access token is not present', async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/SASjsApi/user')
|
||||||
|
.send(user)
|
||||||
|
.expect(401)
|
||||||
|
|
||||||
|
expect(res.text).toEqual('Unauthorized')
|
||||||
|
expect(res.body).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respond with Forbideen if access token is not of an admin account', async () => {
|
||||||
|
const accessToken = generateAccessToken({
|
||||||
|
client_id: client.clientid,
|
||||||
|
username: user.username,
|
||||||
|
isadmin: user.isadmin,
|
||||||
|
isactive: user.isactive
|
||||||
|
})
|
||||||
|
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/SASjsApi/user')
|
||||||
|
.auth(accessToken, { type: 'bearer' })
|
||||||
|
.send(user)
|
||||||
|
.expect(403)
|
||||||
|
|
||||||
|
expect(res.text).toEqual('Admin account required')
|
||||||
|
expect(res.body).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respond with Forbidden if username is already present', async () => {
|
||||||
|
await createUser(user)
|
||||||
|
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/SASjsApi/user')
|
||||||
|
.auth(adminAccessToken, { type: 'bearer' })
|
||||||
|
.send(user)
|
||||||
|
.expect(403)
|
||||||
|
|
||||||
|
expect(res.text).toEqual('Error: Username already exists.')
|
||||||
|
expect(res.body).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respond with Bad Request if username is missing', async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/SASjsApi/user')
|
||||||
|
.auth(adminAccessToken, { type: 'bearer' })
|
||||||
|
.send({
|
||||||
|
...user,
|
||||||
|
username: undefined
|
||||||
|
})
|
||||||
|
.expect(400)
|
||||||
|
|
||||||
|
expect(res.text).toEqual(`"username" is required`)
|
||||||
|
expect(res.body).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respond with Bad Request if password is missing', async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/SASjsApi/user')
|
||||||
|
.auth(adminAccessToken, { type: 'bearer' })
|
||||||
|
.send({
|
||||||
|
...user,
|
||||||
|
password: undefined
|
||||||
|
})
|
||||||
|
.expect(400)
|
||||||
|
|
||||||
|
expect(res.text).toEqual(`"password" is required`)
|
||||||
|
expect(res.body).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respond with Bad Request if displayname is missing', async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/SASjsApi/user')
|
||||||
|
.auth(adminAccessToken, { type: 'bearer' })
|
||||||
|
.send({
|
||||||
|
...user,
|
||||||
|
displayname: undefined
|
||||||
|
})
|
||||||
|
.expect(400)
|
||||||
|
|
||||||
|
expect(res.text).toEqual(`"displayname" is required`)
|
||||||
|
expect(res.body).toEqual({})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -16,8 +16,8 @@ userRouter.post('/', async (req, res) => {
|
|||||||
isadmin: savedUser.isadmin,
|
isadmin: savedUser.isadmin,
|
||||||
isactive: savedUser.isactive
|
isactive: savedUser.isactive
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
res.status(400).send(err)
|
res.status(403).send(err.toString())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user