Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge 124650c into 60de24c
Browse files Browse the repository at this point in the history
  • Loading branch information
ziccardi committed May 18, 2020
2 parents 60de24c + 124650c commit 478375e
Show file tree
Hide file tree
Showing 15 changed files with 631 additions and 401 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -33,7 +33,8 @@
"ts-jest": "25.5.0",
"ts-node": "8.10.1",
"typescript": "3.8.3",
"eslint-plugin-jest": "23.9.0"
"eslint-plugin-jest": "23.9.0",
"guid-typescript": "^1.0.9"
},
"dependencies": {
"axios": "0.19.2",
Expand Down
85 changes: 48 additions & 37 deletions test/UnifiedPushAdminClient.test.ts
@@ -1,72 +1,83 @@
import * as nock from 'nock';
import {
BASE_URL,
KC_CREDENTIALS,
KEYCLOAK_URL,
mockKeyCloak,
mockUps,
NEW_APP,
NEW_APP_NAME,
TEST_NEW_VARIANT_CREATED,
TEST_NEW_VARIANT_TO_CREATE,
} from './mocks/nockMocks';
import {UnifiedPushAdminClient} from '../src';
import {mockData} from './mocks/mockData';
import {AndroidVariant, UnifiedPushAdminClient} from '../src';
import {KeycloakCredentials} from '../src/UnifiedPushAdminClient';
import {UPSMock} from './mocks';
import {utils} from './mocks';
import {KEYCLOAK_URL, KC_CREDENTIALS} from './mocks/rest/keycloak';

beforeAll(() => {
mockUps(BASE_URL, true);
mockKeyCloak();
const BASE_URL = 'http://localhost:8888';

const upsMock = new UPSMock(true);

beforeEach(() => {
upsMock.reset();
});

afterAll(() => {
nock.restore();
upsMock.uninstall();
});

const TEST_APP_ID = '2:2';
const TEST_VARIANT_ID = 'v-2:1';

describe('UnifiedPushAdminClient', () => {
const credentials: KeycloakCredentials = {
kcUrl: KEYCLOAK_URL,
...KC_CREDENTIALS,
type: 'keycloak',
};
const NEW_APP_NAME = 'Test Application 1';

it('Should return all apps', async () => {
utils.generateApps(upsMock, 10);
const apps = await new UnifiedPushAdminClient(BASE_URL, credentials).applications.find();
expect(apps).toEqual(mockData);
expect(apps).toHaveLength(10);
});

it('Should create app', async () => {
const app = await new UnifiedPushAdminClient(BASE_URL, credentials).applications.create(NEW_APP_NAME);
expect(app).toEqual(NEW_APP);
expect(app.name).toEqual(NEW_APP_NAME);
});

it('Should find all variants', async () => {
const variants = await new UnifiedPushAdminClient(BASE_URL, credentials).variants.find(TEST_APP_ID);
expect(variants).toEqual(mockData.find(app => app.pushApplicationID === TEST_APP_ID)!.variants);
const APP_IDS = utils.generateApps(upsMock, 10);
const appId = APP_IDS[5];
const createdVariants = utils.generateVariants(upsMock, appId, 3);

const variants = await new UnifiedPushAdminClient(BASE_URL, credentials).variants.find(appId);
expect(variants).toEqual(createdVariants);
});

it('Should create an android variant', async () => {
const variant = await new UnifiedPushAdminClient(BASE_URL, credentials).variants.create(
TEST_APP_ID,
TEST_NEW_VARIANT_TO_CREATE
);
expect(variant).toEqual(TEST_NEW_VARIANT_CREATED);
});
const APP_IDS = utils.generateApps(upsMock, 10);
const appId = APP_IDS[5];

const variant = await new UnifiedPushAdminClient(BASE_URL, credentials).variants.create(appId, {
type: 'android',
projectNumber: '12345',
googleKey: '34645654',
name: 'My beautiful variant',
} as AndroidVariant);
expect(variant).toMatchObject({
type: 'android',
projectNumber: '12345',
googleKey: '34645654',
name: 'My beautiful variant',
});
});
it('Should delete a varianta', async () => {
const variantsBefore = await new UnifiedPushAdminClient(BASE_URL, credentials).variants.find(TEST_APP_ID, {
variantID: TEST_VARIANT_ID,
const APP_IDS = utils.generateApps(upsMock, 10);
const appId = APP_IDS[5];
const variants = utils.generateVariants(upsMock, appId, 30);

const variantToDelete = variants[15];

const variantsBefore = await new UnifiedPushAdminClient(BASE_URL, credentials).variants.find(appId, {
variantID: variantToDelete.variantID,
});
expect(variantsBefore).toBeDefined();
expect(variantsBefore).toHaveLength(1);
await new UnifiedPushAdminClient(BASE_URL, credentials).variants.delete(TEST_APP_ID, {
variantID: TEST_VARIANT_ID,
await new UnifiedPushAdminClient(BASE_URL, credentials).variants.delete(appId, {
variantID: variantToDelete.variantID,
});
const variantsAfter = await new UnifiedPushAdminClient(BASE_URL, credentials).variants.find(TEST_APP_ID, {
variantID: TEST_VARIANT_ID,
const variantsAfter = await new UnifiedPushAdminClient(BASE_URL, credentials).variants.find(appId, {
variantID: variantToDelete.variantID,
});
expect(variantsAfter).toBeDefined();
expect(variantsAfter).toHaveLength(0);
Expand Down
90 changes: 50 additions & 40 deletions test/applications/ApplicationsAdmin.test.ts
@@ -1,43 +1,55 @@
import * as nock from 'nock';
import axios from 'axios';
import {BASE_URL, mockKeyCloak, mockUps, NEW_APP, NEW_APP_NAME, init} from '../mocks/nockMocks';
import {ApplicationsAdmin} from '../../src/applications/ApplicationsAdmin';
import {mockData as originalData} from '../mocks/mockData';
import {PushApplication} from '../../src/applications';

let mockData: PushApplication[];
beforeAll(() => {
mockUps();
mockKeyCloak();
});
import {UPSMock, utils} from '../mocks';

afterAll(() => {
nock.restore();
});
const BASE_URL = 'http://localhost:8888';
const APP_DEVELOPER_FILTER_OK = 'Test Developer 1';
const APP_DEVELOPER_FILTER_BAD = 'developer 1';

const upsMock = new UPSMock();

const NEW_APP_NAME = 'Test Application 1';

beforeEach(() => {
mockData = [...originalData];
init(mockData);
upsMock.reset();
});

const APP_DEVELOPER_FILTER_OK = 'Test Developer 1';
const APP_DEVELOPER_FILTER_BAD = 'developer 1';
const APP_ID = '1:1';
afterAll(() => {
upsMock.uninstall();
});

describe('Applications Admin', () => {
const api = axios.create({baseURL: `${BASE_URL}/rest`});
const appAdmin = new ApplicationsAdmin();

it('Should return all apps', async () => {
it(`Should create an app named ${NEW_APP_NAME} and should return it.`, async () => {
const newApp = await appAdmin.create(api, NEW_APP_NAME);
expect(newApp.name).toEqual(NEW_APP_NAME);

const allApps = await appAdmin.find(api);
expect(allApps).toHaveLength(1);
expect(allApps[0].name).toEqual(NEW_APP_NAME);
});

it('Should return all apps (1st page)', async () => {
const ids = utils.generateIDs(45).map(id => ({pushApplicationID: id}));
utils.generateApps(upsMock, 45, ids);

const apps = await appAdmin.find(api);
expect(apps).toEqual(mockData);
expect(apps).toHaveLength(10);
expect(apps).toMatchObject(ids.slice(0, 10));
});

it('Should return a given app', async () => {
utils.generateApps(upsMock, 10);
// get one app
const app = (await appAdmin.find(api))[6];

const filteredApp = await appAdmin.find(api, {
pushApplicationID: '2:2',
pushApplicationID: app.pushApplicationID,
});
expect(filteredApp).toEqual([mockData.find(app => app.pushApplicationID === '2:2')]);
expect(filteredApp).toEqual([app]);
});

it('Should return empty result', async () => {
Expand All @@ -48,31 +60,29 @@ describe('Applications Admin', () => {
});

it(`Should return all apps developed by ${APP_DEVELOPER_FILTER_OK}`, async () => {
const filteredApp = await appAdmin.find(api, {
developer: APP_DEVELOPER_FILTER_OK,
});
expect(filteredApp).toEqual(mockData.filter(app => app.developer === APP_DEVELOPER_FILTER_OK));
});
utils.generateApps(upsMock, 8, new Array(20).fill({developer: APP_DEVELOPER_FILTER_OK}));
utils.generateApps(upsMock, 10, new Array(10).fill({developer: 'Dev 1'}));
utils.generateApps(upsMock, 5, new Array(10).fill({developer: 'Dev 2'}));

it(`Should return all apps developed by ${APP_DEVELOPER_FILTER_OK}`, async () => {
const filteredApp = await appAdmin.find(api, {
developer: APP_DEVELOPER_FILTER_OK,
});
expect(filteredApp).toEqual(mockData.filter(app => app.developer === APP_DEVELOPER_FILTER_OK));
});

it(`Should create an app named ${NEW_APP_NAME} and should return it.`, async () => {
const newApp = await appAdmin.create(api, NEW_APP_NAME);
expect(newApp).toEqual(NEW_APP);
expect(filteredApp).toHaveLength(8);
expect(filteredApp).toMatchObject(new Array(8).fill({developer: APP_DEVELOPER_FILTER_OK}));
});

it('Should delete an app using the Id ', async () => {
const appDel = mockData.find(appDel => appDel.pushApplicationID === APP_ID);
const listAppsBeforeDeletion = await appAdmin.find(api);
expect(listAppsBeforeDeletion).toContainEqual(expect.objectContaining(appDel));
await appAdmin.delete(api, {pushApplicationID: '1:1'});
const listAppsAfterDeletion = await appAdmin.find(api);
expect(listAppsAfterDeletion).not.toContainEqual(expect.objectContaining(appDel));
expect(listAppsAfterDeletion).toHaveLength(listAppsBeforeDeletion.length - 1);
const ids = utils.generateIDs(10).map(id => ({pushApplicationID: id}));
await utils.generateApps(upsMock, 10, ids);

const idToDelete = ids[5];

expect(await appAdmin.find(api)).toHaveLength(10);
expect(await appAdmin.find(api, idToDelete)).toHaveLength(1);

await appAdmin.delete(api, idToDelete);

expect(await appAdmin.find(api)).toHaveLength(9);
expect(await appAdmin.find(api, idToDelete)).toHaveLength(0);
});
});
73 changes: 73 additions & 0 deletions test/mocks/engine/UPSEngineMock.ts
@@ -0,0 +1,73 @@
import {PushApplication} from '../../../src/applications';
import {Guid} from 'guid-typescript';
import {Variant} from '../../../src/variants';

export class UPSEngineMock {
private data: PushApplication[] = [];

createApplication(newAppDef: PushApplication) {
const newApp: PushApplication = {...(newAppDef as {})} as PushApplication;

newApp.masterSecret = Guid.raw();
newApp.pushApplicationID = newApp.pushApplicationID || Guid.raw();
newApp.id = Guid.raw();
newApp.developer = newApp.developer || 'admin';
this.data.push(newApp);
return newApp;
}

getApplications(id?: string, page = 1, itemPerPage = 10) {
if (!id) {
const firstIndex = itemPerPage * (page - 1);
const endIndex = firstIndex + itemPerPage;

return this.data.slice(firstIndex, endIndex);
} else {
return this.data.find(item => item.pushApplicationID === id);
}
}

deleteApplication(id: string) {
this.data = this.data.filter(item => item.pushApplicationID !== id);
}

// Variants
createVariant(appId: string, variantDef: Variant) {
const newVariant: Variant = {...(variantDef as {})} as Variant;
newVariant.variantID = newVariant.variantID || Guid.raw();
newVariant.developer = newVariant.developer || 'admin';
newVariant.secret = Guid.raw();
newVariant.id = Guid.raw();

const app = this.data.find(item => item.pushApplicationID === appId)!;
app.variants = app.variants || [];
app.variants.push(newVariant);

return newVariant;
}

getVariants(appId: string, type: string) {
const app = this.getApplications(appId) as PushApplication;
if (!app) {
return null;
}

return app.variants ? app.variants.filter(variant => variant.type === type) : [];
}

deleteVariant(appId: string, type: string, variantId: string) {
const app = this.getApplications(appId) as PushApplication;
if (!app) {
return null;
}

const variant =
app.variants && app.variants.find(variant => variant.type === type && variant.variantID === variantId);
if (variant) {
app.variants = app.variants?.filter(variant => variant.type !== type && variant.variantID !== variantId);
}
return variant;
}

reset = () => (this.data = []);
}
2 changes: 2 additions & 0 deletions test/mocks/index.ts
@@ -0,0 +1,2 @@
export {utils} from './utils';
export {UPSMock} from './rest/UPSMock';

0 comments on commit 478375e

Please sign in to comment.