pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
author Dirk Zimmermann <dz@pep.security>
Thu, 25 Apr 2019 16:25:40 +0200
branchIOS-1542
changeset 8381 907e287ead66
parent 8368 94cad6139b72
child 8384 9404686a5d53
permissions -rw-r--r--
IOS-1542 AccountVerificationServiceDelegate not used anymore.
     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 @testable import pEpForiOS
    11 @testable import MessageModel
    12 
    13 class AccountSettingsViewModelTest: CoreDataDrivenTestBase {
    14 
    15     var viewModel: AccountSettingsViewModel!
    16 
    17     public func testEmail() {
    18         setUpViewModel()
    19 
    20         let email = viewModel.email
    21 
    22         XCTAssertEqual(email, account.user.address)
    23     }
    24 
    25     public func testLoginName() {
    26         setUpViewModel()
    27 
    28         let loginName = viewModel.loginName
    29 
    30         XCTAssertEqual(loginName, account.server(with: .imap)?.credentials.loginName)
    31     }
    32 
    33     public func testName() {
    34         setUpViewModel()
    35 
    36         let name = viewModel.name
    37 
    38         XCTAssertEqual(account.user.userName, name)
    39     }
    40 
    41     public func testSmptServer() {
    42         setUpViewModel()
    43 
    44         let smptServer = viewModel.smtpServer
    45 
    46         XCTAssertEqual(smptServer.address, account.smtpServer?.address)
    47         let port =  account.smtpServer?.port
    48         XCTAssertNotNil(port)
    49         XCTAssertEqual(smptServer.port, "\(String(describing: port!))")
    50         XCTAssertEqual(smptServer.transport, account.smtpServer?.transport?.asString())
    51     }
    52 
    53     public func testImapServer() {
    54         setUpViewModel()
    55 
    56         let imapServer = viewModel.imapServer
    57 
    58         XCTAssertEqual(imapServer.address, account.imapServer?.address)
    59         let port =  account.imapServer?.port
    60         XCTAssertNotNil(port)
    61         XCTAssertEqual(imapServer.port, "\(String(describing: port!))")
    62         XCTAssertEqual(imapServer.transport, account.imapServer?.transport?.asString())
    63     }
    64 
    65     func testUpdate() {
    66         let address = "fakeAddress"
    67         let login = "fakelogin"
    68         let name = "fakeName"
    69         let password = "fakePassword"
    70 
    71         setUpViewModel()
    72 
    73         let server = AccountSettingsViewModel.ServerViewModel(address: address,
    74                                                               port: "123",
    75                                                               transport: "StartTls")
    76 
    77         let verifyExpectation =
    78             expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
    79 
    80         let delegate = AccountVerificationResultDelegateMock()
    81         delegate.expectationDidVerifyCalled = verifyExpectation
    82         viewModel.delegate = delegate
    83 
    84         viewModel.update(loginName: login,
    85                          name: name,
    86                          password: password,
    87                          imap: server,
    88                          smtp: server)
    89 
    90         waitForExpectations(timeout: UnitTestUtils.asyncWaitTime)
    91 
    92         // TODO: What to test here?
    93         /*
    94         let smtp = viewModel.account.smtpServer
    95         let imap = viewModel.account.imapServer
    96 
    97         XCTAssertEqual(smtp?.credentials.loginName, login)
    98         XCTAssertEqual(smtp?.credentials.password, password)
    99         XCTAssertEqual(imap?.credentials.loginName, login)
   100         XCTAssertEqual(imap?.credentials.password, password)
   101 
   102         XCTAssertEqual(imap?.address, address)
   103         XCTAssertEqual(imap?.port, 123)
   104         XCTAssertEqual(imap?.transport, .startTls)
   105 
   106         XCTAssertEqual(smtp?.address, address)
   107         XCTAssertEqual(smtp?.port, 123)
   108         XCTAssertEqual(smtp?.transport, .startTls)
   109          */
   110     }
   111 
   112     public func testSectionIsValid() {
   113         setUpViewModel()
   114         //Header count in AccountSettingViewModel
   115         let headerCount = 3
   116         var validSection: Bool!
   117         for i in 0..<headerCount {
   118             validSection = viewModel.sectionIsValid(section: i)
   119             XCTAssertTrue(validSection)
   120         }
   121 
   122         validSection = viewModel.sectionIsValid(section: headerCount)
   123         XCTAssertFalse(validSection)
   124     }
   125 
   126     public func testVerified() {
   127         setUpViewModel()
   128 
   129         let verifyExpectation =
   130             expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
   131 
   132         let delegate = AccountVerificationResultDelegateMock()
   133         delegate.expectationDidVerifyCalled = verifyExpectation
   134         viewModel.delegate = delegate
   135 
   136         viewModel.didEndVerification(result: .success(()))
   137 
   138         waitForExpectations(timeout: UnitTestUtils.waitTime)
   139     }
   140 
   141 
   142     private func setUpViewModel() {
   143         account.save()
   144         viewModel = AccountSettingsViewModel(account: account)
   145     }
   146 }
   147 
   148 class AccountVerificationResultDelegateMock: AccountVerificationResultDelegate {
   149     static let DID_VERIFY_EXPECTATION = "DID_VERIFY_CALLED"
   150     var expectationDidVerifyCalled: XCTestExpectation?
   151     var error: Error? = nil
   152 
   153     func didVerify(result: AccountVerificationResult, accountInput: VerifiableAccountProtocol?) {
   154         switch result {
   155         case .ok:
   156             self.error = nil
   157         case .noImapConnectData, .noSmtpConnectData:
   158             let theError = NSError(
   159                 domain: #function,
   160                 code: 777,
   161                 userInfo: [NSLocalizedDescriptionKey: "SMTP/IMAP ERROR"])
   162             self.error = theError
   163         case .imapError(let error):
   164             self.error = error
   165         case .smtpError(let error):
   166             self.error = error
   167         }
   168 
   169         guard let expectation = expectationDidVerifyCalled else {
   170             XCTFail()
   171             return
   172         }
   173         expectation.fulfill()
   174     }
   175 }