Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "intuit-oauth",
"version": "2.0.2",
"version": "2.1.0",
"description": "Intuit Node.js client for OAuth2.0 and OpenIDConnect",
"main": "./src/OAuthClient.js",
"scripts": {
Expand Down Expand Up @@ -67,38 +67,24 @@
"dependencies": {
"atob": "2.1.2",
"csrf": "^3.0.4",
"es6-promise": "^4.2.5",
"events": "^3.0.0",
"idtoken-verifier": "^1.2.0",
"jsonwebtoken": "^8.3.0",
"oauth-signature": "^1.3.1",
"object-assign": "^4.1.1",
"popsicle": "10.0.1",
"query-string": "6.2.0",
"query-string": "^6.12.0",
"rsa-pem-from-mod-exp": "^0.8.4",
"winston": "^3.1.0"
},
"devDependencies": {
"body-parser": "^1.15.2",
"btoa": "^1.2.1",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"chance": "^1.1.3",
"cors": "^2.8.1",
"coveralls": "^3.0.7",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^12.0.0",
"eslint-plugin-import": "^2.9.0",
"express": "^4.14.0",
"is-travis": "^1.0.0",
"istanbul": "^0.4.5",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-plugin-import": "^2.20.2",
"mocha": "^5.0.4",
"mocha-lcov-reporter": "^1.3.0",
"nock": "^9.2.3",
"nyc": "^11.6.0",
"phantomjs-prebuilt": "^2.1.4",
"sinon": "^7.5.0",
"standard": "^11.0.0",
"watchify": "^3.7.0"
"sinon": "^9.0.2"
}
}
4 changes: 2 additions & 2 deletions src/response/AuthResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function AuthResponse(params) {
AuthResponse.prototype.processResponse = function processResponse(response) {
this.response = response || '';
this.body = (response && response.body) || '';
this.json = this.body ? JSON.parse(this.body) : null;
this.json = this.body && this.isJson() ? JSON.parse(this.body) : null;
this.intuit_tid = (response && response.headers && response.headers.intuit_tid) || '';
};

Expand Down Expand Up @@ -86,7 +86,7 @@ AuthResponse.prototype.headers = function headers() {
* @returns {*|boolean}
*/
AuthResponse.prototype.valid = function valid() {
return (this.response && Number(this.response.status) === 200);
return (this.response && Number(this.response.status) >= 200 && Number(this.response.status) < 300);
};


Expand Down
66 changes: 66 additions & 0 deletions test/AuthResponseTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const OAuthClientTest = require('../src/OAuthClient');
const AuthResponse = require('../src/response/AuthResponse');
const expectedAccessToken = require('./mocks/bearer-token.json');
const expectedResponseMock = require('./mocks/response.json');
const expectedPdfResponseMock = require('./mocks/pdfResponse.json');


const oauthClient = new OAuthClientTest({
Expand Down Expand Up @@ -120,3 +121,68 @@ describe('Tests for AuthResponse', () => {
expect(() => authResponse.processResponse(null)).to.not.throw();
});
});

describe('Tests for AuthResponse with not json content', () => {
let authResponse;
let getStub;
let expectedResponse;

beforeEach(() => {
expectedResponse = JSON.parse(JSON.stringify(expectedPdfResponseMock));
getStub = sinon.stub().returns('application/pdf');
expectedResponse.get = getStub;

authResponse = new AuthResponse({ token: oauthClient.getToken() });
authResponse.processResponse(expectedResponse);
});

afterEach(() => {
getStub.reset();
});

it('Creates a new auth response instance', () => {
expect(authResponse).to.have.property('token');
expect(authResponse).to.have.property('response');
expect(authResponse).to.have.property('body');
expect(authResponse).to.have.property('json');
expect(authResponse).to.have.property('intuit_tid');
});

it('Process Response', () => {
authResponse.processResponse(expectedResponse);
expect(authResponse.response).to.deep.equal(expectedResponse);
});

it('Process Get Token', () => {
const token = authResponse.getToken();
expect(token).to.have.property('token_type');
expect(token).to.have.property('refresh_token');
expect(token).to.have.property('expires_in');
expect(token).to.have.property('x_refresh_token_expires_in');
});

it('Process Text() when there is body ', () => {
const text = authResponse.text();
expect(text).to.be.a('string');
expect(text).to.be.equal('%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>');
});

it('Process Status of AuthResponse', () => {
const status = authResponse.status();
expect(status).to.be.equal(200);
});

it('Process Headers of AuthResponse', () => {
const headers = authResponse.headers();
expect(headers).to.be.equal(expectedResponse.headers);
});

it('Process Get Json to throw an error', () => {
expect(() => authResponse.getJson()).to.throw(Error);
});

it('GetContentType should handle False', () => {
const contentType = authResponse.getContentType();
expect(contentType).to.be.equal('application/pdf');
});
});
16 changes: 16 additions & 0 deletions test/mocks/pdfResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"url":"https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer",
"headers":{
"content-type":"application/pdf",
"content-length":"1636",
"connection":"close",
"server":"nginx",
"date":"Wed, 05 Sep 2018 05:57:09 GMT",
"intuit_tid":"1234-1234-1234-123",
"cache-control":"no-cache, no-store",
"pragma":"no-cache"
},
"body":"%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>",
"status":200,
"statusText":"OK"
}