Skip to content

Instantly share code, notes, and snippets.

@mutaimwiti
Last active November 25, 2019 18:14
Show Gist options
  • Save mutaimwiti/0542e06a9473edcb9eab773da46333b7 to your computer and use it in GitHub Desktop.
Save mutaimwiti/0542e06a9473edcb9eab773da46333b7 to your computer and use it in GitHub Desktop.
Wrapper ES6
// test/testUtils/app.js
import supertest from 'supertest';
import appDef from '../../src/app';
const { generateAuthToken } = require('../../src/utils');
const { User } = require('../../src/models');
class App {
constructor() {
this.client = supertest(appDef);
this.token = null;
}
async login(user) {
this.token = await generateAuthToken(user);
}
async loginRandom() {
// get a random user - random is just an arbitrary function to get one user
const user = User.random();
this.token = await generateAuthToken(user);
return user;
}
logout() {
this.token = null;
}
preRequest(request) {
return this.token ? request.set('authorization', this.token) : request;
}
get(url) {
const req = this.client.get(url);
return this.preRequest(req);
}
post(url) {
const req = this.client.post(url);
return this.preRequest(req);
}
put(url) {
const req = this.client.put(url);
return this.preRequest(req);
}
patch(url) {
const req = this.client.patch(url);
return this.preRequest(req);
}
delete(url) {
const req = this.client.delete(url);
return this.preRequest(req);
}
}
export default new App();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment