diff --git a/WooCommerce/WooCommerceTests/Mocks/MockWordPressComAccountService.swift b/WooCommerce/WooCommerceTests/Mocks/MockWordPressComAccountService.swift index a3cb9344f07..f04a4b62e39 100644 --- a/WooCommerce/WooCommerceTests/Mocks/MockWordPressComAccountService.swift +++ b/WooCommerce/WooCommerceTests/Mocks/MockWordPressComAccountService.swift @@ -17,7 +17,11 @@ final class MockWordPressComAccountService: WordPressComAccountServiceProtocol { failure(passwordlessAccountCheckError) } - func requestAuthenticationLink(for email: String, jetpackLogin: Bool, success: @escaping () -> Void, failure: @escaping (Error) -> Void) { + func requestAuthenticationLink(for email: String, + jetpackLogin: Bool, + createAccountIfNotFound: Bool, + success: @escaping () -> Void, + failure: @escaping (Error) -> Void) { triggeredRequestAuthenticationLink = true guard let authenticationLinkRequestError else { return success() diff --git a/WooCommerce/WooCommerceTests/ViewRelated/JetpackSetup/WPComLogin/WPComEmailLoginViewModelTests.swift b/WooCommerce/WooCommerceTests/ViewRelated/JetpackSetup/WPComLogin/WPComEmailLoginViewModelTests.swift index 866db08a807..198a08f012f 100644 --- a/WooCommerce/WooCommerceTests/ViewRelated/JetpackSetup/WPComLogin/WPComEmailLoginViewModelTests.swift +++ b/WooCommerce/WooCommerceTests/ViewRelated/JetpackSetup/WPComLogin/WPComEmailLoginViewModelTests.swift @@ -1,4 +1,5 @@ import XCTest +@testable import WordPressKit @testable import WooCommerce final class WPComEmailLoginViewModelTests: XCTestCase { @@ -8,6 +9,7 @@ final class WPComEmailLoginViewModelTests: XCTestCase { let siteURL = "https://example.com" let viewModel = WPComEmailLoginViewModel(siteURL: siteURL, requiresConnectionOnly: false, + allowAccountCreation: false, onPasswordUIRequest: { _ in }, onMagicLinkUIRequest: { _ in }, onError: { _ in }) @@ -24,6 +26,7 @@ final class WPComEmailLoginViewModelTests: XCTestCase { let siteURL = "https://example.com" let viewModel = WPComEmailLoginViewModel(siteURL: siteURL, requiresConnectionOnly: true, + allowAccountCreation: false, onPasswordUIRequest: { _ in }, onMagicLinkUIRequest: { _ in }, onError: { _ in }) @@ -40,6 +43,7 @@ final class WPComEmailLoginViewModelTests: XCTestCase { let siteURL = "https://example.com" let viewModel = WPComEmailLoginViewModel(siteURL: siteURL, requiresConnectionOnly: false, + allowAccountCreation: false, onPasswordUIRequest: { _ in }, onMagicLinkUIRequest: { _ in }, onError: { _ in }) @@ -56,6 +60,7 @@ final class WPComEmailLoginViewModelTests: XCTestCase { let siteURL = "https://example.com" let viewModel = WPComEmailLoginViewModel(siteURL: siteURL, requiresConnectionOnly: true, + allowAccountCreation: false, onPasswordUIRequest: { _ in }, onMagicLinkUIRequest: { _ in }, onError: { _ in }) @@ -72,6 +77,7 @@ final class WPComEmailLoginViewModelTests: XCTestCase { let siteURL = "https://example.com" let viewModel = WPComEmailLoginViewModel(siteURL: siteURL, requiresConnectionOnly: true, + allowAccountCreation: false, onPasswordUIRequest: { _ in }, onMagicLinkUIRequest: { _ in }, onError: { _ in }) @@ -92,6 +98,7 @@ final class WPComEmailLoginViewModelTests: XCTestCase { mockAccountService.shouldReturnPasswordlessAccount = true let viewModel = WPComEmailLoginViewModel(siteURL: "https://example.com", requiresConnectionOnly: true, + allowAccountCreation: false, accountService: mockAccountService, onPasswordUIRequest: { _ in }, onMagicLinkUIRequest: { _ in }, @@ -115,6 +122,7 @@ final class WPComEmailLoginViewModelTests: XCTestCase { var triggeredOnError = false let viewModel = WPComEmailLoginViewModel(siteURL: "https://example.com", requiresConnectionOnly: true, + allowAccountCreation: false, accountService: mockAccountService, onPasswordUIRequest: { _ in }, onMagicLinkUIRequest: { _ in }, @@ -133,6 +141,7 @@ final class WPComEmailLoginViewModelTests: XCTestCase { var triggeredPasswordUIRequest = false let viewModel = WPComEmailLoginViewModel(siteURL: "https://example.com", requiresConnectionOnly: true, + allowAccountCreation: false, accountService: mockAccountService, onPasswordUIRequest: { _ in triggeredPasswordUIRequest = true }, onMagicLinkUIRequest: { _ in }, @@ -150,6 +159,7 @@ final class WPComEmailLoginViewModelTests: XCTestCase { var triggeredOnMagicLinkUIRequest = false let viewModel = WPComEmailLoginViewModel(siteURL: "https://example.com", requiresConnectionOnly: true, + allowAccountCreation: false, accountService: mockAccountService, onPasswordUIRequest: { _ in }, onMagicLinkUIRequest: { _ in triggeredOnMagicLinkUIRequest = true }, @@ -168,6 +178,7 @@ final class WPComEmailLoginViewModelTests: XCTestCase { var triggeredOnError = false let viewModel = WPComEmailLoginViewModel(siteURL: "https://example.com", requiresConnectionOnly: true, + allowAccountCreation: false, accountService: mockAccountService, onPasswordUIRequest: { _ in }, onMagicLinkUIRequest: { _ in }, @@ -178,4 +189,54 @@ final class WPComEmailLoginViewModelTests: XCTestCase { // Then XCTAssertTrue(triggeredOnError) } + + func test_given_unknown_email_when_allowAccountCreation_true_then_create_account() async { + // Given + let mockAccountService = MockWordPressComAccountService() + mockAccountService.passwordlessAccountCheckError = WordPressAPIError.endpointError( + WordPressComRestApiEndpointError( + code: WordPressComRestApiErrorCode.unknown, + apiErrorCode: "unknown_user" + ) + ) + var triggeredOnMagicLinkUIRequest = false + let viewModel = WPComEmailLoginViewModel(siteURL: "https://example.com", + requiresConnectionOnly: true, + allowAccountCreation: true, + accountService: mockAccountService, + onPasswordUIRequest: { _ in }, + onMagicLinkUIRequest: { _ in triggeredOnMagicLinkUIRequest = true }, + onError: { _ in }) + + // When + await viewModel.checkWordPressComAccount(email: "mail@example.com") + + // Then + XCTAssertTrue(triggeredOnMagicLinkUIRequest) + } + + func test_given_unknown_email_when_allowAccountCreation_false_then_trigger_onError() async { + // Given + let mockAccountService = MockWordPressComAccountService() + mockAccountService.passwordlessAccountCheckError = WordPressAPIError.endpointError( + WordPressComRestApiEndpointError( + code: WordPressComRestApiErrorCode.unknown, + apiErrorCode: "unknown_user" + ) + ) + var triggeredOnError = false + let viewModel = WPComEmailLoginViewModel(siteURL: "https://example.com", + requiresConnectionOnly: true, + allowAccountCreation: false, + accountService: mockAccountService, + onPasswordUIRequest: { _ in }, + onMagicLinkUIRequest: { _ in }, + onError: { _ in triggeredOnError = true }) + + // When + await viewModel.checkWordPressComAccount(email: "mail@example.com") + + // Then + XCTAssertTrue(triggeredOnError) + } }