This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 263
/
baker-controller-test.js
136 lines (126 loc) · 5.21 KB
/
baker-controller-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
var fs = require('fs');
var nock = require('nock');
var vows = require('vows');
var assert = require('assert');
var should = require('should');
var request = require('request');
var mysql = require('../lib/mysql');
var conmock = require('./conmock.js');
var fixture = require('../lib/utils.js').fixture;
var controller = require('../controllers/baker.js');
var imagedata = fs.readFileSync(__dirname + '/utils/images/no-badge-data.png')
var scope = nock('http://example.com')
.intercept('/missing.json', 'HEAD').reply(404, '')
.get('/missing.json').reply(404, '')
.intercept('/invalid.json', 'HEAD').reply(200, '')
.get('/invalid.json').reply(200, 'eeeeyyyyy', { 'Content-Type': 'application/json' })
.intercept('/invalid-content-type.json', 'HEAD').reply(200, '')
.get('/invalid-content-type.json').reply(200, fixture())
.intercept('/missing-image.json', 'HEAD').reply(200, '')
.get('/missing-image.json')
.reply(200, fixture({'badge.image': 'http://example.com/image-not-found.png'}), { 'Content-Type': 'application/json' })
.intercept('/image-not-found.png', 'HEAD').reply(404, '')
.get('/image-not-found.png').reply(404, '')
.intercept('/legit.json', 'HEAD').reply(200, '')
.get('/legit.json')
.reply(200, fixture({
'badge.image': 'http://example.com/image.png',
'recipient': 'brian@example.com'
}), { 'Content-Type': 'application/json' })
.intercept('/image.png', 'HEAD').reply(200, '', { 'Content-Type': 'image/png' })
.get('/image.png').reply(200, imagedata, { 'Content-Type': 'image/png' })
.intercept('/legit-award.json', 'HEAD').reply(200, '')
.get('/legit-award.json')
.reply(200, fixture({
'badge.image': 'http://example.com/image-award.png',
'recipient': 'brian@example.com'
}), { 'Content-Type': 'application/json' })
.intercept('/image-award.png', 'HEAD').reply(200, '', { 'Content-Type': 'image/png' })
.get('/image-award.png').reply(200, imagedata, { 'Content-Type': 'image/png' })
;
mysql.prepareTesting();
vows.describe('baker controller testing').addBatch({
'#baker called with a' : {
'missing assertion url': {
topic : function () {
conmock(controller.baker, {}, this.callback);
},
'renders the creator' : function (error, mock) {
mock.fntype.should.equal('render');
mock.path.should.equal('baker');
}
},
'bogus assertion url': {
topic : function () {
var req = { query: { assertion: 'wuuuuut' } };
conmock(controller.baker, req, this.callback);
},
'returns 400, unreachable' : function (error, mock) {
mock.headers.should.have.property('Content-Type', 'application/json');
mock.body.should.match(/unreachable/i);
}
},
'correct, but missing assertion url': {
topic : function () {
var req = { query: { assertion: 'http://example.com/missing.json' } };
conmock(controller.baker, req, this.callback);
},
'returns 400, unreachable assertion' : function (error, mock) {
mock.body.should.match(/unreachable/i);
mock.body.should.match(/assertion/i);
}
},
'valid url, but the endpoint is not the right content type': {
topic : function () {
var req = { query: { assertion: 'http://example.com/invalid-content-type.json' } };
conmock(controller.baker, req, this.callback);
},
'returns 400, content type error' : function (error, mock) {
mock.status.should.equal(400);
mock.body.should.match(/ContentType/);
}
},
'valid url, valid content-type, but the content is unparseable ': {
topic : function () {
var req = { query: { assertion: 'http://example.com/invalid.json' } };
conmock(controller.baker, req, this.callback);
},
'returns 400, parse error' : function (error, mock) {
mock.status.should.equal(400);
mock.body.should.match(/ParseError/);
}
},
'valid assertion, but the image url 404s ': {
topic : function () {
var req = { query: { assertion: 'http://example.com/missing-image.json' } };
conmock(controller.baker, req, this.callback);
},
'returns 400, unreachable image' : function (error, mock) {
mock.status.should.equal(400);
mock.body.match(/unreachable/);
mock.body.match(/image/);
}
},
'valid assertion, valid image url ': {
topic : function () {
var req = { query: { assertion: 'http://example.com/legit.json' } };
conmock(controller.baker, req, this.callback);
},
'returns 200, baked image data' : function (error, mock) {
mock.headers.should.have.property('Content-Type', 'image/png');
mock.status.should.equal(200);
}
},
'valid assertion, valid image url, trying to award': {
topic : function () {
var req = { query: { assertion: 'http://example.com/legit-award.json', award: 'brian@example.com' } };
conmock(controller.baker, req, this.callback);
},
'returns 200, baked image data' : function (error, mock) {
mock.headers.should.have.property('Content-Type', 'image/png');
mock.headers.should.have.property('x-badge-awarded', 'brian@example.com');
mock.status.should.equal(200);
}
}
}
}).export(module);