-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import supertest from 'supertest'; | ||
import { expect } from 'chai'; | ||
const configPath = __dirname + '/fixtures/08-sbvrApi/config.js'; | ||
import { testInit, testDeInit, testLocalServer } from './lib/test-init'; | ||
import * as pine from '../src/server-glue/module'; | ||
import { PineTest } from 'pinejs-client-supertest'; | ||
import type { AnyObject } from 'pinejs-client-core'; | ||
|
||
const { permissions } = pine; | ||
|
||
describe('08 sbvrApi', function () { | ||
let pineServer: Awaited<ReturnType<typeof testInit>>; | ||
let pineTest: PineTest; | ||
before(async () => { | ||
pineServer = await testInit({ | ||
configPath, | ||
deleteDb: true, | ||
exposeAuthEndpoints: true, | ||
}); | ||
pineTest = new PineTest({}, { app: testLocalServer }); | ||
}); | ||
|
||
after(async () => { | ||
testDeInit(pineServer); | ||
}); | ||
|
||
describe('permissions.getUserPermissionsForRole', () => { | ||
let guestId: number; | ||
const testPermissions = ['test.permission1', 'test.permission2']; | ||
before(async () => { | ||
guestId = await getUserId('guest'); | ||
const permissionIds = await createPermissions(testPermissions); | ||
const roleId = await createRole('test', permissionIds); | ||
await grantRoleToUser(roleId, guestId); | ||
}); | ||
|
||
it(`should be able to get a specific role's permissions if role exists and user has it`, async () => { | ||
// pine API only exists on the process it is currently running. | ||
// We use custom endpoints to expose the specific funcionality being tested | ||
const response = await supertest(testLocalServer) | ||
.get('/auth-test/getUserPermissionsForRole') | ||
.send({ userId: guestId, roleName: 'test' }); | ||
expect(response.body).to.deep.equal(testPermissions); | ||
}); | ||
}); | ||
|
||
const getUserId = async (username: string): Promise<number> => { | ||
const { | ||
d: [{ id }], | ||
} = await doAuthRequest('GET', 'user', { username }); | ||
return id; | ||
}; | ||
|
||
const createPermissions = async ( | ||
permissionNames: string[], | ||
): Promise<number[]> => { | ||
return ( | ||
await Promise.all( | ||
permissionNames.map(async (permissionName) => | ||
doAuthRequest('POST', 'permission', { name: permissionName }), | ||
), | ||
) | ||
).map((response) => response.id); | ||
}; | ||
|
||
const createRole = async ( | ||
roleName: string, | ||
permissionIds: number[], | ||
): Promise<number> => { | ||
const { id: roleId } = await doAuthRequest('POST', 'role', { | ||
name: roleName, | ||
}); | ||
await Promise.all( | ||
permissionIds.map(async (permissionId) => | ||
doAuthRequest('POST', 'role__has__permission', { | ||
role: roleId, | ||
permission: permissionId, | ||
}), | ||
), | ||
); | ||
|
||
return roleId; | ||
}; | ||
|
||
const grantRoleToUser = async (role: number, user: number) => { | ||
await doAuthRequest('POST', 'user__has__role', { role, user }); | ||
}; | ||
|
||
const doAuthRequest = async ( | ||
method: 'GET' | 'POST', | ||
resource: string, | ||
body: AnyObject, | ||
) => { | ||
return ( | ||
await pineTest.request({ | ||
apiPrefix: 'Auth/', | ||
method, | ||
resource, | ||
body, | ||
passthrough: { req: permissions.root }, | ||
options: { returnResource: false }, | ||
}) | ||
).body; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Vocabulary: basic | ||
|
||
Term: name | ||
Concept Type: Short Text (Type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { ConfigLoader } from '../../../src/server-glue/module'; | ||
|
||
const apiRoot = 'basic'; | ||
const modelName = 'basic'; | ||
const modelFile = __dirname + '/basic.sbvr'; | ||
|
||
export default { | ||
models: [ | ||
{ | ||
modelName, | ||
modelFile, | ||
apiRoot, | ||
}, | ||
], | ||
users: [ | ||
{ | ||
username: 'guest', | ||
password: ' ', | ||
permissions: ['resource.all'], | ||
}, | ||
], | ||
} as ConfigLoader.Config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters