forked from mozilla/openbadges-backpack
/
backpack-controller.test.js
115 lines (107 loc) · 2.89 KB
/
backpack-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
const _ = require('underscore');
const test = require('tap').test;
const testUtils = require('./');
const backpack = require('../controllers/backpack');
const conmock = require('./conmock');
test('backpack#login', function (t) {
conmock({
handler: backpack.login,
request: {
session: { _csrf: 'cats' }
}
}, function (err, mock) {
t.same(mock.fntype, 'render', 'should try to render the login page');
t.same(mock.options['csrfToken'], 'cats', 'should have right csrf token');
t.end();
});
});
test('backpack#signout', function (t) {
conmock({
handler: backpack.signout,
request: {
session: { email: 'brian@example.org' }
}
}, function (err, mock, req) {
t.same(mock.path, '/backpack/login', 'should redirect to the login page');
t.same(_.keys(req.session).length, 0, 'should wipe out the session');
t.end();
});
});
test('backpack#manage', function (t) {
// #TODO: re-write after making backpack.manage sane.
t.end();
});
test('backpack#settings redirects to login if no user', function (t) {
conmock({handler: backpack.settings()}, function(err, mock) {
t.same(mock.status, 303);
t.same(mock.path, "/backpack/login");
t.end();
});
});
test('backpack#settings handles summarizeForUser() errors', function (t) {
conmock({
handler: backpack.settings({
backpackConnectModel: {
summarizeForUser: function(id, cb) {
cb(new Error("SUMMARIZE ERROR"));
}
}
}),
request: {user: {get: function() {}}}
}, function(err, mock) {
t.same(mock.fntype, 'next');
t.same(mock.nextErr.message, 'SUMMARIZE ERROR');
t.end();
});
});
test('backpack#settings works', function (t) {
conmock({
handler: backpack.settings({
backpackConnectModel: {
summarizeForUser: function(id, cb) {
t.same(id, 5);
cb(null, [{origin: "http://foo.org", permissions: ["bar"]}]);
}
}
}),
request: {
user: {get: function() { return 5; }},
session: {_csrf: "csrf"}
}
}, function(err, mock) {
t.same(mock.fntype, 'render');
t.same(mock.headers, {
"Cache-Control" : "no-cache, must-revalidate"
});
t.same(mock.options, {
error: undefined,
success: undefined,
csrfToken: "csrf",
services: {},
issuers: [{
origin: "http://foo.org",
domain: "foo.org",
permissions: ["bar"]
}]
});
t.end();
});
test('backpack#facebookSharing', function (t) {
conmock({
handler: backpack.facebookSharing,
request: {
body: {
accessToken: 'aaBBccDDeFgHiJkLMnOP',
badgeBodyHash: 'sillybadgehash',
userId: 'me',
comment: 'Commenting on my badge so others know how awesome I am!',
fbAutomaticPush: 'checked'
}
}
}, function (err, mock, req) {
if (err) throw err;
t.equal(mock.status, 303);
t.end();
});
});
testUtils.finish(test);