aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils/login.js
blob: c984c0baf29c663d6b636c118f747ddbc49e2897 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict'

const request = require('supertest')

const loginUtils = {
  login,
  loginAndGetAccessToken,
  getUserAccessToken
}

// ---------------------- Export functions --------------------

function login (url, client, user, expectedStatus, end) {
  if (!end) {
    end = expectedStatus
    expectedStatus = 200
  }

  const path = '/api/v1/users/token'

  const body = {
    client_id: client.id,
    client_secret: client.secret,
    username: user.username,
    password: user.password,
    response_type: 'code',
    grant_type: 'password',
    scope: 'upload'
  }

  request(url)
    .post(path)
    .type('form')
    .send(body)
    .expect(expectedStatus)
    .end(end)
}

function loginAndGetAccessToken (server, callback) {
  login(server.url, server.client, server.user, 200, function (err, res) {
    if (err) return callback(err)

    return callback(null, res.body.access_token)
  })
}

function getUserAccessToken (server, user, callback) {
  login(server.url, server.client, user, 200, function (err, res) {
    if (err) return callback(err)

    return callback(null, res.body.access_token)
  })
}

// ---------------------------------------------------------------------------

module.exports = loginUtils