Skip to content

Commit

Permalink
Edit structure, implement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rickypid committed Jan 7, 2022
1 parent 4ca2274 commit 53aaa2e
Show file tree
Hide file tree
Showing 9 changed files with 474 additions and 383 deletions.
142 changes: 96 additions & 46 deletions src/index.test.ts → lib/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,117 @@
import {expect} from 'chai';
import {IubendaConsentSolution} from '~/index';
import {IubendaConsentSolution, Subject} from '~/index';

const validApiKey = process.env.API_KEY || 'YOUR-VALID-IUBENDA-API-KEY';
const invalidApiKey = 'invalid';
function validApiKey(): string {
return process.env.API_KEY || 'YOUR-VALID-IUBENDA-API-KEY';
}

function invalidApiKey(): string {
return 'invalid';
}

//Test constants
const first_name = 'John';
const first_name_update = 'Jonathan';
const last_name = 'Fix';
const testEmailDomain = '@test.it';

function generateId(): string {
return 'test-' + Date.now().toString();
}

function generateEmailFromId(id: string): string {
return id + testEmailDomain;
}

describe('iubenda-consent-solution-api', function () {
describe('API Key check', function () {
it('Invalid API Key', async function () {
const client = new IubendaConsentSolution({apiKey: invalidApiKey});
const client = new IubendaConsentSolution({apiKey: invalidApiKey()});
const result = await client.getConsents();
if ('error' in result)
expect(result.error).equal(true);
else
expect.fail('Private key is valid!');
});
it('Valid API Key', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey});
const client = new IubendaConsentSolution({apiKey: validApiKey()});
const result = await client.getConsents();
if ('error' in result)
expect.fail('Private key is invalid!');
else
expect(true);
});
});

describe('Subjects', function () {
it('Get Subjects', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey()});
const result = await client.getSubjects();
if ('error' in result)
expect.fail('Response error! ' + result.message);
else
expect(true);
});
it('Get Subject with id', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey()});
const result = await client.getSubjects();
if ('error' in result)
expect.fail('Response error! ' + result.message);
if (result.length > 0 && result[0].id) {
const result1 = await client.getSubject(result[0].id);
if ('error' in result1)
expect.fail('Response error! ' + result1.message);
else
expect(true);
} else
expect.fail('Subjects empty!');
});
it('Create Subject', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey()});
const id: string = Date.now().toString();
const result = await client.createSubject({
id: id,
email: id + testEmailDomain,
first_name: first_name,
last_name: last_name,
});
if ('error' in result)
expect.fail('Response error! ' + result.message);
else
expect(true);
});

it('Update Subject', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey()});
const id: string = generateId();
const subject:Subject = {
id: id,
email: generateEmailFromId(id),
first_name: first_name,
last_name: last_name,
};
const result = await client.createSubject(subject);
if ('error' in result)
expect.fail('Response error! ' + result.message);
subject.first_name = first_name_update;
const result1 = await client.updateSubject(subject, id);
if ('error' in result1)
expect.fail('Response error! ' + result1.message);
else
expect(true);
});
});
describe('Consents', function () {
it('Get Consents', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey});
const client = new IubendaConsentSolution({apiKey: validApiKey()});
const result = await client.getConsents();
if ('error' in result)
expect.fail('Response error! ' + result.message);
else
expect(true);
});
it('Get Consent with id', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey});
const client = new IubendaConsentSolution({apiKey: validApiKey()});
const result = await client.getConsents();
if ('error' in result)
expect.fail('Response error! ' + result.message);
Expand All @@ -46,27 +124,26 @@ describe('iubenda-consent-solution-api', function () {
} else
expect.fail('Consents empty!');
});

//ToDo implements createConsent()
it('Add Consent', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey});
it('Create Consent', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey()});
const id: string = generateId();
const result = await client.createConsent({
autodetect_ip_address: 'false',
subject: {
id: '1',
email: 'test@test.it',
first_name: 'John',
last_name: 'Fix',
id: id,
email: generateEmailFromId(id),
first_name: first_name,
last_name: last_name,
},
legal_notices: [
{identifier: 'privacy_policy',version: '1'},
{identifier: 'terms_and_conditions',version: '1'},
{identifier: 'cookie_policy',version: '1'},
{identifier: 'privacy_policy', version: '1'},
{identifier: 'terms_and_conditions', version: '1'},
{identifier: 'cookie_policy'},
],
proofs: [
{
content: '{}',
form: '<form></form>',
form: '<form>Example Form</form>',
},
],
preferences: {
Expand All @@ -77,38 +154,11 @@ describe('iubenda-consent-solution-api', function () {
},
ip_address: '',
});
if ('error' in result) {
console.log(result);
expect.fail('Response error! ' + result.message);
}
else
expect(true);
});
});
describe('Subjects', function () {
it('Get Subjects', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey});
const result = await client.getSubjects();
if ('error' in result)
expect.fail('Response error! ' + result.message);
else
expect(true);
});
it('Get Subject with id', async function () {
const client = new IubendaConsentSolution({apiKey: validApiKey});
const result = await client.getSubjects();
if ('error' in result)
expect.fail('Response error! ' + result.message);
if (result.length > 0 && result[0].id) {
const result1 = await client.getSubject(result[0].id);
if ('error' in result1)
expect.fail('Response error! ' + result1.message);
else
expect(true);
} else
expect.fail('Subjects empty!');
});
//ToDo implements createSubject(), updateSubject()
});

});
27 changes: 27 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export {
ConsentExtended,
ConsentPostResponse,
ConsentResponse,
ConsentsQueryParameters,
ConsentLegalNoticesEntity,
ConsentPreferences,
ConsentProofsEntity,
ConsentSubject,
Consent,
} from './src/consent';

export {
Subject,
SubjectPostResponse,
SubjectResponse,
SubjectQueryParameters,
SubjectPreferences,
} from './src/subject';

export {
IubendaConsentSolution,
ResponseError,
ClientOption,
} from './src/client';


110 changes: 110 additions & 0 deletions lib/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import {
get,
post,
put,
SuperAgentRequest,
} from 'superagent';

import {
ConsentExtended,
ConsentPostResponse,
ConsentResponse,
ConsentsQueryParameters,
} from './consent';

import {
Subject,
SubjectPostResponse,
SubjectQueryParameters,
SubjectResponse,
} from './subject';

export interface ResponseError {
error: boolean;
status: number;
message: string;
}

export interface ClientOption {
apiKey: string;
beta?: boolean;
unescape_json?: boolean;
}

export class IubendaConsentSolution {
apiKey: string;
beta: boolean;
unescape_json: boolean;

constructor(options: ClientOption) {
this.apiKey = options.apiKey;
this.beta = options.beta || false;
this.unescape_json = options.unescape_json || false;
}

get apiUrl(): string {
let url = 'https://consent.iubenda.com/';
if (this.beta)
url += 'beta/';
return url;
}

private addHeaders(request: SuperAgentRequest): SuperAgentRequest {
return request.set('Content-Type', 'application/json').set('ApiKey', this.apiKey).ok(res => true);
}

private generateUrl(path: string): string {
let url = this.apiUrl + '/' + path;
if (this.unescape_json)
url += '?unescape_json=true';
return url;
}

private static async sendRequest(request: SuperAgentRequest): Promise<ResponseError | any> {
const response = await request;
if (response.status < 300) {
return response.body;
} else {
return {status: response.status, message: response.body.message, error: true};
}
}

async getConsents(query?: ConsentsQueryParameters): Promise<ResponseError | ConsentResponse[]> {
const req = this.addHeaders(get(this.generateUrl('consent')));
if (query) {
req.query(query);
}
return await IubendaConsentSolution.sendRequest(req);
}

async getConsent(id: string): Promise<ResponseError | ConsentExtended> {
return await IubendaConsentSolution.sendRequest(this.addHeaders(get(this.generateUrl('consent/' + id))));
}

async createConsent(consent: ConsentExtended): Promise<ResponseError | ConsentPostResponse> {
return await IubendaConsentSolution.sendRequest(this.addHeaders(post(this.generateUrl('consent')).send(consent)));
}

async getSubjects(query?: SubjectQueryParameters): Promise<ResponseError | SubjectResponse[]> {
const req = this.addHeaders(get(this.generateUrl('subjects')));
if (query) {
req.query(query);
}
return await IubendaConsentSolution.sendRequest(req);
}

async getSubject(id: string): Promise<ResponseError | SubjectResponse> {
const req = this.addHeaders(get(this.generateUrl('subjects/' + id)));
return await IubendaConsentSolution.sendRequest(req);
}

async createSubject(subject: Subject): Promise<ResponseError | SubjectPostResponse> {
const req = this.addHeaders(post(this.generateUrl('subjects')).send(subject));
return await IubendaConsentSolution.sendRequest(req);
}

async updateSubject(subject: Subject, id: string): Promise<ResponseError | SubjectPostResponse> {
const req = this.addHeaders(put(this.generateUrl('subjects/' + id)).send(subject));
return await IubendaConsentSolution.sendRequest(req);
}
}
Loading

0 comments on commit 53aaa2e

Please sign in to comment.