test/unit/services.sign-account.js
import test from 'ava';
import nock from 'nock';
import { client, SignAccount, clinical6 } from '../../src';
test.before('start server', (t) => {
t.context.storage = client.storageUtility;
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.getResponseAll = {
data: [
{
id: 1,
type: 'adobe_sign__accounts',
attributes: {
email: 'person1@example.com',
created_at: '2018-08-13T16:04:10Z',
updated_at: '2018-08-13T16:04:10Z',
linked: false
}
}
]
};
t.context.signAccountJsonApi = {
data: {
type: 'adobe_sign__accounts',
attributes: {
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.signAccount = new SignAccount(t.context.signAccountJsonApi);
});
// SignAccountService.get strategy
/**
* @test {Clinical6.get}
*/
test.serial('[unit] SignAccountService.get should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'SignAccountService.get error: requires authToken';
await t.throwsAsync(clinical6.get(SignAccount), expectedError);
});
/**
* @test {Clinical6.get}
*/
test('[unit] SignAccountService.get should make a properly formatted get request', async (t) => {
const { getResponseAll } = t.context;
let request = {};
nock(client.apiBaseUrl).get(`/v3/adobe_sign/accounts`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseAll];
});
await clinical6.get(SignAccount);
t.is(request.path, `/v3/adobe_sign/accounts`);
t.is(request.headers.accept, 'application/json');
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
});
/**
* @test {Clinical6.get}
*/
test('[unit] SignAccountService.get should receive a valid response for a get request without an id', async (t) => {
const { getResponseAll } = t.context;
nock(client.apiBaseUrl).get(`/v3/adobe_sign/accounts`).reply(200, getResponseAll);
const response = await clinical6.get(SignAccount);
t.truthy(response);
t.is(Object.keys(response).length, 1);
t.is(response[0].id, 1);
t.is(response[0].type, 'adobe_sign__accounts');
t.is(response[0].email, 'person1@example.com');
t.is(response[0].createdAt, '2018-08-13T16:04:10Z');
t.is(response[0].updatedAt, '2018-08-13T16:04:10Z');
t.is(response[0].linked, false);
});
// SignAccountService.insert sign account
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] SignAccountService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'SignAccountService.insert error: requires authToken';
await t.throwsAsync(clinical6.insert(new SignAccount()), expectedError);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] ConsentStrategyService.insert should successfully insert a consentStrategy with a consentStrategy object', async (t) => {
let request = {};
nock(client.apiBaseUrl).post(`/v3/adobe_sign/accounts`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, {
data: {
id: '1',
type: 'adobe_sign__accounts',
attributes: {
email: 'eguillen+unique4@parallel6.com',
created_at: '2018-08-16T18:11:58Z',
updated_at: '2018-08-16T18:11:58Z',
linked: false,
password: null,
first_name: 'CanBe',
last_name: 'Repeated',
title: null,
company_name: 'RepeatableCompany',
country_code: null,
phone_number: null,
locale: null,
time_zone: null
}
}
}];
});
const requestJsonApi = {
data: {
type: 'adobe_sign__accounts',
attributes: {
email: 'eguillen+unique4@parallel6.com',
first_name: 'CanBe',
last_name: 'Repeated',
company_name: 'RepeatableCompany'
}
}
};
const signAccount = new SignAccount(requestJsonApi);
const response = await clinical6.insert(signAccount);
t.is(request.path, `/v3/adobe_sign/accounts`);
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, 1);
t.is(response.type, 'adobe_sign__accounts');
t.is(response.email, 'eguillen+unique4@parallel6.com');
t.is(response.createdAt, '2018-08-16T18:11:58Z');
t.is(response.updatedAt, '2018-08-16T18:11:58Z');
t.is(response.linked, false);
t.is(response.password, null);
t.is(response.firstName, 'CanBe');
t.is(response.lastName, 'Repeated');
t.is(response.title, null);
t.is(response.companyName, 'RepeatableCompany');
t.is(response.countryCode, null);
t.is(response.phoneNumber, null);
t.is(response.locale, null);
t.is(response.timeZone, null);
});
// SignAccountService.delete strategy
/**
* @test {Clinical6.delete}
*/
test('[unit] SignAccountService.delete should throw errors for invalid parameters', async (t) => {
const title = `SignAccountService.delete error`;
await t.throwsAsync(clinical6.delete(new SignAccount()), `${title}: account does not have id`);
});
/**
* @test {Clinical6.delete}
*/
test('[unit] SignAccountService.delete should receive a valid response for a delete request', async (t) => {
const { signAccount } = t.context;
signAccount.id = 5;
let request = {};
nock(client.apiBaseUrl).delete(`/v3/adobe_sign/accounts/${signAccount.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [204, ''];
});
const response = await clinical6.delete(signAccount);
t.is(request.path, `/v3/adobe_sign/accounts/${signAccount.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.serial('[unit] SignAccountService.delete should remove the element from local storage', async (t) => {
const { signAccount, storage } = t.context;
signAccount.id = 5;
nock(client.apiBaseUrl).delete(`/v3/adobe_sign/accounts/${signAccount.id}`).reply(204, '');
await storage.set(signAccount.type, signAccount.toJSON(), { id: signAccount.id });
await clinical6.delete(signAccount);
t.is(storage.has(signAccount.type, { id: signAccount.id }), false);
});