const apiCert = require('../lib/emisApiCert');
const expect = require('chai').expect;
const path = require('path');
const settings = require('../mocks/mockSettings');

const cdbNumber = "50002";
const productId = "88796c07-e0b0-4979-bc28-35962820f93a";
const expectedFolder = "8A6CCCB05ACB4E4026089D0042088D2AAC6548F1";
const expectedFile = "76F55A09ADBA3D2B72F426952E9FB5A685E418603F9CA0EBAC51018BF0219EA"
    + "B87D8D41A63F6FBE726F0AB4B81900F41487945D04EC50F5FE36C3C96.dat";
const expectedFullPath = path.join('C:/ProgramData/PatientApi/', expectedFolder, expectedFile);

describe('emisApiCert', function(){

    it('should return correct full path to api certificate file', function(){

        const apiCertOptions = settings.getOrDefault('clinicalSystemApi');
        const filename = apiCert.buildFileName(productId, cdbNumber, apiCertOptions);
        expect(filename).equal(expectedFullPath);
    });

    it('should return a list of certificate filename for all EMIS organisations', function(){

        const apiCertOptions = settings.getOrDefault('clinicalSystemApi');
        const connectionString = 'emisWebHost=127.0.0.1;organisation=50002;productId=88796c07-e0b0-4979-bc28-35962820f93a;user=Blackpear;password=Black123';
        const filename = apiCert.FileNameFromConnectionString(connectionString, apiCertOptions);
        expect(filename).equal(expectedFullPath);
    });

    it('should not return a certificate filename for non INPS organisations', function(){

        const apiCertOptions = settings.getOrDefault('clinicalSystemApi');
        const connectionString = 'Name=MIGSoap;Address=https://dev.inpses.co.uk:443/vmas/MIG;MaxReceivedMessageSize=2';
        const filename = apiCert.FileNameFromConnectionString(connectionString, apiCertOptions);
        expect(filename).equal(undefined);
    });

/*  This test creates files so only  use when necessary...

    it('should create files on first request', function(){

        var apiCertOptions = settings.getOrDefault('clinicalSystemApi');
        var connectionString1 = 'emisWebHost=127.0.0.1;organisation=50002;productId=88796c07-e0b0-4979-bc28-35962820f93a;user=Blackpear;password=Black123';
        var connectionString2 = 'emisWebHost=127.0.0.1;organisation=99999;productId=88796c07-e0b0-4979-bc28-35962820f93a;user=peeps;password=pass';
        apiCert.createApiCertFile(connectionString1, apiCertOptions);
        apiCert.createApiCertFile(connectionString1, apiCertOptions);
        apiCert.createApiCertFile(connectionString2, apiCertOptions);
    });
*/
});