@@ -6,6 +6,7 @@ class EnvironmentServiceTests: XCTestCase {
6
6
// MARK: Properties
7
7
8
8
var stateService : MockStateService !
9
+ var standardUserDefaults : UserDefaults !
9
10
var subject : EnvironmentService !
10
11
11
12
// MARK: Setup & Teardown
@@ -14,14 +15,20 @@ class EnvironmentServiceTests: XCTestCase {
14
15
super. setUp ( )
15
16
16
17
stateService = MockStateService ( )
18
+ standardUserDefaults = UserDefaults ( suiteName: " test " )
19
+ standardUserDefaults. removeObject ( forKey: " com.apple.configuration.managed " )
17
20
18
- subject = DefaultEnvironmentService ( stateService: stateService)
21
+ subject = DefaultEnvironmentService (
22
+ stateService: stateService,
23
+ standardUserDefaults: standardUserDefaults
24
+ )
19
25
}
20
26
21
27
override func tearDown( ) {
22
28
super. tearDown ( )
23
29
24
30
stateService = nil
31
+ standardUserDefaults = nil
25
32
subject = nil
26
33
}
27
34
@@ -82,6 +89,55 @@ class EnvironmentServiceTests: XCTestCase {
82
89
XCTAssertEqual ( stateService. preAuthEnvironmentUrls, urls)
83
90
}
84
91
92
+ /// `loadURLsForActiveAccount()` loads the managed config URLs.
93
+ func test_loadURLsForActiveAccount_managedConfig( ) async throws {
94
+ standardUserDefaults. setValue (
95
+ [ " baseEnvironmentUrl " : " https://vault.example.com " ] ,
96
+ forKey: " com.apple.configuration.managed "
97
+ )
98
+
99
+ await subject. loadURLsForActiveAccount ( )
100
+
101
+ let urls = try EnvironmentUrlData ( base: XCTUnwrap ( URL ( string: " https://vault.example.com " ) ) )
102
+ XCTAssertEqual ( subject. apiURL, URL ( string: " https://vault.example.com/api " ) )
103
+ XCTAssertEqual ( subject. eventsURL, URL ( string: " https://vault.example.com/events " ) )
104
+ XCTAssertEqual ( subject. iconsURL, URL ( string: " https://vault.example.com/icons " ) )
105
+ XCTAssertEqual ( subject. identityURL, URL ( string: " https://vault.example.com/identity " ) )
106
+ XCTAssertEqual ( subject. importItemsURL, URL ( string: " https://vault.example.com/#/tools/import " ) )
107
+ XCTAssertEqual ( subject. region, . selfHosted)
108
+ XCTAssertEqual ( subject. sendShareURL, URL ( string: " https://vault.example.com/#/send " ) )
109
+ XCTAssertEqual ( subject. settingsURL, URL ( string: " https://vault.example.com/#/settings " ) )
110
+ XCTAssertEqual ( subject. webVaultURL, URL ( string: " https://vault.example.com " ) )
111
+ XCTAssertEqual ( stateService. preAuthEnvironmentUrls, urls)
112
+ }
113
+
114
+ /// `loadURLsForActiveAccount()` doesn't load the managed config URLs if there's an active
115
+ /// account, but sets the pre-auth URLs to the managed config URLs.
116
+ func test_loadURLsForActiveAccount_managedConfigActiveAccount( ) async throws {
117
+ let account = Account . fixture ( )
118
+ stateService. activeAccount = account
119
+ stateService. environmentUrls [ account. profile. userId] = . defaultUS
120
+ standardUserDefaults. setValue (
121
+ [ " baseEnvironmentUrl " : " https://vault.example.com " ] ,
122
+ forKey: " com.apple.configuration.managed "
123
+ )
124
+
125
+ await subject. loadURLsForActiveAccount ( )
126
+
127
+ XCTAssertEqual ( subject. apiURL, URL ( string: " https://vault.bitwarden.com/api " ) )
128
+ XCTAssertEqual ( subject. eventsURL, URL ( string: " https://vault.bitwarden.com/events " ) )
129
+ XCTAssertEqual ( subject. iconsURL, URL ( string: " https://vault.bitwarden.com/icons " ) )
130
+ XCTAssertEqual ( subject. identityURL, URL ( string: " https://vault.bitwarden.com/identity " ) )
131
+ XCTAssertEqual ( subject. importItemsURL, URL ( string: " https://vault.bitwarden.com/#/tools/import " ) )
132
+ XCTAssertEqual ( subject. region, . unitedStates)
133
+ XCTAssertEqual ( subject. sendShareURL, URL ( string: " https://vault.bitwarden.com/#/send " ) )
134
+ XCTAssertEqual ( subject. settingsURL, URL ( string: " https://vault.bitwarden.com/#/settings " ) )
135
+ XCTAssertEqual ( subject. webVaultURL, URL ( string: " https://vault.bitwarden.com " ) )
136
+
137
+ let urls = try EnvironmentUrlData ( base: XCTUnwrap ( URL ( string: " https://vault.example.com " ) ) )
138
+ XCTAssertEqual ( stateService. preAuthEnvironmentUrls, urls)
139
+ }
140
+
85
141
/// `loadURLsForActiveAccount()` loads the default URLs if there's no active account
86
142
/// and no preauth URLs.
87
143
func test_loadURLsForActiveAccount_noAccount( ) async {
0 commit comments