pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
author Alejandro Gelos <agp@pep.security>
Tue, 20 Aug 2019 13:52:25 +0200
branchIOS-1740
changeset 9725 efdbdfc23000
parent 9723 2da4ad6faab3
child 9729 5d894b1a599f
permissions -rw-r--r--
IOS-1740 fix testSavePasswordAfterEndVerification test in AccountSettingsViewModelTest
     1 //
     2 //  AccountSettingsViewModelTest.swift
     3 //  pEpForiOSTests
     4 //
     5 //  Created by Borja González de Pablo on 22/10/2018.
     6 //  Copyright © 2018 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import XCTest
    10 import PantomimeFramework
    11 @testable import pEpForiOS
    12 @testable import MessageModel
    13 
    14 class AccountSettingsViewModelTest: CoreDataDrivenTestBase {
    15 
    16     var viewModel: AccountSettingsViewModel!
    17     var keySyncServiceHandshakeDelegateMoc: KeySyncServiceHandshakeDelegateMoc!
    18 
    19     public func testEmail() {
    20         setUpViewModel()
    21 
    22         let email = viewModel.email
    23 
    24         XCTAssertEqual(email, account.user.address)
    25     }
    26 
    27     public func testLoginName() {
    28         setUpViewModel()
    29 
    30         let loginName = viewModel.loginName
    31 
    32         XCTAssertEqual(loginName, account.imapServer?.credentials.loginName)
    33     }
    34 
    35     public func testName() {
    36         setUpViewModel()
    37 
    38         let name = viewModel.name
    39 
    40         XCTAssertEqual(account.user.userName, name)
    41     }
    42 
    43     public func testSmptServer() {
    44         setUpViewModel()
    45 
    46         let smptServer = viewModel.smtpServer
    47 
    48         XCTAssertEqual(smptServer.address, account.smtpServer?.address)
    49         let port =  account.smtpServer?.port
    50         XCTAssertNotNil(port)
    51         XCTAssertEqual(smptServer.port, "\(String(describing: port!))")
    52         XCTAssertEqual(smptServer.transport, account.smtpServer?.transport.asString())
    53     }
    54 
    55     public func testImapServer() {
    56         setUpViewModel()
    57 
    58         let imapServer = viewModel.imapServer
    59 
    60         XCTAssertEqual(imapServer.address, account.imapServer?.address)
    61         let port =  account.imapServer?.port
    62         XCTAssertNotNil(port)
    63         XCTAssertEqual(imapServer.port, "\(String(describing: port!))")
    64         XCTAssertEqual(imapServer.transport, account.imapServer?.transport.asString())
    65     }
    66 
    67     func testUpdate() {
    68         let address = "localhost"
    69         let login = "fakelogin"
    70         let name = "fakeName"
    71         let password = "fakePassword"
    72         let portString = "1"
    73         let portInt = UInt16(portString)!
    74 
    75         setUpViewModel()
    76 
    77         let server = AccountSettingsViewModel.ServerViewModel(address: address,
    78                                                               port: portString,
    79                                                               transport: "StartTls")
    80 
    81         let verifyExpectation =
    82             expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
    83 
    84         let delegate = AccountVerificationResultDelegateMock()
    85         delegate.expectationDidVerifyCalled = verifyExpectation
    86         viewModel.delegate = delegate
    87 
    88         viewModel.update(loginName: login,
    89                          name: name,
    90                          password: password,
    91                          imap: server,
    92                          smtp: server)
    93 
    94         waitForExpectations(timeout: UnitTestUtils.asyncWaitTime)
    95 
    96         guard let verifier = viewModel.verifiableAccount else {
    97             XCTFail()
    98             return
    99         }
   100 
   101         XCTAssertEqual(verifier.loginName, login)
   102         XCTAssertEqual(verifier.password, password)
   103         XCTAssertEqual(verifier.serverIMAP, address)
   104         XCTAssertEqual(verifier.serverSMTP, address)
   105         XCTAssertEqual(verifier.portIMAP, portInt)
   106         XCTAssertEqual(verifier.portSMTP, portInt)
   107         XCTAssertNil(verifier.accessToken)
   108     }
   109 
   110     public func testSectionIsValid() {
   111         setUpViewModel()
   112         //Header count in AccountSettingViewModel
   113         let headerCount = 3
   114         var validSection: Bool!
   115         for i in 0..<headerCount {
   116             validSection = viewModel.sectionIsValid(section: i)
   117             XCTAssertTrue(validSection)
   118         }
   119 
   120         validSection = viewModel.sectionIsValid(section: headerCount)
   121         XCTAssertFalse(validSection)
   122     }
   123 
   124     public func testVerified() {
   125         setUpViewModel()
   126 
   127         let verifyExpectation =
   128             expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
   129 
   130         let delegate = AccountVerificationResultDelegateMock()
   131         delegate.expectationDidVerifyCalled = verifyExpectation
   132         viewModel.delegate = delegate
   133 
   134         viewModel.didEndVerification(result: .success(()))
   135 
   136         waitForExpectations(timeout: UnitTestUtils.waitTime)
   137     }
   138 
   139     public func testSavePasswordAfterEndVerification() {
   140         // GIVEN
   141         setUpViewModel()
   142         guard let imapPort = account.imapServer?.port,
   143             let smtpPort = account.smtpServer?.port else {
   144                 XCTFail()
   145                 return
   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 
   165 
   166         // WHEN
   167         try? verifiableAccount.save { _ in
   168             savedExpectation.fulfill()
   169         }
   170 
   171         // THEN
   172         waitForExpectations(timeout: UnitTestUtils.asyncWaitTime)
   173         let actualPassword = account.imapServer?.credentials.password
   174         XCTAssertEqual(actualPassword, correctPwd)
   175     }
   176 
   177 
   178     private func setUpViewModel() {
   179         let inbox = CdFolder(context: moc)
   180         inbox.account = account.cdObject
   181         inbox.folderType = .inbox
   182         inbox.name = ImapSync.defaultImapInboxName
   183         moc.saveAndLogErrors()
   184 
   185         keySyncServiceHandshakeDelegateMoc = KeySyncServiceHandshakeDelegateMoc()
   186         let theMessageModelService = MessageModelService(
   187             errorPropagator: ErrorPropagator(),
   188             keySyncServiceDelegate: keySyncServiceHandshakeDelegateMoc, keySyncEnabled: false)
   189 
   190         viewModel = AccountSettingsViewModel(
   191             account: account,
   192             messageModelService: theMessageModelService)
   193     }
   194 }
   195 
   196 class AccountVerificationResultDelegateMock: AccountVerificationResultDelegate {
   197     static let DID_VERIFY_EXPECTATION = "DID_VERIFY_CALLED"
   198     var expectationDidVerifyCalled: XCTestExpectation?
   199     var error: Error? = nil
   200 
   201     func didVerify(result: AccountVerificationResult) {
   202         switch result {
   203         case .ok:
   204             self.error = nil
   205         case .noImapConnectData, .noSmtpConnectData:
   206             let theError = NSError(
   207                 domain: #function,
   208                 code: 777,
   209                 userInfo: [NSLocalizedDescriptionKey: "SMTP/IMAP ERROR"])
   210             self.error = theError
   211         case .imapError(let error):
   212             self.error = error
   213         case .smtpError(let error):
   214             self.error = error
   215         }
   216 
   217         guard let expectation = expectationDidVerifyCalled else {
   218             XCTFail()
   219             return
   220         }
   221         expectation.fulfill()
   222     }
   223 }