@@ -8,6 +8,13 @@ import XCTest
88///
99final class AlamofireNetworkTests : XCTestCase {
1010 private var responseDataSubscription : AnyCancellable ?
11+ private var userDefaults : UserDefaults !
12+ private let userDefaultsKey = " alamofireNetworkTestsKey "
13+
14+ override func setUp( ) {
15+ super. setUp ( )
16+ userDefaults = UserDefaults ( suiteName: UUID ( ) . uuidString)
17+ }
1118
1219 // MARK: - `responseData` with data and error in the callback
1320
@@ -167,6 +174,83 @@ final class AlamofireNetworkTests: XCTestCase {
167174 // Then
168175 XCTAssertTrue ( result. isSuccess)
169176 }
177+
178+ // MARK: - `didFailToAuthenticateRequestWithAppPassword`
179+
180+ func test_didFailToAuthenticateRequestWithAppPassword_notSupported_adds_siteID_to_unsupported_list( ) {
181+ // Given
182+ let siteID : Int64 = 123
183+ let network = AlamofireNetwork ( credentials: nil , userDefaults: userDefaults)
184+ XCTAssertTrue ( userDefaults. applicationPasswordUnsupportedList. isEmpty)
185+
186+ // When
187+ network. didFailToAuthenticateRequestWithAppPassword ( siteID: siteID, reason: . notSupported)
188+
189+ // Then
190+ XCTAssertEqual ( userDefaults. applicationPasswordUnsupportedList, [ siteID] )
191+ }
192+
193+ func test_didFailToAuthenticateRequestWithAppPassword_notSupported_appends_to_existing_unsupported_list( ) {
194+ // Given
195+ let existingSiteID : Int64 = 456
196+ let newSiteID : Int64 = 123
197+ userDefaults. applicationPasswordUnsupportedList = [ existingSiteID]
198+ let network = AlamofireNetwork ( credentials: nil , userDefaults: userDefaults)
199+
200+ // When
201+ network. didFailToAuthenticateRequestWithAppPassword ( siteID: newSiteID, reason: . notSupported)
202+
203+ // Then
204+ XCTAssertEqual ( userDefaults. applicationPasswordUnsupportedList, [ existingSiteID, newSiteID] )
205+ }
206+
207+ func test_didFailToAuthenticateRequestWithAppPassword_unknown_below_threshold_does_not_add_to_unsupported_list( ) {
208+ // Given
209+ let siteID : Int64 = 123
210+ let network = AlamofireNetwork ( credentials: nil , userDefaults: userDefaults)
211+ XCTAssertTrue ( userDefaults. applicationPasswordUnsupportedList. isEmpty)
212+
213+ // When - Call 9 times (below threshold of 10)
214+ for _ in 1 ... 9 {
215+ network. didFailToAuthenticateRequestWithAppPassword ( siteID: siteID, reason: . unknown)
216+ }
217+
218+ // Then
219+ XCTAssertTrue ( userDefaults. applicationPasswordUnsupportedList. isEmpty)
220+ }
221+
222+ func test_didFailToAuthenticateRequestWithAppPassword_unknown_at_threshold_adds_siteID_to_unsupported_list( ) {
223+ // Given
224+ let siteID : Int64 = 123
225+ let network = AlamofireNetwork ( credentials: nil , userDefaults: userDefaults)
226+ XCTAssertTrue ( userDefaults. applicationPasswordUnsupportedList. isEmpty)
227+
228+ // When - Call exactly 10 times (threshold)
229+ for _ in 1 ... 10 {
230+ network. didFailToAuthenticateRequestWithAppPassword ( siteID: siteID, reason: . unknown)
231+ }
232+
233+ // Then
234+ XCTAssertEqual ( userDefaults. applicationPasswordUnsupportedList, [ siteID] )
235+ }
236+
237+ func test_didFailToAuthenticateRequestWithAppPassword_unknown_multiple_sites_tracks_separately( ) {
238+ // Given
239+ let siteID1 : Int64 = 123
240+ let siteID2 : Int64 = 456
241+ let network = AlamofireNetwork ( credentials: nil , userDefaults: userDefaults)
242+
243+ // When - Call site1 5 times, site2 10 times
244+ for _ in 1 ... 5 {
245+ network. didFailToAuthenticateRequestWithAppPassword ( siteID: siteID1, reason: . unknown)
246+ }
247+ for _ in 1 ... 10 {
248+ network. didFailToAuthenticateRequestWithAppPassword ( siteID: siteID2, reason: . unknown)
249+ }
250+
251+ // Then - Only site2 should be in unsupported list
252+ XCTAssertEqual ( userDefaults. applicationPasswordUnsupportedList, [ siteID2] )
253+ }
170254}
171255
172256private extension AlamofireNetworkTests {
0 commit comments