Skip to content

Commit

Permalink
fix: prevent registering device with empty identifier existing BQ tas…
Browse files Browse the repository at this point in the history
…ks (#392)
  • Loading branch information
levibostian authored Oct 11, 2023
1 parent b0f309f commit 867619f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/Common/Background Queue/QueueRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions Sources/Common/Extensions/ResultExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ extension Result {
default: return nil
}
}

var isSuccess: Bool {
success != nil
}
}
36 changes: 36 additions & 0 deletions Tests/Common/Background Queue/QueueRunnerTest.swift
Original file line number Diff line number Diff line change
@@ -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<Void, HttpRequestError>!
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)
}
}
20 changes: 20 additions & 0 deletions Tests/Common/Extensions/ResultExtensionsTests.swift
Original file line number Diff line number Diff line change
@@ -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<String, Error> = .success("hello")

XCTAssertTrue(result.isSuccess)
}

func test_givenFailure_expectIsSuccessFalse() {
let result: Result<String, Error> = .failure(HttpRequestError.cancelled)

XCTAssertFalse(result.isSuccess)
}
}

0 comments on commit 867619f

Please sign in to comment.