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
3 changes: 2 additions & 1 deletion doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ Initializes Smartcar class.
| options.redirectUri | <code>String</code> | | the registered redirect uri of the application |
| [options.scope] | <code>Array.&lt;String&gt;</code> | | requested permission scopes |
| [options.onComplete] | [<code>OnComplete</code>](#OnComplete) | | called on completion of Smartcar Connect |
| [options.testMode] | <code>Boolean</code> | <code>false</code> | launch Smartcar Connect in test mode |
| [options.testMode] | <code>Boolean</code> | <code>false</code> | Deprecated, please use `mode` instead. Launch Smartcar Connect in [test mode](https://smartcar.com/docs/guides/testing/). |
| [options.mode] | <code>String</code> | <code>&#x27;live&#x27;</code> | Determine what mode Smartcar Connect should be launched in. Should be one of test, live or simulated. |

<a name="Smartcar+getAuthUrl"></a>

Expand Down
23 changes: 20 additions & 3 deletions src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ class Smartcar {
* application
* @param {String[]} [options.scope] - requested permission scopes
* @param {OnComplete} [options.onComplete] - called on completion of Smartcar Connect
* @param {Boolean} [options.testMode=false] - launch Smartcar Connect in test mode
*/
* @param {Boolean} [options.testMode=false] - Deprecated, please use `mode` instead.
* Launch Smartcar Connect in [test mode](https://smartcar.com/docs/guides/testing/).
* @param {String} [options.mode='live'] - Determine what mode Smartcar Connect should be
* launched in. Should be one of test, live or simulated.
*/
constructor(options) {
// polyfill String.prototype.startsWith for IE11 support
// istanbul ignore next
Expand All @@ -44,7 +47,21 @@ class Smartcar {
this.redirectUri = options.redirectUri;
this.scope = options.scope;
this.onComplete = options.onComplete;
this.mode = options.testMode === true ? 'test' : 'live';
this.mode = 'live';
if (options.hasOwnProperty('testMode')) {
// eslint-disable-next-line no-console
console.warn(
'The "testMode" parameter is deprecated, please use the "mode" parameter instead.',
);
this.mode = options.testMode === true ? 'test' : 'live';
} else if (options.hasOwnProperty('mode')) {
this.mode = options.mode;
}
if (!['test', 'live', 'simulated'].includes(this.mode)) {
throw new Error(
'The "mode" parameter MUST be one of the following: \'test\', \'live\', \'simulated\'',
);
}
this.responseType = 'code';
// identifier for matching message event and multiple Smartcar instances
// it is a string composed of a timestamp and a 8-digit random number
Expand Down
98 changes: 87 additions & 11 deletions test/unit/sdk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,27 @@ describe('sdk', () => {
expect(link).toEqual(expectedLink);
});

test('generates test mode link', () => {
test('generates live mode link using testMode (Deprecated)', () => {
const options = {
clientId: 'clientId',
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
};

const smartcar = new Smartcar(options);

const expectedLink =
`https://connect.smartcar.com/oauth/authorize?response_type=code&client_id=clientId&redirect_uri=https%3A%2F%2Fsmartcar.com&approval_prompt=force&scope=read_vehicle_info%20read_odometer&mode=live&state=${getEncodedState(smartcar.instanceId)}`;
const link = smartcar.getAuthUrl({
state: originalState,
forcePrompt: true,
});
expect(link).toEqual(expectedLink);
});

test('generates test mode link using testMode (Deprecated)', () => {
const options = {
clientId: 'clientId',
redirectUri: 'https://smartcar.com',
Expand All @@ -730,13 +750,53 @@ describe('sdk', () => {
expect(link).toEqual(expectedLink);
});

test('generates test mode link', () => {
const options = {
clientId: 'clientId',
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
mode: 'test',
};

const smartcar = new Smartcar(options);

const expectedLink =
`https://connect.smartcar.com/oauth/authorize?response_type=code&client_id=clientId&redirect_uri=https%3A%2F%2Fsmartcar.com&approval_prompt=force&scope=read_vehicle_info%20read_odometer&mode=test&state=${getEncodedState(smartcar.instanceId)}`;
const link = smartcar.getAuthUrl({
state: originalState,
forcePrompt: true,
});
expect(link).toEqual(expectedLink);
});

test('generates simulated mode link', () => {
const options = {
clientId: 'clientId',
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
mode: 'simulated',
};

const smartcar = new Smartcar(options);

const expectedLink =
`https://connect.smartcar.com/oauth/authorize?response_type=code&client_id=clientId&redirect_uri=https%3A%2F%2Fsmartcar.com&approval_prompt=force&scope=read_vehicle_info%20read_odometer&mode=simulated&state=${getEncodedState(smartcar.instanceId)}`;
const link = smartcar.getAuthUrl({
state: originalState,
forcePrompt: true,
});
expect(link).toEqual(expectedLink);
});

test('generates live mode link', () => {
const options = {
clientId: 'clientId',
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
mode: 'live',
};

const smartcar = new Smartcar(options);
Expand All @@ -750,13 +810,29 @@ describe('sdk', () => {
expect(link).toEqual(expectedLink);
});

test('constructor errors on invalid mode', () => {
expect(
() =>
new Smartcar({
clientId: 'clientId',
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
mode: 'pizza',
}),
).toThrow(
'The "mode" parameter MUST be one of the following: \'test\', \'live\', \'simulated\'',
);
});


test('generate link when vehicleInfo={...} included', () => {
const options = {
clientId: 'clientId',
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
mode: 'live',
};

const smartcar = new Smartcar(options);
Expand All @@ -781,7 +857,7 @@ describe('sdk', () => {
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
mode: 'live',
};

const smartcar = new Smartcar(options);
Expand All @@ -803,7 +879,7 @@ describe('sdk', () => {
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
mode: 'live',
};

const smartcar = new Smartcar(options);
Expand All @@ -829,7 +905,7 @@ describe('sdk', () => {
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
mode: 'live',
};

const smartcar = new Smartcar(options);
Expand Down Expand Up @@ -857,7 +933,7 @@ describe('sdk', () => {
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
mode: 'live',
};

const smartcar = new Smartcar(options);
Expand Down Expand Up @@ -886,7 +962,7 @@ describe('sdk', () => {
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
mode: 'live',
};

const smartcar = new Smartcar(options);
Expand Down Expand Up @@ -914,7 +990,7 @@ describe('sdk', () => {
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
mode: 'live',
};

const smartcar = new Smartcar(options);
Expand All @@ -940,7 +1016,7 @@ describe('sdk', () => {
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
mode: 'live',
};

const smartcar = new Smartcar(options);
Expand All @@ -963,7 +1039,7 @@ describe('sdk', () => {
redirectUri: 'https://smartcar.com',
scope: ['read_vehicle_info', 'read_odometer'],
onComplete: jest.fn(),
testMode: false,
mode: 'live',
};

const smartcar = new Smartcar(options);
Expand Down