-
Notifications
You must be signed in to change notification settings - Fork 10
Add GetUserSelectableRoles use case #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b743a4e
add use case and unit test
ekraffmiller aa762a8
add integration useCase
ekraffmiller 37e0e8b
add documentation
ekraffmiller 2854a5c
remove debug logs
ekraffmiller 8dcdffc
add index.ts
ekraffmiller 02e283d
fix useCase name
ekraffmiller 0bc6090
add roles directory to export list
ekraffmiller 87d384a
add RolePayload.ts
ekraffmiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| export interface Role { | ||
| id: number | ||
| name: string | ||
| alias: string | ||
| description: string | ||
| permissions: string[] | ||
| } |
This file contains hidden or 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,5 @@ | ||
| import { Role } from '../models/Role' | ||
|
|
||
| export interface IRolesRepository { | ||
| getUserSelectableRoles(): Promise<Role[]> | ||
| } |
This file contains hidden or 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,7 @@ | ||
| export interface RolePayload { | ||
| id: number | ||
| name: string | ||
| alias: string | ||
| description: string | ||
| permissions: string[] | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/roles/domain/repositories/transformers/roleTransformers.ts
This file contains hidden or 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,15 @@ | ||
| import { AxiosResponse } from 'axios' | ||
| import { Role } from '../../models/Role' | ||
| import { RolePayload } from './RolePayload' | ||
|
|
||
| export const transformRolesUserSelectableResponseToRoles = (response: AxiosResponse): Role[] => { | ||
| const roleUserSelectablePayload = response.data.data as RolePayload[] | ||
|
|
||
| return roleUserSelectablePayload.map((role: RolePayload) => ({ | ||
| id: role.id, | ||
| name: role.name, | ||
| alias: role.alias, | ||
| description: role.description, | ||
| permissions: role.permissions | ||
| })) | ||
| } | ||
This file contains hidden or 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,16 @@ | ||
| import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
| import { Role } from '../models/Role' | ||
| import { IRolesRepository } from '../repositories/IRolesRepository' | ||
|
|
||
| export class GetUserSelectableRoles implements UseCase<Role[]> { | ||
| constructor(private readonly rolesRepository: IRolesRepository) {} | ||
|
|
||
| /** | ||
| * Returns the appropriate roles that the calling user can use as filters when searching within their data. | ||
| * | ||
| * @returns {Promise<Role[]>} - A promise that resolves to an array of Role instances. | ||
| */ | ||
| async execute(): Promise<Role[]> { | ||
| return (await this.rolesRepository.getUserSelectableRoles()) as Role[] | ||
| } | ||
| } |
This file contains hidden or 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,10 @@ | ||
| import { RolesRepository } from './infra/repositories/RolesRepository' | ||
| import { GetUserSelectableRoles } from './domain/useCases/GetUserSelectableRoles' | ||
|
|
||
| const rolesRepository = new RolesRepository() | ||
|
|
||
| const getUserSelectableRoles = new GetUserSelectableRoles(rolesRepository) | ||
|
|
||
| export { getUserSelectableRoles } | ||
|
|
||
| export { Role } from './domain/models/Role' |
This file contains hidden or 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,15 @@ | ||
| import { ApiRepository } from '../../../core/infra/repositories/ApiRepository' | ||
| import { IRolesRepository } from '../../domain/repositories/IRolesRepository' | ||
| import { Role } from '../../domain/models/Role' | ||
| import { transformRolesUserSelectableResponseToRoles } from '../../domain/repositories/transformers/roleTransformers' | ||
|
|
||
| export class RolesRepository extends ApiRepository implements IRolesRepository { | ||
| private readonly rolesResourceName: string = 'roles' | ||
| public async getUserSelectableRoles(): Promise<Role[]> { | ||
| return this.doGet(`/${this.rolesResourceName}/userSelectable`, true) | ||
| .then((response) => transformRolesUserSelectableResponseToRoles(response)) | ||
| .catch((error) => { | ||
| throw error | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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,23 @@ | ||
| import { | ||
| ApiConfig, | ||
| DataverseApiAuthMechanism | ||
| } from '../../../src/core/infra/repositories/ApiConfig' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
| import { RolesRepository } from '../../../src/roles/infra/repositories/RolesRepository' | ||
| import { createSuperAdminRoleArray } from '../../testHelpers/roles/roleHelper' | ||
|
|
||
| describe('RolesRepository', () => { | ||
| const sut: RolesRepository = new RolesRepository() | ||
|
|
||
| describe('getUserSelectableRoles', () => { | ||
| test('should return list of selectable roles for authenticated user', async () => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| const actual = await sut.getUserSelectableRoles() | ||
| expect(actual).toStrictEqual(createSuperAdminRoleArray()) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or 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,116 @@ | ||
| import { Role } from '../../../src/roles/domain/models/Role' | ||
|
|
||
| export const createRoleModel = (): Role => { | ||
| return { | ||
| id: 1, | ||
| name: 'admin', | ||
| alias: 'Admin', | ||
| description: | ||
| 'A person who has all permissions for dataverses, datasets, and files, including approving requests for restricted data.', | ||
| permissions: [ | ||
| 'AddDataverse', | ||
| 'AddDataset', | ||
| 'ViewUnpublishedDataverse', | ||
| 'ViewUnpublishedDataset' | ||
| ] | ||
| } | ||
| } | ||
| export const createRoleModelArray = (count: number): Role[] => { | ||
| return Array.from({ length: count }, (_, index) => ({ | ||
| id: index + 1, | ||
| name: `role${index + 1}`, | ||
| alias: `Role ${index + 1}`, | ||
| description: `Description for role ${index + 1}`, | ||
| permissions: [`Permission${index + 1}`] | ||
| })) | ||
| } | ||
|
|
||
| export const createSuperAdminRoleArray = (): Role[] => { | ||
| return [ | ||
| { | ||
| alias: 'admin', | ||
| name: 'Admin', | ||
| permissions: [ | ||
| 'AddDataverse', | ||
| 'AddDataset', | ||
| 'ViewUnpublishedDataverse', | ||
| 'ViewUnpublishedDataset', | ||
| 'DownloadFile', | ||
| 'EditDataverse', | ||
| 'EditDataset', | ||
| 'ManageDataversePermissions', | ||
| 'ManageDatasetPermissions', | ||
| 'ManageFilePermissions', | ||
| 'PublishDataverse', | ||
| 'PublishDataset', | ||
| 'DeleteDataverse', | ||
| 'DeleteDatasetDraft' | ||
| ], | ||
| description: | ||
| 'A person who has all permissions for dataverses, datasets, and files, including approving requests for restricted data.', | ||
| id: 1 | ||
| }, | ||
| { | ||
| alias: 'fileDownloader', | ||
| name: 'File Downloader', | ||
| permissions: ['DownloadFile'], | ||
| description: 'A person who can download a published file.', | ||
| id: 2 | ||
| }, | ||
| { | ||
| alias: 'fullContributor', | ||
| name: 'Dataverse + Dataset Creator', | ||
| permissions: ['AddDataverse', 'AddDataset'], | ||
| description: 'A person who can add subdataverses and datasets within a dataverse.', | ||
| id: 3 | ||
| }, | ||
| { | ||
| alias: 'dvContributor', | ||
| name: 'Dataverse Creator', | ||
| permissions: ['AddDataverse'], | ||
| description: 'A person who can add subdataverses within a dataverse.', | ||
| id: 4 | ||
| }, | ||
| { | ||
| alias: 'dsContributor', | ||
| name: 'Dataset Creator', | ||
| permissions: ['AddDataset'], | ||
| description: 'A person who can add datasets within a dataverse.', | ||
| id: 5 | ||
| }, | ||
| { | ||
| alias: 'contributor', | ||
| name: 'Contributor', | ||
| permissions: ['ViewUnpublishedDataset', 'DownloadFile', 'EditDataset', 'DeleteDatasetDraft'], | ||
| description: | ||
| 'For datasets, a person who can edit License + Terms, and then submit them for review.', | ||
| id: 6 | ||
| }, | ||
| { | ||
| alias: 'curator', | ||
| name: 'Curator', | ||
| permissions: [ | ||
| 'AddDataverse', | ||
| 'AddDataset', | ||
| 'ViewUnpublishedDataverse', | ||
| 'ViewUnpublishedDataset', | ||
| 'DownloadFile', | ||
| 'EditDataset', | ||
| 'ManageDatasetPermissions', | ||
| 'ManageFilePermissions', | ||
| 'PublishDataset', | ||
| 'DeleteDatasetDraft' | ||
| ], | ||
| description: | ||
| 'For datasets, a person who can edit License + Terms, edit Permissions, and publish datasets.', | ||
| id: 7 | ||
| }, | ||
| { | ||
| alias: 'member', | ||
| name: 'Member', | ||
| permissions: ['ViewUnpublishedDataverse', 'ViewUnpublishedDataset', 'DownloadFile'], | ||
| description: 'A person who can view both unpublished dataverses and datasets.', | ||
| id: 8 | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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,25 @@ | ||
| import { ReadError } from '../../../src' | ||
| import { IRolesRepository } from '../../../src/roles/domain/repositories/IRolesRepository' | ||
| import { createRoleModelArray } from '../../testHelpers/roles/roleHelper' | ||
| import { GetUserSelectableRoles } from '../../../src/roles/domain/useCases/GetUserSelectableRoles' | ||
|
|
||
| describe('execute', () => { | ||
| test('should return roles array on repository success', async () => { | ||
| const rolesRepositoryStub: IRolesRepository = {} as IRolesRepository | ||
| const testRoles = createRoleModelArray(5) | ||
| rolesRepositoryStub.getUserSelectableRoles = jest.fn().mockResolvedValue(testRoles) | ||
| const sut = new GetUserSelectableRoles(rolesRepositoryStub) | ||
|
|
||
| const actual = await sut.execute() | ||
|
|
||
| expect(actual).toEqual(testRoles) | ||
| }) | ||
|
|
||
| test('should return error result on repository error', async () => { | ||
| const rolesRepositoryStub: IRolesRepository = {} as IRolesRepository | ||
| rolesRepositoryStub.getUserSelectableRoles = jest.fn().mockRejectedValue(new ReadError()) | ||
| const sut = new GetUserSelectableRoles(rolesRepositoryStub) | ||
|
|
||
| await expect(sut.execute()).rejects.toThrow(ReadError) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.