diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b75bcfd5..8f672741 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,6 +1,6 @@ -name: Build the library +name: build -on: [push] +on: [push, pull_request] jobs: build: @@ -8,6 +8,10 @@ jobs: steps: - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: '10.x' + - name: Install dependencies run: npm install @@ -18,4 +22,9 @@ jobs: run: npm run compile - name: Test - run: npm run test \ No newline at end of file + run: npm run test + + - name: Coveralls + uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0e7e4a12..656fe279 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules build dist .idea +coverage diff --git a/README.md b/README.md index 1d668683..e1d18e07 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # UnifiedPush Server Admin Client +![Build](https://github.com/aerogear/unifiedpush-admin-client/workflows/build/badge.svg) + The _UnifiedPush Server Admin_ library allows to admin the UPS by javascript or typescript code. ## Getting started diff --git a/jest.config.js b/jest.config.js index fd57f4d8..f1d6567b 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,10 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', - rootDir: 'test' -}; \ No newline at end of file + collectCoverage: true, + collectCoverageFrom: ['src/**/*.ts'], + roots: [ + 'test', + 'src' + ] +}; diff --git a/src/main.ts b/src/main.ts deleted file mode 100644 index 6b4fe10d..00000000 --- a/src/main.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { UnifiedPushAdminClient } from './UnifiedPushAdminClient'; - -async function run() { - // const app = await new UPSClient().applications.create(newApp); - // console.log('NewApp: ', app); - - // const apps = await new UnifiedPushClient().applications.find({ - // name: 'New Ziccardi', - // }); - // console.log('apps', apps); - - //B868CC08-BCC8-4A0A-B21E-1AC56AF0C734 - // const variants = await new UnifiedPushClient('http://localhost:9999', { - // kcUrl: 'http://172.18.0.2:8080', - // username: 'admin', - // password: '123', - // }).variants.find('58939fc1-8868-46f5-95e3-0637ea97f9f4'); - - const variants = await new UnifiedPushAdminClient('http://localhost:9999', { - kcUrl: 'http://172.18.0.2:8080', - username: 'admin', - password: '123', - type: 'keycloak', - }).variants.find('58939fc1-8868-46f5-95e3-0637ea97f9f4'); - console.log('variants', variants); -} - -run().catch(err => console.log('err', err)); diff --git a/test/UnifiedPushAdminClient.test.ts b/test/UnifiedPushAdminClient.test.ts new file mode 100644 index 00000000..398cf31e --- /dev/null +++ b/test/UnifiedPushAdminClient.test.ts @@ -0,0 +1,107 @@ +import axios from 'axios'; +import { mockData } from './mockData'; +import { PushApplication, UnifiedPushAdminClient } from '../src'; +import { mocked } from 'ts-jest/dist/util/testing'; + +jest.mock('axios'); + +const mockedAxios = axios as jest.Mocked; + +mocked(axios).create = () => mockedAxios; + +const upsClient = new UnifiedPushAdminClient('http://localhost:9999'); + +describe('Applications Admin', () => { + it('test find without filters', async () => { + mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData })); + + const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.find(); + expect(res).toHaveLength(mockData.length); + expect(mockedAxios.get).toHaveBeenCalledWith('/applications'); + }); + it('test find with single filter', async () => { + mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData })); + + const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.find({ + developer: 'Test Developer 2', + }); + expect(res).toHaveLength(2); + expect(mockedAxios.get).toHaveBeenCalledWith('/applications'); + }); + it('test find with multiple filter', async () => { + mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData })); + + const res = await new UnifiedPushAdminClient('http://localhost:9999').applications.find({ + developer: 'Test Developer 2', + name: 'Application 4', + }); + expect(res).toHaveLength(1); + expect(res[0].name).toEqual('Application 4'); + expect(mockedAxios.get).toHaveBeenCalledWith('/applications'); + }); + + it('test create app', async () => { + 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); + }); +}); + +describe('Variants Admin', () => { + it('test find without filters', async () => { + const appId = '1'; + const selectedApp = mockData.filter(app => app.id === appId)[0]; + mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: selectedApp })); + + const res = await upsClient.variants.find(appId); + expect(res).toHaveLength(selectedApp!.variants!.length); + expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); + }); + + it('test find with single filter', async () => { + const appId = '1'; + const selectedApp = mockData.filter(app => app.id === appId)[0]; + mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp })); + + let res = await upsClient.variants.find(appId, { developer: 'developer 2' }); + expect(res).toHaveLength(1); + expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); + + res = await upsClient.variants.find(appId, { developer: 'developer 1' }); + expect(res).toHaveLength(2); + expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); + }); + + it('test find one variant/no filters', async () => { + const appId = '3'; + const selectedApp = mockData.filter(app => app.id === appId)[0]; + mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp })); + + const res = await upsClient.variants.find(appId); + expect(res).toHaveLength(1); + expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); + }); + + it('test find with bad filter', async () => { + const appId = '1'; + const selectedApp = mockData.filter(app => app.id === appId)[0]; + mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp })); + + const res = await upsClient.variants.find(appId, { developer: 'developer 84' }); + expect(res).toHaveLength(0); + expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); + }); + + it('test app without variants', async () => { + const appId = '4'; + const selectedApp = mockData.filter(app => app.id === appId)[0]; + mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp })); + + const res = await upsClient.variants.find(appId, { developer: 'developer 84' }); + expect(res).toHaveLength(0); + expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); + }); +}); diff --git a/test/applications/ApplicationAdmin.test.ts b/test/applications/ApplicationAdmin.test.ts deleted file mode 100644 index 2f0a955c..00000000 --- a/test/applications/ApplicationAdmin.test.ts +++ /dev/null @@ -1,45 +0,0 @@ -import axios from 'axios'; -import { ApplicationsAdmin } from '../../src/applications/ApplicationsAdmin'; -import { PushApplication } from '../../src/applications'; -import { mockData } from '../mockData'; - -jest.mock('axios'); - -const mockedAxios = axios as jest.Mocked; - -describe('ApplicationsAdmin', () => { - it('test find without filters', async () => { - mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData })); - - const res = await new ApplicationsAdmin().find(mockedAxios); - expect(res).toHaveLength(mockData.length); - expect(mockedAxios.get).toHaveBeenCalledWith('/applications'); - }); - it('test find with single filter', async () => { - mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData })); - - const res = await new ApplicationsAdmin().find(mockedAxios, { developer: 'Test Developer 2' }); - expect(res).toHaveLength(2); - expect(mockedAxios.get).toHaveBeenCalledWith('/applications'); - }); - it('test find with multiple filter', async () => { - mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: mockData })); - - const res = await new ApplicationsAdmin().find(mockedAxios, { - developer: 'Test Developer 2', - name: 'Application 4', - }); - expect(res).toHaveLength(1); - expect(res[0].name).toEqual('Application 4'); - expect(mockedAxios.get).toHaveBeenCalledWith('/applications'); - }); - - it('test create app', async () => { - const appToBeCreated = mockData[3]; - mockedAxios.post.mockImplementationOnce((url: string, app: PushApplication) => Promise.resolve({ data: app })); - - const res = await new ApplicationsAdmin().create(mockedAxios, appToBeCreated); - expect(res).toEqual(appToBeCreated); - expect(mockedAxios.post).toHaveBeenCalledWith('/applications', appToBeCreated); - }); -}); diff --git a/test/variants/VariantsAdmin.test.ts b/test/variants/VariantsAdmin.test.ts deleted file mode 100644 index 2ad7bec6..00000000 --- a/test/variants/VariantsAdmin.test.ts +++ /dev/null @@ -1,63 +0,0 @@ -import axios from 'axios'; -import { mockData } from '../mockData'; -import { VariantsAdmin } from '../../src/variants/VariantsAdmin'; - -jest.mock('axios'); - -const mockedAxios = axios as jest.Mocked; - -describe('Test finders', () => { - it('test find without filters', async () => { - const appId = '1'; - const selectedApp = mockData.filter(app => app.id === appId)[0]; - mockedAxios.get.mockImplementationOnce(() => Promise.resolve({ data: selectedApp })); - - const res = await new VariantsAdmin().find(mockedAxios, appId); - expect(res).toHaveLength(selectedApp!.variants!.length); - expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); - }); - - it('test find with single filter', async () => { - const appId = '1'; - const selectedApp = mockData.filter(app => app.id === appId)[0]; - mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp })); - - let res = await new VariantsAdmin().find(mockedAxios, appId, { developer: 'developer 2' }); - expect(res).toHaveLength(1); - expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); - - res = await new VariantsAdmin().find(mockedAxios, appId, { developer: 'developer 1' }); - expect(res).toHaveLength(2); - expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); - }); - - it('test find one variant/no filters', async () => { - const appId = '3'; - const selectedApp = mockData.filter(app => app.id === appId)[0]; - mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp })); - - const res = await new VariantsAdmin().find(mockedAxios, appId); - expect(res).toHaveLength(1); - expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); - }); - - it('test find with bad filter', async () => { - const appId = '1'; - const selectedApp = mockData.filter(app => app.id === appId)[0]; - mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp })); - - const res = await new VariantsAdmin().find(mockedAxios, appId, { developer: 'developer 84' }); - expect(res).toHaveLength(0); - expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); - }); - - it('test app without variants', async () => { - const appId = '4'; - const selectedApp = mockData.filter(app => app.id === appId)[0]; - mockedAxios.get.mockImplementation(() => Promise.resolve({ data: selectedApp })); - - const res = await new VariantsAdmin().find(mockedAxios, appId, { developer: 'developer 84' }); - expect(res).toHaveLength(0); - expect(mockedAxios.get).toHaveBeenCalledWith(`/applications/${appId}`); - }); -});