File size: 925 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const request = require('supertest');

class APITestHelper {
  constructor(app) {
    this.app = app;
    this.token = null;
  }

  async login(email, password) {
    const response = await request(this.app).post('/api/auth/login').send({ email, password });

    this.token = response.body.accessToken;
    return response;
  }

  async authenticatedRequest(method, path, body = null) {
    const req = request(this.app)[method](path).set('Authorization', `Bearer ${this.token}`);

    if (body) {
      req.send(body);
    }

    return await req;
  }

  async get(path) {
    return this.authenticatedRequest('get', path);
  }

  async post(path, body) {
    return this.authenticatedRequest('post', path, body);
  }

  async put(path, body) {
    return this.authenticatedRequest('put', path, body);
  }

  async delete(path) {
    return this.authenticatedRequest('delete', path);
  }
}

module.exports = APITestHelper;