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: Delete events on an invalid api key response. #211

Merged
merged 1 commit into from
Aug 15, 2024
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 @@ -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))"
)
}
}
Loading