-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathauthenticator-test.js
156 lines (127 loc) · 4.74 KB
/
authenticator-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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var assert = require('assert');
var sinon = require('sinon');
var proxyquire = require('proxyquire');
var server = require('./server');
var ROOT_URL = '/';
describe('Authenticator', function () {
var authenticatorStub;
var credentialsStub;
var PlexAPI;
var api;
beforeEach(function () {
authenticatorStub = sinon.stub().yields(null, 'abc-pretend-to-be-token');
credentialsStub = sinon.stub().returns({
authenticate: authenticatorStub,
});
PlexAPI = proxyquire('..', {
'plex-api-credentials': credentialsStub,
});
api = new PlexAPI({
hostname: 'localhost',
authenticator: {
authenticate: authenticatorStub,
},
});
});
afterEach(server.stop);
describe('.initialize()', function () {
it('is called on authenticator if method exists when creating PlexAPI instances', function () {
var authenticatorSpy = {
initialize: sinon.spy(),
};
api = new PlexAPI({
hostname: 'localhost',
authenticator: authenticatorSpy,
});
assert(authenticatorSpy.initialize.calledOnce);
});
it('provides created PlexAPI object as argument', function () {
var authenticatorSpy = {
initialize: sinon.spy(),
};
api = new PlexAPI({
hostname: 'localhost',
authenticator: authenticatorSpy,
});
assert(authenticatorSpy.initialize.firstCall.calledWith(api));
});
});
describe('.authenticate()', function () {
it('is called on authenticator when Plex Server responds with 401', function () {
server.start({
statusCode: 401,
expectRetry: true,
});
return api.query(ROOT_URL).then(function () {
assert(authenticatorStub.firstCall.calledWith(api), 'authenticator was called');
});
});
it('provides options object and callback as arguments when calling authenticator', function () {
server.start({
statusCode: 401,
expectRetry: true,
});
return api.query(ROOT_URL).then(function () {
var firstArg = authenticatorStub.firstCall.args[0];
var secondArg = authenticatorStub.firstCall.args[1];
assert.equal(typeof firstArg, 'object');
assert.equal(typeof secondArg, 'function');
});
});
it('retries original request with token given from authenticator', function () {
scope = server.start({
statusCode: 401,
expectRetry: true,
});
return api.query(ROOT_URL).then(function (result) {
scope.done();
});
});
it('rejects when providing token and server still responds with 401', function () {
scope = server.start({
statusCode: 401,
retryStatusCode: 401,
expectRetry: true,
});
return api.query(ROOT_URL).then(
function onSuccess(result) {
throw new Error('Query should not have succeeded!');
},
function onError() {
scope.done();
}
);
});
});
describe('default authenticator', function () {
it('uses the plex-api-credentials authenticator when options.username and .password are provided', function () {
server.start({
statusCode: 401,
expectRetry: true,
});
api = new PlexAPI({
hostname: 'localhost',
username: 'foo',
password: 'bar',
});
return api.query(ROOT_URL).then(function () {
assert(authenticatorStub.calledOnce, 'credentials authenticator was called');
});
});
it('rejects with a missing authenticator error when options.username and .password were missing and Plex Server responds with 401', function () {
server.start({
statusCode: 401,
});
api = new PlexAPI({
hostname: 'localhost',
});
return api.query(ROOT_URL).then(null, function (err) {
assert(err instanceof Error, 'rejected with an error instance');
assert(
err.message.match(/you must provide a way to authenticate/),
'error message says authenticator is needed'
);
});
});
});
});