Skip to content

Commit

Permalink
Add and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Nov 15, 2024
1 parent c558367 commit 25f2815
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import XCTest
@testable import WordPressKit
@testable import WooCommerce

final class WPComEmailLoginViewModelTests: XCTestCase {
Expand All @@ -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 })
Expand All @@ -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 })
Expand All @@ -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 })
Expand All @@ -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 })
Expand All @@ -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 })
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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 },
Expand All @@ -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: "[email protected]")

// 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: "[email protected]")

// Then
XCTAssertTrue(triggeredOnError)
}
}

0 comments on commit 25f2815

Please sign in to comment.