From 24342022ddfa1dceae4d85d9eaaaf35d2e568fc1 Mon Sep 17 00:00:00 2001 From: "Qumolama.d" Date: Thu, 5 May 2022 15:36:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.MD | 2 +- src/index.js | 58 +++--- tests/routings/authenticate.test.js | 262 ++++++++++++++++++++++++++++ 3 files changed, 298 insertions(+), 24 deletions(-) create mode 100644 tests/routings/authenticate.test.js diff --git a/README.MD b/README.MD index c1a1a6c..7fa3afb 100644 --- a/README.MD +++ b/README.MD @@ -28,7 +28,7 @@ - [ ] TGbot前端 - [ ] 单元测试 + [ ] API - - [ ] /authserver + - [x] /authserver - [ ] /sessionserver - [ ] /api - [ ] Advanced API diff --git a/src/index.js b/src/index.js index c689002..73670e0 100644 --- a/src/index.js +++ b/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 服务器已启动") -} \ No newline at end of file +} + +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() + } +}) \ No newline at end of file diff --git a/tests/routings/authenticate.test.js b/tests/routings/authenticate.test.js new file mode 100644 index 0000000..07aa16a --- /dev/null +++ b/tests/routings/authenticate.test.js @@ -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() +})