-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c558367
commit 25f2815
Showing
2 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
@@ -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: "[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) | ||
} | ||
} |