This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
87 lines (70 loc) 路 1.73 KB
/
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
import test from 'ava';
import sinon from 'sinon';
import fn from './';
test.beforeEach(t => {
t.context.middleware = fn();
t.context.req = {};
t.context.res = {
status: sinon.spy()
};
});
test('should return a function', t => {
t.is(typeof t.context.middleware, 'function');
});
test('should expect 3 arguments', t => {
t.is(t.context.middleware.length, 3);
});
test('should call next() once', t => {
const next = sinon.spy();
t.context.middleware(t.context.req, t.context.res, next);
t.true(next.calledOnce);
});
test('should expose escher instance to request object', t => {
const FakeEscher = class FakeEscher {
authenticate() {
return true;
}
};
const mw = fn({
Escher: FakeEscher
});
mw(t.context.req, t.context.res, () => {
t.truthy(t.context.req.escher);
t.true(t.context.req.escher instanceof FakeEscher);
});
});
test('should instantiate escher with given options', t => {
const FakeEscher = class FakeEscher {
constructor(options) {
this.credentialScope = options.credentialScope;
}
};
const options = {
Escher: FakeEscher,
credentialScope: 'example/credential/scope'
};
const mw = fn(options);
mw(t.context.req, t.context.res, () => {
t.is(t.context.req.escher.credentialScope, options.credentialScope);
});
});
test('should call escher.authenticate() once', t => {
const req = {
escher: {
authenticate: sinon.spy()
}
};
t.context.middleware(req, t.context.res, () => {
t.true(req.escher.authenticate.calledOnce);
});
});
test('should set response status 401 if escher throws', t => {
const req = {
escher: {
authenticate: sinon.stub().throws()
}
};
t.context.middleware(req, t.context.res, () => {
t.true(t.context.res.status.withArgs(401).calledOnce);
});
});