-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jim Ezesinachi <ezesinachijim@gmail.com>
- Loading branch information
1 parent
7834dc6
commit 43bcf37
Showing
7 changed files
with
178 additions
and
55 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
107 changes: 107 additions & 0 deletions
107
packages/backend/src/tests/propertyBased/properties/permissions.test.ts
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,107 @@ | ||
import { describe, expect } from 'vitest'; | ||
import { test } from '@fast-check/vitest'; | ||
import { AnyQuery, Query } from '@synthql/queries'; | ||
import { ArbitraryQueryBuilder } from '../arbitraries/ArbitraryQueryBuilder'; | ||
import { createQueryEngine } from '../../queryEngine'; | ||
import { DB } from '../../generated'; | ||
|
||
const queryBuilder = ArbitraryQueryBuilder.fromPagila(); | ||
const queryEngine = createQueryEngine({ | ||
dangerouslyIgnorePermissions: false, | ||
}); | ||
|
||
// Create type/interface for context | ||
type UserRole = 'user' | 'admin' | 'super'; | ||
type UserPermission = 'user:read' | 'admin:read' | 'super:read'; | ||
|
||
interface Session { | ||
id: number; | ||
email: string; | ||
isActive: boolean; | ||
roles: UserRole[]; | ||
permissions: UserPermission[]; | ||
} | ||
|
||
// Create context | ||
// This would usually be an object generated from a server | ||
// request handler (e.g a parsed cookie/token) | ||
const context: Session = { | ||
id: 1, | ||
email: 'user@example.com', | ||
isActive: true, | ||
roles: ['user', 'admin', 'super'], | ||
permissions: ['user:read', 'admin:read', 'super:read'], | ||
}; | ||
|
||
describe('Property based tests for permissions', () => { | ||
const numRuns = 100; | ||
const timeout = numRuns * 1000; | ||
const endOnFailure = true; | ||
|
||
test.prop( | ||
[queryBuilder.withCardinality('many').withSomeResults().build()], | ||
{ | ||
verbose: true, | ||
numRuns, | ||
endOnFailure, | ||
}, | ||
)( | ||
[ | ||
'A query with permissions will fail unless all permissions are met', | ||
].join(''), | ||
async (query) => { | ||
const permissionedQuery: Query<DB> = { | ||
...query, | ||
permissions: context.permissions, | ||
name: defaultName(query), | ||
}; | ||
|
||
expect( | ||
async () => await queryEngine.executeAndWait(permissionedQuery), | ||
).rejects.toThrow( | ||
`The query '${permissionedQuery.name}' with a permissions list (ACL) included:`, | ||
); | ||
|
||
// Here we check if it errors when some, | ||
// but not all the permissions, are met | ||
expect( | ||
async () => | ||
await queryEngine.executeAndWait(permissionedQuery, { | ||
context: { permissions: [context.permissions[0]] }, | ||
}), | ||
).rejects.toThrow( | ||
`The query '${permissionedQuery.name}' with a permissions list (ACL) included:`, | ||
); | ||
}, | ||
timeout, | ||
); | ||
|
||
test.prop( | ||
[queryBuilder.withCardinality('many').withSomeResults().build()], | ||
{ | ||
verbose: true, | ||
numRuns, | ||
endOnFailure, | ||
}, | ||
)( | ||
[ | ||
'A query with no permissions will never fail for permission issues', | ||
].join(''), | ||
async (query) => { | ||
expect( | ||
async () => await queryEngine.executeAndWait(query), | ||
).not.toThrow(); | ||
}, | ||
timeout, | ||
); | ||
}); | ||
|
||
function defaultName(query: AnyQuery) { | ||
const whereName = Object.keys(query.where).join('-and-'); | ||
|
||
if (whereName === '') { | ||
return `${query.from}-all`; | ||
} | ||
|
||
return `${query.from}-by-${whereName}`; | ||
} |
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
Oops, something went wrong.