forked from mozilla/openbadges-backpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadge-controller.test.js
88 lines (77 loc) · 2.35 KB
/
badge-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
const _ = require('underscore');
const test = require('tap').test;
const testUtils = require('./');
const badge = require('../controllers/badge');
const conmock = require('./conmock');
const User = require('../models/user');
const Badge = require('../models/badge');
function makeHash (email, salt) {
const sha = require('crypto').createHash('sha256');
return 'sha256$' + sha.update(email + salt).digest('hex');
}
testUtils.prepareDatabase({
'1-real-user': new User({ email: '[email protected]' }),
'2-false-user': new User({ email: '[email protected]' }),
'3-badge-raw': new Badge({
user_id: 1,
type: 'hosted',
endpoint: 'endpoint',
image_path: 'image_path',
body_hash: 'body_hash',
body: testUtils.makeAssertion({
recipient: '[email protected]',
criteria: '/ohsup.html'
})
}),
'4-badge-hashed': new Badge({
user_id: 1,
type: 'hosted',
endpoint: 'endpoint',
image_path: 'image_path',
body_hash: 'body_hash',
body: testUtils.makeAssertion({
recipient: makeHash('[email protected]', 'hashbrowns'),
salt: 'hashbrowns'
})
})
}, function (fixtures) {
test('badge#destroy', function (t) {
const owner = fixtures['1-real-user'];
const thief = fixtures['2-false-user'];
const badgeRaw = fixtures['3-badge-raw'];
const badgeHashed = fixtures['4-badge-hashed'];
const handler = badge.destroy;
conmock({
handler: handler,
request: { badge: badgeRaw }
}, function (err, mock) {
t.same(mock.status, 403, 'cannot delete a badge without a user');
});
conmock({
handler: handler,
request: { user: owner }
}, function (err, mock) {
t.same(mock.status, 404, 'should 404 without a badge');
});
conmock({
handler: handler,
request: { badge: badgeRaw, user: thief }
}, function (err, mock) {
t.same(mock.status, 403, 'cannot delete a badge you do not own');
});
conmock({
handler: handler,
request: { user: owner, badge: badgeRaw }
}, function (err, mock) {
t.same(mock.status, 200, 'should delete badge with unhashed email');
})
conmock({
handler: handler,
request: { user: owner, badge: badgeHashed }
}, function (err, mock) {
t.same(mock.status, 200, 'should delete badge with hashed email');
t.end();
})
});
testUtils.finish(test);
});