Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore): Continue initial sync if atleast one model syncs successfully and other models fail due to Unauthorized API error #3224

Merged
merged 6 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ final class AWSInitialSyncOrchestrator: InitialSyncOrchestrator {
allMessages.joined(separator: "\n"),
underlyingError
)

return .failure(syncError)
}

Expand Down Expand Up @@ -208,7 +209,7 @@ extension AWSInitialSyncOrchestrator {
return errorTypeValue
}

private func isUnauthorizedError(_ error: DataStoreError) -> Bool {
func isUnauthorizedError(_ error: DataStoreError) -> Bool {
guard case let .sync(_, _, underlyingError) = error,
let datastoreError = underlyingError as? DataStoreError
else {
Expand Down Expand Up @@ -245,6 +246,22 @@ extension AWSInitialSyncOrchestrator {
case .unauthorized = AppSyncErrorType(errorTypeValue) {
return true
}

// Check is API error is of unauthorized type
if case let .api(amplifyError, _) = datastoreError,
let apiError = amplifyError as? APIError {
if case .operationError(let errorDescription, _, _) = apiError,
errorDescription.range(of: "Unauthorized",
options: .caseInsensitive) != nil {
return true
}

if case .httpStatusError(let statusCode, _) = apiError,
(statusCode == 401 || statusCode == 403) {
return true
}
}

return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import XCTest
import Foundation

@testable import Amplify
@testable import AmplifyTestCommon
Expand Down Expand Up @@ -101,7 +102,7 @@ class InitialSyncOrchestratorTests: XCTestCase {
Amplify.Hub.removeListener(hubListener)
sink.cancel()
}

/// - Given: An InitialSyncOrchestrator with a model dependency graph, API is expected to return an error for certain models
/// - When:
/// - The orchestrator starts up
Expand Down Expand Up @@ -200,7 +201,7 @@ class InitialSyncOrchestratorTests: XCTestCase {
Amplify.Hub.removeListener(hubListener)
sink.cancel()
}

/// - Given: An InitialSyncOrchestrator with a model dependency graph containing no associations
/// - When:
/// - The orchestrator starts up
Expand Down Expand Up @@ -409,4 +410,47 @@ class InitialSyncOrchestratorTests: XCTestCase {
sink.cancel()
}

/// - Given:
/// An InitialSyncOrchestrator with a model dependency graph
/// - When:
/// isUnauthorized() is called with an API Error with status code 401, 403 or "Unauthorized" description
/// and return false for other cases
/// - Then:
/// - It should return true for unauthorized cases and false for other cases
func testIsUnauthorized() {
let apiPlugin = MockAPICategoryPlugin()
let storageAdapter = MockSQLiteStorageEngineAdapter()
let reconciliationQueue = MockReconciliationQueue()

let orchestrator =
AWSInitialSyncOrchestrator(dataStoreConfiguration: .default,
authModeStrategy: AWSDefaultAuthModeStrategy(),
api: apiPlugin,
reconciliationQueue: reconciliationQueue,
storageAdapter: storageAdapter)

let error1 = DataStoreError.api(APIError.httpStatusError(401, HTTPURLResponse(url: URL(string: "https://aws.amazon.com")!,
statusCode: 401,
httpVersion: nil,
headerFields: nil)!))
XCTAssertTrue(orchestrator.isUnauthorizedError(DataStoreError.sync("", "", error1)))

let error2 = DataStoreError.api(APIError.httpStatusError(403, HTTPURLResponse(url: URL(string: "https://aws.amazon.com")!,
statusCode: 403,
httpVersion: nil,
headerFields: nil)!))
XCTAssertTrue(orchestrator.isUnauthorizedError(DataStoreError.sync("", "", error2)))

let error3 = DataStoreError.api(APIError.httpStatusError(404, HTTPURLResponse(url: URL(string: "https://aws.amazon.com")!,
statusCode: 404,
httpVersion: nil,
headerFields: nil)!))
XCTAssertFalse(orchestrator.isUnauthorizedError(DataStoreError.sync("", "", error3)))

let error4 = DataStoreError.api(APIError.operationError("Unauthorized error", "", nil))
XCTAssertTrue(orchestrator.isUnauthorizedError(DataStoreError.sync("", "", error4)))

let error5 = DataStoreError.api(APIError.operationError("An error occurred", "", nil))
XCTAssertFalse(orchestrator.isUnauthorizedError(DataStoreError.sync("", "", error5)))
}
}
Loading