Skip to content

Commit

Permalink
feat: allow to pass nil to verify for passiveApiKey
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP committed Nov 17, 2024
1 parent 3283946 commit abe9eaf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
32 changes: 31 additions & 1 deletion Example/HCaptcha_Tests/Core/HCaptchaWebViewManager__Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ class HCaptchaWebViewManager__Tests: XCTestCase {
}
}

let manager = HCaptchaWebViewManager(messageBody: "{token: key, action: \"openExternalPage\"}",
let manager = HCaptchaWebViewManager(messageBody: "{action: \"openExternalPage\"}",
apiKey: apiKey,
urlOpener: TestURLOpener(exp1, exp2))
manager.configureWebView { _ in
Expand Down Expand Up @@ -617,4 +617,34 @@ class HCaptchaWebViewManager__Tests: XCTestCase {

waitForExpectations(timeout: 10)
}

func test__Validate_Passive_Key_On_Nil_View() {
let exp = expectation(description: "wait for completion")

let manager = HCaptchaWebViewManager(messageBody: "{token: \"success-token\"}", passiveApiKey: true)
manager.shouldResetOnError = false
manager.completion = { response in
XCTAssertEqual(try? response.dematerialize(), "success-token")
exp.fulfill()
}
manager.validate(on: nil)

waitForExpectations(timeout: 10)
}

func test__Validate_Non_Passive_Key_On_Nil_View() {
let exp = expectation(description: "didLoad never called")

let manager = HCaptchaWebViewManager(messageBody: "{token: \"should-not-be-delivered\"}", passiveApiKey: false)
manager.shouldResetOnError = false

manager.completion = { response in
XCTAssertEqual(HCaptchaError.failedSetup, response.error)
exp.fulfill()
}

manager.validate(on: nil)

waitForExpectations(timeout: 10)
}
}
3 changes: 2 additions & 1 deletion HCaptcha/Classes/HCaptcha.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public class HCaptcha: NSObject {
Starts the challenge validation
*/
@objc
public func validate(on view: UIView, resetOnError: Bool = true, completion: @escaping (HCaptchaResult) -> Void) {
public func validate(on view: UIView? = nil, resetOnError: Bool = true,
completion: @escaping (HCaptchaResult) -> Void) {
Log.debug(".validate on: \(view) resetOnError: \(resetOnError)")

manager.shouldResetOnError = resetOnError
Expand Down
7 changes: 6 additions & 1 deletion HCaptcha/Classes/HCaptchaWebViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,16 @@ internal class HCaptchaWebViewManager: NSObject {

Starts the challenge validation
*/
func validate(on view: UIView) {
func validate(on view: UIView?) {
Log.debug("WebViewManager.validate on: \(view)")
resultHandled = false

if !passiveApiKey {
guard let view = view else {
completion?(HCaptchaResult(self, error: .failedSetup))
return
}

view.addSubview(webView)
if self.didFinishLoading && (webView.bounds.size == CGSize.zero || webView.bounds.size == webViewInitSize) {
self.doConfigureWebView()
Expand Down

0 comments on commit abe9eaf

Please sign in to comment.