Skip to content

Commit 2b080c6

Browse files
committed
add tests
1 parent a57ac4a commit 2b080c6

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

test/client.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var Client = require("../").Client;
22
var nock = require("nock");
3+
var superagent = require("superagent");
4+
const helper = require("./helper");
5+
36
describe("client tests", function(){
47
before(function(){
58
nock.disableNetConnect();
@@ -14,6 +17,106 @@ describe("client tests", function(){
1417
client.should.be.instanceof(Client);
1518
});
1619
});
20+
describe("#prepareRequest", function(){
21+
const fakeUsername = "testuser";
22+
const fakePassword = "testpassword";
23+
const fakeClientId = "testClientId";
24+
const fakeClientSecret = "testClientSecret";
25+
const basicAuthEncoded = Buffer.from(`${fakeUsername}:${fakePassword}`).toString("base64");
26+
const clientAuthEncoded = Buffer.from(`${fakeClientId}:${fakeClientSecret}`).toString("base64");
27+
28+
it("should return request with basic auth", function(){
29+
const client = new Client("accountId", fakeUsername, fakePassword);
30+
const req = superagent.get("https://test.com");
31+
const expectedAuthHeader = "Basic " + basicAuthEncoded;
32+
33+
const preparedReq = client.prepareRequest(req);
34+
preparedReq.should.have.property("_header");
35+
preparedReq._header.should.have.property("authorization");
36+
preparedReq._header.authorization.should.equal(expectedAuthHeader);
37+
});
38+
39+
it("should use supplied bearer token", function(){
40+
const client = new Client("accountId");
41+
client.tempAccessToken = "fake_token";
42+
const req = superagent.get("https://test.com");
43+
const expectedAuthHeader = "Bearer fake_token";
44+
45+
const preparedReq = client.prepareRequest(req);
46+
preparedReq.should.have.property("_header");
47+
preparedReq._header.should.have.property("authorization");
48+
preparedReq._header.authorization.should.equal(expectedAuthHeader);
49+
});
50+
51+
it("should get a new token using clientId and clientSecret", function(done){
52+
const client = new Client("accountId", fakeUsername, fakePassword, fakeClientId, fakeClientSecret);
53+
const req = superagent.get("https://test.com");
54+
55+
const tokenScope = nock("https://api.bandwidth.com")
56+
.post("/api/v1/oauth2/token", "grant_type=client_credentials")
57+
.matchHeader('authorization', header => {
58+
const expected = "Basic " + clientAuthEncoded;
59+
return header === expected;
60+
})
61+
.reply(200, {
62+
access_token: "new_fake_token",
63+
expires_in: 3600
64+
});
65+
66+
67+
setTimeout(() => {
68+
const preparedReq = client.prepareRequest(req);
69+
preparedReq.should.have.property("_header");
70+
preparedReq._header.should.have.property("authorization");
71+
preparedReq._header.authorization.should.equal("Bearer new_fake_token");
72+
tokenScope.done();
73+
done();
74+
}, 50);
75+
});
76+
77+
it("should accept clientId and clientSecret from globalOptions", function(done){
78+
Client.globalOptions.clientId = fakeClientId;
79+
Client.globalOptions.clientSecret = fakeClientSecret;
80+
const client = new Client("accountId", fakeUsername, fakePassword);
81+
const req = superagent.get("https://test.com");
82+
83+
const tokenScope = nock("https://api.bandwidth.com")
84+
.post("/api/v1/oauth2/token", "grant_type=client_credentials")
85+
.matchHeader('authorization', header => {
86+
const expected = "Basic " + clientAuthEncoded;
87+
return header === expected;
88+
})
89+
.reply(200, {
90+
access_token: "new_fake_token",
91+
expires_in: 3600
92+
});
93+
94+
95+
setTimeout(() => {
96+
const preparedReq = client.prepareRequest(req);
97+
preparedReq.should.have.property("_header");
98+
preparedReq._header.should.have.property("authorization");
99+
preparedReq._header.authorization.should.equal("Bearer new_fake_token");
100+
tokenScope.done();
101+
done();
102+
}, 50);
103+
});
104+
105+
it("should throw error if no auth method available", function(){
106+
Client.globalOptions.userName = null;
107+
Client.globalOptions.password = null;
108+
Client.globalOptions.clientId = null;
109+
Client.globalOptions.clientSecret = null;
110+
const client = new Client("accountId");
111+
const req = superagent.get("https://test.com");
112+
113+
(function(){
114+
client.prepareRequest(req)
115+
}).should.throw("No authentication method available");
116+
117+
helper.setupGlobalOptions();
118+
});
119+
});
17120
describe("#makeRequest", function(){
18121
var client = new Client("accountId", "user", "password");
19122
it("should make GET request and parse output xml data", function(done){

0 commit comments

Comments
 (0)