Skip to content

Commit

Permalink
feat: add test cases for all objects and all operations except resour…
Browse files Browse the repository at this point in the history
…ce.ts (#38)

* chore(yarn.lock): update yarn.lock file by nodejs18

* test: add test cases for all objects and all operations
  • Loading branch information
huyonger authored Nov 1, 2023
1 parent adce747 commit 3ab8905
Show file tree
Hide file tree
Showing 42 changed files with 2,110 additions and 501 deletions.
1 change: 1 addition & 0 deletions public/casbinTest.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 16 additions & 26 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ export interface Adapter {
name: string
createdTime: string

type: string
databaseType: string
type?: string
databaseType?: string
host: string
port: number
port?: number
user: string
password: string
database: string
table: string
tableNamePrefix: string
password?: string
database?: string
table?: string
tableNamePrefix?: string

isEnabled: boolean
isEnabled?: boolean
}

export class AdapterSDK {
Expand All @@ -51,10 +51,8 @@ export class AdapterSDK {
return (await this.request.get('/get-adapters', {
params: {
owner: this.config.orgName,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
})) as unknown as Promise<AxiosResponse<Adapter[]>>
})) as unknown as Promise<AxiosResponse<{ data: Adapter[] }>>
}

public async getAdapter(id: string) {
Expand All @@ -65,31 +63,23 @@ export class AdapterSDK {
return (await this.request.get('/get-adapter', {
params: {
id: `${this.config.orgName}/${id}`,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
})) as unknown as Promise<AxiosResponse<Adapter>>
})) as unknown as Promise<AxiosResponse<{ data: Adapter }>>
}

public async modifyAdapter(method: string, adapter: Adapter) {
if (!this.request) {
throw new Error('request init failed')
}

const url = this.config.endpoint + `/${method}`
const url = `/${method}`
adapter.owner = this.config.orgName
const adapterInfo = JSON.stringify(adapter)
return (await this.request.post(
url,
{ adapterInfo },
{
params: {
id: `${adapter.owner}/${adapter.name}`,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},

return (await this.request.post(url, adapter, {
params: {
id: `${adapter.owner}/${adapter.name}`,
},
)) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
})) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
}

public async addAdapter(adapter: Adapter) {
Expand Down
21 changes: 0 additions & 21 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,6 @@ export class ApplicationSDK {
params: {
owner: 'admin',
},
headers: {
Authorization:
'Basic ' +
Buffer.from(
`${this.config.clientId}:${this.config.clientSecret}`,
).toString('base64'),
},
})) as unknown as Promise<AxiosResponse<{ data: Application[] }>>
}

Expand All @@ -119,13 +112,6 @@ export class ApplicationSDK {
params: {
id: `admin/${name}`,
},
headers: {
Authorization:
'Basic ' +
Buffer.from(
`${this.config.clientId}:${this.config.clientSecret}`,
).toString('base64'),
},
})) as unknown as Promise<AxiosResponse<{ data: Application }>>
}

Expand All @@ -140,13 +126,6 @@ export class ApplicationSDK {
params: {
id: `${application.owner}/${application.name}`,
},
headers: {
Authorization:
'Basic ' +
Buffer.from(
`${this.config.clientId}:${this.config.clientSecret}`,
).toString('base64'),
},
})) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
}

Expand Down
33 changes: 11 additions & 22 deletions src/cert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export interface Cert {
bitSize: number
expireInYears: number

certificate: string
privateKey: string
authorityPublicKey: string
authorityRootPublicKey: string
certificate?: string
privateKey?: string
authorityPublicKey?: string
authorityRootPublicKey?: string
}

export class CertSDK {
Expand All @@ -51,10 +51,8 @@ export class CertSDK {
return (await this.request.get('/get-certs', {
params: {
owner: this.config.orgName,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
})) as unknown as Promise<AxiosResponse<Cert[]>>
})) as unknown as Promise<AxiosResponse<{ data: Cert[] }>>
}

public async getCert(id: string) {
Expand All @@ -65,31 +63,22 @@ export class CertSDK {
return (await this.request.get('/get-cert', {
params: {
id: `${this.config.orgName}/${id}`,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
})) as unknown as Promise<AxiosResponse<Cert>>
})) as unknown as Promise<AxiosResponse<{ data: Cert }>>
}

public async modifyCert(method: string, cert: Cert) {
if (!this.request) {
throw new Error('request init failed')
}

const url = this.config.endpoint + `/${method}`
const url = `/${method}`
cert.owner = this.config.orgName
const certInfo = JSON.stringify(cert)
return (await this.request.post(
url,
{ certInfo },
{
params: {
id: `${cert.owner}/${cert.name}`,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
return (await this.request.post(url, cert, {
params: {
id: `${cert.owner}/${cert.name}`,
},
)) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
})) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
}

public async addCert(cert: Cert) {
Expand Down
27 changes: 8 additions & 19 deletions src/enforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ export interface Enforcer {
owner: string
name: string
createdTime: string
updatedTime: string
updatedTime?: string
displayName: string
description: string

model: string
adapter: string
isEnabled: boolean
isEnabled?: boolean

// *casbin.Enforcer
}
Expand All @@ -48,10 +48,8 @@ export class EnforcerSDK {
return (await this.request.get('/get-enforcers', {
params: {
owner: this.config.orgName,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
})) as unknown as Promise<AxiosResponse<Enforcer[]>>
})) as unknown as Promise<AxiosResponse<{ data: Enforcer[] }>>
}

public async getEnforcer(id: string) {
Expand All @@ -62,10 +60,8 @@ export class EnforcerSDK {
return (await this.request.get('/get-enforcer', {
params: {
id: `${this.config.orgName}/${id}`,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
})) as unknown as Promise<AxiosResponse<Enforcer>>
})) as unknown as Promise<AxiosResponse<{ data: Enforcer }>>
}

public async modifyEnforcer(method: string, enforcer: Enforcer) {
Expand All @@ -75,18 +71,11 @@ export class EnforcerSDK {

const url = `/${method}`
enforcer.owner = this.config.orgName
const enforcerInfo = JSON.stringify(enforcer)
return (await this.request.post(
url,
{ enforcerInfo },
{
params: {
id: `${enforcer.owner}/${enforcer.name}`,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
return (await this.request.post(url, enforcer, {
params: {
id: `${enforcer.owner}/${enforcer.name}`,
},
)) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
})) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
}

public async addEnforcer(enforcer: Enforcer) {
Expand Down
37 changes: 13 additions & 24 deletions src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ export interface Group {
owner: string
name: string
createdTime: string
updatedTime: string
updatedTime?: string

displayName: string
manager: string
contactEmail: string
type: string
parentId: string
isTopGroup: boolean
manager?: string
contactEmail?: string
type?: string
parentId?: string
isTopGroup?: boolean
users?: User[]

title?: string
key?: string
children?: Group[]

isEnabled: boolean
isEnabled?: boolean
}

export class GroupSDK {
Expand All @@ -55,10 +55,8 @@ export class GroupSDK {
return (await this.request.get('/get-groups', {
params: {
owner: this.config.orgName,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
})) as unknown as Promise<AxiosResponse<Group[]>>
})) as unknown as Promise<AxiosResponse<{ data: Group[] }>>
}

public async getGroup(id: string) {
Expand All @@ -69,10 +67,8 @@ export class GroupSDK {
return (await this.request.get('/get-group', {
params: {
id: `${this.config.orgName}/${id}`,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
})) as unknown as Promise<AxiosResponse<Group>>
})) as unknown as Promise<AxiosResponse<{ data: Group }>>
}

public async modifyGroup(method: string, group: Group) {
Expand All @@ -82,18 +78,11 @@ export class GroupSDK {

const url = `/${method}`
group.owner = this.config.orgName
const groupInfo = JSON.stringify(group)
return (await this.request.post(
url,
{ groupInfo },
{
params: {
id: `${group.owner}/${group.name}`,
clientId: this.config.clientId,
clientSecret: this.config.clientSecret,
},
return (await this.request.post(url, group, {
params: {
id: `${group.owner}/${group.name}`,
},
)) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
})) as unknown as Promise<AxiosResponse<Record<string, unknown>>>
}

public async addGroup(group: Group) {
Expand Down
Loading

0 comments on commit 3ab8905

Please sign in to comment.