mirror of
https://github.com/bitwarden/ios.git
synced 2025-12-10 00:42:29 -06:00
[PM-26060] Consolidate network alerts (#2128)
This commit is contained in:
parent
df7ea5a6a6
commit
5dc773e753
@ -1,67 +0,0 @@
|
||||
import BitwardenKit
|
||||
import BitwardenResources
|
||||
import BitwardenSdk
|
||||
import Foundation
|
||||
|
||||
// MARK: Alert+Networking
|
||||
|
||||
extension Alert {
|
||||
/// An alert shown to the user when they aren't connected to the internet.
|
||||
///
|
||||
/// - Parameter tryAgain: An action allowing the user to retry the request.
|
||||
///
|
||||
/// - Returns: An alert notifying the user that they aren't connected to the internet.
|
||||
///
|
||||
static func internetConnectionError(_ tryAgain: (() async -> Void)? = nil) -> Alert {
|
||||
Alert(
|
||||
title: Localizations.internetConnectionRequiredTitle,
|
||||
message: Localizations.internetConnectionRequiredMessage,
|
||||
alertActions: [
|
||||
AlertAction(title: Localizations.tryAgain, style: .default) { _ in
|
||||
if let tryAgain {
|
||||
await tryAgain()
|
||||
}
|
||||
},
|
||||
AlertAction(title: Localizations.cancel, style: .cancel),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
/// Creates an alert for the networking error that was received.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - error: The networking error that occurred.
|
||||
/// - tryAgain: An action allowing the user to retry the request.
|
||||
///
|
||||
/// - Returns: An alert notifying the user that a networking error occurred.
|
||||
///
|
||||
static func networkResponseError(
|
||||
_ error: Error,
|
||||
_ tryAgain: (() async -> Void)? = nil,
|
||||
) -> Alert {
|
||||
switch error {
|
||||
case let serverError as ServerError:
|
||||
defaultAlert(
|
||||
title: Localizations.anErrorHasOccurred,
|
||||
message: serverError.message,
|
||||
)
|
||||
case let error as URLError where error.code == .notConnectedToInternet || error.code == .networkConnectionLost:
|
||||
internetConnectionError(tryAgain)
|
||||
case let error as URLError where error.code == .timedOut:
|
||||
defaultAlert(
|
||||
title: Localizations.anErrorHasOccurred,
|
||||
message: error.localizedDescription,
|
||||
alertActions: [
|
||||
AlertAction(title: Localizations.tryAgain, style: .default) { _ in
|
||||
if let tryAgain {
|
||||
await tryAgain()
|
||||
}
|
||||
},
|
||||
AlertAction(title: Localizations.cancel, style: .cancel),
|
||||
],
|
||||
)
|
||||
default:
|
||||
defaultAlert(title: Localizations.anErrorHasOccurred)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,102 +0,0 @@
|
||||
// swiftlint:disable:this file_name
|
||||
|
||||
import BitwardenKit
|
||||
import BitwardenKitMocks
|
||||
import BitwardenResources
|
||||
import Foundation
|
||||
import Networking
|
||||
import TestHelpers
|
||||
import XCTest
|
||||
|
||||
@testable import AuthenticatorShared
|
||||
|
||||
// MARK: - Alert+NetworkingTests
|
||||
|
||||
class AlertNetworkingTests: BitwardenTestCase {
|
||||
// MARK: Tests
|
||||
|
||||
/// Tests the `networkConnectionLostError` alert contains the correct properties.
|
||||
func test_networkConnectionLost() {
|
||||
let urlError = URLError(.networkConnectionLost)
|
||||
let subject = Alert.networkResponseError(urlError) {}
|
||||
|
||||
XCTAssertEqual(subject.title, Localizations.internetConnectionRequiredTitle)
|
||||
XCTAssertEqual(subject.message, Localizations.internetConnectionRequiredMessage)
|
||||
XCTAssertEqual(subject.preferredStyle, .alert)
|
||||
XCTAssertEqual(subject.alertActions.count, 2)
|
||||
|
||||
let action = subject.alertActions[0]
|
||||
XCTAssertEqual(action.title, Localizations.tryAgain)
|
||||
XCTAssertEqual(action.style, .default)
|
||||
XCTAssertNotNil(action.handler)
|
||||
}
|
||||
|
||||
/// `.networkResponseError` builds an alert to display a server error message.
|
||||
func test_networkResponseError_serverError() throws {
|
||||
let response = HTTPResponse.failure(statusCode: 400, body: APITestData.bitwardenErrorMessage.data)
|
||||
let error = try ServerError.error(errorResponse: ErrorResponseModel(response: response))
|
||||
let subject = Alert.networkResponseError(error)
|
||||
|
||||
XCTAssertEqual(subject.title, Localizations.anErrorHasOccurred)
|
||||
XCTAssertEqual(subject.message, "You do not have permissions to edit this.")
|
||||
XCTAssertEqual(subject.preferredStyle, .alert)
|
||||
XCTAssertEqual(subject.alertActions.count, 1)
|
||||
|
||||
let action = subject.alertActions[0]
|
||||
XCTAssertEqual(action.title, Localizations.ok)
|
||||
XCTAssertEqual(action.style, .cancel)
|
||||
XCTAssertNil(action.handler)
|
||||
}
|
||||
|
||||
/// Tests the `internetConnectionError` alert contains the correct properties.
|
||||
func test_noInternetConnection() {
|
||||
let urlError = URLError(.notConnectedToInternet)
|
||||
let subject = Alert.networkResponseError(urlError) {}
|
||||
|
||||
XCTAssertEqual(subject.title, Localizations.internetConnectionRequiredTitle)
|
||||
XCTAssertEqual(subject.message, Localizations.internetConnectionRequiredMessage)
|
||||
XCTAssertEqual(subject.preferredStyle, .alert)
|
||||
XCTAssertEqual(subject.alertActions.count, 2)
|
||||
|
||||
let action = subject.alertActions[0]
|
||||
XCTAssertEqual(action.title, Localizations.tryAgain)
|
||||
XCTAssertEqual(action.style, .default)
|
||||
XCTAssertNotNil(action.handler)
|
||||
}
|
||||
|
||||
/// `ResponseValidationError` builds an alert to display a server error message.
|
||||
func test_responseValidationError() throws {
|
||||
let response = HTTPResponse.failure(statusCode: 400, body: APITestData.responseValidationError.data)
|
||||
let error = try ServerError.validationError(
|
||||
validationErrorResponse: ResponseValidationErrorModel(
|
||||
response: response,
|
||||
),
|
||||
)
|
||||
let subject = Alert.networkResponseError(error)
|
||||
|
||||
XCTAssertEqual(subject.title, Localizations.anErrorHasOccurred)
|
||||
XCTAssertEqual(subject.message, "Username or password is incorrect. Try again.")
|
||||
XCTAssertEqual(subject.preferredStyle, .alert)
|
||||
XCTAssertEqual(subject.alertActions.count, 1)
|
||||
|
||||
let action = subject.alertActions[0]
|
||||
XCTAssertEqual(action.title, Localizations.ok)
|
||||
XCTAssertEqual(action.style, .cancel)
|
||||
XCTAssertNil(action.handler)
|
||||
}
|
||||
|
||||
/// Tests the `timeoutError` alert contains the correct properties.
|
||||
func test_timeoutError() {
|
||||
let urlError = URLError(.timedOut)
|
||||
let subject = Alert.networkResponseError(urlError) {}
|
||||
|
||||
XCTAssertEqual(subject.message, urlError.localizedDescription)
|
||||
XCTAssertEqual(subject.preferredStyle, .alert)
|
||||
XCTAssertEqual(subject.alertActions.count, 2)
|
||||
|
||||
let action = subject.alertActions[0]
|
||||
XCTAssertEqual(action.title, Localizations.tryAgain)
|
||||
XCTAssertEqual(action.style, .default)
|
||||
XCTAssertNotNil(action.handler)
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,9 @@
|
||||
import BitwardenKit
|
||||
import BitwardenResources
|
||||
import BitwardenSdk
|
||||
import Foundation
|
||||
|
||||
// MARK: Alert+Networking
|
||||
|
||||
extension Alert {
|
||||
public extension Alert {
|
||||
/// An alert shown to the user when they aren't connected to the internet.
|
||||
///
|
||||
/// - Parameter tryAgain: An action allowing the user to retry the request.
|
||||
@ -6,8 +6,6 @@ import Networking
|
||||
import TestHelpers
|
||||
import XCTest
|
||||
|
||||
@testable import BitwardenShared
|
||||
|
||||
// MARK: - Alert+NetworkingTests
|
||||
|
||||
class AlertNetworkingTests: BitwardenTestCase {
|
||||
Loading…
x
Reference in New Issue
Block a user