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

Commit

Permalink
Merge 06ba658 into 826e4ca
Browse files Browse the repository at this point in the history
  • Loading branch information
ziccardi committed Apr 1, 2020
2 parents 826e4ca + 06ba658 commit 4c0c27b
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 29 deletions.
31 changes: 18 additions & 13 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 @@
"typescript": "3.8.3"
},
"dependencies": {
"axios": "0.19.2"
"axios": "0.19.2",
"form-data": "3.0.0"
},
"repository": {
"type": "git",
Expand Down
3 changes: 1 addition & 2 deletions src/UnifiedPushAdminClient.ts
Expand Up @@ -75,8 +75,7 @@ export class UnifiedPushAdminClient {
* Creates an application in the UPS
* @param app the application to be created
*/
create: async (app: PushApplication): Promise<PushApplication> =>
this.applicationsAdmin.create(await this.auth(), app),
create: async (name: string): Promise<PushApplication> => this.applicationsAdmin.create(await this.auth(), name),
};

readonly variants = {
Expand Down
17 changes: 9 additions & 8 deletions src/applications/ApplicationsAdmin.ts
Expand Up @@ -6,17 +6,18 @@ export class ApplicationsAdmin {
let url = `/applications`;
if (filter && filter.id) {
url = `${url}/${filter.id}`;
return (await api.get(url)).data;
} else {
let apps: PushApplication[] = (await api.get(url)).data;
if (filter) {
apps = applyPushApplicationFilter(apps, filter);
const res = (await api.get(url)).data;
if (res instanceof Array) {
return res;
}
return apps;

return [res];
} else {
return applyPushApplicationFilter((await api.get(url)).data, filter);
}
}

async create(api: AxiosInstance, app: PushApplication): Promise<PushApplication> {
return (await api.post(`/applications`, app)).data;
async create(api: AxiosInstance, name: string): Promise<PushApplication> {
return (await api.post(`/applications`, { name })).data;
}
}
2 changes: 1 addition & 1 deletion src/applications/PushApplication.ts
Expand Up @@ -25,7 +25,7 @@ export interface PushApplication extends PushApplicationFilter {
*/
export const applyPushApplicationFilter = (
apps: PushApplication[],
filter: PushApplicationFilter
filter?: PushApplicationFilter
): PushApplication[] => {
if (filter) {
return apps.filter(
Expand Down
23 changes: 23 additions & 0 deletions src/variants/VariantsAdmin.ts
@@ -1,5 +1,8 @@
import { AxiosInstance } from 'axios';
import { applyVariantFilter, Variant, VariantFilter } from './Variant';
import * as FormData from 'form-data';
import * as fs from 'fs';
import { IOSVariant } from './IOSVariant';

export class VariantsAdmin {
async find(api: AxiosInstance, appId: string, filter?: VariantFilter): Promise<Variant[]> {
Expand All @@ -19,7 +22,27 @@ export class VariantsAdmin {
return applyVariantFilter(variants, filter);
}

private async createIOSVariant(api: AxiosInstance, appId: string, variant: IOSVariant): Promise<Variant> {
const formData = new FormData();

formData.append('name', variant.name);
formData.append('production', `${variant.production}`);
formData.append('passphrase', variant.password);
formData.append('certificate', fs.readFileSync(variant.certificate!));

const requestConfig = {
headers: {
'Content-Type': 'multipart/form-data',
...formData.getHeaders(),
},
};
return (await api.post(`/applications/${appId}/${variant.type}`, formData.getBuffer(), requestConfig)).data;
}

async create(api: AxiosInstance, appId: string, variant: Variant): Promise<Variant> {
if (variant.type === 'ios') {
return this.createIOSVariant(api, appId, variant as IOSVariant);
}
return (await api.post(`/applications/${appId}/${variant.type}`, variant)).data as Variant;
}
}
54 changes: 50 additions & 4 deletions test/UnifiedPushAdminClient.test.ts
Expand Up @@ -19,7 +19,27 @@ describe('Applications Admin', () => {
expect(res).toHaveLength(mockData.length);
expect(mockedAxios.get).toHaveBeenCalledWith('/applications');
});
it('test find with single filter', async () => {
it('test filter by id - returning singe result', async () => {
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData.find(value => value.id === '2') }));

const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.find({
id: '2',
});
expect(res).toHaveLength(1);
expect(mockedAxios.get).toHaveBeenCalledWith('/applications/2');
});

it('test filter by id - returning array with one object', async () => {
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: [mockData.find(value => value.id === '2')] }));

const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.find({
id: '2',
});
expect(res).toHaveLength(1);
expect(mockedAxios.get).toHaveBeenCalledWith('/applications/2');
});

it('test find filter by Developer', async () => {
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData }));

const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.find({
Expand All @@ -28,6 +48,18 @@ describe('Applications Admin', () => {
expect(res).toHaveLength(2);
expect(mockedAxios.get).toHaveBeenCalledWith('/applications');
});

it('test find filter by name', async () => {
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData }));

const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.find({
name: 'Application 3',
});
expect(res).toHaveLength(1);
expect(res[0]).toEqual(mockData.find(app => app.name === 'Application 3'));
expect(mockedAxios.get).toHaveBeenCalledWith('/applications');
});

it('test find with multiple filter', async () => {
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData }));

Expand All @@ -44,9 +76,9 @@ describe('Applications Admin', () => {
const appToBeCreated = mockData[3];
mockedAxios.post.mockImplementationOnce((url: string, app: PushApplication) => Promise.resolve({ data: app }));

const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.create(appToBeCreated);
expect(res).toEqual(appToBeCreated);
expect(mockedAxios.post).toHaveBeenCalledWith('/applications', appToBeCreated);
const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.create(appToBeCreated.name);
expect(res).toEqual({ name: appToBeCreated.name });
expect(mockedAxios.post).toHaveBeenCalledWith('/applications', { name: appToBeCreated.name });
});
});

Expand All @@ -61,6 +93,20 @@ describe('Variants Admin', () => {
expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`);
});

it('test filter by type', async () => {
const appId = '1';
const selectedApp = mockData.filter(app => app.id === appId)[0];
mockedAxios.get.mockImplementation(() =>
Promise.resolve({ data: selectedApp.variants!.filter(variant => variant.type === 'android') })
);

const res = await upsClient.variants.find(appId, { type: 'android' });
expect(res).toHaveLength(1);
expect(res[0].type).toEqual('android');
expect(res[0].name).toEqual('Variant 1');
expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}/android`);
});

it('test find with single filter', async () => {
const appId = '1';
const selectedApp = mockData.filter(app => app.id === appId)[0];
Expand Down

0 comments on commit 4c0c27b

Please sign in to comment.