Skip to content

Commit

Permalink
Fix test auth for no2FA since SMS fix.
Browse files Browse the repository at this point in the history
When fixing SMS code triggers by posting an empty CREATE request
to the /authorizations API, that meant an error was returned when
before it was a successful response of authorizations for a GET
request.

This now deals with that, and adds tests.
  • Loading branch information
henry committed Nov 14, 2014
1 parent 2967f30 commit f8149fc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/github-oauth-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ function userRequires2FA (callback) {
if (err) {
// Can't check the response headers on error, so do a string
// match on the message.
if (err.code === 401 && JSON.parse(err.message).message === 'Must specify two-factor authentication OTP code.') {
var errData = JSON.parse(err.message);
if (errData.message === 'Must specify two-factor authentication OTP code.') {
has2FA = true;
callback(null, has2FA);
} else if (errData.errors && errData.errors[0] && errData.errors[0].code === 'missing_field') {
has2FA = false;
callback(null, has2FA);
} else {
callback(err);
}
Expand Down
28 changes: 21 additions & 7 deletions test/api-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var NOCK_API_ENDPOINT = '/authorizations';
// jscs:disable validateQuoteMarks

// POSTS
var NOCK_POST_EMPTY = {};
var NOCK_POST_TEST = {
"scopes": [],
"note": "test",
Expand All @@ -31,6 +32,15 @@ var NOCK_BODY_BAD_TOKEN_EXISTS = {
"field": "description"
}]
};
var NOCK_BODY_BAD_NO2FA_TEST_CREATE = {
"message": "Validation Failed",
"documentation_url": "https: //developer.github.com/v3/oauth_authorizations/#create-a-new-authorization",
"errors": [{
"resource": "OauthAccess",
"code": "missing_field",
"field": "description"
}]
};

// BODY RESULTS
var NOCK_BODY_TOKEN = {
Expand Down Expand Up @@ -95,6 +105,10 @@ var NOCK_HEADERS_BAD_TOKEN_EXISTS = _.defaults({
status: '422 Unprocessable Entity'
}, RATELIMIT_HAS2FA, DEFAULT_HEADERS);

var NOCK_HEADERS_BAD_NO2FA_TEST_CREATE = _.defaults({
status: '422 Unprocessable Entity'
}, RATELIMIT_NO2FA, DEFAULT_HEADERS);

var NOCK_HEADERS_CREATED = _.defaults({
status: '201 Created',
location: 'https://api.github.com/authorizations/9999999'
Expand All @@ -120,7 +134,7 @@ var NOCK_HEADERS_EMPTY_AUTHORIZATIONS_HAS2FA = _.defaults({}, RATELIMIT_HAS2FA);
// Bad no 2FA: test auth
function testAuthNo2FABad () {
this.nock(NOCK_API_URL)
.get(NOCK_API_ENDPOINT)
.post(NOCK_API_ENDPOINT, NOCK_POST_EMPTY)
.reply(
401,
NOCK_BODY_BAD_USERPASS,
Expand All @@ -130,7 +144,7 @@ function testAuthNo2FABad () {
// Bad has 2FA: test auth
function testAuthHas2FABad () {
this.nock(NOCK_API_URL)
.get(NOCK_API_ENDPOINT)
.post(NOCK_API_ENDPOINT, NOCK_POST_EMPTY)
.reply(
401,
NOCK_BODY_BAD_2FA,
Expand Down Expand Up @@ -162,17 +176,17 @@ function makeNewHas2FABad () {
// Good no 2FA: test auth
function testAuthNo2FAGood () {
this.nock(NOCK_API_URL)
.get(NOCK_API_ENDPOINT)
.post(NOCK_API_ENDPOINT, NOCK_POST_EMPTY)
.reply(
200,
NOCK_BODY_EMPTY_AUTHORIZATIONS,
NOCK_HEADERS_EMPTY_AUTHORIZATIONS_NO2FA
422,
NOCK_BODY_BAD_NO2FA_TEST_CREATE,
NOCK_HEADERS_BAD_NO2FA_TEST_CREATE
);
}
// Good has 2FA: test auth
function testAuthHas2FAGood () {
this.nock(NOCK_API_URL)
.get(NOCK_API_ENDPOINT)
.post(NOCK_API_ENDPOINT, NOCK_POST_EMPTY)
.reply(
401,
NOCK_BODY_BAD_2FA,
Expand Down
23 changes: 23 additions & 0 deletions test/github-oauth-prompt_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,29 @@ describe('Oauth', function () {
});
});

describe('userRequires2FA', function () {
it('should error for bad username and password', function () {
apiResponse.testAuth.no2FA.bad();
oauth.userRequires2FA(function (err, res) {
assert.throws(function () {
assert.ifError(err);
});
});
});
it('should respond false for good username and password, no2FA', function () {
apiResponse.testAuth.no2FA.good();
oauth.userRequires2FA(function (err, res) {
assert.equal(res, false);
});
});
it('should respond true for good username and password, has2FA', function () {
apiResponse.testAuth.has2FA.good();
oauth.userRequires2FA(function (err, res) {
assert.equal(res, true);
});
});
});

// Authentication test.
describe('authentication', function () {

Expand Down

0 comments on commit f8149fc

Please sign in to comment.