-
Notifications
You must be signed in to change notification settings - Fork 17
/
poppins-mocks.js
162 lines (140 loc) · 3.71 KB
/
poppins-mocks.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// see https://github.com/visionmedia/superagent
var request = require('supertest');
// see https://github.com/pgte/nock
var nock = require('nock');
var Q = require('q');
Q.longStackSupport = true;
var expect = require('expect.js');
var Poppins = require('./poppins.js').Poppins;
var debugging = false;
beforeEach(function() {
nock.cleanAll();
});
afterEach(function() {
closePoppins();
});
var exports = module.exports = {
poppins: null,
debug: debug,
createPoppins: createPoppins,
github: {
mock: githubMock,
mockGet: githubMockGet,
repoPath: repoPath,
issuesPath: issuesPath
},
request: poppinsRequest
};
function debug() {
debugging = true;
// can't turned off right now :-(
nock.recorder.rec();
}
function createPoppins(initData) {
closePoppins();
initData = initData || {};
initData.openIssues = initData.openIssues || [];
initData.closedIssues = initData.closedIssues || [];
var nockScopes = [];
nockScopes.push(githubMockGet(repoPath(), initData.repo || {
"id": 460078
}));
nockScopes.push(githubMockGet(issuesPath({state: 'open'}), initData.openIssues));
if (initData.openIssues.length) {
// if we have issues, poppins will ask for the next page
nockScopes.push(githubMockGet(issuesPath({state: 'open'}), []));
}
nockScopes.push(githubMockGet(issuesPath({state: 'closed'}), initData.closedIssues));
if (initData.closedIssues.length) {
// if we have issues, poppins will ask for the next page
nockScopes.push(githubMockGet(issuesPath({state: 'closed'}), []));
}
var poppins = exports.poppins = new Poppins({
target: {
user: 'someGithubUser',
repo: 'someGithubRepo'
},
login: {
username: 'someUser',
password: 'somePassword'
},
// port for poppins to listen on and URL for Github to ping
hook: {
url: 'http://example.com:1234',
port: 1234
}
});
clearPoppinsCache(poppins);
if (debugging) {
poppins.on('log', function(msg) {
console.log(msg);
});
}
return poppins.start().then(function() {
// assert that the repo, the open and closed issues were read.
// Then the corresponding mocks also have been removed.
nockScopes.forEach(function(nockScope) {
nockScope.done();
});
});
}
function closePoppins() {
if (exports.poppins) {
exports.poppins.stop();
exports.poppins = null;
}
}
function clearPoppinsCache(poppins) {
if (poppins.cache.exists('repo')) {
poppins.cache.clear('repo');
}
if (poppins.cache.exists('issues')) {
poppins.cache.clear('issues');
}
if (poppins.cache.exists('hook')) {
poppins.cache.clear('hook');
}
}
function githubMock(pathRegexStr) {
// using string regex so we don't have to escape
// slashes when defining them.
var pathRegex = new RegExp(pathRegexStr);
return nock('https://api.github.com:443')
.filteringPath(function(path) {
if (pathRegex.test(path)) {
return '/path';
} else {
return '/notMatched'
}
});
}
function githubMockGet(pathRegEx, response) {
return githubMock(pathRegEx)
.get('/path')
.reply(200, JSON.stringify(response),
{ 'content-type': 'application/json; charset=utf-8',
status: '200 OK'
});
}
function repoPath() {
return '/repos/[^/]+/[^/]+$';
}
function issuesPath(filters) {
var res = '/issues', prop;
for (prop in filters) {
res += '.*'+prop+'='+filters[prop];
}
return res;
}
function poppinsRequest(data, callback) {
var deferred = Q.defer();
request(exports.poppins.server).post('/')
.send({
payload: JSON.stringify(data)
})
.type('application/json')
.set('Accept', 'application/json')
.expect(202)
.end(deferred.makeNodeResolver());
return deferred.promise;
}