]>
Commit | Line | Data |
---|---|---|
1 | import * as request from 'supertest' | |
2 | ||
3 | import { ServerInfo } from '../server/servers' | |
4 | import { getClient } from '../server/clients' | |
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 | ||
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 | ||
40 | async function serverLogin (server: Server) { | |
41 | const res = await login(server.url, server.client, server.user, 200) | |
42 | ||
43 | return res.body.access_token as string | |
44 | } | |
45 | ||
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 | ||
64 | async function userLogin (server: Server, user: User, expectedStatus = 200) { | |
65 | const res = await login(server.url, server.client, user, expectedStatus) | |
66 | ||
67 | return res.body.access_token as string | |
68 | } | |
69 | ||
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 | ||
87 | function setAccessTokensToServers (servers: ServerInfo[]) { | |
88 | const tasks: Promise<any>[] = [] | |
89 | ||
90 | for (const server of servers) { | |
91 | const p = serverLogin(server).then(t => { server.accessToken = t }) | |
92 | tasks.push(p) | |
93 | } | |
94 | ||
95 | return Promise.all(tasks) | |
96 | } | |
97 | ||
98 | function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = 200) { | |
99 | const path = '/api/v1/users/token' | |
100 | ||
101 | const body = { | |
102 | client_id: server.client.id, | |
103 | client_secret: server.client.secret, | |
104 | username: username, | |
105 | response_type: 'code', | |
106 | grant_type: 'password', | |
107 | scope: 'upload', | |
108 | externalAuthToken | |
109 | } | |
110 | ||
111 | return request(server.url) | |
112 | .post(path) | |
113 | .type('form') | |
114 | .send(body) | |
115 | .expect(expectedStatus) | |
116 | } | |
117 | ||
118 | // --------------------------------------------------------------------------- | |
119 | ||
120 | export { | |
121 | login, | |
122 | logout, | |
123 | serverLogin, | |
124 | refreshToken, | |
125 | userLogin, | |
126 | getAccessToken, | |
127 | setAccessTokensToServers, | |
128 | Server, | |
129 | Client, | |
130 | User, | |
131 | loginUsingExternalToken | |
132 | } |