]>
Commit | Line | Data |
---|---|---|
0e1dc3e7 C |
1 | import * as request from 'supertest' |
2 | ||
c5d31dba | 3 | import { ServerInfo } from '../server/servers' |
8d2be0ed | 4 | import { getClient } from '../server/clients' |
f2eb23cd | 5 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
0e1dc3e7 C |
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 | ||
f2eb23cd | 11 | function login (url: string, client: Client, user: User, expectedStatus = HttpStatusCode.OK_200) { |
0e1dc3e7 C |
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 | ||
f2eb23cd | 31 | function logout (url: string, token: string, expectedStatus = HttpStatusCode.OK_200) { |
7fed6375 C |
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 | ||
eec63bbc | 41 | async function serverLogin (server: Server) { |
f2eb23cd | 42 | const res = await login(server.url, server.client, server.user, HttpStatusCode.OK_200) |
0e1dc3e7 C |
43 | |
44 | return res.body.access_token as string | |
45 | } | |
46 | ||
f2eb23cd | 47 | function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = HttpStatusCode.OK_200) { |
e307e4fc C |
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 | ||
f2eb23cd | 65 | async function userLogin (server: Server, user: User, expectedStatus = HttpStatusCode.OK_200) { |
f8b8c36b | 66 | const res = await login(server.url, server.client, user, expectedStatus) |
0e1dc3e7 C |
67 | |
68 | return res.body.access_token as string | |
69 | } | |
70 | ||
8d2be0ed C |
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 | ||
0e1dc3e7 C |
88 | function setAccessTokensToServers (servers: ServerInfo[]) { |
89 | const tasks: Promise<any>[] = [] | |
90 | ||
91 | for (const server of servers) { | |
a1587156 | 92 | const p = serverLogin(server).then(t => { server.accessToken = t }) |
0e1dc3e7 C |
93 | tasks.push(p) |
94 | } | |
95 | ||
96 | return Promise.all(tasks) | |
97 | } | |
98 | ||
f2eb23cd | 99 | function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = HttpStatusCode.OK_200) { |
9107d791 C |
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 | ||
0e1dc3e7 C |
119 | // --------------------------------------------------------------------------- |
120 | ||
121 | export { | |
122 | login, | |
7fed6375 | 123 | logout, |
eec63bbc | 124 | serverLogin, |
e307e4fc | 125 | refreshToken, |
eec63bbc | 126 | userLogin, |
8d2be0ed | 127 | getAccessToken, |
c1e791ba RK |
128 | setAccessTokensToServers, |
129 | Server, | |
130 | Client, | |
9107d791 C |
131 | User, |
132 | loginUsingExternalToken | |
0e1dc3e7 | 133 | } |