2
2
SubscriptionProcessor ,
3
3
USER_CREDENTIALS ,
4
4
} from '../src/sync/processors/subscription' ;
5
- import { TransformerMutationType } from '../src/sync/utils' ;
6
5
import { SchemaModel } from '../src/types' ;
7
6
8
7
describe ( 'sync engine subscription module' , ( ) => {
@@ -22,7 +21,85 @@ describe('sync engine subscription module', () => {
22
21
ownerField : 'owner' ,
23
22
allow : 'owner' ,
24
23
identityClaim : 'cognito:username' ,
25
- operations : [ 'create' , 'update' , 'delete' ] ,
24
+ operations : [ 'create' , 'update' , 'delete' , 'read' ] ,
25
+ } ,
26
+ ] ,
27
+ } ,
28
+ } ,
29
+ ] ,
30
+ fields : {
31
+ id : {
32
+ name : 'id' ,
33
+ isArray : false ,
34
+ type : 'ID' ,
35
+ isRequired : true ,
36
+ attributes : [ ] ,
37
+ } ,
38
+ title : {
39
+ name : 'title' ,
40
+ isArray : false ,
41
+ type : 'String' ,
42
+ isRequired : true ,
43
+ attributes : [ ] ,
44
+ } ,
45
+ owner : {
46
+ name : 'owner' ,
47
+ isArray : false ,
48
+ type : 'String' ,
49
+ isRequired : false ,
50
+ attributes : [ ] ,
51
+ } ,
52
+ } ,
53
+ } ;
54
+ const tokenPayload = {
55
+ sub : 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' ,
56
+ 'cognito:groups' : [ 'mygroup' ] ,
57
+ email_verified : true ,
58
+ iss : 'https://cognito-idp.us-west-2.amazonaws.com/us-west-2_XXXXXXXX' ,
59
+ phone_number_verified : false ,
60
+ 'cognito:username' : 'user1' ,
61
+ aud : '6l99pm4b729dn8c7bj7d3t1lnc' ,
62
+ event_id : 'b4c25daa-0c03-4617-aab8-e5c74403536b' ,
63
+ token_use : 'id' ,
64
+ auth_time : 1578541322 ,
65
+ phone_number : '+12068220398' ,
66
+ exp : 1578544922 ,
67
+ iat : 1578541322 ,
68
+ email : 'user1@user.com' ,
69
+ } ;
70
+ const authInfo = {
71
+ authMode : 'AMAZON_COGNITO_USER_POOLS' ,
72
+ isOwner : true ,
73
+ ownerField : 'owner' ,
74
+ ownerValue : 'user1' ,
75
+ } ;
76
+
77
+ expect (
78
+ // @ts -ignore
79
+ SubscriptionProcessor . prototype . getAuthorizationInfo (
80
+ model ,
81
+ USER_CREDENTIALS . auth ,
82
+ tokenPayload
83
+ )
84
+ ) . toEqual ( authInfo ) ;
85
+ } ) ;
86
+ test ( 'owner authorization with only read operation' , ( ) => {
87
+ const model : SchemaModel = {
88
+ syncable : true ,
89
+ name : 'Post' ,
90
+ pluralName : 'Posts' ,
91
+ attributes : [
92
+ { type : 'model' , properties : { } } ,
93
+ {
94
+ type : 'auth' ,
95
+ properties : {
96
+ rules : [
97
+ {
98
+ provider : 'userPools' ,
99
+ ownerField : 'owner' ,
100
+ allow : 'owner' ,
101
+ identityClaim : 'cognito:username' ,
102
+ operations : [ 'read' ] ,
26
103
} ,
27
104
] ,
28
105
} ,
@@ -79,7 +156,84 @@ describe('sync engine subscription module', () => {
79
156
// @ts -ignore
80
157
SubscriptionProcessor . prototype . getAuthorizationInfo (
81
158
model ,
82
- TransformerMutationType . CREATE ,
159
+ USER_CREDENTIALS . auth ,
160
+ tokenPayload
161
+ )
162
+ ) . toEqual ( authInfo ) ;
163
+ } ) ;
164
+ test ( 'owner authorization without read operation' , ( ) => {
165
+ const model : SchemaModel = {
166
+ syncable : true ,
167
+ name : 'Post' ,
168
+ pluralName : 'Posts' ,
169
+ attributes : [
170
+ { type : 'model' , properties : { } } ,
171
+ {
172
+ type : 'auth' ,
173
+ properties : {
174
+ rules : [
175
+ {
176
+ provider : 'userPools' ,
177
+ ownerField : 'owner' ,
178
+ allow : 'owner' ,
179
+ identityClaim : 'cognito:username' ,
180
+ operations : [ 'create' , 'update' , 'delete' ] ,
181
+ } ,
182
+ ] ,
183
+ } ,
184
+ } ,
185
+ ] ,
186
+ fields : {
187
+ id : {
188
+ name : 'id' ,
189
+ isArray : false ,
190
+ type : 'ID' ,
191
+ isRequired : true ,
192
+ attributes : [ ] ,
193
+ } ,
194
+ title : {
195
+ name : 'title' ,
196
+ isArray : false ,
197
+ type : 'String' ,
198
+ isRequired : true ,
199
+ attributes : [ ] ,
200
+ } ,
201
+ owner : {
202
+ name : 'owner' ,
203
+ isArray : false ,
204
+ type : 'String' ,
205
+ isRequired : false ,
206
+ attributes : [ ] ,
207
+ } ,
208
+ } ,
209
+ } ;
210
+ const tokenPayload = {
211
+ sub : 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' ,
212
+ 'cognito:groups' : [ 'mygroup' ] ,
213
+ email_verified : true ,
214
+ iss : 'https://cognito-idp.us-west-2.amazonaws.com/us-west-2_XXXXXXXX' ,
215
+ phone_number_verified : false ,
216
+ 'cognito:username' : 'user1' ,
217
+ aud : '6l99pm4b729dn8c7bj7d3t1lnc' ,
218
+ event_id : 'b4c25daa-0c03-4617-aab8-e5c74403536b' ,
219
+ token_use : 'id' ,
220
+ auth_time : 1578541322 ,
221
+ phone_number : '+12068220398' ,
222
+ exp : 1578544922 ,
223
+ iat : 1578541322 ,
224
+ email : 'user1@user.com' ,
225
+ } ;
226
+ const authInfo = {
227
+ authMode : 'AMAZON_COGNITO_USER_POOLS' ,
228
+ isOwner : false ,
229
+ ownerField : 'owner' ,
230
+ ownerValue : 'user1' ,
231
+ } ;
232
+
233
+ expect (
234
+ // @ts -ignore
235
+ SubscriptionProcessor . prototype . getAuthorizationInfo (
236
+ model ,
83
237
USER_CREDENTIALS . auth ,
84
238
tokenPayload
85
239
)
@@ -108,7 +262,7 @@ describe('sync engine subscription module', () => {
108
262
ownerField : 'owner' ,
109
263
allow : 'owner' ,
110
264
identityClaim : 'cognito:username' ,
111
- operations : [ 'create' , 'update' , 'delete' ] ,
265
+ operations : [ 'create' , 'update' , 'delete' , 'read' ] ,
112
266
} ,
113
267
] ,
114
268
} ,
@@ -165,7 +319,6 @@ describe('sync engine subscription module', () => {
165
319
// @ts -ignore
166
320
SubscriptionProcessor . prototype . getAuthorizationInfo (
167
321
model ,
168
- TransformerMutationType . CREATE ,
169
322
USER_CREDENTIALS . auth ,
170
323
tokenPayload
171
324
)
@@ -188,7 +341,7 @@ describe('sync engine subscription module', () => {
188
341
allow : 'groups' ,
189
342
groups : [ 'mygroup' ] ,
190
343
identityClaim : 'cognito:username' ,
191
- operations : [ 'create' , 'update' , 'delete' ] ,
344
+ operations : [ 'create' , 'update' , 'delete' , 'read' ] ,
192
345
} ,
193
346
] ,
194
347
} ,
@@ -243,7 +396,6 @@ describe('sync engine subscription module', () => {
243
396
// @ts -ignore
244
397
SubscriptionProcessor . prototype . getAuthorizationInfo (
245
398
model ,
246
- TransformerMutationType . CREATE ,
247
399
USER_CREDENTIALS . auth ,
248
400
tokenPayload
249
401
)
@@ -263,7 +415,7 @@ describe('sync engine subscription module', () => {
263
415
{
264
416
provider : 'iam' ,
265
417
allow : 'public' ,
266
- operations : [ 'create' , 'update' , 'delete' ] ,
418
+ operations : [ 'create' , 'update' , 'delete' , 'read' ] ,
267
419
} ,
268
420
] ,
269
421
} ,
@@ -302,7 +454,6 @@ describe('sync engine subscription module', () => {
302
454
// @ts -ignore
303
455
SubscriptionProcessor . prototype . getAuthorizationInfo (
304
456
model ,
305
- TransformerMutationType . CREATE ,
306
457
USER_CREDENTIALS . unauth
307
458
)
308
459
) . toEqual ( authInfo ) ;
@@ -321,7 +472,7 @@ describe('sync engine subscription module', () => {
321
472
{
322
473
provider : 'iam' ,
323
474
allow : 'private' ,
324
- operations : [ 'create' , 'update' , 'delete' ] ,
475
+ operations : [ 'create' , 'update' , 'delete' , 'read' ] ,
325
476
} ,
326
477
] ,
327
478
} ,
@@ -360,7 +511,6 @@ describe('sync engine subscription module', () => {
360
511
// @ts -ignore
361
512
SubscriptionProcessor . prototype . getAuthorizationInfo (
362
513
model ,
363
- TransformerMutationType . CREATE ,
364
514
USER_CREDENTIALS . unauth
365
515
)
366
516
) . toEqual ( null ) ;
@@ -379,7 +529,7 @@ describe('sync engine subscription module', () => {
379
529
{
380
530
provider : 'iam' ,
381
531
allow : 'private' ,
382
- operations : [ 'create' , 'update' , 'delete' ] ,
532
+ operations : [ 'create' , 'update' , 'delete' , 'read' ] ,
383
533
} ,
384
534
] ,
385
535
} ,
@@ -418,7 +568,6 @@ describe('sync engine subscription module', () => {
418
568
// @ts -ignore
419
569
SubscriptionProcessor . prototype . getAuthorizationInfo (
420
570
model ,
421
- TransformerMutationType . CREATE ,
422
571
USER_CREDENTIALS . auth
423
572
)
424
573
) . toEqual ( authInfo ) ;
@@ -437,7 +586,7 @@ describe('sync engine subscription module', () => {
437
586
{
438
587
provider : 'apiKey' ,
439
588
allow : 'public' ,
440
- operations : [ 'create' , 'update' , 'delete' ] ,
589
+ operations : [ 'create' , 'update' , 'delete' , 'read' ] ,
441
590
} ,
442
591
] ,
443
592
} ,
@@ -476,7 +625,6 @@ describe('sync engine subscription module', () => {
476
625
// @ts -ignore
477
626
SubscriptionProcessor . prototype . getAuthorizationInfo (
478
627
model ,
479
- TransformerMutationType . CREATE ,
480
628
USER_CREDENTIALS . none
481
629
)
482
630
) . toEqual ( authInfo ) ;
0 commit comments