]>
Commit | Line | Data |
---|---|---|
1 | import * as request from 'supertest' | |
2 | ||
3 | import { ServerInfo } from '../server/servers' | |
4 | import { getClient } from '../server/clients' | |
5 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
6 | ||
7 | type Client = { id: string, secret: string } | |
8 | type User = { username: string, password: string } | |
9 | type Server = { url: string, client: Client, user: User } | |
10 | ||
11 | function login (url: string, client: Client, user: User, expectedStatus = HttpStatusCode.OK_200) { | |
12 | const path = '/api/v1/users/token' | |
13 | ||
14 | const body = { | |
15 | client_id: client.id, | |
16 | client_secret: client.secret, | |
17 | username: user.username, | |
18 | password: user.password, | |
19 | response_type: 'code', | |
20 | grant_type: 'password', | |
21 | scope: 'upload' | |
22 | } | |
23 | ||
24 | return request(url) | |
25 | .post(path) | |
26 | .type('form') | |
27 | .send(body) | |
28 | .expect(expectedStatus) | |
29 | } | |
30 | ||
31 | function logout (url: string, token: string, expectedStatus = HttpStatusCode.OK_200) { | |
32 | const path = '/api/v1/users/revoke-token' | |
33 | ||
34 | return request(url) | |
35 | .post(path) | |
36 | .set('Authorization', 'Bearer ' + token) | |
37 | .type('form') | |
38 | .expect(expectedStatus) | |
39 | } | |
40 | ||
41 | async function serverLogin (server: Server) { | |
42 | const res = await login(server.url, server.client, server.user, HttpStatusCode.OK_200) | |
43 | ||
44 | return res.body.access_token as string | |
45 | } | |
46 | ||
47 | function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = HttpStatusCode.OK_200) { | |
48 | const path = '/api/v1/users/token' | |
49 | ||
50 | const body = { | |
51 | client_id: server.client.id, | |
52 | client_secret: server.client.secret, | |
53 | refresh_token: refreshToken, | |
54 | response_type: 'code', | |
55 | grant_type: 'refresh_token' | |
56 | } | |
57 | ||
58 | return request(server.url) | |
59 | .post(path) | |
60 | .type('form') | |
61 | .send(body) | |
62 | .expect(expectedStatus) | |
63 | } | |
64 | ||
65 | async function userLogin (server: Server, user: User, expectedStatus = HttpStatusCode.OK_200) { | |
66 | const res = await login(server.url, server.client, user, expectedStatus) | |
67 | ||
68 | return res.body.access_token as string | |
69 | } | |
70 | ||
71 | async function getAccessToken (url: string, username: string, password: string) { | |
72 | const resClient = await getClient(url) | |
73 | const client = { | |
74 | id: resClient.body.client_id, | |
75 | secret: resClient.body.client_secret | |
76 | } | |
77 | ||
78 | const user = { username, password } | |
79 | ||
80 | try { | |
81 | const res = await login(url, client, user) | |
82 | return res.body.access_token | |
83 | } catch (err) { | |
84 | throw new Error('Cannot authenticate. Please check your username/password.') | |
85 | } | |
86 | } | |
87 | ||
88 | function setAccessTokensToServers (servers: ServerInfo[]) { | |
89 | const tasks: Promise<any>[] = [] | |
90 | ||
91 | for (const server of servers) { | |
92 | const p = serverLogin(server).then(t => { server.accessToken = t }) | |
93 | tasks.push(p) | |
94 | } | |
95 | ||
96 | return Promise.all(tasks) | |
97 | } | |
98 | ||
99 | function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = HttpStatusCode.OK_200) { | |
100 | const path = '/api/v1/users/token' | |
101 | ||
102 | const body = { | |
103 | client_id: server.client.id, | |
104 | client_secret: server.client.secret, | |
105 | username: username, | |
106 | response_type: 'code', | |
107 | grant_type: 'password', | |
108 | scope: 'upload', | |
109 | externalAuthToken | |
110 | } | |
111 | ||
112 | return request(server.url) | |
113 | .post(path) | |
114 | .type('form') | |
115 | .send(body) | |
116 | .expect(expectedStatus) | |
117 | } | |
118 | ||
119 | // --------------------------------------------------------------------------- | |
120 | ||
121 | export { | |
122 | login, | |
123 | logout, | |
124 | serverLogin, | |
125 | refreshToken, | |
126 | userLogin, | |
127 | getAccessToken, | |
128 | setAccessTokensToServers, | |
129 | Server, | |
130 | Client, | |
131 | User, | |
132 | loginUsingExternalToken | |
133 | } |