This repository has been archived by the owner on Jul 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dreddhooks.js
116 lines (100 loc) · 3.37 KB
/
dreddhooks.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
require('dotenv').load({silent: true});
const hooks = require('hooks');
const jwt = require('jsonwebtoken');
const config = require('config');
const db = require('./models/database');
const User = require('./models/users');
const UserEmail = require('./models/userEmails');
const _ = require('lodash');
let responseStash = {};
const superuserToken = jwt.sign({id: 42}, config.get('secret'), {expiresIn: '100h'});
const userToken = jwt.sign({id: 1010}, config.get('secret'), {expiresIn: '100h'});
const supplyAuthToken = function(authorization) {
// hooks.log('getting token for ' + authorization);
let token;
switch(authorization) {
case 'Superuser':
token = superuserToken;
break;
case 'User':
token = userToken;
break;
default:
token = 'NONE';
break;
}
return token;
};
const reseed = function(done) {
return db.knex.seed.run().then(function() {
hooks.log('db seeded');
done();
})
};
//before doing any tests, re-seed the database
hooks.beforeAll(function(transactions, done) {
return reseed(done);
});
hooks.beforeEach(function(transaction, done) {
var name = transaction.name;
if(name.match(/Accounts > Account > Update A Single Account/) ||
name.match(/Accounts > Account > List All Accounts/)) {
return reseed(done);
} else {
return done();
}
});
hooks.afterEach(function(transaction, done) {
var name = transaction.name;
if(name.match(/Users > User > Delete a Single User/) ||
name.match(/Accounts > Account > Delete A Single Account/)) {
return reseed(done);
} else {
return done();
}
});
hooks.beforeEach(function (transaction) {
hooks.log(typeof(transaction.request.headers));
hooks.log(transaction.request.headers.Authorization);
if(typeof(transaction.request.headers) === 'object' &&
transaction.request.headers.Authorization !== undefined) {
transaction.request['headers']['Cookie'] = "gaia.token=" + supplyAuthToken(transaction.request.headers.Authorization);
delete(transaction.request['headers']['Authorization']);
}
});
//Before creating this user, remove any matching emails
hooks.before("Users > User Collection > Create a Single User", function(transaction) {
let body = JSON.parse(transaction.request.body);
if(body && body.emails !== undefined) {
db.knex('xref_user_emails')
.whereIn('email', body.emails)
.del()
.then(function(count) {
hooks.log('deleted ' + count + ' rows');
});
}
});
//Before updating the user, save the pre-update state
hooks.before("Users > User > Update A Single User", function(transaction) {
// hooks.log('Update a single user');
let uri = transaction.request.uri;
let id = uri.replace(/.*\/(\d+)$/,'$1');
User.where({id: Number(id)}).fetch().then(function(user) {
responseStash.user = user;
// hooks.log('responsestash.user1', responseStash.user);
});
// hooks.log('responsestash.user2', responseStash.user);
});
//After updating the user, apply the pre-update state
hooks.after("Users > User > Update A Single User", function(transaction, done) {
let user = _.pick(responseStash.user.attributes, ['id', 'firstName', 'lastName']);
return new User(user).save()
.finally(function() {
done();
});
});
/** NOT YET IMPLEMENTED **/
hooks.before("Accounts > Account Users > Add a Single User to an Account", function(transaction) {
transaction.skip = true;
});