-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from IQSS/165-get-collection-user-permissions
Adds GetCollectionUserPermissions use case
- Loading branch information
Showing
13 changed files
with
303 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface CollectionUserPermissions { | ||
canAddCollection: boolean | ||
canAddDataset: boolean | ||
canViewUnpublishedCollection: boolean | ||
canEditCollection: boolean | ||
canManageCollectionPermissions: boolean | ||
canPublishCollection: boolean | ||
canDeleteCollection: boolean | ||
} |
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
22 changes: 22 additions & 0 deletions
22
src/collections/domain/useCases/GetCollectionUserPermissions.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,22 @@ | ||
import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
import { CollectionUserPermissions } from '../models/CollectionUserPermissions' | ||
import { ICollectionsRepository } from '../repositories/ICollectionsRepository' | ||
|
||
export class GetCollectionUserPermissions implements UseCase<CollectionUserPermissions> { | ||
private collectionsRepository: ICollectionsRepository | ||
|
||
constructor(collectionsRepository: ICollectionsRepository) { | ||
this.collectionsRepository = collectionsRepository | ||
} | ||
|
||
/** | ||
* Returns an instance of CollectionUserPermissions that includes the permissions that the calling user has on a particular Collection. | ||
* | ||
* @param {number | string} [collectionIdOrAlias = 'root'] - A generic collection identifier, which can be either a string (for queries by CollectionAlias), or a number (for queries by CollectionId) | ||
* If this parameter is not set, the default value is: 'root' | ||
* @returns {Promise<CollectionUserPermissions>} | ||
*/ | ||
async execute(collectionIdOrAlias: number | string): Promise<CollectionUserPermissions> { | ||
return await this.collectionsRepository.getCollectionUserPermissions(collectionIdOrAlias) | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/collections/infra/repositories/transformers/CollectionUserPermissionsPayload.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,9 @@ | ||
export interface CollectionUserPermissionsPayload { | ||
canAddDataverse: boolean | ||
canAddDataset: boolean | ||
canViewUnpublishedDataverse: boolean | ||
canEditDataverse: boolean | ||
canManageDataversePermissions: boolean | ||
canPublishDataverse: boolean | ||
canDeleteDataverse: boolean | ||
} |
17 changes: 17 additions & 0 deletions
17
src/collections/infra/repositories/transformers/collectionUserPermissionsTransformers.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,17 @@ | ||
import { AxiosResponse } from 'axios' | ||
import { CollectionUserPermissions } from '../../../domain/models/CollectionUserPermissions' | ||
|
||
export const transformCollectionUserPermissionsResponseToCollectionUserPermissions = ( | ||
response: AxiosResponse | ||
): CollectionUserPermissions => { | ||
const collectionUserPermissionsPayload = response.data.data | ||
return { | ||
canAddCollection: collectionUserPermissionsPayload.canAddDataverse, | ||
canAddDataset: collectionUserPermissionsPayload.canAddDataset, | ||
canViewUnpublishedCollection: collectionUserPermissionsPayload.canViewUnpublishedDataverse, | ||
canEditCollection: collectionUserPermissionsPayload.canEditDataverse, | ||
canManageCollectionPermissions: collectionUserPermissionsPayload.canManageDataversePermissions, | ||
canPublishCollection: collectionUserPermissionsPayload.canPublishDataverse, | ||
canDeleteCollection: collectionUserPermissionsPayload.canDeleteDataverse | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
test/functional/collections/GetCollectionUserPermissions.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,52 @@ | ||
import { | ||
ApiConfig, | ||
CollectionUserPermissions, | ||
ReadError, | ||
getCollectionUserPermissions | ||
} from '../../../src' | ||
import { TestConstants } from '../../testHelpers/TestConstants' | ||
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
import { ROOT_COLLECTION_ALIAS } from '../../../src/collections/domain/models/Collection' | ||
|
||
describe('execute', () => { | ||
beforeEach(async () => { | ||
ApiConfig.init( | ||
TestConstants.TEST_API_URL, | ||
DataverseApiAuthMechanism.API_KEY, | ||
process.env.TEST_API_KEY | ||
) | ||
}) | ||
|
||
test('should return user permissions when a valid collection alias is provided', async () => { | ||
let actual: CollectionUserPermissions | ||
try { | ||
actual = await getCollectionUserPermissions.execute(ROOT_COLLECTION_ALIAS) | ||
} catch (error) { | ||
throw new Error('Permissions should be retrieved') | ||
} finally { | ||
expect(actual.canAddDataset).toBe(true) | ||
expect(actual.canAddCollection).toBe(true) | ||
expect(actual.canDeleteCollection).toBe(true) | ||
expect(actual.canEditCollection).toBe(true) | ||
expect(actual.canManageCollectionPermissions).toBe(true) | ||
expect(actual.canPublishCollection).toBe(true) | ||
expect(actual.canViewUnpublishedCollection).toBe(true) | ||
} | ||
}) | ||
|
||
test('should throw an error when collection does not exist', async () => { | ||
expect.assertions(2) | ||
let readError: ReadError | ||
try { | ||
await getCollectionUserPermissions.execute(TestConstants.TEST_DUMMY_COLLECTION_ID) | ||
throw new Error('Use case should throw an error') | ||
} catch (error) { | ||
readError = error | ||
} finally { | ||
expect(readError).toBeInstanceOf(ReadError) | ||
expect(readError.message).toEqual( | ||
`There was an error when reading the resource. Reason was: [404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_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
26 changes: 26 additions & 0 deletions
26
test/testHelpers/collections/collectionUserPermissionsHelper.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,26 @@ | ||
import { CollectionUserPermissions } from '../../../src/collections/domain/models/CollectionUserPermissions' | ||
import { CollectionUserPermissionsPayload } from '../../../src/collections/infra/repositories/transformers/CollectionUserPermissionsPayload' | ||
|
||
export const createCollectionUserPermissionsModel = (): CollectionUserPermissions => { | ||
return { | ||
canAddCollection: true, | ||
canAddDataset: true, | ||
canViewUnpublishedCollection: true, | ||
canEditCollection: true, | ||
canManageCollectionPermissions: true, | ||
canPublishCollection: true, | ||
canDeleteCollection: true | ||
} | ||
} | ||
|
||
export const createCollectionUserPermissionsPayload = (): CollectionUserPermissionsPayload => { | ||
return { | ||
canAddDataverse: true, | ||
canAddDataset: true, | ||
canViewUnpublishedDataverse: true, | ||
canEditDataverse: true, | ||
canManageDataversePermissions: true, | ||
canPublishDataverse: true, | ||
canDeleteDataverse: true | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
test/unit/collections/GetCollectionUserPermissions.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,34 @@ | ||
import { GetCollectionUserPermissions } from '../../../src/collections/domain/useCases/GetCollectionUserPermissions' | ||
import { ReadError } from '../../../src/core/domain/repositories/ReadError' | ||
import { ICollectionsRepository } from '../../../src/collections/domain/repositories/ICollectionsRepository' | ||
import { createCollectionUserPermissionsModel } from '../../testHelpers/collections/collectionUserPermissionsHelper' | ||
|
||
describe('execute', () => { | ||
const testCollectionAlias = 'test' | ||
|
||
test('should return collection user permissions on repository success', async () => { | ||
const testCollectionUserPermissions = createCollectionUserPermissionsModel() | ||
const collectionsRepositoryStub: ICollectionsRepository = {} as ICollectionsRepository | ||
collectionsRepositoryStub.getCollectionUserPermissions = jest | ||
.fn() | ||
.mockResolvedValue(testCollectionUserPermissions) | ||
const sut = new GetCollectionUserPermissions(collectionsRepositoryStub) | ||
|
||
const actual = await sut.execute(testCollectionAlias) | ||
|
||
expect(actual).toEqual(testCollectionUserPermissions) | ||
expect(collectionsRepositoryStub.getCollectionUserPermissions).toHaveBeenCalledWith( | ||
testCollectionAlias | ||
) | ||
}) | ||
|
||
test('should return error result on repository error', async () => { | ||
const collectionsRepositoryStub: ICollectionsRepository = {} as ICollectionsRepository | ||
collectionsRepositoryStub.getCollectionUserPermissions = jest | ||
.fn() | ||
.mockRejectedValue(new ReadError()) | ||
const sut = new GetCollectionUserPermissions(collectionsRepositoryStub) | ||
|
||
await expect(sut.execute(testCollectionAlias)).rejects.toThrow(ReadError) | ||
}) | ||
}) |