test/unit/helpers.device.js
import test from 'ava';
import nock from 'nock';
import {
client,
Device,
} from '../../src';
// See tests.js for testing client creation
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
// patch response
t.context.updateResponse = {
data: {
id: '288',
type: 'devices',
attributes: {
udid: 'xzv8hcoqitac137bsiq9',
technology: 'ios',
access_token: 'dummy_token',
push_id: 'mdsv5swmu39rcccnak9nuzkgfdw8nz3igazqq15lpo05vgpvdz1vvcp5gfsarvgl',
created_at: '2018-11-02T20:43:49Z',
updated_at: '2018-11-02T20:43:49Z',
app_version: null
}
}
};
// insertResponse
t.context.insertResponse = {
data: {
id: '285',
type: 'devices',
attributes: {
udid: null,
technology: null,
access_token: 'd1ac4d1c454abbe50ba12b9b577f9614c3f7817ec91727db00d9cf1c93cb4848',
push_id: null,
created_at: '2018-11-02T20:43:49Z',
updated_at: '2018-11-02T20:43:49Z',
app_version: null
}
}
};
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
// deviceJsonApi
t.context.deviceJsonApi = {
id: 1,
type: 'devices',
attributes: {
udid: 'this-is-a-udid-string',
technology: 'ios',
access_token: 'cd68fa04e458d6d1a9d29faec6a329d3',
push_id: '7a02jf0s8dhf8a',
created_at: '2017-05-19T17:21:26.311Z',
updated_at: '2017-05-19T17:21:26.311Z',
app_version: '0.0.5'
}
};
// deviceJson
t.context.deviceJson = {
id: '1',
udid: 'this-is-a-udid-string',
technology: 'ios',
access_token: 'cd68fa04e458d6d1a9d29faec6a329d3',
push_id: '7a02jf0s8dhf8a',
created_at: '2017-05-19T17:21:26.311Z',
updated_at: '2017-05-19T17:21:26.311Z',
app_version: '0.0.5'
};
t.context.reportedActionJsonApi = {
action: 'callback',
resource: 'patient',
context: {
value: 'approved'
}
};
t.context.storage = client.storageUtility;
t.context.device = new Device(t.context.deviceJsonApi);
});
/**
* @test {Device}
*/
test('[unit] Device should handle device data with a normal json format', (t) => {
const { deviceJson } = t.context;
const device = new Device(deviceJson);
t.is(device.id, 1);
t.is(device.type, 'devices');
t.is(device.udid, 'this-is-a-udid-string');
t.is(device.uuid, 'this-is-a-udid-string');
device.uuid = 'this-is-a-udid-string';
t.is(device.uuid, 'this-is-a-udid-string');
t.is(device.technology, 'ios');
t.is(device.accessToken, 'cd68fa04e458d6d1a9d29faec6a329d3');
t.is(device.pushId, '7a02jf0s8dhf8a');
t.is(device.createdAt, '2017-05-19T17:21:26.311Z');
t.is(device.updatedAt, '2017-05-19T17:21:26.311Z');
t.is(device.appVersion, '0.0.5');
});
/**
* @test {Device}
*/
test('[unit] Device should handle device data with json api format', (t) => {
const { deviceJsonApi } = t.context;
const device = new Device({ data: deviceJsonApi });
t.is(device.id, 1);
t.is(device.type, 'devices');
t.is(device.udid, 'this-is-a-udid-string');
t.is(device.uuid, 'this-is-a-udid-string');
device.uuid = 'this-is-a-udid-string';
t.is(device.uuid, 'this-is-a-udid-string');
t.is(device.technology, 'ios');
t.is(device.accessToken, 'cd68fa04e458d6d1a9d29faec6a329d3');
t.is(device.pushId, '7a02jf0s8dhf8a');
t.is(device.createdAt, '2017-05-19T17:21:26.311Z');
t.is(device.updatedAt, '2017-05-19T17:21:26.311Z');
t.is(device.appVersion, '0.0.5');
});
/**
* @test {Device}
*/
test('[unit] Device should generate json api format when converted to string', (t) => {
const { deviceJsonApi } = t.context;
let device = new Device({ data: deviceJsonApi });
t.is(JSON.stringify(device), JSON.stringify(deviceJsonApi));
device = new Device({ data: deviceJsonApi });
t.is(JSON.stringify(device), JSON.stringify(deviceJsonApi));
});
/**
* @test {Device.delete}
*/
test('[unit] Device.delete should successfully delete an event', async (t) => {
const { deviceJsonApi } = t.context;
const device = new Device({ data: deviceJsonApi });
let request = {};
// mock delete response
nock(client.apiBaseUrl).delete(`/v3/devices/${device.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [204, ''];
});
const response = await device.delete();
t.is(request.path, `/v3/devices/${device.id}`);
t.is(request.headers.accept, 'application/json');
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.falsy(response);
});
// Device.save
/**
* @test {Device.save}
*/
test('[unit] Device.save should successfully insert a device with a device object when id does not exist', async (t) => {
const { deviceJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(deviceJsonApi));
delete json.id;
client.device.pushId = '7a02jf0s8dhf8a';
client.mobileApplicationKey = '508b09b82b9436dde789c9375e98ed5c';
let request = {};
nock(client.apiBaseUrl).post(`/v3/devices`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await client.device.save();
t.is(request.path, `/v3/devices`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, {
data: {
type: 'devices',
attributes: {
mobile_application_key: client.mobileApplicationKey,
push_id: '7a02jf0s8dhf8a'
}
}
});
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 285);
t.is(response.type, 'devices');
t.is(response.accessToken, 'd1ac4d1c454abbe50ba12b9b577f9614c3f7817ec91727db00d9cf1c93cb4848');
t.is(response.createdAt, '2018-11-02T20:43:49Z');
t.is(response.updatedAt, '2018-11-02T20:43:49Z');
});
/**
* @test {Device.save}
*/
test('[unit] Device.save should successfully update a device with a device object when id exists', async (t) => {
const { deviceJsonApi, updateResponse } = t.context;
const device = new Device({ data: deviceJsonApi });
let request = {};
nock(client.apiBaseUrl).patch(`/v3/devices/${device.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, updateResponse];
});
const response = await device.save();
t.is(request.path, `/v3/devices/${device.id}`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: deviceJsonApi });
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 288);
t.is(response.type, 'devices');
t.is(device.uuid, 'this-is-a-udid-string');
t.is(device.technology, 'ios');
t.is(device.accessToken, 'cd68fa04e458d6d1a9d29faec6a329d3');
t.is(device.pushId, '7a02jf0s8dhf8a');
t.is(response.createdAt, '2018-11-02T20:43:49Z');
t.is(response.updatedAt, '2018-11-02T20:43:49Z');
t.is(device.appVersion, '0.0.5');
});