test/unit/services.badge.js
import test from 'ava';
import nock from 'nock';
import {
client,
clinical6,
Badge,
} from '../../src';
test.before('start server', (t) => {
t.context.storage = client.storageUtility;
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.getResponseAll = {
data: [
{
id: '17',
type: 'badges',
attributes: {
created_at: '2017-08-09T16:23:56Z',
updated_at: '2017-08-09T16:23:56Z',
title: 'dummy_462',
description: 'Demo Content info...',
redemption_points: 250,
image: {
url: '/uploads/test/reward/image/17/1.png',
small: {
url: '/uploads/test/reward/image/17/small_1.png'
},
small_hd: {
url: '/uploads/test/reward/image/17/small_hd_1.png'
},
fullscreen: {
url: '/uploads/test/reward/image/17/fullscreen_1.png'
},
main: {
url: '/uploads/test/reward/image/17/main_1.png'
},
fullscreen_hd: {
url: '/uploads/test/reward/image/17/fullscreen_hd_1.png'
},
main_hd: {
url: '/uploads/test/reward/image/17/main_hd_1.png'
}
},
position: 1,
enabled: true,
featured: null,
based_on: null,
threshold: null,
time_unit: null,
start_point: null,
start_at: null,
disabled_image: {
url: '/default/default.png',
small: {
url: '/default/small_default.png'
},
small_hd: {
url: '/default/small_hd_default.png'
},
fullscreen: {
url: '/default/fullscreen_default.png'
},
main: {
url: '/default/main_default.png'
},
fullscreen_hd: {
url: '/default/fullscreen_hd_default.png'
},
main_hd: {
url: '/default/main_hd_default.png'
}
},
cache_token: 'xMnLvzU_uu4'
}
},
{
id: '18',
type: 'badges',
attributes: {
created_at: '2017-08-09T16:23:56Z',
updated_at: '2017-08-09T16:23:56Z',
title: 'dummy_463',
description: 'Demo Content info...',
redemption_points: 250,
image: {
url: '/uploads/test/reward/image/18/1.png',
small: {
url: '/uploads/test/reward/image/18/small_1.png'
},
small_hd: {
url: '/uploads/test/reward/image/18/small_hd_1.png'
},
fullscreen: {
url: '/uploads/test/reward/image/18/fullscreen_1.png'
},
main: {
url: '/uploads/test/reward/image/18/main_1.png'
},
fullscreen_hd: {
url: '/uploads/test/reward/image/18/fullscreen_hd_1.png'
},
main_hd: {
url: '/uploads/test/reward/image/18/main_hd_1.png'
}
},
position: 2,
enabled: true,
featured: null,
based_on: null,
threshold: null,
time_unit: null,
start_point: null,
start_at: null,
disabled_image: {
url: '/default/default.png',
small: {
url: '/default/small_default.png'
},
small_hd: {
url: '/default/small_hd_default.png'
},
fullscreen: {
url: '/default/fullscreen_default.png'
},
main: {
url: '/default/main_default.png'
},
fullscreen_hd: {
url: '/default/fullscreen_hd_default.png'
},
main_hd: {
url: '/default/main_hd_default.png'
}
},
cache_token: 'DGMVAICukoc'
}
},
{
id: '19',
type: 'badges',
attributes: {
created_at: '2017-08-09T16:23:56Z',
updated_at: '2017-08-09T16:23:56Z',
title: 'dummy_464',
description: 'Demo Content info...',
redemption_points: 250,
image: {
url: '/uploads/test/reward/image/19/1.png',
small: {
url: '/uploads/test/reward/image/19/small_1.png'
},
small_hd: {
url: '/uploads/test/reward/image/19/small_hd_1.png'
},
fullscreen: {
url: '/uploads/test/reward/image/19/fullscreen_1.png'
},
main: {
url: '/uploads/test/reward/image/19/main_1.png'
},
fullscreen_hd: {
url: '/uploads/test/reward/image/19/fullscreen_hd_1.png'
},
main_hd: {
url: '/uploads/test/reward/image/19/main_hd_1.png'
}
},
position: 3,
enabled: true,
featured: null,
based_on: null,
threshold: null,
time_unit: null,
start_point: null,
start_at: null,
disabled_image: {
url: '/default/default.png',
small: {
url: '/default/small_default.png'
},
small_hd: {
url: '/default/small_hd_default.png'
},
fullscreen: {
url: '/default/fullscreen_default.png'
},
main: {
url: '/default/main_default.png'
},
fullscreen_hd: {
url: '/default/fullscreen_hd_default.png'
},
main_hd: {
url: '/default/main_hd_default.png'
}
},
cache_token: '6vjdF2bL2Tg'
}
}
]
};
t.context.getResponseId = {
data: {
id: '4',
type: 'badges',
attributes: {
created_at: '2017-08-09T16:23:55Z',
updated_at: '2017-08-09T16:23:55Z',
title: 'dummy_440',
description: 'Demo Content info...',
redemption_points: 250,
image: {
url: '/uploads/test/reward/image/4/1.png',
small: {
url: '/uploads/test/reward/image/4/small_1.png'
},
small_hd: {
url: '/uploads/test/reward/image/4/small_hd_1.png'
},
fullscreen: {
url: '/uploads/test/reward/image/4/fullscreen_1.png'
},
main: {
url: '/uploads/test/reward/image/4/main_1.png'
},
fullscreen_hd: {
url: '/uploads/test/reward/image/4/fullscreen_hd_1.png'
},
main_hd: {
url: '/uploads/test/reward/image/4/main_hd_1.png'
}
},
position: 4,
enabled: true,
featured: null,
based_on: null,
threshold: null,
time_unit: null,
start_point: null,
start_at: null,
disabled_image: {
url: '/default/default.png',
small: {
url: '/default/small_default.png'
},
small_hd: {
url: '/default/small_hd_default.png'
},
fullscreen: {
url: '/default/fullscreen_default.png'
},
main: {
url: '/default/main_default.png'
},
fullscreen_hd: {
url: '/default/fullscreen_hd_default.png'
},
main_hd: {
url: '/default/main_hd_default.png'
}
},
cache_token: 'QBPu8whNUSE'
}
}
};
t.context.postResponse = {
data: {
id: '16',
type: 'badges',
attributes: {
created_at: '2017-08-09T16:23:56Z',
updated_at: '2017-08-09T16:23:56Z',
title: 'dummy_459',
description: 'Demo Content info...',
redemption_points: 250,
image: {
url: '/default/default.png',
small: {
url: '/default/small_default.png'
},
small_hd: {
url: '/default/small_hd_default.png'
},
fullscreen: {
url: '/default/fullscreen_default.png'
},
main: {
url: '/default/main_default.png'
},
fullscreen_hd: {
url: '/default/fullscreen_hd_default.png'
},
main_hd: {
url: '/default/main_hd_default.png'
}
},
position: 4,
enabled: true,
featured: null,
based_on: null,
threshold: null,
time_unit: null,
start_point: null,
start_at: null,
disabled_image: {
url: '/default/default.png',
small: {
url: '/default/small_default.png'
},
small_hd: {
url: '/default/small_hd_default.png'
},
fullscreen: {
url: '/default/fullscreen_default.png'
},
main: {
url: '/default/main_default.png'
},
fullscreen_hd: {
url: '/default/fullscreen_hd_default.png'
},
main_hd: {
url: '/default/main_hd_default.png'
}
},
cache_token: '3F2Lr5caMIQ'
}
}
};
t.context.patchResponse = {
data: {
id: '4',
type: 'badges',
attributes: {
created_at: '2017-08-09T17:26:59Z',
updated_at: '2017-08-09T17:26:59Z',
title: 'dummy_1065',
description: 'Demo Content info...',
redemption_points: 250,
image: {
url: '/uploads/test/reward/image/4/1.png',
small: {
url: '/uploads/test/reward/image/4/small_1.png'
},
small_hd: {
url: '/uploads/test/reward/image/4/small_hd_1.png'
},
fullscreen: {
url: '/uploads/test/reward/image/4/fullscreen_1.png'
},
main: {
url: '/uploads/test/reward/image/4/main_1.png'
},
fullscreen_hd: {
url: '/uploads/test/reward/image/4/fullscreen_hd_1.png'
},
main_hd: {
url: '/uploads/test/reward/image/4/main_hd_1.png'
}
},
position: 4,
enabled: true,
featured: null,
based_on: null,
threshold: null,
time_unit: null,
start_point: null,
start_at: null,
disabled_image: {
url: '/default/default.png',
small: {
url: '/default/small_default.png'
},
small_hd: {
url: '/default/small_hd_default.png'
},
fullscreen: {
url: '/default/fullscreen_default.png'
},
main: {
url: '/default/main_default.png'
},
fullscreen_hd: {
url: '/default/fullscreen_hd_default.png'
},
main_hd: {
url: '/default/main_hd_default.png'
}
},
cache_token: 'iP78O8Ag65c'
}
}
};
t.context.badgeJsonApi = {
data: {
id: 4,
type: 'badges',
attributes: {
title: 'dummy_1065',
description: 'Demo Content info...',
redemption_points: 250
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.badge = new Badge(t.context.badgeJsonApi);
});
// test.afterEach(t => t.context.server.restore());
// BadgeService.delete method
/**
* @test {Clinical6.delete}
*/
test('[unit] BadgeService.delete should throw errors for invalid parameters', async (t) => {
const undefinedError = 'BadgeService.delete error: badge does not have id';
await t.throwsAsync(clinical6.delete(new Badge()), undefinedError);
});
/**
* @test {Clinical6.delete}
*/
test('[unit] BadgeService.delete should make a properly formatted delete request and response', async (t) => {
const { badge } = t.context;
badge.id = 5;
let request = {};
nock(client.apiBaseUrl).delete(/\/v3\/badges\/([0-9]*)$/).reply(function () {
request = this.req;
return [200, ''];
});
const response = await clinical6.delete(badge);
t.is(request.path, `/v3/badges/${badge.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);
});
/**
* @test {Clinical6.delete}
*/
test('[unit] BadgeService.delete should remove the element from local storage', async (t) => {
const { badge, storage } = t.context;
badge.id = 5;
nock(client.apiBaseUrl).delete(/\/v3\/badges\/([0-9]*)$/).reply(200, '');
await storage.set('badges', badge.toJSON(), { id: badge.id });
await clinical6.delete(badge);
t.is(storage.has('badges', { id: badge.id }), false);
});
// BadgeService.get method
/**
* @test {Clinical6.get}
*/
test.serial('[unit] BadgeService.get should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'BadgeService.get error: requires authToken';
await t.throwsAsync(clinical6.get(Badge), expectedError);
});
/**
* @test {Clinical6.get}
*/
test('[unit] BadgeService.get should make a properly formatted get request and response without an id', async (t) => {
const { getResponseAll } = t.context;
let request = {};
nock(client.apiBaseUrl).get('/v3/badges').reply(function () {
request = this.req;
return [200, getResponseAll];
});
const response = await clinical6.get(Badge);
t.is(request.path, `/v3/badges`);
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.truthy(response);
t.is(Object.keys(response).length, 3);
});
/**
* @test {Clinical6.get}
*/
test('[unit] BadgeService.get should receive a valid response for a get request with an id', async (t) => {
const { getResponseId } = t.context;
nock(client.apiBaseUrl).get('/v3/badges/4').reply(200, getResponseId);
const response = await clinical6.get(new Badge({ id: 4 }));
t.truthy(response);
t.is(response.id, 4);
t.is(response.type, 'badges');
t.is(response.createdAt, '2017-08-09T16:23:55Z');
t.is(response.updatedAt, '2017-08-09T16:23:55Z');
t.is(response.title, 'dummy_440');
t.is(response.description, 'Demo Content info...');
t.is(response.redemptionPoints, 250);
t.is(response.image.url, '/uploads/test/reward/image/4/1.png');
t.is(response.position, 4);
t.is(response.enabled, true);
t.is(response.featured, null);
t.is(response.basedOn, null);
t.is(response.threshold, null);
t.is(response.timeUnit, null);
t.is(response.startPoint, null);
t.is(response.startAt, null);
t.is(response.disabledImage.url, '/default/default.png');
t.is(response.cacheToken, 'QBPu8whNUSE');
});
// BadgeService.insert method
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] BadgeService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'BadgeService.insert error: requires authToken';
await t.throwsAsync(clinical6.insert(new Badge()), expectedError);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] BadgeService.insert should successfully insert a badge with a badge object', async (t) => {
const { postResponse } = t.context;
let request = {};
nock(client.apiBaseUrl).post('/v3/badges').reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, postResponse];
});
const requestJsonApi = {
data: {
type: 'badges',
attributes: {
title: 'dummy_459',
description: 'Demo Content info...',
redemption_points: 250
}
}
};
client.mobileApplicationKey = '0355b2fb1c8fa7210bec203abb32b916';
const badge = new Badge(requestJsonApi);
const response = await clinical6.insert(badge);
t.is(request.path, `/v3/badges`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, requestJsonApi);
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 16);
t.is(response.type, 'badges');
t.is(response.cacheToken, '3F2Lr5caMIQ');
});
/**
* @test {Clinical6.insert}
*/
// test('[unit] BadgeService.insert should throw an error with an invalid token', async (t) => {
// clinical6.cacheMode = 'networkOnly';
// t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/badges`,
// [422, { 'Content-Type': 'application/json' }, JSON.stringify({
// errors: [
// {
// source: {
// pointer: '/data/attributes/mobile_application_key'
// },
// detail: 'can\'t be blank'
// }
// ]
// })]
// );
// const requestJsonApi = {
// data: {
// type: 'badges',
// attributes: {
// push_id: 'FAKE_ID',
// mobile_application_key: 'invalid'
// }
// }
// };
// client.mobileApplicationKey = 'invalid';
// let response;
// await clinical6.insert().catch(res => (response = res));
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/v3/badges`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify(requestJsonApi));
// t.is(request.requestHeaders['Content-Type'], 'application/json');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
// t.truthy(response);
// });
// BadgeService.update method
/**
* @test {Clinical6.update}
*/
test('[unit] BadgeService.update should successfully update a badge with a badge object', async (t) => {
const { badge, patchResponse, badgeJsonApi } = t.context;
let request = {};
nock(client.apiBaseUrl).patch(/\/v3\/badges\/([0-9]*)$/).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, patchResponse];
});
const response = await clinical6.update(badge);
t.is(request.path, `/v3/badges/${badge.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.deepEqual(request.requestBody, badgeJsonApi);
t.is(response.id, 4);
t.is(response.type, 'badges');
});