Skip to content

Commit 52e863f

Browse files
committed
Merge pull request #44 from VisualTesting/feature/confirm
Implementing confirm.
2 parents bb20692 + 4a946e9 commit 52e863f

4 files changed

Lines changed: 176 additions & 10 deletions

File tree

server/controllers/api.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,72 @@ Api.prototype = {
207207
},
208208

209209
confirm: function(req, res) {
210-
throw new Error('not implemented');
210+
var params = req.body;
211+
212+
var project = params.project;
213+
var build = params.build;
214+
215+
if (!project || !build) {
216+
res.status(400).json({
217+
status: 'failure',
218+
message: 'invalid arguments'
219+
});
220+
221+
return;
222+
}
223+
224+
storage.hasProject(project)
225+
.then(function(projectExists) {
226+
if (!projectExists) {
227+
res.status(400).json({
228+
status: 'failure',
229+
message: 'unknown project'
230+
});
231+
232+
return;
233+
}
234+
235+
storage.hasBuild({
236+
project: project,
237+
build: build
238+
})
239+
.then(function(buildExists) {
240+
if (!buildExists) {
241+
res.status(400).json({
242+
status: 'failure',
243+
message: 'unknown build'
244+
});
245+
246+
return;
247+
}
248+
249+
var info;
250+
251+
return storage.getBuildInfo({
252+
project: project,
253+
build: build
254+
})
255+
.then(function(buildInfo) {
256+
buildInfo.status = 'approved';
257+
info = buildInfo;
258+
259+
buildInfo.project = project;
260+
261+
return storage.updateBuildInfo(buildInfo);
262+
})
263+
.then(function() {
264+
res.status(200).json({
265+
status: 'success'
266+
});
267+
268+
actions.setBuildStatus({
269+
project: project,
270+
sha: info.head,
271+
status: 'success'
272+
});
273+
});
274+
});
275+
});
211276
},
212277

213278
getImage: function(req, res) {

server/routes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ module.exports = function(options) {
130130
/*
131131
Approve a build
132132
POST Params
133-
- id
133+
- project string
134+
- build string
134135
Response:
135136
{
136137
status: "success"

server/utils/storage.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ var Storage = {
234234
assert.isObject(options);
235235
assert.isString(options.project);
236236
assert.isString(options.build);
237-
assert.include(['success', 'failed'], options.status);
237+
assert.isString(options.status);
238238

239-
if (options.status === 'failed') {
239+
if (options.diffs) {
240240
assert.isObject(options.diffs);
241241
}
242242

@@ -253,12 +253,7 @@ var Storage = {
253253
})
254254
.then(function(data) {
255255
data.status = status;
256-
257-
if (status === 'success') {
258-
delete data.diffs;
259-
} else if (status === 'failed') {
260-
data.diffs = diffs;
261-
}
256+
data.diffs = diffs;
262257

263258
return fs.outputJSONAsync(buildFile, data);
264259
});

test/api-test.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,111 @@ describe('module/api', function() {
517517
});
518518
});
519519

520+
describe('#confirm', function() {
521+
describe('with unknown project', function() {
522+
it('should 400 with message', function() {
523+
storageStub.hasProject = this.sinon.stub().resolves(false);
524+
525+
return api.post('/api/confirm')
526+
.send({
527+
project: 'project',
528+
build: 'build'
529+
})
530+
.expect(400)
531+
.expect(function(data) {
532+
var body = data.body;
533+
assert.equal(body.status, 'failure');
534+
assert.equal(body.message, 'unknown project');
535+
});
536+
});
537+
});
538+
539+
describe('with unknown build', function() {
540+
it('should 400 with message', function() {
541+
storageStub.hasProject = this.sinon.stub().resolves(true);
542+
storageStub.hasBuild = this.sinon.stub().resolves(false);
543+
544+
return api.post('/api/confirm')
545+
.send({
546+
project: 'project',
547+
build: 'build'
548+
})
549+
.expect(400)
550+
.expect(function(data) {
551+
var body = data.body;
552+
assert.equal(body.status, 'failure');
553+
assert.equal(body.message, 'unknown build');
554+
});
555+
});
556+
});
557+
558+
describe('with known project and build', function() {
559+
var stub;
560+
var buildInfo;
561+
562+
beforeEach(function() {
563+
buildInfo = {
564+
project: 'project',
565+
head: 'head',
566+
status: 'failed',
567+
diffs: {
568+
'Chrome 28': [
569+
'homepage.navbar.700.png',
570+
'homepage.navbar.1300.png',
571+
'homepage.search.700.png',
572+
'homepage.search.1300.png'
573+
],
574+
'IE 8': [
575+
'homepage.navbar.700.png',
576+
'homepage.search.700.png'
577+
]
578+
}
579+
};
580+
581+
storageStub.hasProject = this.sinon.stub().resolves(true);
582+
storageStub.hasBuild = this.sinon.stub().resolves(true);
583+
storageStub.updateBuildInfo = this.sinon.stub().resolves();
584+
storageStub.getBuildInfo = this.sinon.stub().resolves(buildInfo);
585+
586+
instance = api.post('/api/confirm')
587+
.send({
588+
project: 'project',
589+
build: 'build'
590+
});
591+
});
592+
593+
it('should be 200 with success', function() {
594+
return instance
595+
.expect(200)
596+
.expect(function(data) {
597+
var body = data.body;
598+
assert.equal(body.status, 'success');
599+
});
600+
});
601+
602+
it('should call storage.updateBuildInfo', function() {
603+
return instance.expect(function() {
604+
buildInfo.status = 'approved';
605+
606+
assert.calledOnce(storageStub.updateBuildInfo.withArgs(buildInfo));
607+
});
608+
});
609+
610+
it('should call actions.setBuildStatus', function() {
611+
return instance
612+
.expect(function() {
613+
assert.calledOnce(actionsStub.setBuildStatus
614+
.withArgs({
615+
project: 'project',
616+
sha: 'head',
617+
status: 'success'
618+
})
619+
);
620+
});
621+
});
622+
});
623+
});
624+
520625
describe('#getImage', function() {
521626
it('should call getImage', function() {
522627
storageStub.getImage = this.sinon.stub();

0 commit comments

Comments
 (0)