-
Notifications
You must be signed in to change notification settings - Fork 213
/
diff.unit.test.ts
239 lines (212 loc) · 6.98 KB
/
diff.unit.test.ts
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import {expect, test} from '@oclif/test'
describe('pipelines:diff', function () {
const apiUrl = 'https://api.heroku.com'
const kolkrabbiApi = 'https://kolkrabbi.heroku.com'
const githubApi = 'https://api.github.com'
const pipeline = {
id: '123-pipeline-456',
name: 'example-pipeline',
}
const targetApp = {
id: '123-target-app-456',
name: 'example-staging',
pipeline,
}
const targetCoupling = {
app: targetApp,
id: '123-target-app-456',
pipeline,
stage: 'staging',
}
const targetGitHubApp = {
repo: 'heroku/example-app',
}
const downstreamApp1 = {
id: '123-downstream-app-1-456',
name: 'example-production-eu',
pipeline,
}
const downstreamCoupling1 = {
app: downstreamApp1,
id: '123-target-app-456',
pipeline,
stage: 'production',
}
const downstreamApp1GitHub = {
repo: 'heroku/example-app',
}
const downstreamApp2 = {
id: '123-downstream-app-2-456',
name: 'example-production-us',
pipeline,
}
const downstreamCoupling2 = {
app: downstreamApp2,
id: '123-target-app-456',
pipeline,
stage: 'production',
}
const downstreamApp2GitHub = {
repo: 'heroku/some-other-app',
}
function mockPipelineCoupling(testInstance: typeof test) {
return testInstance
.nock(apiUrl, api => {
api
.get(`/apps/${targetApp.name}/pipeline-couplings`)
.reply(200, targetCoupling)
})
}
function mockApps(testInstance: typeof test) {
return testInstance
.nock(apiUrl, api => {
api
.get(`/pipelines/${pipeline.id}/pipeline-couplings`)
.reply(200, [targetCoupling, downstreamCoupling1, downstreamCoupling2])
api
.post('/filters/apps')
.reply(200, [targetApp, downstreamApp1, downstreamApp2])
})
}
describe('for app without a pipeline', function () {
test
.nock(apiUrl, api => {
api
.get(`/apps/${targetApp.name}/pipeline-couplings`)
.reply(404, {message: 'Not found.'})
})
.command(['pipelines:diff', `--app=${targetApp.name}`])
.catch(error => {
expect(error.message).to.contain('to be a part of any pipeline')
})
.it('should return an error')
})
describe('for app with a pipeline but no downstream apps', function () {
mockPipelineCoupling(test)
.nock(apiUrl, api => {
api
.get(`/pipelines/${pipeline.id}/pipeline-couplings`)
.reply(200, [targetCoupling])
.post('/filters/apps')
.reply(200, [targetApp])
})
.command(['pipelines:diff', `--app=${targetApp.name}`])
.catch(error => {
expect(error.message).to.contain('no downstream apps')
})
.it('should return an error')
})
describe('for invalid apps with a pipeline', function () {
const mockedTest = (testInstance: typeof test) => {
const t = mockPipelineCoupling(mockApps(testInstance))
return t
.nock(kolkrabbiApi, api => {
api
.get(`/apps/${downstreamApp1.id}/github`)
.reply(200, downstreamApp1GitHub)
.get(`/apps/${downstreamApp2.id}/github`)
.reply(200, downstreamApp2GitHub)
})
}
mockedTest(test)
.nock(kolkrabbiApi, api => {
api
.get(`/apps/${targetApp.id}/github`)
.reply(404, {message: 'Not found.'})
})
.command(['pipelines:diff', `--app=${targetApp.name}`])
.catch(error => {
expect(error.message).to.contain('connected to GitHub')
})
.it('should return an error if the target app is not connected to GitHub')
mockedTest(test)
.nock(kolkrabbiApi, api => {
api
.get(`/apps/${targetApp.id}/github`)
.reply(200, targetGitHubApp)
})
.command(['pipelines:diff', `--app=${targetApp.name}`])
.catch(error => {
expect(error.message).to.contain('No release was found')
})
.it('should return an error if the target app has a release with no slug')
mockedTest(test)
.nock(kolkrabbiApi, api => {
api
.get(`/apps/${targetApp.id}/github`)
.reply(200, targetGitHubApp)
})
.nock(apiUrl, api => {
api
.get(`/apps/${targetApp.id}/releases`)
.reply(200, [])
})
.command(['pipelines:diff', `--app=${targetApp.name}`])
.catch(error => {
expect(error.message).to.contain('No release was found')
})
.it('should return an error if the target app has no release')
})
describe('for valid apps with a pipeline', function () {
const targetSlugId = 'target-slug-id'
const downstreamSlugId = 'downstream-slug-id'
const mockedTest = (testInstance: typeof test) => {
const t = mockPipelineCoupling(mockApps(testInstance))
return t
.nock(kolkrabbiApi, api => {
api
.get(`/apps/${targetApp.id}/github`)
.reply(200, targetGitHubApp)
.get(`/apps/${downstreamApp1.id}/github`)
.reply(200, downstreamApp1GitHub)
.get(`/apps/${downstreamApp2.id}/github`)
.reply(200, downstreamApp2GitHub)
.get('/account/github/token')
.reply(200, {github: {token: 'github-token'}})
})
.nock(apiUrl, api => {
api
.get(`/apps/${targetApp.id}/releases`)
.reply(200, [{slug: {id: targetSlugId}, status: 'succeeded'}])
.get(`/apps/${downstreamApp1.id}/releases`)
.reply(200, [
{status: 'failed'},
{slug: {id: downstreamSlugId}, status: 'succeeded'},
])
})
}
mockedTest(test)
.stdout()
.nock(apiUrl, api => {
api
.get(`/apps/${targetApp.id}/slugs/${targetSlugId}`)
.reply(200, {commit: 'COMMIT-HASH'})
.get(`/apps/${downstreamApp1.id}/slugs/${downstreamSlugId}`)
.reply(200, {commit: 'COMMIT-HASH'})
})
.command(['pipelines:diff', `--app=${targetApp.name}`])
.it('should not compare apps if update to date NOR if repo differs', ctx => {
expect(ctx.stdout).to.contain(`⬢ ${targetApp.name} is up to date with ⬢ ${downstreamApp1.name}`)
expect(ctx.stdout).to.contain(`⬢ ${targetApp.name} was not compared to ⬢ ${downstreamApp2.name}`)
})
const hashes = ['hash-1', 'hash-2']
mockedTest(test)
.stdout()
.nock(apiUrl, api => {
api
.get(`/apps/${targetApp.id}/slugs/${targetSlugId}`)
.reply(200, {commit: hashes[0]})
.get(`/apps/${downstreamApp1.id}/slugs/${downstreamSlugId}`)
.reply(200, {commit: hashes[1]})
})
.nock(githubApi, api => {
api
.get(`/repos/${targetGitHubApp.repo}/compare/${hashes[1]}...${hashes[0]}`)
.reply(404)
})
.command(['pipelines:diff', `--app=${targetApp.name}`])
.it('should handle non-200 responses from GitHub', ctx => {
expect(ctx.stdout).to.contain('unable to perform a diff')
})
})
})