Skip to content

Commit

Permalink
[PM-8216] Add Change Email and Set Up Two-Factor URLs (#1210)
Browse files Browse the repository at this point in the history
  • Loading branch information
KatherineInCode authored Dec 16, 2024
1 parent 361cc87 commit 4a78df8
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ struct EnvironmentUrlData: Codable, Equatable, Hashable {
extension EnvironmentUrlData {
// MARK: Properties

/// The URL for the user to change their email.
var changeEmailURL: URL? {
subpageURL(additionalPath: "settings/account")
}

/// The base url for importing items.
var importItemsURL: URL? {
subpageUrl(additionalPath: "tools/import")
subpageURL(additionalPath: "tools/import")
}

/// Whether all of the environment URLs are not set.
Expand All @@ -78,8 +83,8 @@ extension EnvironmentUrlData {
}

/// The URL for the recovery code help page.
var recoveryCodeUrl: URL? {
subpageUrl(additionalPath: "recover-2fa")
var recoveryCodeURL: URL? {
subpageURL(additionalPath: "recover-2fa")
}

/// Gets the region depending on the base url.
Expand All @@ -99,12 +104,17 @@ extension EnvironmentUrlData {
guard region != .unitedStates else {
return URL(string: "https://send.bitwarden.com/#")!
}
return subpageUrl(additionalPath: "send")
return subpageURL(additionalPath: "send")
}

/// The base url for the settings screen.
var settingsURL: URL? {
subpageUrl(additionalPath: "settings")
subpageURL(additionalPath: "settings")
}

/// The URL to set up two-factor login.
var setUpTwoFactorURL: URL? {
subpageURL(additionalPath: "settings/security/two-factor")
}

/// The host of URL to the user's web vault.
Expand All @@ -119,7 +129,7 @@ extension EnvironmentUrlData {
///
/// - Parameters:
/// - additionalPath: The additional path string to append to the vault's base URL
private func subpageUrl(additionalPath: String) -> URL? {
private func subpageURL(additionalPath: String) -> URL? {
// Foundation's URL appending methods percent encode the path component that is passed into the method,
// which includes the `#` symbol. Since the `#` character is a critical portion of these urls, we use String
// concatenation to get around this limitation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ import XCTest
class EnvironmentUrlDataTests: XCTestCase {
// MARK: Tests

/// `changeEmailURL` returns the change email URL for the base URL.
func test_changeEmailURL_baseURL() {
let subject = EnvironmentUrlData(base: URL(string: "https://vault.example.com"))
XCTAssertEqual(subject.changeEmailURL?.absoluteString, "https://vault.example.com/#/settings/account")
}

/// `changeEmailURL` returns the default change email base URL.
func test_changeEmailURL_noURLs() {
let subject = EnvironmentUrlData(base: nil, webVault: nil)
XCTAssertNil(subject.changeEmailURL?.absoluteString)
}

/// `changeEmailURL` returns the change email URL for the web vault URL.
func test_changeEmailURL_webVaultURL() {
let subject = EnvironmentUrlData(
base: URL(string: "https://vault.example.com"),
webVault: URL(string: "https://web.vault.example.com")
)
XCTAssertEqual(subject.changeEmailURL?.absoluteString, "https://web.vault.example.com/#/settings/account")
}

/// `defaultUS` returns the properly configured `EnvironmentUrlData`
/// with the deafult Urls for united states region.
func test_defaultUS() {
Expand Down Expand Up @@ -39,19 +60,19 @@ class EnvironmentUrlDataTests: XCTestCase {
)
}

/// `importItemsURL` returns the import items url for the base url.
/// `importItemsURL` returns the import items URL for the base URL.
func test_importItemsURL_baseURL() {
let subject = EnvironmentUrlData(base: URL(string: "https://vault.example.com"))
XCTAssertEqual(subject.importItemsURL?.absoluteString, "https://vault.example.com/#/tools/import")
}

/// `importItemsURL` returns the default import items base url.
/// `importItemsURL` returns the default import items base URL.
func test_importItemsURL_noURLs() {
let subject = EnvironmentUrlData(base: nil, webVault: nil)
XCTAssertNil(subject.importItemsURL?.absoluteString)
}

/// `importItemsURL` returns the import items url for the web vault url.
/// `importItemsURL` returns the import items URL for the web vault URL.
func test_importItemsURL_webVaultURL() {
let subject = EnvironmentUrlData(
base: URL(string: "https://vault.example.com"),
Expand All @@ -76,49 +97,70 @@ class EnvironmentUrlDataTests: XCTestCase {
XCTAssertFalse(EnvironmentUrlData(webVault: .example).isEmpty)
}

/// `region` returns `.unitedStates` if base url is the same as the default for US.
/// `recoveryCodeURL` returns the recovery code URL for the base URL.
func test_recoveryCodeURL_baseURL() {
let subject = EnvironmentUrlData(base: URL(string: "https://vault.example.com"))
XCTAssertEqual(subject.recoveryCodeURL?.absoluteString, "https://vault.example.com/#/recover-2fa")
}

/// `recoveryCodeURL` returns the default settings base URL.
func test_recoveryCodeURL_noURLs() {
let subject = EnvironmentUrlData(base: nil, webVault: nil)
XCTAssertNil(subject.recoveryCodeURL?.absoluteString)
}

/// `recoveryCodeURL` returns the settings URL for the web vault URL.
func test_recoveryCodeURL_webVaultURL() {
let subject = EnvironmentUrlData(
base: URL(string: "https://vault.example.com"),
webVault: URL(string: "https://web.vault.example.com")
)
XCTAssertEqual(subject.recoveryCodeURL?.absoluteString, "https://web.vault.example.com/#/recover-2fa")
}

/// `region` returns `.unitedStates` if base URL is the same as the default for US.
func test_region_unitedStates() {
let subject = EnvironmentUrlData(base: URL(string: "https://vault.bitwarden.com")!)
XCTAssertTrue(subject.region == .unitedStates)
}

/// `region` returns `.europe` if base url is the same as the default for EU.
/// `region` returns `.europe` if base URL is the same as the default for EU.
func test_region_europe() {
let subject = EnvironmentUrlData(base: URL(string: "https://vault.bitwarden.eu")!)
XCTAssertTrue(subject.region == .europe)
}

/// `region` returns `.selfHosted` if base url is neither the default for US nor for EU.
/// `region` returns `.selfHosted` if base URL is neither the default for US nor for EU.
func test_region_selfHost() {
let subject = EnvironmentUrlData(base: URL(string: "https://example.com")!)
XCTAssertTrue(subject.region == .selfHosted)
}

/// `sendShareURL` returns the send url for the united states region.
/// `sendShareURL` returns the send URL for the united states region.
func test_sendShareURL_unitedStates() {
let subject = EnvironmentUrlData.defaultUS
XCTAssertEqual(subject.sendShareURL?.absoluteString, "https://send.bitwarden.com/#")
}

/// `sendShareURL` returns the send url for the europe region.
/// `sendShareURL` returns the send URL for the europe region.
func test_sendShareURL_europe() {
let subject = EnvironmentUrlData.defaultEU
XCTAssertEqual(subject.sendShareURL?.absoluteString, "https://vault.bitwarden.eu/#/send")
}

/// `sendShareURL` returns the send url for the base url.
/// `sendShareURL` returns the send URL for the base URL.
func test_sendShareURL_baseURL() {
let subject = EnvironmentUrlData(base: URL(string: "https://vault.example.com"))
XCTAssertEqual(subject.sendShareURL?.absoluteString, "https://vault.example.com/#/send")
}

/// `sendShareURL` returns the default send base url.
/// `sendShareURL` returns the default send base URL.
func test_sendShareURL_noURLs() {
let subject = EnvironmentUrlData(base: nil, webVault: nil)
XCTAssertNil(subject.sendShareURL?.absoluteString)
}

/// `sendShareURL` returns the send url for the web vault url.
/// `sendShareURL` returns the send URL for the web vault URL.
func test_sendShareURL_webVaultURL() {
let subject = EnvironmentUrlData(
base: URL(string: "https://vault.example.com"),
Expand All @@ -127,19 +169,19 @@ class EnvironmentUrlDataTests: XCTestCase {
XCTAssertEqual(subject.sendShareURL?.absoluteString, "https://web.vault.example.com/#/send")
}

/// `settingsURL` returns the settings url for the base url.
/// `settingsURL` returns the settings URL for the base URL.
func test_settingsURL_baseURL() {
let subject = EnvironmentUrlData(base: URL(string: "https://vault.example.com"))
XCTAssertEqual(subject.settingsURL?.absoluteString, "https://vault.example.com/#/settings")
}

/// `settingsURL` returns the default settings base url.
/// `settingsURL` returns the default settings base URL.
func test_settingsURL_noURLs() {
let subject = EnvironmentUrlData(base: nil, webVault: nil)
XCTAssertNil(subject.settingsURL?.absoluteString)
}

/// `settingsURL` returns the settings url for the web vault url.
/// `settingsURL` returns the settings URL for the web vault URL.
func test_settingsURL_webVaultURL() {
let subject = EnvironmentUrlData(
base: URL(string: "https://vault.example.com"),
Expand All @@ -148,6 +190,27 @@ class EnvironmentUrlDataTests: XCTestCase {
XCTAssertEqual(subject.settingsURL?.absoluteString, "https://web.vault.example.com/#/settings")
}

/// `setUpTwoFactorURL` returns the change email URL for the base URL.
func test_setUpTwoFactorURL_baseURL() {
let subject = EnvironmentUrlData(base: URL(string: "https://vault.example.com"))
XCTAssertEqual(subject.setUpTwoFactorURL?.absoluteString, "https://vault.example.com/#/settings/security/two-factor")
}

/// `setUpTwoFactorURL` returns the default change email base URL.
func test_setUpTwoFactorURL_noURLs() {
let subject = EnvironmentUrlData(base: nil, webVault: nil)
XCTAssertNil(subject.setUpTwoFactorURL?.absoluteString)
}

/// `setUpTwoFactorURL` returns the change email URL for the web vault URL.
func test_setUpTwoFactorURL_webVaultURL() {
let subject = EnvironmentUrlData(
base: URL(string: "https://vault.example.com"),
webVault: URL(string: "https://web.vault.example.com")
)
XCTAssertEqual(subject.setUpTwoFactorURL?.absoluteString, "https://web.vault.example.com/#/settings/security/two-factor")
}

/// `webVaultHost` returns the host for the base URL if no web vault URL is set.
func test_webVaultHost_baseURL() {
let subject = EnvironmentUrlData(base: URL(string: "https://vault.example.com"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ struct EnvironmentUrls: Equatable {
/// The base URL.
let baseURL: URL

/// The URL for changing email address.
let changeEmailURL: URL

/// The URL for the events API.
let eventsURL: URL

Expand All @@ -32,6 +35,9 @@ struct EnvironmentUrls: Equatable {
/// The URL for vault settings.
let settingsURL: URL

/// The URL for setting up two-factor login.
let setUpTwoFactorURL: URL

/// The URL for the web vault.
let webVaultURL: URL
}
Expand Down Expand Up @@ -65,10 +71,12 @@ extension EnvironmentUrls {
webVaultURL = environmentUrlData.webVault ?? URL(string: "https://vault.bitwarden.com")!
}
importItemsURL = environmentUrlData.importItemsURL ?? URL(string: "https://vault.bitwarden.com/#/tools/import")!
recoveryCodeURL = environmentUrlData.recoveryCodeUrl ?? URL(
recoveryCodeURL = environmentUrlData.recoveryCodeURL ?? URL(
string: "https://vault.bitwarden.com/#/recover-2fa"
)!
sendShareURL = environmentUrlData.sendShareURL ?? URL(string: "https://send.bitwarden.com/#")!
settingsURL = environmentUrlData.settingsURL ?? webVaultURL
changeEmailURL = environmentUrlData.changeEmailURL ?? settingsURL
setUpTwoFactorURL = environmentUrlData.setUpTwoFactorURL ?? settingsURL
}
}
Loading

0 comments on commit 4a78df8

Please sign in to comment.