@@ -72,29 +72,58 @@ const matrix3: DecisionMatrix = [
72
72
[ { defaultDecision : Allow } , [ Abstain , Abstain , Abstain ] , Allow ] ,
73
73
] ;
74
74
75
+ const matrix4 : DecisionMatrix = [
76
+ [ { defaultMetadata : { } } , [ Abstain , Abstain , Abstain ] , Deny ] ,
77
+ [
78
+ { defaultMetadata : { } , defaultDecision : Deny } ,
79
+ [ Abstain , Abstain , Abstain ] ,
80
+ Deny ,
81
+ ] ,
82
+ [
83
+ { defaultMetadata : { } , defaultDecision : Allow } ,
84
+ [ Abstain , Abstain , Abstain ] ,
85
+ Allow ,
86
+ ] ,
87
+ ] ;
88
+
89
+ // Decisions controlled by options.defaultDecision
90
+ const matrix5 : DecisionMatrix = [
91
+ [ { } , [ Abstain , Abstain , Abstain ] , Allow ] ,
92
+ [ { defaultDecision : Deny } , [ Abstain , Abstain , Abstain ] , Allow ] ,
93
+ [ { defaultDecision : Allow } , [ Abstain , Abstain , Abstain ] , Allow ] ,
94
+ ] ;
95
+
75
96
describe ( 'Authorization' , ( ) => {
76
97
let app : Application ;
77
98
let controller : OrderController ;
78
99
let reqCtx : Context ;
79
100
80
101
it ( 'always use explicit decisions' , async ( ) => {
81
- await runTest ( matrix1 ) ;
102
+ await testCancelOrder ( matrix1 ) ;
82
103
} ) ;
83
104
84
105
it ( 'honors decisions and options.precedence' , async ( ) => {
85
- await runTest ( matrix2 ) ;
106
+ await testCancelOrder ( matrix2 ) ;
86
107
} ) ;
87
108
88
109
it ( 'honors decisions and options.defaultDecision' , async ( ) => {
89
- await runTest ( matrix3 ) ;
110
+ await testCancelOrder ( matrix3 ) ;
111
+ } ) ;
112
+
113
+ it ( 'honors decisions with options.defaultMetadata' , async ( ) => {
114
+ await testPlaceOrder ( matrix4 ) ;
90
115
} ) ;
91
116
92
- async function runTest ( matrix : DecisionMatrix ) {
117
+ it ( 'honors decisions without options.defaultMetadata' , async ( ) => {
118
+ await testPlaceOrder ( matrix5 ) ;
119
+ } ) ;
120
+
121
+ async function testCancelOrder ( matrix : DecisionMatrix ) {
93
122
let index = 0 ;
94
123
for ( const row of matrix ) {
95
124
givenRequestContext ( ) ;
96
125
setupAuthorization ( row [ 0 ] , ...row [ 1 ] ) ;
97
- const finalDecision = await run ( ) ;
126
+ const finalDecision = await cancelOrder ( ) ;
98
127
const expectedDecision = row [ 2 ] ;
99
128
expect ( `${ index } :${ finalDecision } ` ) . to . equal (
100
129
`${ index } :${ expectedDecision } ` ,
@@ -103,10 +132,35 @@ describe('Authorization', () => {
103
132
}
104
133
}
105
134
106
- async function run ( ) {
135
+ async function testPlaceOrder ( matrix : DecisionMatrix ) {
136
+ let index = 0 ;
137
+ for ( const row of matrix ) {
138
+ givenRequestContext ( ) ;
139
+ setupAuthorization ( row [ 0 ] , ...row [ 1 ] ) ;
140
+ const finalDecision = await placeOrder ( ) ;
141
+ const expectedDecision = row [ 2 ] ;
142
+ expect ( `${ index } :${ finalDecision } ` ) . to . equal (
143
+ `${ index } :${ expectedDecision } ` ,
144
+ ) ;
145
+ index ++ ;
146
+ }
147
+ }
148
+
149
+ async function cancelOrder ( ) {
150
+ let finalDecision = Deny ;
151
+ try {
152
+ await invokeMethod ( controller , 'cancelOrder' , reqCtx , [ 'order-01' ] ) ;
153
+ finalDecision = Allow ;
154
+ } catch ( err ) {
155
+ finalDecision = Deny ;
156
+ }
157
+ return finalDecision ;
158
+ }
159
+
160
+ async function placeOrder ( ) {
107
161
let finalDecision = Deny ;
108
162
try {
109
- await invokeMethod ( controller , 'handleOrder ' , reqCtx , [ 'order -01' ] ) ;
163
+ await invokeMethod ( controller , 'placeOrder ' , reqCtx , [ 'prod -01' , 10 ] ) ;
110
164
finalDecision = Allow ;
111
165
} catch ( err ) {
112
166
finalDecision = Deny ;
@@ -116,9 +170,15 @@ describe('Authorization', () => {
116
170
117
171
class OrderController {
118
172
@authorize ( { } )
119
- async handleOrder ( orderId : string ) {
173
+ async cancelOrder ( orderId : string ) {
120
174
return orderId ;
121
175
}
176
+
177
+ // This method is not decorated with `@authorize`. The decision depends on
178
+ // authorizationOptions.defaultMetadata
179
+ async placeOrder ( productId : string , quantity : number ) {
180
+ return '001' ;
181
+ }
122
182
}
123
183
124
184
function setupAuthorization (
0 commit comments