generated from bitwarden/template
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PM-11974: Fix login with device notification for inactive account not…
… switching to that account (#1157)
- Loading branch information
1 parent
8f76eab
commit 88da015
Showing
5 changed files
with
84 additions
and
62 deletions.
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
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 |
---|---|---|
|
@@ -371,6 +371,7 @@ class NotificationServiceTests: BitwardenTestCase { // swiftlint:disable:this ty | |
|
||
/// `messageReceived(_:notificationDismissed:notificationTapped:)` tells | ||
/// the delegate to show the switch account alert if it's a login request for a non-active account. | ||
@MainActor | ||
func test_messageReceived_loginRequest_differentAccount() async throws { | ||
// Set up the mock data. | ||
stateService.setIsAuthenticated() | ||
|
@@ -379,7 +380,7 @@ class NotificationServiceTests: BitwardenTestCase { // swiftlint:disable:this ty | |
authService.getPendingLoginRequestResult = .success([.fixture()]) | ||
let loginRequestNotification = LoginRequestNotification(id: "requestId", userId: "differentUser") | ||
let notificationData = try JSONEncoder().encode(loginRequestNotification) | ||
let message: [AnyHashable: Any] = [ | ||
nonisolated(unsafe) let message: [AnyHashable: Any] = [ | ||
"data": [ | ||
"type": NotificationType.authRequest.rawValue, | ||
"payload": String(data: notificationData, encoding: .utf8) ?? "", | ||
|
@@ -392,12 +393,12 @@ class NotificationServiceTests: BitwardenTestCase { // swiftlint:disable:this ty | |
// Confirm the results. | ||
XCTAssertEqual(stateService.loginRequest, loginRequestNotification) | ||
XCTAssertEqual(delegate.switchAccountsAccount, .fixture(profile: .fixture(userId: "differentUser"))) | ||
XCTAssertEqual(delegate.switchAccountsLoginRequest, .fixture()) | ||
XCTAssertEqual(delegate.switchAccountsShowAlert, true) | ||
} | ||
|
||
/// `messageReceived(_:notificationDismissed:notificationTapped:)` tells | ||
/// the delegate to show the login request if it's a login request for the active account. | ||
@MainActor | ||
func test_messageReceived_loginRequest_sameAccount() async throws { | ||
// Set up the mock data. | ||
stateService.setIsAuthenticated() | ||
|
@@ -406,7 +407,7 @@ class NotificationServiceTests: BitwardenTestCase { // swiftlint:disable:this ty | |
authService.getPendingLoginRequestResult = .success([.fixture()]) | ||
let loginRequestNotification = LoginRequestNotification(id: "requestId", userId: "1") | ||
let notificationData = try JSONEncoder().encode(loginRequestNotification) | ||
let message: [AnyHashable: Any] = [ | ||
nonisolated(unsafe) let message: [AnyHashable: Any] = [ | ||
"data": [ | ||
"type": NotificationType.authRequest.rawValue, | ||
"payload": String(data: notificationData, encoding: .utf8) ?? "", | ||
|
@@ -423,14 +424,15 @@ class NotificationServiceTests: BitwardenTestCase { // swiftlint:disable:this ty | |
|
||
/// `messageReceived(_:notificationDismissed:notificationTapped:)` handles logout requests and will not route | ||
/// to the landing screen if the logged-out account was not the currently active account. | ||
@MainActor | ||
func test_messageReceived_logout_nonActiveUser() async throws { | ||
// Set up the mock data. | ||
stateService.setIsAuthenticated() | ||
let activeAccount: Account = .fixture() | ||
let nonActiveAccount: Account = .fixture(profile: .fixture(userId: "b245a33f")) | ||
stateService.accounts = [activeAccount, nonActiveAccount] | ||
|
||
let message: [AnyHashable: Any] = [ | ||
nonisolated(unsafe) let message: [AnyHashable: Any] = [ | ||
"data": [ | ||
"type": NotificationType.logOut.rawValue, | ||
"payload": "{\"UserId\":\"\(nonActiveAccount.profile.userId)\"}", | ||
|
@@ -445,14 +447,15 @@ class NotificationServiceTests: BitwardenTestCase { // swiftlint:disable:this ty | |
|
||
/// `messageReceived(_:notificationDismissed:notificationTapped:)` handles logout requests and will route | ||
/// to the landing screen if the logged-out account was the currently active account. | ||
@MainActor | ||
func test_messageReceived_logout_activeUser() async throws { | ||
// Set up the mock data. | ||
stateService.setIsAuthenticated() | ||
let activeAccount: Account = .fixture() | ||
let nonActiveAccount: Account = .fixture(profile: .fixture(userId: "b245a33f")) | ||
stateService.accounts = [activeAccount, nonActiveAccount] | ||
|
||
let message: [AnyHashable: Any] = [ | ||
nonisolated(unsafe) let message: [AnyHashable: Any] = [ | ||
"data": [ | ||
"type": NotificationType.logOut.rawValue, | ||
"payload": "{\"UserId\":\"\(activeAccount.profile.userId)\"}", | ||
|
@@ -466,15 +469,16 @@ class NotificationServiceTests: BitwardenTestCase { // swiftlint:disable:this ty | |
} | ||
|
||
/// `messageReceived(_:notificationDismissed:notificationTapped:)` handles notifications being dismissed. | ||
@MainActor | ||
func test_messageReceived_notificationDismissed() async throws { | ||
// Set up the mock data. | ||
stateService.loginRequest = LoginRequestNotification(id: "1", userId: "2") | ||
let loginRequest = LoginRequestPushNotification( | ||
timeoutInMinutes: 15, | ||
userEmail: "[email protected]" | ||
userId: "2" | ||
) | ||
let testData = try JSONEncoder().encode(loginRequest) | ||
let message: [AnyHashable: Any] = [ | ||
nonisolated(unsafe) let message: [AnyHashable: Any] = [ | ||
"notificationData": String(data: testData, encoding: .utf8) ?? "", | ||
] | ||
|
||
|
@@ -486,6 +490,7 @@ class NotificationServiceTests: BitwardenTestCase { // swiftlint:disable:this ty | |
} | ||
|
||
/// `messageReceived(_:notificationDismissed:notificationTapped:)` handles notifications being tapped. | ||
@MainActor | ||
func test_messageReceived_notificationTapped() async throws { | ||
// Set up the mock data. | ||
stateService.accounts = [.fixture()] | ||
|
@@ -494,10 +499,10 @@ class NotificationServiceTests: BitwardenTestCase { // swiftlint:disable:this ty | |
authService.getPendingLoginRequestResult = .success([.fixture(id: "requestId")]) | ||
let loginRequest = LoginRequestPushNotification( | ||
timeoutInMinutes: 15, | ||
userEmail: Account.fixture().profile.email | ||
userId: Account.fixture().profile.userId | ||
) | ||
let testData = try JSONEncoder().encode(loginRequest) | ||
let message: [AnyHashable: Any] = [ | ||
nonisolated(unsafe) let message: [AnyHashable: Any] = [ | ||
"notificationData": String(data: testData, encoding: .utf8) ?? "", | ||
] | ||
|
||
|
@@ -506,31 +511,27 @@ class NotificationServiceTests: BitwardenTestCase { // swiftlint:disable:this ty | |
|
||
// Confirm the results. | ||
XCTAssertEqual(delegate.switchAccountsAccount, .fixture()) | ||
XCTAssertEqual(delegate.switchAccountsLoginRequest, .fixture(id: "requestId")) | ||
XCTAssertEqual(delegate.switchAccountsShowAlert, false) | ||
} | ||
|
||
/// `messageReceived(_:notificationDismissed:notificationTapped:)` handles errors. | ||
@MainActor | ||
func test_messageReceived_notificationTapped_error() async throws { | ||
// Set up the mock data. | ||
stateService.accounts = [.fixture()] | ||
stateService.activeAccount = .fixtureAccountLogin() | ||
stateService.loginRequest = LoginRequestNotification(id: "requestId", userId: "1") | ||
authService.getPendingLoginRequestResult = .failure(BitwardenTestError.example) | ||
let loginRequest = LoginRequestPushNotification( | ||
timeoutInMinutes: 15, | ||
userEmail: Account.fixture().profile.email | ||
userId: Account.fixture().profile.userId | ||
) | ||
let testData = try JSONEncoder().encode(loginRequest) | ||
let message: [AnyHashable: Any] = [ | ||
nonisolated(unsafe) let message: [AnyHashable: Any] = [ | ||
"notificationData": String(data: testData, encoding: .utf8) ?? "", | ||
] | ||
|
||
// Test. | ||
await subject.messageReceived(message, notificationDismissed: nil, notificationTapped: true) | ||
|
||
// Confirm the results. | ||
XCTAssertEqual(errorReporter.errors.last as? BitwardenTestError, .example) | ||
XCTAssertEqual(errorReporter.errors.last as? StateServiceError, .noAccounts) | ||
} | ||
} | ||
|
||
|
@@ -542,7 +543,6 @@ class MockNotificationServiceDelegate: NotificationServiceDelegate { | |
var showLoginRequestRequest: LoginRequest? | ||
|
||
var switchAccountsAccount: Account? | ||
var switchAccountsLoginRequest: LoginRequest? | ||
var switchAccountsShowAlert: Bool? | ||
|
||
func routeToLanding() async { | ||
|
@@ -553,9 +553,8 @@ class MockNotificationServiceDelegate: NotificationServiceDelegate { | |
showLoginRequestRequest = loginRequest | ||
} | ||
|
||
func switchAccounts(to account: Account, for loginRequest: LoginRequest, showAlert: Bool) { | ||
func switchAccountsForLoginRequest(to account: Account, showAlert: Bool) { | ||
switchAccountsAccount = account | ||
switchAccountsLoginRequest = loginRequest | ||
switchAccountsShowAlert = showAlert | ||
} | ||
} // swiftlint:disable:this file_length |
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