Skip to content

Commit

Permalink
fix: Delete events on an invalid api key response. (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
crleona authored Aug 15, 2024
1 parent c8c13fc commit 9329a26
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class PersistentStorageResponseHandler: ResponseHandler {
return
}

if events.count == 1 {
let error = data["error"] as? String ?? ""
let error = data["error"] as? String ?? ""

let isInvalidApiKey = error == "Invalid API key: \(configuration.apiKey)"
if events.count == 1 || isInvalidApiKey {
triggerEventsCallback(
events: events,
code: HttpClient.HttpStatus.BAD_REQUEST.rawValue,
Expand Down Expand Up @@ -81,7 +83,6 @@ class PersistentStorageResponseHandler: ResponseHandler {
}
}

let error = data["error"] as? String ?? ""
triggerEventsCallback(events: eventsToDrop, code: HttpClient.HttpStatus.BAD_REQUEST.rawValue, message: error)

eventsToRetry.forEach { event in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,31 @@ final class PersistentStorageResponseHandlerTests: XCTestCase {
"removeEventCallback(insertId: e3e4488d-6877-4775-ae88-344df7ccd5d8)"
)
}

func testInvalidAPIKey() {
// 2 valid events
let eventsString = """
[
{"event_type":"valid-event","insert_id":"1621D025-A754-42EB-9305-307F36217C78","user_id":"test-user"},
{"event_type":"valid-event","insert_id":"AE7550E1-C8F0-4583-81D3-0561830A09DD","user_id":"test-user"},
]
"""

let fakePersistentStorage = FakePersistentStorage(storagePrefix: "storage",
logger: logger,
diagonostics: diagonostics)
let handler = PersistentStorageResponseHandler(
configuration: configuration,
storage: fakePersistentStorage,
eventPipeline: eventPipeline,
eventBlock: eventBlock,
eventsString: eventsString
)

handler.handleBadRequestResponse(data: ["error": "Invalid API key: \(configuration.apiKey)"])
XCTAssertEqual(
fakePersistentStorage.haveBeenCalledWith[0],
"remove(eventBlock: \(eventBlock.absoluteURL))"
)
}
}

0 comments on commit 9329a26

Please sign in to comment.