diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/api/server/debug.ts | 25 | ||||
-rw-r--r-- | server/controllers/api/server/index.ts | 2 | ||||
-rw-r--r-- | server/tests/api/check-params/debug.ts | 78 | ||||
-rw-r--r-- | server/tests/api/check-params/index.ts | 1 |
4 files changed, 106 insertions, 0 deletions
diff --git a/server/controllers/api/server/debug.ts b/server/controllers/api/server/debug.ts new file mode 100644 index 000000000..4450038f6 --- /dev/null +++ b/server/controllers/api/server/debug.ts | |||
@@ -0,0 +1,25 @@ | |||
1 | import * as express from 'express' | ||
2 | import { UserRight } from '../../../../shared/models/users' | ||
3 | import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares' | ||
4 | |||
5 | const debugRouter = express.Router() | ||
6 | |||
7 | debugRouter.get('/debug', | ||
8 | authenticate, | ||
9 | ensureUserHasRight(UserRight.MANAGE_DEBUG), | ||
10 | asyncMiddleware(getDebug) | ||
11 | ) | ||
12 | |||
13 | // --------------------------------------------------------------------------- | ||
14 | |||
15 | export { | ||
16 | debugRouter | ||
17 | } | ||
18 | |||
19 | // --------------------------------------------------------------------------- | ||
20 | |||
21 | async function getDebug (req: express.Request, res: express.Response) { | ||
22 | return res.json({ | ||
23 | ip: req.ip | ||
24 | }).end() | ||
25 | } | ||
diff --git a/server/controllers/api/server/index.ts b/server/controllers/api/server/index.ts index de09588df..6b8793a19 100644 --- a/server/controllers/api/server/index.ts +++ b/server/controllers/api/server/index.ts | |||
@@ -5,6 +5,7 @@ import { serverRedundancyRouter } from './redundancy' | |||
5 | import { serverBlocklistRouter } from './server-blocklist' | 5 | import { serverBlocklistRouter } from './server-blocklist' |
6 | import { contactRouter } from './contact' | 6 | import { contactRouter } from './contact' |
7 | import { logsRouter } from './logs' | 7 | import { logsRouter } from './logs' |
8 | import { debugRouter } from './debug' | ||
8 | 9 | ||
9 | const serverRouter = express.Router() | 10 | const serverRouter = express.Router() |
10 | 11 | ||
@@ -14,6 +15,7 @@ serverRouter.use('/', statsRouter) | |||
14 | serverRouter.use('/', serverBlocklistRouter) | 15 | serverRouter.use('/', serverBlocklistRouter) |
15 | serverRouter.use('/', contactRouter) | 16 | serverRouter.use('/', contactRouter) |
16 | serverRouter.use('/', logsRouter) | 17 | serverRouter.use('/', logsRouter) |
18 | serverRouter.use('/', debugRouter) | ||
17 | 19 | ||
18 | // --------------------------------------------------------------------------- | 20 | // --------------------------------------------------------------------------- |
19 | 21 | ||
diff --git a/server/tests/api/check-params/debug.ts b/server/tests/api/check-params/debug.ts new file mode 100644 index 000000000..9bf664657 --- /dev/null +++ b/server/tests/api/check-params/debug.ts | |||
@@ -0,0 +1,78 @@ | |||
1 | /* tslint:disable:no-unused-expression */ | ||
2 | |||
3 | import 'mocha' | ||
4 | |||
5 | import { | ||
6 | createUser, | ||
7 | flushTests, | ||
8 | killallServers, | ||
9 | runServer, | ||
10 | ServerInfo, | ||
11 | setAccessTokensToServers, | ||
12 | userLogin | ||
13 | } from '../../../../shared/utils' | ||
14 | import { makeGetRequest } from '../../../../shared/utils/requests/requests' | ||
15 | |||
16 | describe('Test debug API validators', function () { | ||
17 | const path = '/api/v1/server/debug' | ||
18 | let server: ServerInfo | ||
19 | let userAccessToken = '' | ||
20 | |||
21 | // --------------------------------------------------------------- | ||
22 | |||
23 | before(async function () { | ||
24 | this.timeout(120000) | ||
25 | |||
26 | await flushTests() | ||
27 | |||
28 | server = await runServer(1) | ||
29 | |||
30 | await setAccessTokensToServers([ server ]) | ||
31 | |||
32 | const user = { | ||
33 | username: 'user1', | ||
34 | password: 'my super password' | ||
35 | } | ||
36 | await createUser(server.url, server.accessToken, user.username, user.password) | ||
37 | userAccessToken = await userLogin(server, user) | ||
38 | }) | ||
39 | |||
40 | describe('When getting debug endpoint', function () { | ||
41 | |||
42 | it('Should fail with a non authenticated user', async function () { | ||
43 | await makeGetRequest({ | ||
44 | url: server.url, | ||
45 | path, | ||
46 | statusCodeExpected: 401 | ||
47 | }) | ||
48 | }) | ||
49 | |||
50 | it('Should fail with a non admin user', async function () { | ||
51 | await makeGetRequest({ | ||
52 | url: server.url, | ||
53 | path, | ||
54 | token: userAccessToken, | ||
55 | statusCodeExpected: 403 | ||
56 | }) | ||
57 | }) | ||
58 | |||
59 | it('Should succeed with the correct params', async function () { | ||
60 | await makeGetRequest({ | ||
61 | url: server.url, | ||
62 | path, | ||
63 | token: server.accessToken, | ||
64 | query: { startDate: new Date().toISOString() }, | ||
65 | statusCodeExpected: 200 | ||
66 | }) | ||
67 | }) | ||
68 | }) | ||
69 | |||
70 | after(async function () { | ||
71 | killallServers([ server ]) | ||
72 | |||
73 | // Keep the logs if the test failed | ||
74 | if (this['ok']) { | ||
75 | await flushTests() | ||
76 | } | ||
77 | }) | ||
78 | }) | ||
diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts index bdac95025..844fa31c5 100644 --- a/server/tests/api/check-params/index.ts +++ b/server/tests/api/check-params/index.ts | |||
@@ -2,6 +2,7 @@ import './accounts' | |||
2 | import './blocklist' | 2 | import './blocklist' |
3 | import './config' | 3 | import './config' |
4 | import './contact-form' | 4 | import './contact-form' |
5 | import './debug' | ||
5 | import './follows' | 6 | import './follows' |
6 | import './jobs' | 7 | import './jobs' |
7 | import './logs' | 8 | import './logs' |