1
1
import { MethodModesExtractor } from '../../../../src/authorization/permissions/MethodModesExtractor' ;
2
+ import type { AccessMap } from '../../../../src/authorization/permissions/Permissions' ;
2
3
import { AccessMode } from '../../../../src/authorization/permissions/Permissions' ;
3
4
import type { Operation } from '../../../../src/http/Operation' ;
5
+ import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation' ;
6
+ import type { ResourceIdentifier } from '../../../../src/http/representation/ResourceIdentifier' ;
4
7
import type { ResourceSet } from '../../../../src/storage/ResourceSet' ;
5
8
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError' ;
9
+ import { IdentifierSetMultiMap } from '../../../../src/util/map/IdentifierMap' ;
10
+ import { compareMaps } from '../../../util/Util' ;
6
11
7
12
describe ( 'A MethodModesExtractor' , ( ) : void => {
13
+ const target : ResourceIdentifier = { path : 'http://example.com/foo' } ;
14
+ const operation : Operation = {
15
+ method : 'GET' ,
16
+ target,
17
+ preferences : { } ,
18
+ body : new BasicRepresentation ( ) ,
19
+ } ;
8
20
let resourceSet : jest . Mocked < ResourceSet > ;
9
21
let extractor : MethodModesExtractor ;
10
22
23
+ function getMap ( modes : AccessMode [ ] , identifier ?: ResourceIdentifier ) : AccessMap {
24
+ return new IdentifierSetMultiMap (
25
+ modes . map ( ( mode ) : [ ResourceIdentifier , AccessMode ] => [ identifier ?? target , mode ] ) ,
26
+ ) ;
27
+ }
28
+
11
29
beforeEach ( async ( ) : Promise < void > => {
12
30
resourceSet = {
13
31
hasResource : jest . fn ( ) . mockResolvedValue ( true ) ,
@@ -16,44 +34,43 @@ describe('A MethodModesExtractor', (): void => {
16
34
} ) ;
17
35
18
36
it ( 'can handle HEAD/GET/POST/PUT/DELETE.' , async ( ) : Promise < void > => {
19
- await expect ( extractor . canHandle ( { method : 'HEAD' } as Operation ) ) . resolves . toBeUndefined ( ) ;
20
- await expect ( extractor . canHandle ( { method : 'GET' } as Operation ) ) . resolves . toBeUndefined ( ) ;
21
- await expect ( extractor . canHandle ( { method : 'POST' } as Operation ) ) . resolves . toBeUndefined ( ) ;
22
- await expect ( extractor . canHandle ( { method : 'PUT' } as Operation ) ) . resolves . toBeUndefined ( ) ;
23
- await expect ( extractor . canHandle ( { method : 'DELETE' } as Operation ) ) . resolves . toBeUndefined ( ) ;
24
- await expect ( extractor . canHandle ( { method : 'PATCH' } as Operation ) ) . rejects . toThrow ( NotImplementedHttpError ) ;
37
+ await expect ( extractor . canHandle ( { ... operation , method : 'HEAD' } ) ) . resolves . toBeUndefined ( ) ;
38
+ await expect ( extractor . canHandle ( { ... operation , method : 'GET' } ) ) . resolves . toBeUndefined ( ) ;
39
+ await expect ( extractor . canHandle ( { ... operation , method : 'POST' } ) ) . resolves . toBeUndefined ( ) ;
40
+ await expect ( extractor . canHandle ( { ... operation , method : 'PUT' } ) ) . resolves . toBeUndefined ( ) ;
41
+ await expect ( extractor . canHandle ( { ... operation , method : 'DELETE' } ) ) . resolves . toBeUndefined ( ) ;
42
+ await expect ( extractor . canHandle ( { ... operation , method : 'PATCH' } ) ) . rejects . toThrow ( NotImplementedHttpError ) ;
25
43
} ) ;
26
44
27
45
it ( 'requires read for HEAD operations.' , async ( ) : Promise < void > => {
28
- await expect ( extractor . handle ( { method : 'HEAD' } as Operation ) ) . resolves . toEqual ( new Set ( [ AccessMode . read ] ) ) ;
46
+ compareMaps ( await extractor . handle ( { ... operation , method : 'HEAD' } ) , getMap ( [ AccessMode . read ] ) ) ;
29
47
} ) ;
30
48
31
49
it ( 'requires read for GET operations.' , async ( ) : Promise < void > => {
32
- await expect ( extractor . handle ( { method : 'GET' } as Operation ) ) . resolves . toEqual ( new Set ( [ AccessMode . read ] ) ) ;
50
+ compareMaps ( await extractor . handle ( { ... operation , method : 'GET' } ) , getMap ( [ AccessMode . read ] ) ) ;
33
51
} ) ;
34
52
35
53
it ( 'requires append for POST operations.' , async ( ) : Promise < void > => {
36
- await expect ( extractor . handle ( { method : 'POST' } as Operation ) ) . resolves . toEqual ( new Set ( [ AccessMode . append ] ) ) ;
54
+ compareMaps ( await extractor . handle ( { ... operation , method : 'POST' } ) , getMap ( [ AccessMode . append ] ) ) ;
37
55
} ) ;
38
56
39
57
it ( 'requires write for PUT operations.' , async ( ) : Promise < void > => {
40
- await expect ( extractor . handle ( { method : 'PUT' } as Operation ) )
41
- . resolves . toEqual ( new Set ( [ AccessMode . write ] ) ) ;
58
+ compareMaps ( await extractor . handle ( { ...operation , method : 'PUT' } ) , getMap ( [ AccessMode . write ] ) ) ;
42
59
} ) ;
43
60
44
61
it ( 'requires create for PUT operations if the target does not exist.' , async ( ) : Promise < void > => {
45
62
resourceSet . hasResource . mockResolvedValueOnce ( false ) ;
46
- await expect ( extractor . handle ( { method : 'PUT' } as Operation ) )
47
- . resolves . toEqual ( new Set ( [ AccessMode . write , AccessMode . create ] ) ) ;
63
+ compareMaps ( await extractor . handle ( { ... operation , method : 'PUT' } ) ,
64
+ getMap ( [ AccessMode . write , AccessMode . create ] ) ) ;
48
65
} ) ;
49
66
50
67
it ( 'requires delete for DELETE operations.' , async ( ) : Promise < void > => {
51
- await expect ( extractor . handle ( { method : 'DELETE' , target : { path : 'http://example.com/foo' } } as Operation ) )
52
- . resolves . toEqual ( new Set ( [ AccessMode . delete ] ) ) ;
68
+ compareMaps ( await extractor . handle ( { ...operation , method : 'DELETE' } ) , getMap ( [ AccessMode . delete ] ) ) ;
53
69
} ) ;
54
70
55
71
it ( 'also requires read for DELETE operations on containers.' , async ( ) : Promise < void > => {
56
- await expect ( extractor . handle ( { method : 'DELETE' , target : { path : 'http://example.com/foo/' } } as Operation ) )
57
- . resolves . toEqual ( new Set ( [ AccessMode . delete , AccessMode . read ] ) ) ;
72
+ const identifier = { path : 'http://example.com/foo/' } ;
73
+ compareMaps ( await extractor . handle ( { ...operation , method : 'DELETE' , target : identifier } ) ,
74
+ getMap ( [ AccessMode . delete , AccessMode . read ] , identifier ) ) ;
58
75
} ) ;
59
76
} ) ;
0 commit comments