pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
branchIOS-1740
changeset 9725 efdbdfc23000
parent 9723 2da4ad6faab3
child 9729 5d894b1a599f
equal deleted inserted replaced
9724:23a57a9beeb1 9725:efdbdfc23000
     5 //  Created by Borja González de Pablo on 22/10/2018.
     5 //  Created by Borja González de Pablo on 22/10/2018.
     6 //  Copyright © 2018 p≡p Security S.A. All rights reserved.
     6 //  Copyright © 2018 p≡p Security S.A. All rights reserved.
     7 //
     7 //
     8 
     8 
     9 import XCTest
     9 import XCTest
       
    10 import PantomimeFramework
    10 @testable import pEpForiOS
    11 @testable import pEpForiOS
    11 @testable import MessageModel
    12 @testable import MessageModel
    12 
    13 
    13 class AccountSettingsViewModelTest: CoreDataDrivenTestBase {
    14 class AccountSettingsViewModelTest: CoreDataDrivenTestBase {
    14 
    15 
   136     }
   137     }
   137 
   138 
   138     public func testSavePasswordAfterEndVerification() {
   139     public func testSavePasswordAfterEndVerification() {
   139         // GIVEN
   140         // GIVEN
   140         setUpViewModel()
   141         setUpViewModel()
   141         let successResult: Result<Void, Error> = .success(())
   142         guard let imapPort = account.imapServer?.port,
   142         let expectedPassword = "passwordChanged"
   143             let smtpPort = account.smtpServer?.port else {
   143         let imap = AccountSettingsViewModel.ServerViewModel(address: nil, port: nil, transport: nil)
   144                 XCTFail()
   144         let smtp = AccountSettingsViewModel.ServerViewModel(address: nil, port: nil, transport: nil)
   145                 return
   145         viewModel.update(loginName: "", name: "", password: expectedPassword, imap: imap, smtp: smtp)
   146         }
       
   147         let correctPwd = account.imapServer?.credentials.password
       
   148         let wrongPwd = "Wrong Password"
       
   149         account.imapServer?.credentials.password = wrongPwd
       
   150 
       
   151         let savedExpectation = expectation(description: "Did save expectation")
       
   152         let verifiableAccount = VerifiableAccount(messageModelService: viewModel.messageModelService,
       
   153                                                   address: account.user.address,
       
   154                                                   userName: account.user.userName,
       
   155                                                   loginName: account.imapServer!.credentials.loginName,
       
   156                                                   password: correctPwd,
       
   157                                                   serverIMAP: account.imapServer?.address,
       
   158                                                   portIMAP: imapPort,
       
   159                                                   transportIMAP: ConnectionTransport.init(transport: account.imapServer!.transport),
       
   160                                                   serverSMTP: account.smtpServer?.address,
       
   161                                                   portSMTP: smtpPort,
       
   162                                                   transportSMTP: ConnectionTransport.init(transport: account.smtpServer!.transport),
       
   163                                                   automaticallyTrustedImapServer: true)
       
   164 
   146 
   165 
   147         // WHEN
   166         // WHEN
   148         viewModel.didEndVerification(result: successResult)
   167         try? verifiableAccount.save { _ in
       
   168             savedExpectation.fulfill()
       
   169         }
   149 
   170 
   150         // THEN
   171         // THEN
   151         let connectionInfo = account.cdObject.imapConnectInfo
   172         waitForExpectations(timeout: UnitTestUtils.asyncWaitTime)
   152         let actualPassword = connectionInfo?.loginPassword
   173         let actualPassword = account.imapServer?.credentials.password
   153         XCTAssertEqual(actualPassword, expectedPassword)
   174         XCTAssertEqual(actualPassword, correctPwd)
   154     }
   175     }
   155 
   176 
   156 
   177 
   157     private func setUpViewModel() {
   178     private func setUpViewModel() {
   158         account.save()
   179         let inbox = CdFolder(context: moc)
       
   180         inbox.account = account.cdObject
       
   181         inbox.folderType = .inbox
       
   182         inbox.name = ImapSync.defaultImapInboxName
       
   183         moc.saveAndLogErrors()
       
   184 
   159         keySyncServiceHandshakeDelegateMoc = KeySyncServiceHandshakeDelegateMoc()
   185         keySyncServiceHandshakeDelegateMoc = KeySyncServiceHandshakeDelegateMoc()
   160         let theMessageModelService = MessageModelService(
   186         let theMessageModelService = MessageModelService(
   161             errorPropagator: ErrorPropagator(),
   187             errorPropagator: ErrorPropagator(),
   162             keySyncServiceDelegate: keySyncServiceHandshakeDelegateMoc, keySyncEnabled: false)
   188             keySyncServiceDelegate: keySyncServiceHandshakeDelegateMoc, keySyncEnabled: false)
   163 
   189