@@ -293,6 +293,66 @@ public void ownerArgumentNotAddedIfOwnerIsInCustomGroup() throws AmplifyExceptio
293293 }
294294 }
295295
296+ /**
297+ * Verify owner argument is NOT added if model contains both public key and owner-based authorization and the
298+ * requested auth type is API_KEY.
299+ * @throws AmplifyException if a ModelSchema can't be derived from the Model class.
300+ */
301+ @ Test
302+ public void doesNotAddOwnerWhenMultiAuthWithPublicKey () throws AmplifyException {
303+ final AuthorizationType mode = AuthorizationType .API_KEY ;
304+
305+ // PublicAndOwner combines public and owner-based auth
306+ for (SubscriptionType subscriptionType : SubscriptionType .values ()) {
307+ GraphQLRequest <PublicAndOwner > originalRequest = createRequest (PublicAndOwner .class , subscriptionType );
308+ GraphQLRequest <PublicAndOwner > modifiedRequest = decorator .decorate (originalRequest , mode );
309+ assertNull (getOwnerField (modifiedRequest ));
310+ }
311+
312+ // PublicAndOwnerOidc combines public and owner-based auth with an OIDC claim
313+ for (SubscriptionType subscriptionType : SubscriptionType .values ()) {
314+ GraphQLRequest <PublicAndOwnerOidc > originalRequest =
315+ createRequest (PublicAndOwnerOidc .class , subscriptionType );
316+ GraphQLRequest <PublicAndOwnerOidc > modifiedRequest = decorator .decorate (originalRequest , mode );
317+ assertNull (getOwnerField (modifiedRequest ));
318+ }
319+ }
320+
321+ /**
322+ * Verify owner argument is added if model contains both owner-based and public-key
323+ * authorization and the auth mode is cognito.
324+ * @throws AmplifyException if a ModelSchema can't be derived from the Model class.
325+ */
326+ @ Test
327+ public void addsOwnerWhenMultiAuthWithCognito () throws AmplifyException {
328+ final AuthorizationType mode = AuthorizationType .AMAZON_COGNITO_USER_POOLS ;
329+ final String expectedOwner = FakeCognitoAuthProvider .USERNAME ;
330+
331+ for (SubscriptionType subscriptionType : SubscriptionType .values ()) {
332+ GraphQLRequest <PublicAndOwner > originalRequest = createRequest (PublicAndOwner .class , subscriptionType );
333+ GraphQLRequest <PublicAndOwner > modifiedRequest = decorator .decorate (originalRequest , mode );
334+ assertEquals (expectedOwner , getOwnerField (modifiedRequest ));
335+ }
336+ }
337+
338+ /**
339+ * Verify owner argument is added if model contains both owner-based and public-key
340+ * authorization and the auth mode is oidc.
341+ * @throws AmplifyException if a ModelSchema can't be derived from the Model class.
342+ */
343+ @ Test
344+ public void addsOwnerWhenMultiAuthWithOidc () throws AmplifyException {
345+ final AuthorizationType mode = AuthorizationType .OPENID_CONNECT ;
346+ final String expectedOwner = FakeOidcAuthProvider .SUB ;
347+
348+ for (SubscriptionType subscriptionType : SubscriptionType .values ()) {
349+ GraphQLRequest <PublicAndOwnerOidc > originalRequest =
350+ createRequest (PublicAndOwnerOidc .class , subscriptionType );
351+ GraphQLRequest <PublicAndOwnerOidc > modifiedRequest = decorator .decorate (originalRequest , mode );
352+ assertEquals (expectedOwner , getOwnerField (modifiedRequest ));
353+ }
354+ }
355+
296356 private <M extends Model > String getOwnerField (GraphQLRequest <M > request ) {
297357 if (request .getVariables ().containsKey ("owner" )) {
298358 return (String ) request .getVariables ().get ("owner" );
@@ -412,4 +472,16 @@ private abstract static class OwnerInCustomGroup implements Model {}
412472 )
413473 })
414474 private abstract static class OwnerNotInCustomGroup implements Model {}
475+
476+ @ ModelConfig (authRules = {
477+ @ AuthRule (allow = AuthStrategy .PUBLIC , operations = ModelOperation .READ ),
478+ @ AuthRule (allow = AuthStrategy .OWNER )
479+ })
480+ private abstract static class PublicAndOwner implements Model {}
481+
482+ @ ModelConfig (authRules = {
483+ @ AuthRule (allow = AuthStrategy .PUBLIC , operations = ModelOperation .READ ),
484+ @ AuthRule (allow = AuthStrategy .OWNER , identityClaim = "sub" )
485+ })
486+ private abstract static class PublicAndOwnerOidc implements Model {}
415487}
0 commit comments