]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const request = require('supertest') | |
4 | ||
5 | const loginUtils = { | |
6 | login: login, | |
7 | loginAndGetAccessToken: loginAndGetAccessToken | |
8 | } | |
9 | ||
10 | // ---------------------- Export functions -------------------- | |
11 | ||
12 | function login (url, client, user, expectedStatus, end) { | |
13 | if (!end) { | |
14 | end = expectedStatus | |
15 | expectedStatus = 200 | |
16 | } | |
17 | ||
18 | const path = '/api/v1/users/token' | |
19 | ||
20 | const body = { | |
21 | client_id: client.id, | |
22 | client_secret: client.secret, | |
23 | username: user.username, | |
24 | password: user.password, | |
25 | response_type: 'code', | |
26 | grant_type: 'password', | |
27 | scope: 'upload' | |
28 | } | |
29 | ||
30 | request(url) | |
31 | .post(path) | |
32 | .type('form') | |
33 | .send(body) | |
34 | .expect(expectedStatus) | |
35 | .end(end) | |
36 | } | |
37 | ||
38 | function loginAndGetAccessToken (server, callback) { | |
39 | login(server.url, server.client, server.user, 200, function (err, res) { | |
40 | if (err) return callback(err) | |
41 | ||
42 | return callback(null, res.body.access_token) | |
43 | }) | |
44 | } | |
45 | ||
46 | // --------------------------------------------------------------------------- | |
47 | ||
48 | module.exports = loginUtils |