Skip to content

Commit

Permalink
added sdkmanager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
larryrider committed Feb 14, 2024
1 parent 7f8f869 commit 3ff871b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 42 deletions.
54 changes: 22 additions & 32 deletions src/services/SDKManager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,22 @@ export type SdkManagerApiSecurity = ApiSecurity & { newToken: string };
* based on the current apiSecurity details
*/
export class SdkManager {
public static readonly instance: SdkManager = new SdkManager();
private static apiSecurity?: SdkManagerApiSecurity = undefined;
private static instance: SdkManager = new SdkManager();

/**
* Sets the security details needed to create SDK clients
* @param apiSecurity Security properties to be setted
*/
static init = (apiSecurity: SdkManagerApiSecurity) => {
SdkManager.setApiSecurity(apiSecurity);
};

static setApiSecurity = (apiSecurity: SdkManagerApiSecurity) => {
public static init = (apiSecurity: SdkManagerApiSecurity) => {
SdkManager.apiSecurity = apiSecurity;
};

static clean = () => {
SdkManager.apiSecurity = undefined;
};

static getInstance = () => {
if (!SdkManager.instance) {
throw new Error('No instance found, call init method first');
}
return SdkManager.instance;
};

public getApiSecurity = (config = { throwErrorOnMissingCredentials: true }): SdkManagerApiSecurity => {
public static getApiSecurity = (config = { throwErrorOnMissingCredentials: true }): SdkManagerApiSecurity => {
if (!SdkManager.apiSecurity && config.throwErrorOnMissingCredentials)
throw new Error('Api security properties not found in SdkManager');

Expand All @@ -50,30 +40,30 @@ export class SdkManager {
};

/** Auth SDK */
get authV2() {
getAuthV2() {
const DRIVE_NEW_API_URL = ConfigService.instance.get('DRIVE_NEW_API_URL');

const apiSecurity = this.getApiSecurity({ throwErrorOnMissingCredentials: false });
const apiSecurity = SdkManager.getApiSecurity({ throwErrorOnMissingCredentials: false });
const appDetails = SdkManager.getAppDetails();

return Auth.client(DRIVE_NEW_API_URL, appDetails, apiSecurity);
}

/** Auth old client SDK */
get auth() {
getAuth() {
const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL');

const apiSecurity = this.getApiSecurity({ throwErrorOnMissingCredentials: false });
const apiSecurity = SdkManager.getApiSecurity({ throwErrorOnMissingCredentials: false });
const appDetails = SdkManager.getAppDetails();

return Auth.client(DRIVE_API_URL, appDetails, apiSecurity);
}

/** Payments SDK */
get payments() {
getPayments() {
const PAYMENTS_API_URL = ConfigService.instance.get('PAYMENTS_API_URL');

const newToken = this.getApiSecurity().newToken;
const newToken = SdkManager.getApiSecurity().newToken;
const appDetails = SdkManager.getAppDetails();

return Drive.Payments.client(PAYMENTS_API_URL, appDetails, {
Expand All @@ -83,40 +73,40 @@ export class SdkManager {
}

/** Users SDK */
get users() {
getUsers() {
const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL');

const apiSecurity = this.getApiSecurity({ throwErrorOnMissingCredentials: false });
const apiSecurity = SdkManager.getApiSecurity({ throwErrorOnMissingCredentials: false });
const appDetails = SdkManager.getAppDetails();

return Drive.Users.client(DRIVE_API_URL, appDetails, apiSecurity);
}

/** Referrals SDK */
get referrals() {
getReferrals() {
const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL');

const apiSecurity = this.getApiSecurity();
const apiSecurity = SdkManager.getApiSecurity();
const appDetails = SdkManager.getAppDetails();

return Drive.Referrals.client(DRIVE_API_URL, appDetails, apiSecurity);
}

/** Storage SDK */
get storage() {
getStorage() {
const DRIVE_API_URL = ConfigService.instance.get('DRIVE_API_URL');

const apiSecurity = this.getApiSecurity();
const apiSecurity = SdkManager.getApiSecurity();
const appDetails = SdkManager.getAppDetails();

return Drive.Storage.client(DRIVE_API_URL, appDetails, apiSecurity);
}

/** Trash SDK */
get trash() {
getTrash() {
const DRIVE_NEW_API_URL = ConfigService.instance.get('DRIVE_NEW_API_URL');

const newToken = this.getApiSecurity().newToken;
const newToken = SdkManager.getApiSecurity().newToken;
const appDetails = SdkManager.getAppDetails();

return Trash.client(DRIVE_NEW_API_URL, appDetails, {
Expand All @@ -126,19 +116,19 @@ export class SdkManager {
}

/** Photos SDK */
get photos() {
getPhotos() {
const PHOTOS_API_URL = ConfigService.instance.get('PHOTOS_API_URL');

const newToken = this.getApiSecurity().newToken;
const newToken = SdkManager.getApiSecurity().newToken;

return new photos.Photos(PHOTOS_API_URL, newToken);
}

/** Share SDK */
get share() {
getShare() {
const DRIVE_NEW_API_URL = ConfigService.instance.get('DRIVE_NEW_API_URL');

const newToken = this.getApiSecurity().newToken;
const newToken = SdkManager.getApiSecurity().newToken;
const appDetails = SdkManager.getAppDetails();

return Drive.Share.client(DRIVE_NEW_API_URL, appDetails, {
Expand Down
15 changes: 5 additions & 10 deletions test/services/keys.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { KeysService } from '../../src/services/keys.service';
import { ConfigService } from '../../src/services/config.service';
import { AesInit, CorruptedEncryptedPrivateKeyError } from '../../src/types/keys.types';

import { config } from 'dotenv';
config();

describe('Keys service', () => {
let keysServiceSandbox: SinonSandbox;

Expand All @@ -32,13 +29,11 @@ describe('Keys service', () => {
keysServiceSandbox.stub(openpgp, 'createMessage').resolves();
keysServiceSandbox.stub(openpgp, 'encrypt').resolves();
keysServiceSandbox.stub(openpgp, 'readMessage').resolves();
keysServiceSandbox
.stub(openpgp, 'decrypt')
.returns(
Promise.resolve({ data: 'validate-keys' } as openpgp.DecryptMessageResult & {
data: openpgp.MaybeStream<string>;
}),
);
keysServiceSandbox.stub(openpgp, 'decrypt').returns(
Promise.resolve({ data: 'validate-keys' } as openpgp.DecryptMessageResult & {
data: openpgp.MaybeStream<string>;
}),
);

await KeysService.instance.assertValidateKeys('dontcareprivate', 'dontcarepublic');
expect(true).to.be.true; //checks that assertValidateKeys does not throw any error
Expand Down
76 changes: 76 additions & 0 deletions test/services/sdkmanager.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import chai, { expect } from 'chai';
import sinon, { SinonSandbox } from 'sinon';
import sinonChai from 'sinon-chai';
import crypto from 'crypto';
import { SdkManager, SdkManagerApiSecurity } from '../../src/services/SDKManager.service';
import { ConfigKeys } from '../../src/types/config.types';
import { ConfigService } from '../../src/services/config.service';
import { AppDetails } from '@internxt/sdk/dist/shared';

chai.use(sinonChai);

describe('SDKManager service', () => {
let sdkManagerServiceSandbox: SinonSandbox;

beforeEach(() => {
sdkManagerServiceSandbox = sinon.createSandbox();
});

afterEach(function () {
sdkManagerServiceSandbox.restore();
});

it('When SDKManager apiSecurity is requested, then it is returned from static property', () => {
const apiSecurity: SdkManagerApiSecurity = {
newToken: crypto.randomBytes(16).toString('hex'),
token: crypto.randomBytes(16).toString('hex'),
};
SdkManager.init(apiSecurity);

expect(SdkManager.getApiSecurity()).to.eql(apiSecurity);
});

it('When SDKManager apiSecurity is requested but it is not started, then an error is thrown', () => {
try {
SdkManager.getApiSecurity();
expect(false).to.be.true; //should throw error
} catch {
/* no op */
}
});

it('When SDKManager is cleaned, then apiSecurity property is cleaned', () => {
const apiSecurity: SdkManagerApiSecurity = {
newToken: crypto.randomBytes(16).toString('hex'),
token: crypto.randomBytes(16).toString('hex'),
};
SdkManager.init(apiSecurity);
SdkManager.clean();
try {
SdkManager.getApiSecurity({ throwErrorOnMissingCredentials: true });
expect(false).to.be.true; //should throw error
} catch {
/* no op */
}
const apiSecurityResponse = SdkManager.getApiSecurity({ throwErrorOnMissingCredentials: false });
expect(apiSecurityResponse).to.be.undefined;
});

it('When AuthV2 client is requested, then it is generated using internxt sdk', () => {
const apiSecurity: SdkManagerApiSecurity = {
newToken: crypto.randomBytes(16).toString('hex'),
token: crypto.randomBytes(16).toString('hex'),
};
const appDetails: AppDetails = {
clientName: crypto.randomBytes(16).toString('hex'),
clientVersion: crypto.randomBytes(16).toString('hex'),
};
const envEndpoint: { key: keyof ConfigKeys; value: string } = { key: 'DRIVE_NEW_API_URL', value: 'test/api' };
SdkManager.init(apiSecurity);

sdkManagerServiceSandbox.stub(ConfigService.instance, 'get').withArgs(envEndpoint.key).returns(envEndpoint.value);
sdkManagerServiceSandbox.stub(SdkManager, 'getApiSecurity').returns(apiSecurity);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
sdkManagerServiceSandbox.stub(SdkManager, <any>'getAppDetails').returns(appDetails);
});
});

0 comments on commit 3ff871b

Please sign in to comment.