From a60be2a70914b2fc00581d0802773fa4acc0dc60 Mon Sep 17 00:00:00 2001 From: Abhash Kumar Singh Date: Wed, 1 Nov 2023 13:26:19 -0700 Subject: [PATCH] Address review comments --- .../Auth/AWSAuthModeStrategy.swift | 17 ++++++------ .../Auth/AuthModeStrategyTests.swift | 26 +++++++++---------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/AmplifyPlugins/Core/AWSPluginsCore/Auth/AWSAuthModeStrategy.swift b/AmplifyPlugins/Core/AWSPluginsCore/Auth/AWSAuthModeStrategy.swift index 2667999996..1f3edd8786 100644 --- a/AmplifyPlugins/Core/AWSPluginsCore/Auth/AWSAuthModeStrategy.swift +++ b/AmplifyPlugins/Core/AWSPluginsCore/Auth/AWSAuthModeStrategy.swift @@ -204,15 +204,16 @@ public class AWSMultiAuthModeStrategy: AuthModeStrategy { /// - operations: model operations /// - Returns: an iterator for the applicable auth rules public func authTypesFor(schema: ModelSchema, - operations: [ModelOperation]) async -> AWSAuthorizationTypeIterator { - var applicableAuthRules = Set() - for operation in operations { - let rules = schema.authRules.filter(modelOperation: operation) - applicableAuthRules = applicableAuthRules.union(Set(rules)) - } + operations: [ModelOperation]) async -> AWSAuthorizationTypeIterator { + var sortedRules = operations + .flatMap { schema.authRules.filter(modelOperation: $0) } + .reduce(into: [AuthRule](), { array, rule in + if !array.contains(rule) { + array.append(rule) + } + }) + .sorted(by: AWSMultiAuthModeStrategy.comparator) - var sortedRules = applicableAuthRules.sorted(by: AWSMultiAuthModeStrategy.comparator) - // if there isn't a user signed in, returns only public or custom rules if let authDelegate = authDelegate, await !authDelegate.isUserLoggedIn() { sortedRules = sortedRules.filter { rule in diff --git a/AmplifyPlugins/Core/AWSPluginsCoreTests/Auth/AuthModeStrategyTests.swift b/AmplifyPlugins/Core/AWSPluginsCoreTests/Auth/AuthModeStrategyTests.swift index f02a93e3d7..304aac6add 100644 --- a/AmplifyPlugins/Core/AWSPluginsCoreTests/Auth/AuthModeStrategyTests.swift +++ b/AmplifyPlugins/Core/AWSPluginsCoreTests/Auth/AuthModeStrategyTests.swift @@ -11,7 +11,7 @@ import XCTest @testable import AWSPluginsCore class AuthModeStrategyTests: XCTestCase { - + // Given: default strategy and a model schema // When: authTypesFor for .create operation is called // Then: an empty iterator is returned @@ -20,7 +20,7 @@ class AuthModeStrategyTests: XCTestCase { let authTypesIterator = authMode.authTypesFor(schema: AnyModelTester.schema, operation: .create) XCTAssertEqual(authTypesIterator.count, 0) } - + // Given: multi-auth strategy and a model schema // When: authTypesFor for .create operation is called // Then: auth types are returned in order according to priority rules @@ -31,7 +31,7 @@ class AuthModeStrategyTests: XCTestCase { XCTAssertEqual(authTypesIterator.next(), .amazonCognitoUserPools) XCTAssertEqual(authTypesIterator.next(), .apiKey) } - + // Given: multi-auth strategy and a model schema without auth provider // When: auth types are requested // Then: default values based on the auth strategy should be returned @@ -42,7 +42,7 @@ class AuthModeStrategyTests: XCTestCase { XCTAssertEqual(authTypesIterator.next(), .amazonCognitoUserPools) XCTAssertEqual(authTypesIterator.next(), .apiKey) } - + // Given: multi-auth strategy and a model schema with 4 auth rules // When: authTypesFor for .create operation is called // Then: applicable auth types are ordered according to priority rules @@ -55,7 +55,7 @@ class AuthModeStrategyTests: XCTestCase { XCTAssertEqual(authTypesIterator.next(), .amazonCognitoUserPools) XCTAssertEqual(authTypesIterator.next(), .awsIAM) } - + // Given: multi-auth strategy and a model schema multiple public rules // When: authTypesFor for .create operation is called // Then: applicable auth types are ordered according to priority rules @@ -68,7 +68,7 @@ class AuthModeStrategyTests: XCTestCase { XCTAssertEqual(authTypesIterator.next(), .awsIAM) XCTAssertEqual(authTypesIterator.next(), .apiKey) } - + // Given: multi-auth strategy and a model schema // When: authTypesFor for .create operation is called // Then: applicable auth types returned are only the @@ -80,7 +80,7 @@ class AuthModeStrategyTests: XCTestCase { XCTAssertEqual(authTypesIterator.next(), .amazonCognitoUserPools) XCTAssertEqual(authTypesIterator.next(), .amazonCognitoUserPools) } - + // Given: multi-auth strategy a model schema // When: authTypesFor for .create operation is called for unauthenticated user // Then: applicable auth types returned are only public rules @@ -94,7 +94,7 @@ class AuthModeStrategyTests: XCTestCase { XCTAssertEqual(authTypesIterator.count, 1) XCTAssertEqual(authTypesIterator.next(), .apiKey) } - + // Given: multi-auth model schema with a custom strategy // When: authTypesFor for .create operation is called // Then: applicable auth types returned respect the priority rules @@ -107,7 +107,7 @@ class AuthModeStrategyTests: XCTestCase { XCTAssertEqual(authTypesIterator.next(), .amazonCognitoUserPools) XCTAssertEqual(authTypesIterator.next(), .awsIAM) } - + // Given: multi-auth model schema with a custom strategy // When: authTypesFor for .create operation is called for unauthenticated user // Then: applicable auth types returned are public rules or custom @@ -115,14 +115,14 @@ class AuthModeStrategyTests: XCTestCase { let authMode = AWSMultiAuthModeStrategy() let delegate = UnauthenticatedUserDelegate() authMode.authDelegate = delegate - + var authTypesIterator = await authMode.authTypesFor(schema: ModelWithCustomStrategy.schema, operation: .create) XCTAssertEqual(authTypesIterator.count, 2) XCTAssertEqual(authTypesIterator.next(), .function) XCTAssertEqual(authTypesIterator.next(), .awsIAM) } - + // Given: multi-auth strategy and a model schema without auth provider // When: auth types are requested with multiple operation // Then: default values based on the auth strategy should be returned @@ -133,7 +133,7 @@ class AuthModeStrategyTests: XCTestCase { XCTAssertEqual(authTypesIterator.next(), .amazonCognitoUserPools) XCTAssertEqual(authTypesIterator.next(), .apiKey) } - + // Given: multi-auth strategy and a model schema with auth provider // When: auth types are requested with multiple operation // Then: auth rule for public access should be returned @@ -145,7 +145,7 @@ class AuthModeStrategyTests: XCTestCase { XCTAssertEqual(authTypesIterator.count, 1) XCTAssertEqual(authTypesIterator.next(), .apiKey) } - + } // MARK: - Test models