test/unit/helpers.allowed-action.js
import test from 'ava';
import sinon from 'sinon';
import {
client,
AllowedAction,
} from '../../src';
// See tests.js for testing client creation
const allowedActionJsonApi = {
id: 11,
type: 'allowed_actions',
attributes: {
name: 'manage'
},
relationships: {
permission: {
data: {
id: 4,
type: 'permissions'
}
}
}
};
const allowedActionJson = {
id: 11,
type: 'allowed_actions',
name: 'manage',
relationships: {
permission: {
data: {
id: 4,
type: 'permissions'
}
}
}
};
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.server = sinon.fakeServer.create();
t.context.server.autoRespond = true;
t.context.server.respondWith('DELETE', `${client.apiBaseUrl}/v3/allowed_actions/*`,
// [200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' })]
[204, { 'Content-Type': 'application/json' }, JSON.stringify({ status: '204 No Content' })]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/allowed_actions`,
[201, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: 11,
type: 'allowed_actions',
attributes: {
name: 'manage'
},
relationships: {
permission: {
data: {
id: 4,
type: 'permissions'
}
}
}
}
})]);
t.context.storage = client.storageUtility;
client.allowedAction = new AllowedAction({});
t.context.allowedActionJsonApi = {
id: 11,
type: 'allowed_actions',
attributes: {
name: 'manage'
},
relationships: {
permission: {
data: {
id: 4,
type: 'permissions'
}
}
}
};
t.context.allowedAction = new AllowedAction(t.context.allowedActionJsonApi);
});
test.afterEach(t => t.context.server.restore());
/**
* @test {AllowedAction}
*/
test('[unit] AllowedAction should handle allowedAction data with a normal json format', (t) => {
const allowedAction = new AllowedAction(allowedActionJson);
t.is(allowedAction.id, 11);
t.is(allowedAction.type, 'allowed_actions');
t.is(allowedAction.name, 'manage');
t.is(allowedAction.relationships.relationships.permission.data.id, 4);
t.is(allowedAction.relationships.relationships.permission.data.type, 'permissions');
});
/**
* @test {AllowedAction}
*/
test('[unit] AllowedAction should handle allowedAction data with json api format', (t) => {
const allowedAction = new AllowedAction({ data: allowedActionJsonApi });
t.is(allowedAction.id, 11);
t.is(allowedAction.type, 'allowed_actions');
t.is(allowedAction.name, 'manage');
t.is(allowedAction.relationships.relationships.permission.data.id, 4);
t.is(allowedAction.relationships.relationships.permission.data.type, 'permissions');
});
/**
* @test {AllowedAction}
*/
test('[unit] AllowedAction should generate json api format when converted to string', (t) => {
let allowedAction = new AllowedAction({ data: allowedActionJsonApi });
let json = allowedAction.toJSON();
t.deepEqual(json, allowedActionJsonApi);
allowedAction = new AllowedAction({ data: allowedActionJsonApi });
json = allowedAction.toJSON();
t.deepEqual(json, allowedActionJsonApi);
});
/**
* @test {AllowedAction.delete}
*/
test('[unit] AllowedAction.delete should successfully delete a allowedAction', async (t) => {
const allowedAction = new AllowedAction({ data: allowedActionJsonApi });
const response = await allowedAction.delete();
// 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.truthy(response);
});
/**
* @test {AllowedAction.save}
*/
test('[unit] AllowedAction.save should successfully insert a allowedAction with a allowedAction object when id does not exist', async (t) => {
const json = JSON.parse(JSON.stringify(allowedActionJsonApi));
delete json.id;
const allowedAction = new AllowedAction({ data: json });
const response = await allowedAction.save();
// 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), { data: json });
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.id, 11);
t.is(response.type, 'allowed_actions');
t.is(response.name, 'manage');
});