Skip to content

Commit

Permalink
CR changes. Added validators to AuthClient
Browse files Browse the repository at this point in the history
  • Loading branch information
KarthikBhaskara committed Jul 11, 2018
1 parent 2615888 commit 5a766ae
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 119 deletions.
24 changes: 22 additions & 2 deletions lib/auth-client.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

const Joi = require('joi');
const qs = require('querystring');

const util = require('./util');
const config = require('./config');
const validators = require('./validators');

/** @exports AuthClient*/

Expand Down Expand Up @@ -51,7 +51,27 @@ const validators = require('./validators');
*/
function AuthClient(options) {

validators.validateRequestData(options);
const schema = Joi.object().keys({
clientId: Joi.string().guid({
version: [
'uuidv4',
],
}).required(),
clientSecret: Joi.string().guid({
version: [
'uuidv4',
],
}).required(),
redirectUri: Joi.string().uri().required(),
scope: Joi.array().items(Joi.string()),
development: Joi.boolean(),
});

try {
Joi.assert(options, schema);
} catch (err) {
throw err;
}

this.clientId = options.clientId;
this.clientSecret = options.clientSecret;
Expand Down
33 changes: 0 additions & 33 deletions lib/validators.js

This file was deleted.

49 changes: 47 additions & 2 deletions test/lib/auth-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const test = require('ava');
const nock = require('nock');

const AuthClient = require('../../lib/auth-client');
const errors = require('../../lib/errors');

const CLIENT_ID = '4cf82729-4275-46d9-9255-8437ba777151';
const INVALID_CLIENT_ID = '4cf82729-4275-46d9-9255-87ba151';
const CLIENT_SECRET = '4cf82729-4275-46d9-9255-8437ba777151';

test('constructor', function(t) {
Expand All @@ -34,7 +34,52 @@ test('constructor - validation error', function(t) {
clientId: 'f3266b17-961d-4295-8544-054c7bd94fbb',
redirectUri: 'https://insurance.co/callback',
scope: ['read_odometer', 'read_vehicle_info'],
}), errors.ValidationError);
}), Error);

});

test('constructor - invalid uuid parameter', function(t) {

t.throws(() => new AuthClient({
clientId: INVALID_CLIENT_ID,
clientSecret: CLIENT_SECRET,
redirectUri: 'https://insurance.co/callback',
scope: ['read_odometer', 'read_vehicle_info'],
}), Error);

});

test('constructor - invalid scope parameter', function(t) {

t.throws(() => new AuthClient({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
redirectUri: 'https://insurance.co/callback',
scope: 'read_odometer',
}), Error);

});

test('constructor - invalid development parameter', function(t) {

t.throws(() => new AuthClient({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
redirectUri: 'https://insurance.co/callback',
scope: ['read_odometer', 'read_vehicle_info'],
development: 'truthsies',
}), Error);

});

test('iOS and Android redirect uri', function(t) {

t.notThrows(() => new AuthClient({
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
redirectUri: 'sc4a1b01e5-0497-417c-a30e-6df6ba33ba46://callback',
scope: ['read_odometer', 'read_vehicle_info'],
}));

});

Expand Down
82 changes: 0 additions & 82 deletions test/lib/validators.js

This file was deleted.

0 comments on commit 5a766ae

Please sign in to comment.