pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
author Dirk Zimmermann <dz@pep.security>
Wed, 24 Apr 2019 12:43:18 +0200
branchIOS-1542
changeset 8368 94cad6139b72
parent 8367 f75f27b477c6
child 8381 907e287ead66
child 8435 5c04ce53366e
child 8632 5d877367bbef
permissions -rw-r--r--
IOS-1542 Give the test some time to run.
     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.verified(account: account,
   137                            service: AccountVerificationService(),
   138                            result: .ok)
   139 
   140         waitForExpectations(timeout: UnitTestUtils.waitTime)
   141     }
   142 
   143 
   144     private func setUpViewModel() {
   145         account.save()
   146         viewModel = AccountSettingsViewModel(account: account)
   147     }
   148 }
   149 
   150 class AccountVerificationResultDelegateMock: AccountVerificationResultDelegate {
   151     static let DID_VERIFY_EXPECTATION = "DID_VERIFY_CALLED"
   152     var expectationDidVerifyCalled: XCTestExpectation?
   153     var error: Error? = nil
   154 
   155     func didVerify(result: AccountVerificationResult, accountInput: VerifiableAccountProtocol?) {
   156         switch result {
   157         case .ok:
   158             self.error = nil
   159         case .noImapConnectData, .noSmtpConnectData:
   160             let theError = NSError(
   161                 domain: #function,
   162                 code: 777,
   163                 userInfo: [NSLocalizedDescriptionKey: "SMTP/IMAP ERROR"])
   164             self.error = theError
   165         case .imapError(let error):
   166             self.error = error
   167         case .smtpError(let error):
   168             self.error = error
   169         }
   170 
   171         guard let expectation = expectationDidVerifyCalled else {
   172             XCTFail()
   173             return
   174         }
   175         expectation.fulfill()
   176     }
   177 }