|
| 1 | +import Axios from 'axios' |
| 2 | +import { expect } from 'chai' |
| 3 | +import { App } from '../../lib/app' |
| 4 | +import { describe, it } from 'mocha' |
| 5 | +import MockAdapter from 'axios-mock-adapter' |
| 6 | +import { appMock, noticeMock, oAuthMock } from './mock/objects' |
| 7 | + |
| 8 | +describe('Contentstack apps test', () => { |
| 9 | + it('App without app uid', done => { |
| 10 | + const app = makeApp({}) |
| 11 | + expect(app.urlPath).to.be.equal('/apps') |
| 12 | + expect(app.create).to.not.equal(undefined) |
| 13 | + expect(app.findAll).to.not.equal(undefined) |
| 14 | + expect(app.fetch).to.be.equal(undefined) |
| 15 | + expect(app.update).to.be.equal(undefined) |
| 16 | + expect(app.delete).to.be.equal(undefined) |
| 17 | + expect(app.fetchOAuth).to.be.equal(undefined) |
| 18 | + expect(app.updateOAuth).to.be.equal(undefined) |
| 19 | + expect(app.install).to.be.equal(undefined) |
| 20 | + expect(app.installation).to.be.equal(undefined) |
| 21 | + done() |
| 22 | + }) |
| 23 | + |
| 24 | + it('App with app uid', done => { |
| 25 | + const uid = 'APP_UID' |
| 26 | + const app = makeApp({ data: { uid } }) |
| 27 | + expect(app.urlPath).to.be.equal(`/apps/${uid}`) |
| 28 | + expect(app.create).to.be.equal(undefined) |
| 29 | + expect(app.findAll).to.be.equal(undefined) |
| 30 | + expect(app.fetch).to.not.equal(undefined) |
| 31 | + expect(app.update).to.not.equal(undefined) |
| 32 | + expect(app.delete).to.not.equal(undefined) |
| 33 | + expect(app.fetchOAuth).to.not.equal(undefined) |
| 34 | + expect(app.updateOAuth).to.not.equal(undefined) |
| 35 | + expect(app.install).to.not.equal(undefined) |
| 36 | + expect(app.installation).to.not.equal(undefined) |
| 37 | + done() |
| 38 | + }) |
| 39 | + |
| 40 | + it('Create app test', done => { |
| 41 | + const mock = new MockAdapter(Axios) |
| 42 | + mock.onPost('/apps').reply(200, { |
| 43 | + data: { |
| 44 | + ...appMock |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + makeApp({}) |
| 49 | + .create({}) |
| 50 | + .then((app) => { |
| 51 | + checkApp(app) |
| 52 | + done() |
| 53 | + }) |
| 54 | + .catch(done) |
| 55 | + }) |
| 56 | + |
| 57 | + it('Update app test', done => { |
| 58 | + const mock = new MockAdapter(Axios) |
| 59 | + const uid = appMock.uid |
| 60 | + mock.onPut(`/apps/${uid}`).reply(200, { |
| 61 | + data: { |
| 62 | + ...appMock |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + makeApp({ data: { uid } }) |
| 67 | + .update() |
| 68 | + .then((app) => { |
| 69 | + checkApp(app) |
| 70 | + done() |
| 71 | + }) |
| 72 | + .catch(done) |
| 73 | + }) |
| 74 | + |
| 75 | + it('Get app from UID test', done => { |
| 76 | + const mock = new MockAdapter(Axios) |
| 77 | + const uid = appMock.uid |
| 78 | + mock.onGet(`/apps/${uid}`).reply(200, { |
| 79 | + data: { |
| 80 | + ...appMock |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + makeApp({ data: { uid } }) |
| 85 | + .fetch() |
| 86 | + .then((app) => { |
| 87 | + checkApp(app) |
| 88 | + done() |
| 89 | + }) |
| 90 | + .catch(done) |
| 91 | + }) |
| 92 | + |
| 93 | + it('Delete app from UID test', done => { |
| 94 | + const mock = new MockAdapter(Axios) |
| 95 | + const uid = appMock.uid |
| 96 | + mock.onDelete(`/apps/${uid}`).reply(200, { |
| 97 | + ...noticeMock |
| 98 | + }) |
| 99 | + |
| 100 | + makeApp({ data: { uid } }) |
| 101 | + .delete() |
| 102 | + .then((response) => { |
| 103 | + expect(response.notice).to.not.equal(undefined) |
| 104 | + expect(response.notice).to.be.equal(noticeMock.notice) |
| 105 | + done() |
| 106 | + }) |
| 107 | + .catch(done) |
| 108 | + }) |
| 109 | + |
| 110 | + it('Get all apps in organization test', done => { |
| 111 | + const mock = new MockAdapter(Axios) |
| 112 | + mock.onGet(`/apps`).reply(200, { |
| 113 | + data: [appMock] |
| 114 | + }) |
| 115 | + |
| 116 | + makeApp({}) |
| 117 | + .findAll() |
| 118 | + .then((apps) => { |
| 119 | + checkApp(apps.items[0]) |
| 120 | + done() |
| 121 | + }) |
| 122 | + .catch(done) |
| 123 | + }) |
| 124 | + |
| 125 | + it('Get oAuth configuration test', done => { |
| 126 | + const mock = new MockAdapter(Axios) |
| 127 | + const uid = appMock.uid |
| 128 | + mock.onGet(`/apps/${uid}/oauth`).reply(200, { |
| 129 | + data: { |
| 130 | + ...oAuthMock |
| 131 | + } |
| 132 | + }) |
| 133 | + |
| 134 | + makeApp({ data: { uid } }) |
| 135 | + .fetchOAuth() |
| 136 | + .then((oAuthConfig) => { |
| 137 | + expect(oAuthConfig.client_id).to.be.equal(oAuthMock.client_id) |
| 138 | + expect(oAuthConfig.client_secret).to.be.equal(oAuthMock.client_secret) |
| 139 | + expect(oAuthConfig.redirect_uri).to.be.equal(oAuthMock.redirect_uri) |
| 140 | + expect(oAuthConfig.app_token_config.enabled).to.be.equal(oAuthMock.app_token_config.enabled) |
| 141 | + expect(oAuthConfig.user_token_config.enabled).to.be.equal(oAuthMock.user_token_config.enabled) |
| 142 | + done() |
| 143 | + }) |
| 144 | + .catch(done) |
| 145 | + }) |
| 146 | + |
| 147 | + it('Update oAuth configuration test', done => { |
| 148 | + const mock = new MockAdapter(Axios) |
| 149 | + const uid = appMock.uid |
| 150 | + mock.onPut(`/apps/${uid}/oauth`).reply(200, { |
| 151 | + data: { |
| 152 | + ...oAuthMock |
| 153 | + } |
| 154 | + }) |
| 155 | + const config = { ...oAuthMock } |
| 156 | + makeApp({ data: { uid } }) |
| 157 | + .updateOAuth({ config }) |
| 158 | + .then((oAuthConfig) => { |
| 159 | + expect(oAuthConfig.client_id).to.be.equal(oAuthMock.client_id) |
| 160 | + expect(oAuthConfig.client_secret).to.be.equal(oAuthMock.client_secret) |
| 161 | + expect(oAuthConfig.redirect_uri).to.be.equal(oAuthMock.redirect_uri) |
| 162 | + expect(oAuthConfig.app_token_config.enabled).to.be.equal(oAuthMock.app_token_config.enabled) |
| 163 | + expect(oAuthConfig.user_token_config.enabled).to.be.equal(oAuthMock.user_token_config.enabled) |
| 164 | + done() |
| 165 | + }) |
| 166 | + .catch(done) |
| 167 | + }) |
| 168 | +}) |
| 169 | + |
| 170 | +function checkApp (app) { |
| 171 | + expect(app.urlPath).to.be.equal('/apps/UID') |
| 172 | + expect(app.created_at).to.be.equal('created_at_date') |
| 173 | + expect(app.updated_at).to.be.equal('updated_at_date') |
| 174 | + expect(app.uid).to.be.equal('UID') |
| 175 | + expect(app.name).to.be.equal('Name of the app') |
| 176 | + expect(app.description).to.be.equal('Description of the app') |
| 177 | + expect(app.organization_uid).to.be.equal('org_uid') |
| 178 | +} |
| 179 | + |
| 180 | +function makeApp (data) { |
| 181 | + return new App(Axios, data) |
| 182 | +} |
0 commit comments