From 5a766ae7a515d3529505832b19d626d7a9f89eb8 Mon Sep 17 00:00:00 2001 From: Karthik Bhaskara Date: Tue, 10 Jul 2018 17:55:36 -0700 Subject: [PATCH] CR changes. Added validators to AuthClient --- lib/auth-client.js | 24 +++++++++++- lib/validators.js | 33 ----------------- test/lib/auth-client.js | 49 +++++++++++++++++++++++- test/lib/validators.js | 82 ----------------------------------------- 4 files changed, 69 insertions(+), 119 deletions(-) delete mode 100644 lib/validators.js delete mode 100644 test/lib/validators.js diff --git a/lib/auth-client.js b/lib/auth-client.js index 34674557..8aee0c74 100644 --- a/lib/auth-client.js +++ b/lib/auth-client.js @@ -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*/ @@ -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; diff --git a/lib/validators.js b/lib/validators.js deleted file mode 100644 index 51c82133..00000000 --- a/lib/validators.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -const Joi = require('joi'); - -const errors = require('./errors'); - -const validators = {schemas: {}}; - -validators.schemas.requestData = 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(), -}); - -validators.validateRequestData = function(req) { - try { - Joi.assert(req, validators.schemas.requestData); - } catch (err) { - throw new errors.ValidationError(err); - } -}; - -module.exports = validators; diff --git a/test/lib/auth-client.js b/test/lib/auth-client.js index 375810e2..9dd3beba 100644 --- a/test/lib/auth-client.js +++ b/test/lib/auth-client.js @@ -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) { @@ -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'], + })); }); diff --git a/test/lib/validators.js b/test/lib/validators.js deleted file mode 100644 index dc80f3b3..00000000 --- a/test/lib/validators.js +++ /dev/null @@ -1,82 +0,0 @@ -'use strict'; - -const test = require('ava'); - -const validators = require('../../lib/validators'); -const errors = require('../../lib/errors'); - -const CLIENT_ID = '4cf82729-4275-46d9-9255-8437ba777151'; -const CLIENT_SECRET = '4cf82729-4275-46d9-9255-8437ba777151'; -const INVALID_CLIENT_ID = '4cf82729-4275-46d9-9255-87ba151'; - -test('valid request parameters', function(t) { - const requestData = { - clientId: CLIENT_ID, - clientSecret: CLIENT_SECRET, - redirectUri: 'https://insurance.co/callback', - scope: ['read_odometer', 'read_vehicle_info'], - }; - - t.notThrows(() => - validators.validateRequestData(requestData), errors.ValidationError); -}); - -test('invalid uuid parameter', function(t) { - const requestData = { - clientId: INVALID_CLIENT_ID, - clientSecret: CLIENT_SECRET, - redirectUri: 'https://insurance.co/callback', - scope: ['read_odometer', 'read_vehicle_info'], - }; - - t.throws(() => - validators.validateRequestData(requestData), errors.ValidationError); -}); - -test('missing required parameter', function(t) { - const requestData = { - clientId: CLIENT_ID, - redirectUri: 'https://insurance.co/callback', - scope: ['read_odometer', 'read_vehicle_info'], - }; - - t.throws(() => - validators.validateRequestData(requestData), errors.ValidationError); -}); - -test('invalid scope parameter', function(t) { - const requestData = { - clientId: CLIENT_ID, - clientSecret: CLIENT_SECRET, - redirectUri: 'https://insurance.co/callback', - scope: 'read_odometer', - }; - - t.throws(() => - validators.validateRequestData(requestData), errors.ValidationError); -}); - -test('invalid development parameter', function(t) { - const requestData = { - clientId: CLIENT_ID, - clientSecret: CLIENT_SECRET, - redirectUri: 'https://insurance.co/callback', - scope: ['read_odometer', 'read_vehicle_info'], - development: 'truthsies', - }; - - t.throws(() => - validators.validateRequestData(requestData), errors.ValidationError); -}); - -test('iOS and Android redirect uri', function(t) { - const requestData = { - clientId: CLIENT_ID, - clientSecret: CLIENT_SECRET, - redirectUri: 'sc4a1b01e5-0497-417c-a30e-6df6ba33ba46://callback', - scope: ['read_odometer', 'read_vehicle_info'], - }; - - t.notThrows(() => - validators.validateRequestData(requestData), errors.ValidationError); -});