]>
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' |
0e1dc3e7 C |
5 | |
6 | type Client = { id: string, secret: string } | |
7 | type User = { username: string, password: string } | |
8 | type Server = { url: string, client: Client, user: User } | |
9 | ||
10 | function login (url: string, client: Client, user: User, expectedStatus = 200) { | |
11 | const path = '/api/v1/users/token' | |
12 | ||
13 | const body = { | |
14 | client_id: client.id, | |
15 | client_secret: client.secret, | |
16 | username: user.username, | |
17 | password: user.password, | |
18 | response_type: 'code', | |
19 | grant_type: 'password', | |
20 | scope: 'upload' | |
21 | } | |
22 | ||
23 | return request(url) | |
24 | .post(path) | |
25 | .type('form') | |
26 | .send(body) | |
27 | .expect(expectedStatus) | |
28 | } | |
29 | ||
7fed6375 C |
30 | function logout (url: string, token: string, expectedStatus = 200) { |
31 | const path = '/api/v1/users/revoke-token' | |
32 | ||
33 | return request(url) | |
34 | .post(path) | |
35 | .set('Authorization', 'Bearer ' + token) | |
36 | .type('form') | |
37 | .expect(expectedStatus) | |
38 | } | |
39 | ||
eec63bbc | 40 | async function serverLogin (server: Server) { |
0e1dc3e7 C |
41 | const res = await login(server.url, server.client, server.user, 200) |
42 | ||
43 | return res.body.access_token as string | |
44 | } | |
45 | ||
e307e4fc C |
46 | function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = 200) { |
47 | const path = '/api/v1/users/token' | |
48 | ||
49 | const body = { | |
50 | client_id: server.client.id, | |
51 | client_secret: server.client.secret, | |
52 | refresh_token: refreshToken, | |
53 | response_type: 'code', | |
54 | grant_type: 'refresh_token' | |
55 | } | |
56 | ||
57 | return request(server.url) | |
58 | .post(path) | |
59 | .type('form') | |
60 | .send(body) | |
61 | .expect(expectedStatus) | |
62 | } | |
63 | ||
f8b8c36b C |
64 | async function userLogin (server: Server, user: User, expectedStatus = 200) { |
65 | const res = await login(server.url, server.client, user, expectedStatus) | |
0e1dc3e7 C |
66 | |
67 | return res.body.access_token as string | |
68 | } | |
69 | ||
8d2be0ed C |
70 | async function getAccessToken (url: string, username: string, password: string) { |
71 | const resClient = await getClient(url) | |
72 | const client = { | |
73 | id: resClient.body.client_id, | |
74 | secret: resClient.body.client_secret | |
75 | } | |
76 | ||
77 | const user = { username, password } | |
78 | ||
79 | try { | |
80 | const res = await login(url, client, user) | |
81 | return res.body.access_token | |
82 | } catch (err) { | |
83 | throw new Error('Cannot authenticate. Please check your username/password.') | |
84 | } | |
85 | } | |
86 | ||
0e1dc3e7 C |
87 | function setAccessTokensToServers (servers: ServerInfo[]) { |
88 | const tasks: Promise<any>[] = [] | |
89 | ||
90 | for (const server of servers) { | |
a1587156 | 91 | const p = serverLogin(server).then(t => { server.accessToken = t }) |
0e1dc3e7 C |
92 | tasks.push(p) |
93 | } | |
94 | ||
95 | return Promise.all(tasks) | |
96 | } | |
97 | ||
98 | // --------------------------------------------------------------------------- | |
99 | ||
100 | export { | |
101 | login, | |
7fed6375 | 102 | logout, |
eec63bbc | 103 | serverLogin, |
e307e4fc | 104 | refreshToken, |
eec63bbc | 105 | userLogin, |
8d2be0ed | 106 | getAccessToken, |
c1e791ba RK |
107 | setAccessTokensToServers, |
108 | Server, | |
109 | Client, | |
110 | User | |
0e1dc3e7 | 111 | } |