Skip to content

Commit 93f3e38

Browse files
committed
feat: 🚧 authorization apps api [CS-35317]
1 parent 8c1dbe1 commit 93f3e38

File tree

11 files changed

+371
-5
lines changed

11 files changed

+371
-5
lines changed

lib/app/authorization/index.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import error from '../../core/contentstackError'
3+
4+
export function Authorization (http, data, params) {
5+
http.defaults.versioningStrategy = undefined
6+
this.params = params || {}
7+
if (data) {
8+
if (data.organization_uid) {
9+
this.params = {
10+
organization_uid: data.organization_uid
11+
}
12+
}
13+
if (data.app_uid) {
14+
this.urlPath = `/manifests/${data.app_uid}/authorizations`
15+
/**
16+
* @description List all user authorizations made to an authorized app under a particular organization
17+
* @memberof Authorization
18+
* @func findAll
19+
* @returns {Promise<Response>}
20+
*
21+
* @example
22+
* import * as contentstack from '@contentstack/management'
23+
* const client = contentstack.client({ authtoken: 'TOKEN'})
24+
*
25+
* client.organization('organization_uid').app('manifest_uid').authorization().findAll()
26+
* .then((response) => console.log(response))
27+
*/
28+
this.findAll = async (param = {}) => {
29+
try {
30+
const headers = {
31+
headers: { ...cloneDeep(this.params) },
32+
params: {
33+
...cloneDeep(param)
34+
}
35+
}
36+
37+
const response = await http.get(this.urlPath, headers)
38+
if (response.data) {
39+
return response.data
40+
} else {
41+
throw error(response)
42+
}
43+
} catch (err) {
44+
throw error(err)
45+
}
46+
}
47+
/**
48+
* @description Revoke all users tokens issued to an authorized app for the particular organization
49+
* @memberof Authorization
50+
* @func revokeAll
51+
* @returns {Promise<Response>}
52+
*
53+
* @example
54+
* import * as contentstack from '@contentstack/management'
55+
* const client = contentstack.client({ authtoken: 'TOKEN'})
56+
*
57+
* client.organization('organization_uid').app('manifest_uid').authorization().revokeAll()
58+
* .then((response) => console.log(response))
59+
*/
60+
this.revokeAll = async () => {
61+
try {
62+
const headers = {
63+
headers: { ...cloneDeep(this.params) }
64+
}
65+
66+
const response = await http.delete(this.urlPath, headers)
67+
if (response.data) {
68+
return response.data
69+
} else {
70+
throw error(response)
71+
}
72+
} catch (err) {
73+
throw error(err)
74+
}
75+
}
76+
/**
77+
* @description Revoke user token issued to an authorized app for the particular organization
78+
* @memberof Authorization
79+
* @func revoke
80+
* @returns {Promise<Response>}
81+
*
82+
* @example
83+
* import * as contentstack from '@contentstack/management'
84+
* const client = contentstack.client({ authtoken: 'TOKEN'})
85+
*
86+
* client.organization('organization_uid').app('manifest_uid').authorization().revoke('authorization_uid')
87+
* .then((response) => console.log(response))
88+
*/
89+
this.revoke = async (authorizationUid) => {
90+
try {
91+
const headers = {
92+
headers: { ...cloneDeep(this.params) }
93+
}
94+
95+
const response = await http.delete(`${this.urlPath}/${authorizationUid}`, headers)
96+
if (response.data) {
97+
return response.data
98+
} else {
99+
throw error(response)
100+
}
101+
} catch (err) {
102+
throw error(err)
103+
}
104+
}
105+
}
106+
}
107+
}

lib/app/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cloneDeep from 'lodash/cloneDeep'
22
import error from '../core/contentstackError'
33
import { create, deleteEntity, fetch, fetchAll, update } from '../entity'
4+
import { Authorization } from './authorization'
45
import { Hosting } from './hosting'
56
import { Installation } from './installation'
67

@@ -295,6 +296,20 @@ export function App (http, data) {
295296
throw error(err)
296297
}
297298
}
299+
/**
300+
* @description The Authorization will allow you to get all authorization, revoke specific or all authorization
301+
* @memberof App
302+
* @func authorization
303+
* @returns {Authorization}
304+
*
305+
* @example
306+
* import * as contentstack from '@contentstack/management'
307+
* const client = contentstack.client({ authtoken: 'TOKEN'})
308+
* client.organization('organization_uid').app('manifest_uid').authorization()
309+
*/
310+
this.authorization = () => {
311+
return new Authorization(http, { app_uid: this.uid }, this.params)
312+
}
298313
} else {
299314
/**
300315
* @description The create manifest call is used for creating a new app/manifest in your Contentstack organization.

test/api/authorization-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import dotenv from 'dotenv'
2+
import { describe, it, setup } from 'mocha'
3+
import { jsonReader } from '../utility/fileOperations/readwrite'
4+
import { contentstackClient } from '../utility/ContentstackClient.js'
5+
import { expect } from 'chai'
6+
7+
dotenv.config()
8+
9+
const orgID = process.env.ORGANIZATION
10+
let client = {}
11+
let apps = {}
12+
13+
describe('Apps api Test', () => {
14+
setup(() => {
15+
const user = jsonReader('loggedinuser.json')
16+
client = contentstackClient(user.authtoken)
17+
apps = jsonReader('apps.json')
18+
})
19+
20+
it('fetch all authorization apps test', done => {
21+
client.organization(orgID).app(apps.uid).authorization().findAll()
22+
.then((response) => {
23+
expect(response).to.not.equal(null)
24+
done()
25+
})
26+
.catch(done)
27+
})
28+
29+
it('revoke all authorization apps test', done => {
30+
client.organization(orgID).app(apps.uid).authorization().revokeAll()
31+
.then((response) => {
32+
expect(response).to.not.equal(null)
33+
done()
34+
})
35+
.catch(done)
36+
})
37+
38+
it('revoke all authorization apps test', done => {
39+
client.organization(orgID).app(apps.uid).authorization().revoke('uid')
40+
.then((response) => {
41+
expect(response).to.not.equal(null)
42+
done()
43+
})
44+
.catch(done)
45+
})
46+
})

test/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ require('./api/organization-test')
33
require('./api/stack-test')
44
require('./api/app-test')
55
require('./api/hosting-test')
6-
require('./api/app-delete-test')
76
require('./api/app-request-test')
7+
require('./api/authorization-test')
8+
require('./api/app-delete-test')
89
require('./api/branch-test')
910
require('./api/branchAlias-test')
1011
require('./api/locale-test')

test/typescript/app-request.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { expect } from 'chai'
22
import * as dotenv from 'dotenv'
3-
import { Hosting } from '../../types/app/hosting'
43
import { AppRequest } from '../../types/app/request'
54
dotenv.config()
65
let requestUID = ''

test/typescript/authorization.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expect } from 'chai'
2+
import * as dotenv from 'dotenv'
3+
import { Authorization } from '../../types/app/authorization'
4+
dotenv.config()
5+
6+
7+
export function authorization (authorization: Authorization) {
8+
describe('Authorization Apps api', () => {
9+
test('test get all authorization for apps', done => {
10+
authorization.findAll()
11+
.then((response) => {
12+
expect(response).to.not.equal(undefined)
13+
done()
14+
})
15+
.catch(done)
16+
})
17+
test('test revoke all authorization for apps', done => {
18+
authorization.revokeAll()
19+
.then((response) => {
20+
expect(response).to.not.equal(undefined)
21+
done()
22+
})
23+
.catch(done)
24+
})
25+
})
26+
}

test/typescript/index.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import { createDeliveryToken, deleteDeliveryToken, deliveryToken, queryDeliveryT
1515
import { createRole, findAllRole, getRole, getRoleUid, queryRole } from './role';
1616
import { createApp, deleteApp, fetchApp, installation, updateApp, updateAuth } from './app';
1717
import { deployment, hosting } from './hosting';
18-
import { appRequest, orgAppRequest } from './app-request';
18+
import { orgAppRequest } from './app-request';
19+
import { authorization } from './authorization';
1920
dotenv.config()
2021
jest.setTimeout(10000);
2122

@@ -43,8 +44,9 @@ describe('Typescript API test', () => {
4344
installation(org)
4445
hosting(org.app(process.env.APP_UID as string).hosting())
4546
deployment(org.app(process.env.APP_UID as string).hosting())
46-
appRequest(org.app(process.env.APP_UID as string).request())
47-
orgAppRequest(org.app().request())
47+
orgAppRequest(org.app(process.env.APP_UID as string).request())
48+
authorization(org.app(process.env.APP_UID as string).authorization())
49+
orgAppRequest(org.request())
4850
deleteApp(org)
4951

5052
const stack = client.stack({api_key: process.env.APIKEY as string})

0 commit comments

Comments
 (0)