-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.test.js
206 lines (160 loc) · 5.56 KB
/
server.test.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var test = require('microtest').module(__dirname + '/server.js')
, assert = require('assert')
, fs = require('fs')
, path = require('path')
, querystring = require('querystring')
test.requires('http')
test.requires('fs')
test.requires('child_process')
test.requires('path')
test.requires('querystring')
var JSON_STRING = fs.readFileSync('fixture-hook.json', 'utf8')
, REQ = querystring.stringify({ payload: JSON_STRING })
, REQ_ONE = REQ.slice(0, 50)
, REQ_TWO = REQ.slice(50)
, SERVER = test.object('server')
, CONFIG = fs.readFileSync('config.test.json', 'utf8')
, CHILD = test.object('child')
, createServerCall = test.expect(test.required.http, 'createServer')
.andReturn(SERVER)
, listenCall = test.expect(SERVER, 'listen')
, readFileCall = test.expect(test.required.fs, 'readFile')
var EXPORTS = test.compile()
test.describe('load config', function () {
var args = readFileCall.calls[0].args
assert.equal(3, args.length)
assert.equal('config.json', args[0])
assert.equal('utf8', args[1])
assert.equal('function', typeof args[2])
args[2](null, CONFIG)
assert.equal('object', typeof EXPORTS.config)
assert.equal('bash scripts/deploy.sh', EXPORTS.config['secretpath'].command)
})
test.describe('http#createServer', function () {
var args = createServerCall.calls[0].args
assert.equal(1, args.length)
assert.equal('function', typeof args[0])
})
test.describe('server#request', function () {
var cb = createServerCall.calls[0].args[0]
, REQUEST = test.object('request')
, RESPONSE = test.object('response')
, dataCall, endCall, dataArgs, endArgs
, onGithubHookCall, args
, writeHeadCall, parseCall
test.expect(REQUEST, 'setEncoding', 1, ['utf8'])
dataCall = test.expect(REQUEST, 'on', 1)
endCall = test.expect(REQUEST, 'on', 1)
writeHeadCall = test.expect(RESPONSE, 'writeHead')
test.expect(RESPONSE, 'end', [])
REQUEST.url = '/' + Object.keys(EXPORTS.config)[0]
cb(REQUEST, RESPONSE)
dataArgs = dataCall.calls[0].args
endArgs = endCall.calls[0].args
assert.equal(2, dataArgs.length)
assert.equal('function', typeof dataArgs[1])
assert.equal(2, endArgs.length)
assert.equal('function', typeof endArgs[1])
test.expect(
test.required.querystring
, 'parse'
, 1
, [REQ])
.andReturn({ payload: JSON_STRING })
onGithubHookCall = test.expect(EXPORTS, 'onGithubHook')
dataArgs[1](REQ_ONE)
dataArgs[1](REQ_TWO)
endArgs[1]()
args = writeHeadCall.calls[0].args
assert.equal(2, args.length)
assert.equal(200, args[0])
assert.equal('object', typeof args[1])
args = onGithubHookCall.calls[0].args
assert.equal(2, args.length)
assert.equal('object', typeof args[0])
assert.equal('refs/heads/master', args[0].ref)
assert.equal(REQUEST.url.slice(1), args[1])
})
test.describe("server#request bad path", function () {
var REQUEST = test.object('request')
, RESPONSE = test.object('response')
, args, writeHeadCall
REQUEST.url = '/badpath'
writeHeadCall = test.expect(RESPONSE, 'writeHead')
test.expect(RESPONSE, 'end', ['Not found'])
createServerCall.calls[0].args[0](REQUEST, RESPONSE)
args = writeHeadCall.calls[0].args
assert.equal(2, args.length)
assert.equal(404, args[0])
assert.equal('object', typeof args[1])
})
test.describe('exports#onGithubHook', function () {
var JSON_OBJ = JSON.parse(JSON_STRING.trim())
, WRITE_STREAM = test.object('write_stream')
, now = new Date().toISOString()
, LOGNAME = now + '-' + JSON_OBJ.after.slice(0, 7)
, LOGPATH = path.join(EXPORTS.config.secretpath.logdir, LOGNAME)
, createWriteStreamCall, pipeCall, args, execCall
CHILD.stdout = test.object('stdout')
CHILD.stderr = test.object('stderr')
execCall = test.expect
( test.required.child_process
, 'exec'
, 1
, [ EXPORTS.config.secretpath.command
+ ' "' + JSON_OBJ.after + '"'
+ ' "' + JSON_OBJ.ref + '"'
+ ' "' + JSON_OBJ.repository.owner.name + '"'
+ ' "' + JSON_OBJ.repository.name + '"'
]
, CHILD
)
test.expect(
test.required.path
, 'join'
, 1)
.andReturn(LOGPATH)
createWriteStreamCall = test.expect(
test.required.fs
, 'createWriteStream'
, 1
, [LOGPATH + '.log'])
.andReturn(WRITE_STREAM)
test.expect(CHILD.stdout, 'pipe', 1, [WRITE_STREAM])
pipeCall = test.expect(CHILD.stderr, 'pipe', 1)
EXPORTS.onGithubHook(JSON_OBJ, 'secretpath')
args = pipeCall.calls[0].args
assert.equal(2, args.length)
assert.equal(WRITE_STREAM, args[0])
assert.deepEqual(
{ end: false
}
, args[1])
})
test.describe('exports#onGithubHook bad branch', function () {
var JSON_OBJ = JSON.parse(JSON_STRING.trim())
JSON_OBJ.ref = 'refs/heads/bad'
EXPORTS.onGithubHook(JSON_OBJ, 'secretpath')
})
test.describe('exports#onGithubHook no branch no logdir', function () {
var JSON_OBJ = JSON.parse(JSON_STRING.trim())
, execCall, args
execCall = test.expect(
test.required.child_process
, 'exec'
, 1
, EXPORTS.config.secretpath.command
+ ' "' + JSON_OBJ.after + '"'
+ ' "' + JSON_OBJ.ref + '"'
+ ' "' + JSON_OBJ.repository.owner.name + '"'
+ ' "' + JSON_OBJ.repository.name + '"')
.andReturn(CHILD)
EXPORTS.config.secretpath.branch = undefined
EXPORTS.config.secretpath.logdir = undefined
EXPORTS.onGithubHook(JSON_OBJ, 'secretpath')
})
test.describe('server#listen', function () {
var args = listenCall.calls[0].args
assert.equal(1, args.length)
assert.equal('number', typeof args[0])
})