test/unit/services.consent-grant.js
import test from 'ava';
import nock from 'nock';
import {
client,
clinical6,
ConsentGrant,
} from '../../src';
test.before('start server', (t) => {
t.context.storage = client.storageUtility;
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.consentGrantJsonApi = {
data: {
type: 'consent__grants',
attributes: {
signing_password: '1212'
},
relationships: {
granted_for: {
data: {
id: 12,
type: 'mobile_users'
}
},
strategy: {
data: {
id: 1,
type: 'consent__strategies'
}
},
form: {
data: {
id: 1,
type: 'consent__forms'
}
}
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.consentGrant = new ConsentGrant(t.context.consentGrantJsonApi);
});
// ConsentGrantService.insert method
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] ConsentGrantService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'ConsentGrantService.insert error: requires authToken';
await t.throwsAsync(clinical6.insert(new ConsentGrant()), expectedError);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] ConsentGrantService.insert should successfully insert a consentGrant with a consentGrant object', async (t) => {
let request = {};
nock(client.apiBaseUrl).post(`/v3/consent/grants`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, {
data: {
id: 1,
type: 'consent__grants',
attributes: {
sign_url: 'https://secure.na2.echosign.com/public/apiesign?pid=CBFCIBAA3AAABLblqZhDh-nuCTcEgLnmzdUqCA9hnpAkdYgacHN02RG3TU4J5lsgkWSxbmdwnT82PW-Rvgk0*',
created_at: '2018-07-03T19:06:38Z',
updated_at: '2018-07-03T19:06:38Z'
},
relationships: {
granted_for: {
data: {
id: 12,
type: 'mobile_users'
}
},
granted_by: {
data: {
id: 12,
type: 'mobile_users'
}
},
strategy: {
data: {
id: 1,
type: 'consent__strategies'
}
},
form_version: {
data: {
id: 1,
type: 'consent__form_versions'
}
},
agreement: {
data: {
id: 7,
type: 'agreement__agreements'
}
}
}
}
}];
});
const requestJsonApi = {
data: {
type: 'consent__grants',
attributes: {
signing_password: '1212'
},
relationships: {
granted_for: {
data: {
id: 12,
type: 'mobile_users'
}
},
strategy: {
data: {
id: 1,
type: 'consent__strategies'
}
},
form: {
data: {
id: 1,
type: 'consent__forms'
}
}
}
}
};
const consentGrant = new ConsentGrant(requestJsonApi);
const response = await clinical6.insert(consentGrant);
t.is(request.path, `/v3/consent/grants`);
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, 'consent__grants');
t.is(response.signUrl, 'https://secure.na2.echosign.com/public/apiesign?pid=CBFCIBAA3AAABLblqZhDh-nuCTcEgLnmzdUqCA9hnpAkdYgacHN02RG3TU4J5lsgkWSxbmdwnT82PW-Rvgk0*');
t.is(response.createdAt, '2018-07-03T19:06:38Z');
t.is(response.updatedAt, '2018-07-03T19:06:38Z');
t.is(response.grantedFor.id, 12);
t.is(response.grantedFor.type, 'mobile_users');
t.is(response.grantedBy.id, 12);
t.is(response.grantedBy.type, 'mobile_users');
t.is(response.strategy.id, 1);
t.is(response.strategy.type, 'consent__strategies');
t.is(response.formVersion.id, 1);
t.is(response.formVersion.type, 'consent__form_versions');
t.is(response.agreement.id, 7);
t.is(response.agreement.type, 'agreement__agreements');
});