diff --git a/Sources/Common/Background Queue/QueueRunner.swift b/Sources/Common/Background Queue/QueueRunner.swift index 8bdd27962..340f83b61 100644 --- a/Sources/Common/Background Queue/QueueRunner.swift +++ b/Sources/Common/Background Queue/QueueRunner.swift @@ -92,6 +92,13 @@ private extension CioQueueRunner { return onComplete(failureIfDontDecodeTaskData) } + // If a HTTP request is made to register device token with an empty identifier, either (1) a 302 will occur or (2) a "devices" profile will be created in a CIO workspace. Neither are ideal. + // The SDK has logic to prevent *new* register device tasks being added to the BQ if identifier is empty but existing tasks in the BQ will still occur. + // This logic is to clear those tasks from the BQ. + if taskData.profileIdentifier.isBlankOrEmpty() { + return onComplete(.success(())) + } + performHttpRequest( endpoint: .registerDevice(identifier: taskData.profileIdentifier), requestBody: taskData.attributesJsonString?.data, diff --git a/Sources/Common/Extensions/ResultExtensions.swift b/Sources/Common/Extensions/ResultExtensions.swift index 1817cbb59..65d53cc4b 100644 --- a/Sources/Common/Extensions/ResultExtensions.swift +++ b/Sources/Common/Extensions/ResultExtensions.swift @@ -14,4 +14,8 @@ extension Result { default: return nil } } + + var isSuccess: Bool { + success != nil + } } diff --git a/Tests/Common/Background Queue/QueueRunnerTest.swift b/Tests/Common/Background Queue/QueueRunnerTest.swift new file mode 100644 index 000000000..57c655dd9 --- /dev/null +++ b/Tests/Common/Background Queue/QueueRunnerTest.swift @@ -0,0 +1,36 @@ +@testable import CioInternalCommon +import Foundation +import SharedTests +import XCTest + +class QueueRunnerTest: UnitTest { + private var runner: CioQueueRunner! + + private let httpClientMock = HttpClientMock() + + override func setUp() { + super.setUp() + + runner = CioQueueRunner(jsonAdapter: jsonAdapter, logger: log, httpClient: httpClientMock, hooksManager: diGraph.hooksManager, sdkConfig: sdkConfig) + } + + // MARK: registerPushToken + + func test_registerPushToken_givenIdentifierEmpty_expectHttpRequestNotMade_expectBQDeletesTask() { + let givenRegisterTokenTask = RegisterPushNotificationQueueTaskData(profileIdentifier: " ", attributesJsonString: nil) + + let givenQueueTask = QueueTask(storageId: .random, type: QueueTaskType.registerPushToken.rawValue, data: jsonAdapter.toJson(givenRegisterTokenTask)!, runResults: QueueTaskRunResults(totalRuns: 0)) + + let expectToCompleteRunning = expectation(description: "expect to complete running") + var actualResult: Result! + runner.runTask(givenQueueTask) { result in + actualResult = result + expectToCompleteRunning.fulfill() + } + + waitForExpectations() + + XCTAssertTrue(actualResult.isSuccess) // the BQ will delete the task if http was successful + XCTAssertFalse(httpClientMock.mockCalled) + } +} diff --git a/Tests/Common/Extensions/ResultExtensionsTests.swift b/Tests/Common/Extensions/ResultExtensionsTests.swift new file mode 100644 index 000000000..bd837559c --- /dev/null +++ b/Tests/Common/Extensions/ResultExtensionsTests.swift @@ -0,0 +1,20 @@ +@testable import CioInternalCommon +import Foundation +import SharedTests +import XCTest + +class ResultExtensionsTest: UnitTest { + // MARK: isSuccess + + func test_givenSuccess_expectIsSuccessTrue() { + let result: Result = .success("hello") + + XCTAssertTrue(result.isSuccess) + } + + func test_givenFailure_expectIsSuccessFalse() { + let result: Result = .failure(HttpRequestError.cancelled) + + XCTAssertFalse(result.isSuccess) + } +}