Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Oct 22, 2024
1 parent 6221b2a commit 9e8ecd9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import {SciLogDbApplication} from '../..';
import {
clearDatabase,
createAdminToken,
createAUser,
createToken,
createUserToken,
setupApplication,
} from './test-helper';
import _ from 'lodash';
import {arrayOfUniqueFrom} from '../../utils/misc';

describe('Basesnippet', function (this: Suite) {
this.timeout(5000);
Expand All @@ -16,7 +19,6 @@ describe('Basesnippet', function (this: Suite) {
let token: string;
let adminToken: string;
let baseSnippetId: string;
let nonVisibleSnippetId: string;
const baseSnippet = {
ownerGroup: 'basesnippetAcceptance',
createACL: ['basesnippetAcceptance'],
Expand Down Expand Up @@ -494,7 +496,7 @@ describe('Basesnippet', function (this: Suite) {
.expect(404);
});

it('post a basesnippet with authentication and parentId from existing snippet should return 200 and have ownergroup with priority on parent ACLS', async () => {
it('post a basesnippet with authentication and parentId from existing snippet should return 200 and append to parentACL', async () => {
await client
.post('/basesnippets')
.set('Authorization', 'Bearer ' + token)
Expand All @@ -503,10 +505,13 @@ describe('Basesnippet', function (this: Suite) {
.expect(200)
.then(
result => (
expect(result.body).to.containEql(baseSnippet),
expect(result.body).to.containEql(_.omit(baseSnippet, 'createACL')),
expect(result.body.snippetType).to.be.eql('base'),
expect(result.body.readACL).to.be.eql(['basesnippetAcceptance']),
expect(result.body.createACL).to.be.eql(['basesnippetAcceptance']),
expect(result.body.createACL).to.be.eql([
'basesnippetAcceptance',
'aNewCreateACL',
]),
expect(result.body.updateACL).to.be.eql(['basesnippetAcceptance']),
expect(result.body.deleteACL).to.be.eql(['basesnippetAcceptance']),
expect(result.body.shareACL).to.be.eql(['basesnippetAcceptance']),
Expand All @@ -518,7 +523,7 @@ describe('Basesnippet', function (this: Suite) {
});
});

it('post a basesnippet with authentication and parentId from existing snippet setting explict ACLS should return 200 and have set ACLS with priority on ownergroup and parentACLs', async () => {
it('post a basesnippet with authentication and parentId from existing snippet setting explict ACLS should return 200 and have set ACLS merging parent and child', async () => {
await client
.post('/basesnippets')
.set('Authorization', 'Bearer ' + token)
Expand All @@ -533,17 +538,30 @@ describe('Basesnippet', function (this: Suite) {
.then(
result => (
expect(result.body).to.containEql({
..._.omit(baseSnippet, ['ownerGroup', 'readACL', 'updateACL']),
..._.omit(baseSnippet, [
'ownerGroup',
'readACL',
'updateACL',
'createACL',
]),
ownerGroup: 'aReadACL',
}),
expect(result.body.snippetType).to.be.eql('base'),
expect(result.body.readACL).to.be.eql(['aReadACL']),
expect(result.body.createACL).to.be.eql(['basesnippetAcceptance']),
expect(result.body.updateACL).to.be.eql(['anUpdateACL']),
expect(result.body.readACL).to.be.eql([
'aReadACL',
'basesnippetAcceptance',
]),
expect(result.body.createACL).to.be.eql([
'basesnippetAcceptance',
'aNewCreateACL',
]),
expect(result.body.updateACL).to.be.eql([
'anUpdateACL',
'basesnippetAcceptance',
]),
expect(result.body.deleteACL).to.be.eql(['basesnippetAcceptance']),
expect(result.body.shareACL).to.be.eql(['basesnippetAcceptance']),
expect(result.body.adminACL).to.be.eql(['admin']),
(nonVisibleSnippetId = result.body.id)
expect(result.body.adminACL).to.be.eql(['admin'])
),
)
.catch(err => {
Expand All @@ -552,11 +570,18 @@ describe('Basesnippet', function (this: Suite) {
});

it('get snippet with ID with token having changed readACL should return 404', async () => {
const unAuthUser = await createAUser(app, ['unAuthorised'], {
email: 'unauth.com',
firstName: 'un',
lastName: 'Auth',
roles: [],
});
const unAuthToken = await createToken(client, unAuthUser);
await client
.get(`/basesnippets/${nonVisibleSnippetId}`)
.set('Authorization', 'Bearer ' + token)
.get(`/basesnippets/${baseSnippetId}`)
.set('Authorization', 'Bearer ' + unAuthToken)
.set('Content-Type', 'application/json')
.expect(404);
.expect(403);
});

it('get snippet with token and ownerGroup filter should be greater than one', async () => {
Expand Down Expand Up @@ -750,14 +775,20 @@ describe('Basesnippet', function (this: Suite) {
.then(
result => (
expect(result.body.ownerGroup).to.be.eql('basesnippetAcceptance'),
expect(result.body.accessGroups).to.be.eql([
'basesnippetAcceptance',
'someNew',
]),
expect(result.body.readACL).to.be.eql([
'basesnippetAcceptance',
'someNew',
])
expect(result.body.accessGroups).to.be.eql(
arrayOfUniqueFrom(
'basesnippetAcceptance',
'someNew',
t.input.accessGroups,
),
),
expect(result.body.readACL).to.be.eql(
arrayOfUniqueFrom(
'basesnippetAcceptance',
'someNew',
t.input.accessGroups,
),
)
),
)
.catch(err => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,17 @@ describe('Location', function (this: Suite) {
.expect(200)
.then(
result => (
expect(result.body).to.containEql(locationSnippet),
expect(result.body.readACL).to.be.eql([locationSnippet.ownerGroup]),
expect(result.body).to.containEql(
_.omit(locationSnippet, ['readACL', 'deleteACL']),
),
expect(result.body.readACL).to.be.eql([
locationSnippet.ownerGroup,
'any-authenticated-user',
]),
expect(result.body.deleteACL).to.be.eql([
locationSnippet.ownerGroup,
'admin',
]),
(locationSnippetId = result.body.id)
),
)
Expand Down Expand Up @@ -106,7 +115,17 @@ describe('Location', function (this: Suite) {
.then(
result => (
expect(result.body.length).to.be.eql(1),
expect(result.body[0]).to.containEql(locationSnippet)
expect(result.body[0]).to.containEql(
_.omit(locationSnippet, ['readACL', 'deleteACL']),
),
expect(result.body[0].readACL).to.be.eql([
locationSnippet.ownerGroup,
'any-authenticated-user',
]),
expect(result.body[0].deleteACL).to.be.eql([
locationSnippet.ownerGroup,
'admin',
])
),
)
.catch(err => {
Expand All @@ -127,7 +146,21 @@ describe('Location', function (this: Suite) {
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
.then(result => expect(result.body).to.containEql(locationSnippet))
.then(
result => (
expect(result.body).to.containEql(
_.omit(locationSnippet, ['readACL', 'deleteACL']),
),
expect(result.body.readACL).to.be.eql([
locationSnippet.ownerGroup,
'any-authenticated-user',
]),
expect(result.body.deleteACL).to.be.eql([
locationSnippet.ownerGroup,
'admin',
])
),
)
.catch(err => {
throw err;
});
Expand Down
2 changes: 1 addition & 1 deletion sci-log-db/src/__tests__/acceptance/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export async function createAUser(
return newUser;
}

async function createToken(client: Client, user: User) {
export async function createToken(client: Client, user: User) {
const loginResponse = await client
.post('/users/login')
.send({principal: user.email, password: userPassword});
Expand Down

0 comments on commit 9e8ecd9

Please sign in to comment.