@@ -668,6 +668,142 @@ final class AlamofireNetworkTests: XCTestCase {
668668 let networkError = result. 1 as? NetworkError
669669 XCTAssertEqual ( networkError? . errorCode, " failed " )
670670 }
671+
672+ // MARK: - Authentication Mode Tests
673+
674+ func test_authenticationMode_is_appPasswords_for_wporg_credentials( ) {
675+ // Given
676+ let wporgCredentials = Credentials . wporg ( username: " user " , password: " pass " , siteAddress: " https://example.com " )
677+
678+ // When
679+ let network = AlamofireNetwork ( credentials: wporgCredentials, sessionManager: createSessionWithMockURLProtocol ( ) )
680+
681+ // Then
682+ let expectation = XCTestExpectation ( description: " Authentication mode should be set " )
683+ DispatchQueue . main. async {
684+ XCTAssertEqual ( network. authenticationMode, . appPasswords)
685+ expectation. fulfill ( )
686+ }
687+ wait ( for: [ expectation] , timeout: 1.0 )
688+ }
689+
690+ func test_authenticationMode_is_appPasswords_for_applicationPassword_credentials( ) {
691+ // Given
692+ let appPasswordCredentials = Credentials . applicationPassword ( username: " user " , password: " pass " , siteAddress: " https://example.com " )
693+
694+ // When
695+ let network = AlamofireNetwork ( credentials: appPasswordCredentials, sessionManager: createSessionWithMockURLProtocol ( ) )
696+
697+ // Then
698+ let expectation = XCTestExpectation ( description: " Authentication mode should be set " )
699+ DispatchQueue . main. async {
700+ XCTAssertEqual ( network. authenticationMode, . appPasswords)
701+ expectation. fulfill ( )
702+ }
703+ wait ( for: [ expectation] , timeout: 1.0 )
704+ }
705+
706+ func test_authenticationMode_is_jetpackTunnel_for_wpcom_credentials( ) {
707+ // Given
708+ let wpcomCredentials = createWPComCredentials ( )
709+
710+ // When
711+ let network = AlamofireNetwork ( credentials: wpcomCredentials, sessionManager: createSessionWithMockURLProtocol ( ) )
712+
713+ // Then
714+ let expectation = XCTestExpectation ( description: " Authentication mode should be set " )
715+ DispatchQueue . main. async {
716+ XCTAssertEqual ( network. authenticationMode, . jetpackTunnel)
717+ expectation. fulfill ( )
718+ }
719+ wait ( for: [ expectation] , timeout: 1.0 )
720+ }
721+
722+ func test_authenticationMode_is_nil_for_no_credentials( ) {
723+ // When
724+ let network = AlamofireNetwork ( credentials: nil , sessionManager: createSessionWithMockURLProtocol ( ) )
725+
726+ // Then
727+ let expectation = XCTestExpectation ( description: " Authentication mode should be set " )
728+ DispatchQueue . main. async {
729+ XCTAssertNil ( network. authenticationMode)
730+ expectation. fulfill ( )
731+ }
732+ wait ( for: [ expectation] , timeout: 1.0 )
733+ }
734+
735+ func test_authenticationMode_changes_to_appPasswordsWithJetpack_when_app_password_switching_enabled( ) {
736+ // Given
737+ let siteID : Int64 = 123
738+ let wpcomCredentials = createWPComCredentials ( )
739+ let network = createNetworkWithSelectedSite ( siteID: siteID, credentials: wpcomCredentials, userDefaults: userDefaults)
740+
741+ // When - Enable app password switching
742+ network. updateAppPasswordSwitching ( enabled: true )
743+
744+ // Then
745+ let expectation = XCTestExpectation ( description: " Authentication mode should change " )
746+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) {
747+ XCTAssertEqual ( network. authenticationMode, . appPasswordsWithJetpack)
748+ expectation. fulfill ( )
749+ }
750+ wait ( for: [ expectation] , timeout: 1.0 )
751+ }
752+
753+ func test_authenticationMode_reverts_to_jetpackTunnel_when_app_password_switching_disabled( ) {
754+ // Given
755+ let siteID : Int64 = 456
756+ let wpcomCredentials = createWPComCredentials ( )
757+ let network = createNetworkWithSelectedSite ( siteID: siteID, credentials: wpcomCredentials, userDefaults: userDefaults)
758+
759+ // When - Enable then disable app password switching
760+ network. updateAppPasswordSwitching ( enabled: true )
761+ network. updateAppPasswordSwitching ( enabled: false )
762+
763+ // Then
764+ let expectation = XCTestExpectation ( description: " Authentication mode should revert " )
765+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) {
766+ XCTAssertEqual ( network. authenticationMode, . jetpackTunnel)
767+ expectation. fulfill ( )
768+ }
769+ wait ( for: [ expectation] , timeout: 1.0 )
770+ }
771+
772+ func test_authenticationMode_remains_jetpackTunnel_when_site_flagged_as_unsupported( ) {
773+ // Given
774+ let siteID : Int64 = 789
775+ let wpcomCredentials = createWPComCredentials ( )
776+ userDefaults. applicationPasswordUnsupportedList = [ String ( siteID) : Date ( ) ]
777+ let network = createNetworkWithSelectedSite ( siteID: siteID, credentials: wpcomCredentials, userDefaults: userDefaults)
778+
779+ // When - Enable app password switching for an unsupported site
780+ network. updateAppPasswordSwitching ( enabled: true )
781+
782+ // Then
783+ let expectation = XCTestExpectation ( description: " Authentication mode should remain jetpackTunnel " )
784+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) {
785+ XCTAssertEqual ( network. authenticationMode, . jetpackTunnel)
786+ expectation. fulfill ( )
787+ }
788+ wait ( for: [ expectation] , timeout: 1.0 )
789+ }
790+
791+ func test_authenticationMode_does_not_change_for_non_wpcom_credentials( ) {
792+ // Given
793+ let wporgCredentials = Credentials . wporg ( username: " user " , password: " pass " , siteAddress: " https://example.com " )
794+ let network = AlamofireNetwork ( credentials: wporgCredentials, sessionManager: createSessionWithMockURLProtocol ( ) )
795+
796+ // When - Try to enable app password switching (should have no effect)
797+ network. updateAppPasswordSwitching ( enabled: true )
798+
799+ // Then
800+ let expectation = XCTestExpectation ( description: " Authentication mode should remain unchanged " )
801+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) {
802+ XCTAssertEqual ( network. authenticationMode, . appPasswords)
803+ expectation. fulfill ( )
804+ }
805+ wait ( for: [ expectation] , timeout: 1.0 )
806+ }
671807}
672808
673809private extension AlamofireNetworkTests {
0 commit comments