添加单元测试
This commit is contained in:
parent
bd8b866f07
commit
24342022dd
|
@ -28,7 +28,7 @@
|
|||
- [ ] TGbot前端
|
||||
- [ ] 单元测试
|
||||
+ [ ] API
|
||||
- [ ] /authserver
|
||||
- [x] /authserver
|
||||
- [ ] /sessionserver
|
||||
- [ ] /api
|
||||
- [ ] Advanced API
|
||||
|
|
58
src/index.js
58
src/index.js
|
@ -5,30 +5,17 @@ import * as Hooks from './hooks.js'
|
|||
import * as AuthenticateRoutings from './routes/authenticate.js'
|
||||
import { config } from './config.js'
|
||||
|
||||
console.log(`
|
||||
================================================================
|
||||
__ _____ ______ __ __ _ __
|
||||
/ / / ___// __ \\ \\/ /___ _____ _____/ /________ ______(_) /
|
||||
/ / \\__ \\/ /_/ /\\ / __ \`/ __ \`/ __ / ___/ __ \`/ ___/ / /
|
||||
/ /______/ / ____/ / / /_/ / /_/ / /_/ / / / /_/ (__ ) / /
|
||||
/_____/____/_/ /_/\\__, /\\__, /\\__,_/_/ \\__,_/____/_/_/
|
||||
/____//____/
|
||||
================================================================\n`)
|
||||
|
||||
if(typeof PROGRAM_PRODUCTION === 'undefined') {
|
||||
console.warn("⚠ 警告: 您运行的不是正式版本,可能会不稳定,仅限开发环境运行!\n")
|
||||
}
|
||||
|
||||
export const server = fastify({
|
||||
logger: {
|
||||
prettyPrint: true
|
||||
prettyPrint: true,
|
||||
level: 'error'
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
//export const logger = server.log
|
||||
|
||||
;(async () => {
|
||||
export const setup = async () => {
|
||||
const mongooseClient = await mongoose.connect(config.database.url)
|
||||
const models = registerModels(mongooseClient)
|
||||
|
||||
|
@ -64,14 +51,39 @@ export const server = fastify({
|
|||
permissions: [{ node: 'login', allowed: true, duration: 0, eternal: true, startDate: Date.now(), highPriority: false }]
|
||||
}).save()
|
||||
*/
|
||||
}
|
||||
|
||||
if(!process.env["UNIT_TEST"]) {
|
||||
await launch(config)
|
||||
}
|
||||
})()
|
||||
|
||||
const launch = async (config) => {
|
||||
const launch = async () => {
|
||||
process.on('SIGINT', () => {
|
||||
new Promise(shutdown)
|
||||
})
|
||||
|
||||
await server.listen(config.server.port, config.server.url)
|
||||
|
||||
server.log.info("老色批世界树 > 基于 fastify 的高性能 HTTP 服务器已启动")
|
||||
}
|
||||
}
|
||||
|
||||
export const shutdown = async () => {
|
||||
server.close()
|
||||
mongoose.disconnect()
|
||||
}
|
||||
|
||||
(async () => {
|
||||
if(!process.env["UNIT_TEST"]) {
|
||||
console.log(`
|
||||
================================================================
|
||||
__ _____ ______ __ __ _ __
|
||||
/ / / ___// __ \\ \\/ /___ _____ _____/ /________ ______(_) /
|
||||
/ / \\__ \\/ /_/ /\\ / __ \`/ __ \`/ __ / ___/ __ \`/ ___/ / /
|
||||
/ /______/ / ____/ / / /_/ / /_/ / /_/ / / / /_/ (__ ) / /
|
||||
/_____/____/_/ /_/\\__, /\\__, /\\__,_/_/ \\__,_/____/_/_/
|
||||
/____//____/
|
||||
================================================================\n`)
|
||||
|
||||
if(typeof PROGRAM_PRODUCTION === 'undefined') {
|
||||
console.warn("⚠ 警告: 您运行的不是正式版本,可能会不稳定,仅限开发环境运行!\n")
|
||||
}
|
||||
await setup()
|
||||
await launch()
|
||||
}
|
||||
})
|
|
@ -0,0 +1,262 @@
|
|||
import { server, setup, shutdown } from '../../src/index.js'
|
||||
|
||||
beforeAll(() => {
|
||||
return setup()
|
||||
})
|
||||
|
||||
const login = async () => {
|
||||
const { accessToken, clientToken, selectedProfile, user } = JSON.parse((await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/authenticate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
username: 'i@lama.icu',
|
||||
password: '123456',
|
||||
clientToken: 'UNIT_TEST',
|
||||
requestUser: true,
|
||||
agent: {
|
||||
name: 'minecraft',
|
||||
version: 1
|
||||
}
|
||||
}
|
||||
})).body)
|
||||
|
||||
return { accessToken, clientToken, selectedProfile, user }
|
||||
}
|
||||
|
||||
test('/authserver/authenticate', async function() {
|
||||
const response = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/authenticate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
username: 'i@lama.icu',
|
||||
password: '123456',
|
||||
clientToken: 'UNIT_TEST',
|
||||
requestUser: true,
|
||||
agent: {
|
||||
name: 'minecraft',
|
||||
version: 1
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
expect(response.statusCode).toBe(200)
|
||||
})
|
||||
|
||||
test('/authserver/refresh', async function() {
|
||||
const credentals = await login()
|
||||
const refresh1 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/refresh',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals.accessToken,
|
||||
clientToken: credentals.clientToken,
|
||||
}
|
||||
})
|
||||
|
||||
const newToken = JSON.parse(refresh1.body).accessToken
|
||||
|
||||
expect(refresh1.statusCode).toBe(200)
|
||||
|
||||
const refresh2 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/refresh',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals.accessToken,
|
||||
clientToken: credentals.clientToken,
|
||||
}
|
||||
})
|
||||
|
||||
expect(refresh2.statusCode).toBe(401)
|
||||
|
||||
const refresh3 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/refresh',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals.accessToken,
|
||||
clientToken: Math.random() + "",
|
||||
}
|
||||
})
|
||||
|
||||
expect(refresh3.statusCode).toBe(401)
|
||||
|
||||
const refresh4 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/refresh',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: newToken,
|
||||
}
|
||||
})
|
||||
|
||||
expect(refresh4.statusCode).toBe(200)
|
||||
})
|
||||
|
||||
test('/authserver/validate', async function() {
|
||||
const credentals = await login()
|
||||
const validate1 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/validate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals.accessToken,
|
||||
clientToken: credentals.clientToken,
|
||||
}
|
||||
})
|
||||
|
||||
expect(validate1.statusCode).toBe(204)
|
||||
|
||||
const validate2 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/validate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals.accessToken,
|
||||
clientToken: credentals.clientToken + "hjfidhsw",
|
||||
}
|
||||
})
|
||||
|
||||
expect(validate2.statusCode).toBe(401)
|
||||
|
||||
const validate3 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/validate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals.accessToken + "hjfidhsw",
|
||||
clientToken: credentals.clientToken,
|
||||
}
|
||||
})
|
||||
|
||||
expect(validate3.statusCode).toBe(401)
|
||||
|
||||
const validate4 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/validate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals.accessToken,
|
||||
}
|
||||
})
|
||||
|
||||
expect(validate4.statusCode).toBe(204)
|
||||
})
|
||||
|
||||
test('/authserver/invalidate', async function() {
|
||||
let credentals = await login()
|
||||
const invalidate1 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/invalidate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals.accessToken,
|
||||
clientToken: credentals.clientToken,
|
||||
}
|
||||
})
|
||||
|
||||
expect(invalidate1.statusCode).toBe(204)
|
||||
|
||||
const credentals2 = await login()
|
||||
const invalidate2 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/validate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals2.accessToken,
|
||||
clientToken: credentals2.clientToken + "hjfidhsw",
|
||||
}
|
||||
})
|
||||
|
||||
expect(invalidate2.statusCode).toBe(401)
|
||||
|
||||
const validate = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/validate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals.accessToken,
|
||||
clientToken: credentals.clientToken,
|
||||
}
|
||||
})
|
||||
|
||||
expect(validate.statusCode).toBe(401)
|
||||
})
|
||||
|
||||
test('/authserver/signout', async function() {
|
||||
const credentals1 = await login()
|
||||
const credentals2 = await login()
|
||||
const signout = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/signout',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
username: 'i@lama.icu',
|
||||
password: '123456',
|
||||
}
|
||||
})
|
||||
|
||||
expect(signout.statusCode).toBe(204)
|
||||
|
||||
const validate1 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/validate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals1.accessToken,
|
||||
clientToken: credentals1.clientToken,
|
||||
}
|
||||
})
|
||||
|
||||
const validate2 = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/authserver/validate',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
payload: {
|
||||
accessToken: credentals2.accessToken,
|
||||
clientToken: credentals2.clientToken,
|
||||
}
|
||||
})
|
||||
|
||||
expect(validate1.statusCode).toBe(401)
|
||||
expect(validate2.statusCode).toBe(401)
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
return shutdown()
|
||||
})
|
Loading…
Reference in New Issue