Skip to content

Commit

Permalink
Merge pull request #67 from Hacksore/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Hacksore authored Sep 11, 2020
2 parents 1f51c0c + f5a30b2 commit 5b1ee7a
Show file tree
Hide file tree
Showing 14 changed files with 594 additions and 1,335 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 Hacksore
Copyright (c) 2020 Hacksore

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
39 changes: 32 additions & 7 deletions __tests__/bluelinky.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import got from 'got';
import BlueLinky from '../lib/index';
jest.mock('got');

const gotMock = got as any;

const VEHICLE_MOCK_DATA = {
enrolledVehicleDetails: [
{
Expand Down Expand Up @@ -31,6 +33,30 @@ const VEHICLE_MOCK_DATA = {
],
};

describe('BlueLinky', () => {
it('calls error even when bad creds', done => {
gotMock.mockReturnValueOnce({
body: {
error: {
message: 'you did something wrong!',
},
},
statusCode: 401,
});

const client = new BlueLinky({
username: '[email protected]',
password: '123',
pin: '1234',
region: 'US',
});

client.on('error', error => {
done();
});
});
});

describe('BlueLinky', () => {
beforeEach(() => {
(got as any)
Expand Down Expand Up @@ -75,9 +101,9 @@ describe('BlueLinky', () => {
}).toThrowError('Your region is not supported yet.');
});

it('ready event is called after login', (done) => {
it('ready event is called after login', done => {
const client = new BlueLinky({
username: 'someone@gmai.com',
username: 'someone@gmail.com',
password: 'hunter1',
pin: '1234',
region: 'US',
Expand All @@ -88,30 +114,29 @@ describe('BlueLinky', () => {
});
});

it('getVehicle throws error on bad input', (done) => {
it('getVehicle throws error on bad input', done => {
const client = new BlueLinky({
username: 'someone@gmai.com',
username: 'someone@gmail.com',
password: 'hunter1',
pin: '1234',
region: 'US',
});

client.on('ready', () => {
expect(() => client.getVehicle('test')).toThrowError();

done();
});
});

it('getVehicle returns correct vehicle', (done) => {
it('getVehicle returns correct vehicle', done => {
const client = new BlueLinky({
username: '[email protected]',
password: 'hunter1',
pin: '1234',
region: 'US',
});

client.on('ready', (vehicles) => {
client.on('ready', vehicles => {
const veh1 = client.getVehicle('JEST_TESTING_1');
const veh2 = client.getVehicle('JEST_TESTING_2');

Expand Down
94 changes: 46 additions & 48 deletions __tests__/mock/americanStatus.json
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
{
"vehicleStatus": {
"dateTime": "2020-05-20T23:20:39Z",
"acc": false,
"trunkOpen": false,
"doorLock": false,
"defrostStatus": "false",
"transCond": true,
"doorLockStatus": "false",
"doorOpen": {
"frontRight": 1,
"frontLeft": 1,
"backLeft": 0,
"backRight": 0
},
"airCtrlOn": false,
"airTemp": {
"unit": 1,
"hvacTempType": 1,
"value": "LO"
},
"battery": {
"batSignalReferenceValue": {},
"batSoc": 67,
"batState": 0,
"sjbDeliveryMode": 1
},
"ign3": false,
"ignitionStatus": "false",
"lowFuelLight": false,
"sideBackWindowHeat": 0,
"dte": {
"unit": 3,
"value": 142
},
"engine": false,
"defrost": false,
"hoodOpen": false,
"airConditionStatus": "false",
"steerWheelHeat": 0,
"tirePressureLamp": {
"tirePressureWarningLampRearLeft": 0,
"tirePressureWarningLampFrontLeft": 0,
"tirePressureWarningLampFrontRight": 0,
"tirePressureWarningLampAll": 0,
"tirePressureWarningLampRearRight": 0
},
"trunkOpenStatus": "false"
}
"dateTime": "2020-05-20T23:20:39Z",
"acc": false,
"trunkOpen": false,
"doorLock": false,
"defrostStatus": "false",
"transCond": true,
"doorLockStatus": "false",
"doorOpen": {
"frontRight": 1,
"frontLeft": 1,
"backLeft": 0,
"backRight": 0
},
"airCtrlOn": false,
"airTemp": {
"unit": 1,
"hvacTempType": 1,
"value": "LO"
},
"battery": {
"batSignalReferenceValue": {},
"batSoc": 67,
"batState": 0,
"sjbDeliveryMode": 1
},
"ign3": false,
"ignitionStatus": "false",
"lowFuelLight": false,
"sideBackWindowHeat": 0,
"dte": {
"unit": 3,
"value": 142
},
"engine": false,
"defrost": false,
"hoodOpen": false,
"airConditionStatus": "false",
"steerWheelHeat": 0,
"tirePressureLamp": {
"tirePressureWarningLampRearLeft": 0,
"tirePressureWarningLampFrontLeft": 0,
"tirePressureWarningLampFrontRight": 0,
"tirePressureWarningLampAll": 0,
"tirePressureWarningLampRearRight": 0
},
"trunkOpenStatus": "false"
}
9 changes: 5 additions & 4 deletions __tests__/vehicle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ describe('AmericanVehicle', () => {

it('call status commmand', async () => {
(got as any).mockReturnValueOnce({
body: JSON.stringify(AMERICAN_STATUS_MOCK),
body: JSON.stringify({ vehicleStatus: AMERICAN_STATUS_MOCK }),
statusCode: 200,
});

const response = await vehicle.status({ parsed: true });
expect(response.engine.range).toEqual(AMERICAN_STATUS_MOCK.vehicleStatus.dte.value);
expect(response.engine.range).toEqual(AMERICAN_STATUS_MOCK.dte.value);
});
});

Expand Down Expand Up @@ -130,12 +130,13 @@ describe('EuropeanVehicle', () => {
});

it('call status commmand', async () => {
// mocks the pin code request
// mock the enterPin request
(got as any).mockReturnValueOnce({
body: {},
body: { controlToken: 'fake', controlTokenExpiresAt: 10000000 },
statusCode: 200,
});

// mock the status request
(got as any).mockReturnValueOnce({
body: EUROPE_STATUS_MOCK,
statusCode: 200,
Expand Down
61 changes: 27 additions & 34 deletions lib/controllers/american.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,51 +40,44 @@ export class AmericanController extends SessionController {
+new Date() / 1000 + parseInt(response.body.expires_in)
);

return Promise.resolve('Token refreshed');
return 'Token refreshed';
}

return Promise.resolve('Token not expired, no need to refresh');
return 'Token not expired, no need to refresh';
}

// TODO: come up with a better return value?
public async login(): Promise<string> {
try {
logger.debug('Logging in to API');
logger.debug('Logging in to API');

const response = await got(`${BASE_URL}/v2/ac/oauth/token`, {
method: 'POST',
body: {
username: this.userConfig.username,
password: this.userConfig.password,
},
headers: {
'client_secret': CLIENT_SECRET,
'client_id': CLIENT_ID,
},
json: true,
});

if (response.statusCode !== 200) {
return Promise.resolve('login bad');
}

this.session.accessToken = response.body.access_token;
this.session.refreshToken = response.body.refresh_token;
this.session.tokenExpiresAt = Math.floor(
+new Date() / 1000 + parseInt(response.body.expires_in)
);
const response = await got(`${BASE_URL}/v2/ac/oauth/token`, {
method: 'POST',
body: {
username: this.userConfig.username,
password: this.userConfig.password,
},
headers: {
'client_secret': CLIENT_SECRET,
'client_id': CLIENT_ID,
},
json: true,
});

return Promise.resolve('login good');
} catch (err) {
logger.debug(err.body);
Promise.reject(err);
if (response.statusCode !== 200) {
return 'login bad';
}

return Promise.reject('login bad');
this.session.accessToken = response.body.access_token;
this.session.refreshToken = response.body.refresh_token;
this.session.tokenExpiresAt = Math.floor(
+new Date() / 1000 + parseInt(response.body.expires_in)
);

return 'login good';
}

public async logout(): Promise<string> {
return Promise.resolve('OK');
return 'OK';
}

async getVehicles(): Promise<Array<Vehicle>> {
Expand All @@ -104,7 +97,7 @@ export class AmericanController extends SessionController {

if (data.enrolledVehicleDetails === undefined) {
this.vehicles = [];
return Promise.resolve(this.vehicles);
return this.vehicles;
}

data.enrolledVehicleDetails.forEach(vehicle => {
Expand All @@ -123,6 +116,6 @@ export class AmericanController extends SessionController {
this.vehicles.push(new AmericanVehicle(vehicleConfig, this));
});

return Promise.resolve(this.vehicles);
return this.vehicles;
}
}
Loading

0 comments on commit 5b1ee7a

Please sign in to comment.