Skip to content

Commit 2f74c6b

Browse files
authored
fix(@aws-amplify/datastore): fix DS subscriptions involving read operation (#6954)
* fix DS subscriptions involving read operation
1 parent 7a3bc80 commit 2f74c6b

File tree

3 files changed

+204
-54
lines changed

3 files changed

+204
-54
lines changed

packages/datastore/__tests__/subscription.test.ts

+163-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
SubscriptionProcessor,
33
USER_CREDENTIALS,
44
} from '../src/sync/processors/subscription';
5-
import { TransformerMutationType } from '../src/sync/utils';
65
import { SchemaModel } from '../src/types';
76

87
describe('sync engine subscription module', () => {
@@ -22,7 +21,85 @@ describe('sync engine subscription module', () => {
2221
ownerField: 'owner',
2322
allow: 'owner',
2423
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'],
26103
},
27104
],
28105
},
@@ -79,7 +156,84 @@ describe('sync engine subscription module', () => {
79156
// @ts-ignore
80157
SubscriptionProcessor.prototype.getAuthorizationInfo(
81158
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,
83237
USER_CREDENTIALS.auth,
84238
tokenPayload
85239
)
@@ -108,7 +262,7 @@ describe('sync engine subscription module', () => {
108262
ownerField: 'owner',
109263
allow: 'owner',
110264
identityClaim: 'cognito:username',
111-
operations: ['create', 'update', 'delete'],
265+
operations: ['create', 'update', 'delete', 'read'],
112266
},
113267
],
114268
},
@@ -165,7 +319,6 @@ describe('sync engine subscription module', () => {
165319
// @ts-ignore
166320
SubscriptionProcessor.prototype.getAuthorizationInfo(
167321
model,
168-
TransformerMutationType.CREATE,
169322
USER_CREDENTIALS.auth,
170323
tokenPayload
171324
)
@@ -188,7 +341,7 @@ describe('sync engine subscription module', () => {
188341
allow: 'groups',
189342
groups: ['mygroup'],
190343
identityClaim: 'cognito:username',
191-
operations: ['create', 'update', 'delete'],
344+
operations: ['create', 'update', 'delete', 'read'],
192345
},
193346
],
194347
},
@@ -243,7 +396,6 @@ describe('sync engine subscription module', () => {
243396
// @ts-ignore
244397
SubscriptionProcessor.prototype.getAuthorizationInfo(
245398
model,
246-
TransformerMutationType.CREATE,
247399
USER_CREDENTIALS.auth,
248400
tokenPayload
249401
)
@@ -263,7 +415,7 @@ describe('sync engine subscription module', () => {
263415
{
264416
provider: 'iam',
265417
allow: 'public',
266-
operations: ['create', 'update', 'delete'],
418+
operations: ['create', 'update', 'delete', 'read'],
267419
},
268420
],
269421
},
@@ -302,7 +454,6 @@ describe('sync engine subscription module', () => {
302454
// @ts-ignore
303455
SubscriptionProcessor.prototype.getAuthorizationInfo(
304456
model,
305-
TransformerMutationType.CREATE,
306457
USER_CREDENTIALS.unauth
307458
)
308459
).toEqual(authInfo);
@@ -321,7 +472,7 @@ describe('sync engine subscription module', () => {
321472
{
322473
provider: 'iam',
323474
allow: 'private',
324-
operations: ['create', 'update', 'delete'],
475+
operations: ['create', 'update', 'delete', 'read'],
325476
},
326477
],
327478
},
@@ -360,7 +511,6 @@ describe('sync engine subscription module', () => {
360511
// @ts-ignore
361512
SubscriptionProcessor.prototype.getAuthorizationInfo(
362513
model,
363-
TransformerMutationType.CREATE,
364514
USER_CREDENTIALS.unauth
365515
)
366516
).toEqual(null);
@@ -379,7 +529,7 @@ describe('sync engine subscription module', () => {
379529
{
380530
provider: 'iam',
381531
allow: 'private',
382-
operations: ['create', 'update', 'delete'],
532+
operations: ['create', 'update', 'delete', 'read'],
383533
},
384534
],
385535
},
@@ -418,7 +568,6 @@ describe('sync engine subscription module', () => {
418568
// @ts-ignore
419569
SubscriptionProcessor.prototype.getAuthorizationInfo(
420570
model,
421-
TransformerMutationType.CREATE,
422571
USER_CREDENTIALS.auth
423572
)
424573
).toEqual(authInfo);
@@ -437,7 +586,7 @@ describe('sync engine subscription module', () => {
437586
{
438587
provider: 'apiKey',
439588
allow: 'public',
440-
operations: ['create', 'update', 'delete'],
589+
operations: ['create', 'update', 'delete', 'read'],
441590
},
442591
],
443592
},
@@ -476,7 +625,6 @@ describe('sync engine subscription module', () => {
476625
// @ts-ignore
477626
SubscriptionProcessor.prototype.getAuthorizationInfo(
478627
model,
479-
TransformerMutationType.CREATE,
480628
USER_CREDENTIALS.none
481629
)
482630
).toEqual(authInfo);

packages/datastore/src/sync/processors/subscription.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class SubscriptionProcessor {
6161
const { authMode, isOwner, ownerField, ownerValue } =
6262
this.getAuthorizationInfo(
6363
model,
64-
transformerMutationType,
6564
userCredentials,
6665
cognitoTokenPayload,
6766
oidcTokenPayload
@@ -79,7 +78,6 @@ class SubscriptionProcessor {
7978

8079
private getAuthorizationInfo(
8180
model: SchemaModel,
82-
transformerMutationType: TransformerMutationType,
8381
userCredentials: USER_CREDENTIALS,
8482
cognitoTokenPayload: { [field: string]: any } = {},
8583
oidcTokenPayload: { [field: string]: any } = {}
@@ -90,7 +88,7 @@ class SubscriptionProcessor {
9088
ownerValue?: string;
9189
} {
9290
let result;
93-
const rules = getAuthorizationRules(model, transformerMutationType);
91+
const rules = getAuthorizationRules(model);
9492

9593
// check if has apiKey and public authorization
9694
const apiKeyAuth = rules.find(

0 commit comments

Comments
 (0)