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