-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphql,auth): Add authorization to resolvers and relations
- Loading branch information
1 parent
29fdfa7
commit 9d76787
Showing
57 changed files
with
915 additions
and
198 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
packages/query-graphql/__tests__/auth/default-crud-auth.service.spec.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,56 @@ | ||
import { CRUDAuth, Relation } from '../../src'; | ||
import { createDefaultCRUDAuthService } from '../../src/auth'; | ||
|
||
describe('createDefaultCRUDAuthService', () => { | ||
type UserContext = { user: { id: number } }; | ||
class TestRelation { | ||
relationOwnerId!: number; | ||
} | ||
|
||
@CRUDAuth({ filter: (ctx: UserContext) => ({ decoratorOwnerId: { eq: ctx.user.id } }) }) | ||
class TestDecoratorRelation { | ||
decoratorOwnerId!: number; | ||
} | ||
|
||
@CRUDAuth({ filter: (ctx: UserContext) => ({ ownerId: { eq: ctx.user.id } }) }) | ||
@Relation('relations', () => TestRelation, { | ||
auth: { filter: (ctx: UserContext) => ({ relationOwnerId: { eq: ctx.user.id } }) }, | ||
}) | ||
@Relation('decoratorRelations', () => [TestDecoratorRelation]) | ||
class TestDTO { | ||
ownerId!: number; | ||
} | ||
|
||
it('should create an auth filter', async () => { | ||
const Service = createDefaultCRUDAuthService(TestDTO); | ||
const filter = await new Service().authFilter({ user: { id: 2 } }); | ||
expect(filter).toEqual({ ownerId: { eq: 2 } }); | ||
}); | ||
|
||
it('should return an empty filter if auth not found', async () => { | ||
class TestNoAuthDTO { | ||
ownerId!: number; | ||
} | ||
const Service = createDefaultCRUDAuthService(TestNoAuthDTO); | ||
const filter = await new Service().authFilter({ user: { id: 2 } }); | ||
expect(filter).toEqual({}); | ||
}); | ||
|
||
it('should create an auth filter for relations using the default auth decorator', async () => { | ||
const Service = createDefaultCRUDAuthService(TestDTO); | ||
const filter = await new Service().relationAuthFilter('decoratorRelations', { user: { id: 2 } }); | ||
expect(filter).toEqual({ decoratorOwnerId: { eq: 2 } }); | ||
}); | ||
|
||
it('should create an auth filter for relations using the relation options', async () => { | ||
const Service = createDefaultCRUDAuthService(TestDTO); | ||
const filter = await new Service().relationAuthFilter('relations', { user: { id: 2 } }); | ||
expect(filter).toEqual({ relationOwnerId: { eq: 2 } }); | ||
}); | ||
|
||
it('should return an empty object for an unknown relation', async () => { | ||
const Service = createDefaultCRUDAuthService(TestDTO); | ||
const filter = await new Service().relationAuthFilter('unknownRelations', { user: { id: 2 } }); | ||
expect(filter).toEqual({}); | ||
}); | ||
}); |
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
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
14 changes: 14 additions & 0 deletions
14
packages/query-graphql/__tests__/resolvers/__fixtures__/test-resolver-auth.service.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,14 @@ | ||
import { Filter } from '@nestjs-query/core'; | ||
import { CRUDAuthService, AuthorizationService } from '../../../src'; | ||
import { TestResolverDTO } from './test-resolver.dto'; | ||
|
||
@AuthorizationService(TestResolverDTO) | ||
export class TestResolverAuthService implements CRUDAuthService<TestResolverDTO> { | ||
authFilter(context: any): Promise<Filter<TestResolverDTO>> { | ||
return Promise.reject(new Error('authFilter Not Implemented')); | ||
} | ||
|
||
relationAuthFilter<Relation>(relationName: string, context: any): Promise<Filter<Relation>> { | ||
return Promise.reject(new Error('relationAuthFilter Not Implemented')); | ||
} | ||
} |
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.