test/unit/helpers.consent-grant.js
import test from 'ava';
import nock from 'nock';
import {
client,
ConsentGrant,
} from '../../src';
// See tests.js for testing client creation
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.insertResponse = {
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',
consented_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'
}
}
}
}
};
t.context.consentGrantJsonApi = {
type: 'consent__grants',
attributes: {
signing_password: '1212',
created_at: undefined,
updated_at: undefined,
consented_at: undefined,
sign_url: undefined,
progress: undefined
},
relationships: {
granted_for: {
data: {
id: 12,
type: 'mobile_users'
}
},
strategy: {
data: {
id: 1,
type: 'consent__strategies'
}
},
form: {
data: {
id: 1,
type: 'consent__forms'
}
}
}
};
t.context.consentGrantJson = {
type: 'consent__grants',
signing_password: '1212',
created_at: undefined,
updated_at: undefined,
consented_at: undefined,
sign_url: undefined,
progress: undefined,
relationships: {
granted_for: {
data: {
id: 12,
type: 'mobile_users'
}
},
strategy: {
data: {
id: 1,
type: 'consent__strategies'
}
},
form: {
data: {
id: 1,
type: 'consent__forms'
}
}
}
};
client.storageUtility.clear();
t.context.storage = client.storageUtility;
client.consentGrant = new ConsentGrant({});
t.context.consentGrant = new ConsentGrant({ data: t.context.consentGrantJsonApi });
});
/**
* @test {ConsentGrant}
*/
test('[unit] ConsentGrant should handle consentGrant data with a normal json format', (t) => {
const { consentGrantJson } = t.context;
const consentGrant = new ConsentGrant(consentGrantJson);
t.is(consentGrant.type, 'consent__grants');
t.is(consentGrant.signingPassword, '1212');
t.is(consentGrant.createdAt, undefined);
t.is(consentGrant.updatedAt, undefined);
t.is(consentGrant.consentedAt, undefined);
t.is(consentGrant.signUrl, undefined);
t.is(consentGrant.progress, undefined);
t.is(consentGrant.grantedFor.id, 12);
t.is(consentGrant.grantedFor.type, 'mobile_users');
t.is(consentGrant.strategy.id, 1);
t.is(consentGrant.strategy.type, 'consent__strategies');
t.is(consentGrant.form.id, 1);
t.is(consentGrant.form.type, 'consent__forms');
});
/**
* @test {ConsentGrant}
*/
test('[unit] ConsentGrant should handle consentGrant data with json api format', (t) => {
const { consentGrantJsonApi } = t.context;
const consentGrant = new ConsentGrant({ data: consentGrantJsonApi });
t.is(consentGrant.type, 'consent__grants');
t.is(consentGrant.signingPassword, '1212');
t.is(consentGrant.createdAt, undefined);
t.is(consentGrant.updatedAt, undefined);
t.is(consentGrant.consentedAt, undefined);
t.is(consentGrant.signUrl, undefined);
t.is(consentGrant.progress, undefined);
t.is(consentGrant.grantedFor.id, 12);
t.is(consentGrant.grantedFor.type, 'mobile_users');
t.is(consentGrant.strategy.id, 1);
t.is(consentGrant.strategy.type, 'consent__strategies');
t.is(consentGrant.form.id, 1);
t.is(consentGrant.form.type, 'consent__forms');
});
/**
* @test {ConsentGrant}
*/
test('[unit] ConsentGrant should generate json api format when converted to string', (t) => {
const { consentGrantJsonApi } = t.context;
let consentGrant = new ConsentGrant({ data: consentGrantJsonApi });
t.deepEqual(consentGrant.toJSON(), consentGrantJsonApi);
consentGrant = new ConsentGrant({ data: consentGrantJsonApi });
t.deepEqual(consentGrant.toJSON(), consentGrantJsonApi);
});
/**
* @test {ConsentGrant.save}
*/
test('[unit] ConsentGrant.save should successfully save a new consentGrant when id does not exist', async (t) => {
const { consentGrantJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(consentGrantJsonApi));
// delete json.id;
const consentGrant = new ConsentGrant({ data: json });
let request = {};
nock(client.apiBaseUrl).post(`/v3/consent/grants`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await consentGrant.save();
t.is(request.path, `/v3/consent/grants`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: json });
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.consentedAt, '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');
});