test/unit/services.allowed-action.v3.js
import test from 'ava';
import sinon from 'sinon';
import { client, AllowedAction, clinical6 } from '../../src';
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.server = sinon.fakeServer.create();
// t.context.server.autoRespond = true;
t.context.server.respondImmediately = true;
t.context.server.respondWith('DELETE', `${client.apiBaseUrl}/v3/allowed_actions/*`,
[200, { 'Content-Type': 'application/json' }, '']);
t.context.storage = client.storageUtility;
t.context.allowedActionJsonApi = {
data: {
id: 12,
type: 'allowed_actions',
attributes: {
name: 'testing'
},
relationships: {
permission: {
data: {
id: 4,
type: 'permissions'
}
}
}
}
};
t.context.allowedAction = new AllowedAction(t.context.allowedActionJsonApi);
});
test.afterEach(t => t.context.server.restore());
// AllowedActionService.delete method
/**
* @test {Clinical6.delete}
*/
test('[unit] AllowedActionService.delete should make a properly formatted delete request', async (t) => {
const { allowedAction } = t.context;
allowedAction.id = 5;
const response = await clinical6.delete(allowedAction);
// const request = t.context.server.requests[0];
// t.is(request.method, 'DELETE');
// t.is(request.url, `${client.apiBaseUrl}/v3/allowed_actions/${allowedAction.id}`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.falsy(response);
});
/**
* @test {Clinical6.delete}
*/
test('[unit] AllowedActionService.delete should receive a valid response for a delete request', async (t) => {
const { allowedAction } = t.context;
allowedAction.id = 5;
const response = await clinical6.delete(allowedAction);
// const request = t.context.server.requests[0];
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.falsy(response);
});
/**
* @test {Clinical6.delete}
*/
test('[unit] AllowedActionService.delete should remove the element from local storage', async (t) => {
const { allowedAction, storage } = t.context;
allowedAction.id = 5;
await storage.set(allowedAction.type, allowedAction.toJSON(), { id: allowedAction.id });
await clinical6.delete(allowedAction);
t.is(storage.has(allowedAction.type, { id: allowedAction.id }), false);
});
// AllowedActionService.get method
// AllowedActionService.insert method
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] AllowedActionService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const title = 'AllowedActionService.insert error';
await t.throwsAsync(clinical6.insert(new AllowedAction()), `${title}: requires authToken`);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] AllowedActionService.insert should successfully insert an allowed action with an allowed action object', async (t) => {
t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/allowed_actions`,
[201, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: 12,
type: 'allowed_actions',
attributes: {
name: 'testing'
},
relationships: {
permission: {
data: {
id: 4,
type: 'permissions'
}
}
}
}
})]);
const requestJsonApi = {
data: {
type: 'allowed_actions',
attributes: {
name: 'testing'
},
relationships: {
permission: {
data: {
id: 4,
type: 'permissions'
}
}
}
}
};
const allowedAction = new AllowedAction(requestJsonApi);
const response = await clinical6.insert(allowedAction);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/v3/allowed_actions`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.deepEqual(JSON.parse(request.requestBody), requestJsonApi);
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.id, 12);
t.is(response.type, 'allowed_actions');
t.is(response.name, 'testing');
t.is(response.relationships.relationships.permission.data.id, 4);
t.is(response.relationships.relationships.permission.data.type, 'permissions');
t.is(response.permission.id, 4);
t.is(response.permission.type, 'permissions');
});
// AllowedActionService.update method