-
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.
added permissions API, permissions property and defualt middleware logic
Signed-off-by: Jim Ezesinachi <[email protected]>
- Loading branch information
1 parent
c9e0ae4
commit 7834dc6
Showing
10 changed files
with
178 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
import { test, describe, expect } from 'vitest'; | ||
import { DB, from } from '../tests/generated'; | ||
import { Query } from '@synthql/queries'; | ||
import { col, Query } from '@synthql/queries'; | ||
import { middleware } from './middleware'; | ||
import { createQueryEngine } from '../tests/queryEngine'; | ||
|
||
// Create type/interface for context | ||
type UserRole = 'user' | 'admin' | 'super'; | ||
type UserPermission = 'user:read' | 'admin:read' | 'super:read'; | ||
|
||
interface Session { | ||
id: number; | ||
email: string; | ||
roles: UserRole[]; | ||
isActive: boolean; | ||
roles: UserRole[]; | ||
permissions: UserPermission[]; | ||
} | ||
|
||
// Create middleware | ||
|
@@ -28,24 +31,28 @@ const restrictPaymentsByCustomer = middleware<Query<DB, 'payment'>, Session>({ | |
}), | ||
}); | ||
|
||
describe('createExpressSynthqlHandler', async () => { | ||
test('1', async () => { | ||
const queryEngine = createQueryEngine([restrictPaymentsByCustomer]); | ||
describe('middleware', async () => { | ||
test('Query middleware is correctly executed', async () => { | ||
const queryEngine = createQueryEngine({ | ||
middlewares: [restrictPaymentsByCustomer], | ||
dangerouslyAllowNoPermissions: false, | ||
}); | ||
|
||
// 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: '[email protected]', | ||
roles: ['user', 'admin', 'super'], | ||
isActive: true, | ||
roles: ['user', 'admin', 'super'], | ||
permissions: ['user:read', 'admin:read', 'super:read'], | ||
}; | ||
|
||
// Create base query | ||
const q = from('payment').one(); | ||
const q = createPaymentQuery().one(); | ||
|
||
const queryWithContextManuallyAdded = from('payment') | ||
const queryWithContextManuallyAdded = createPaymentQuery() | ||
.where({ | ||
customer_id: context.id, | ||
}) | ||
|
@@ -54,8 +61,31 @@ describe('createExpressSynthqlHandler', async () => { | |
const result = await queryEngine.executeAndWait(q, { context }); | ||
|
||
const resultFromQueryWithContextManuallyAdded = | ||
await queryEngine.executeAndWait(queryWithContextManuallyAdded); | ||
await queryEngine.executeAndWait(queryWithContextManuallyAdded, { | ||
context: { permissions: context.permissions }, | ||
}); | ||
|
||
expect(result).toEqual(resultFromQueryWithContextManuallyAdded); | ||
}); | ||
}); | ||
|
||
function createPaymentQuery() { | ||
return from('payment') | ||
.permissions('user:read') | ||
.include({ | ||
customer: from('customer') | ||
.permissions('admin:read') | ||
.where({ | ||
customer_id: col('payment.customer_id'), | ||
}) | ||
.one(), | ||
}) | ||
.groupBy( | ||
'amount', | ||
'customer_id', | ||
'payment_date', | ||
'payment_id', | ||
'rental_id', | ||
'staff_id', | ||
); | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.