From a88fcb98501cc521dbe3201ca6aacd87d444a34c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Wosko?= Date: Thu, 27 Sep 2018 14:57:51 -0300 Subject: [PATCH] Feature: enable validation to be skipped for testing * fix visibility of shouldSkipForUITests flag * fix visibility of shouldSkipForUITests flag * fix token bypassing * Add improvements from review fix #51 --- Example/ReCaptcha.xcodeproj/project.pbxproj | 2 +- .../AppIcon.appiconset/Contents.json | 5 +++++ Example/ReCaptcha_Tests/.swiftlint.yml | 2 ++ .../Core/ReCaptchaWebViewManager__Tests.swift | 16 ++++++++++++++++ ReCaptcha/Classes/ReCaptcha.swift | 6 ++++++ ReCaptcha/Classes/ReCaptchaWebViewManager.swift | 9 +++++++++ 6 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Example/ReCaptcha.xcodeproj/project.pbxproj b/Example/ReCaptcha.xcodeproj/project.pbxproj index 5d9964f..6d5ed0e 100644 --- a/Example/ReCaptcha.xcodeproj/project.pbxproj +++ b/Example/ReCaptcha.xcodeproj/project.pbxproj @@ -500,7 +500,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "\"${PODS_ROOT}/SwiftLint/swiftlint\" --path \"${PROJECT_DIR}/..\""; + shellScript = "\"${PODS_ROOT}/SwiftLint/swiftlint\" --path \"${PROJECT_DIR}/..\"\n"; }; F28FACA6200E447600E14987 /* ShellScript */ = { isa = PBXShellScriptBuildPhase; diff --git a/Example/ReCaptcha/Images.xcassets/AppIcon.appiconset/Contents.json b/Example/ReCaptcha/Images.xcassets/AppIcon.appiconset/Contents.json index b8236c6..19882d5 100644 --- a/Example/ReCaptcha/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/Example/ReCaptcha/Images.xcassets/AppIcon.appiconset/Contents.json @@ -39,6 +39,11 @@ "idiom" : "iphone", "size" : "60x60", "scale" : "3x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" } ], "info" : { diff --git a/Example/ReCaptcha_Tests/.swiftlint.yml b/Example/ReCaptcha_Tests/.swiftlint.yml index 8967e41..777177c 100644 --- a/Example/ReCaptcha_Tests/.swiftlint.yml +++ b/Example/ReCaptcha_Tests/.swiftlint.yml @@ -5,3 +5,5 @@ disabled_rules: - explicit_top_level_acl - function_body_length - identifier_name + - file_length + - type_body_length diff --git a/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift b/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift index d690a0f..1d5c494 100644 --- a/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift +++ b/Example/ReCaptcha_Tests/Core/ReCaptchaWebViewManager__Tests.swift @@ -366,6 +366,22 @@ class ReCaptchaWebViewManager__Tests: XCTestCase { XCTAssertEqual(result?.token, apiKey) } + func test__Validate__Should_Skip_For_Tests() { + let exp = expectation(description: "did skip validation") + + let manager = ReCaptchaWebViewManager() + manager.shouldSkipForTests = true + + manager.completion = { result in + XCTAssertEqual(result.token, "") + exp.fulfill() + } + + manager.validate(on: presenterView) + + waitForExpectations(timeout: 1) + } + // MARK: Force Challenge Visible func test__Force_Visible_Challenge() { diff --git a/ReCaptcha/Classes/ReCaptcha.swift b/ReCaptcha/Classes/ReCaptcha.swift index 8276185..25d4903 100644 --- a/ReCaptcha/Classes/ReCaptcha.swift +++ b/ReCaptcha/Classes/ReCaptcha.swift @@ -205,6 +205,12 @@ public class ReCaptcha { get { return manager.forceVisibleChallenge } set { manager.forceVisibleChallenge = newValue } } + + /// Allows validation stubbing for testing + public var shouldSkipForTests: Bool { + get { return manager.shouldSkipForTests } + set { manager.shouldSkipForTests = newValue } + } #endif } diff --git a/ReCaptcha/Classes/ReCaptchaWebViewManager.swift b/ReCaptcha/Classes/ReCaptchaWebViewManager.swift index bf9164c..4222e38 100644 --- a/ReCaptcha/Classes/ReCaptchaWebViewManager.swift +++ b/ReCaptcha/Classes/ReCaptchaWebViewManager.swift @@ -114,6 +114,9 @@ internal class ReCaptchaWebViewManager { ) } } + + /// Allows validation stubbing for testing + public var shouldSkipForTests = false #endif /// Sends the result message @@ -195,6 +198,12 @@ internal class ReCaptchaWebViewManager { Starts the challenge validation */ func validate(on view: UIView) { +#if DEBUG + guard !shouldSkipForTests else { + completion?(.token("")) + return + } +#endif webView.isHidden = false view.addSubview(webView)