pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
author Dirk Zimmermann <dz@pep.security>
Mon, 03 Jun 2019 14:02:01 +0200
branchIOS-1602
changeset 8886 7f671f0feab3
parent 8834 ad8eb0a7e341
child 9099 43115d37c44c
permissions -rw-r--r--
IOS-1602 Using new ErrorNotifyHandshakeDelegate.
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
andreas@8460
    30
        XCTAssertEqual(loginName, account.imapServer?.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!))")
andreas@8460
    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!))")
andreas@8460
    62
        XCTAssertEqual(imapServer.transport, account.imapServer?.transport.asString())
borja@6875
    63
    }
borja@6875
    64
borja@6875
    65
    func testUpdate() {
dz@8384
    66
        let address = "localhost"
borja@6907
    67
        let login = "fakelogin"
borja@6907
    68
        let name = "fakeName"
borja@6907
    69
        let password = "fakePassword"
dz@8384
    70
        let portString = "1"
dz@8384
    71
        let portInt = UInt16(portString)!
borja@6907
    72
borja@6875
    73
        setUpViewModel()
borja@6907
    74
borja@6907
    75
        let server = AccountSettingsViewModel.ServerViewModel(address: address,
dz@8384
    76
                                                              port: portString,
borja@6875
    77
                                                              transport: "StartTls")
borja@6875
    78
dz@8367
    79
        let verifyExpectation =
dz@8367
    80
            expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
dz@8367
    81
dz@8367
    82
        let delegate = AccountVerificationResultDelegateMock()
dz@8367
    83
        delegate.expectationDidVerifyCalled = verifyExpectation
dz@8367
    84
        viewModel.delegate = delegate
dz@8367
    85
borja@6907
    86
        viewModel.update(loginName: login,
borja@6907
    87
                         name: name,
borja@6907
    88
                         password: password,
borja@6875
    89
                         imap: server,
borja@6875
    90
                         smtp: server)
dz@8367
    91
dz@8368
    92
        waitForExpectations(timeout: UnitTestUtils.asyncWaitTime)
dz@8367
    93
dz@8384
    94
        guard let verifier = viewModel.verifiableAccount else {
dz@8384
    95
            XCTFail()
dz@8384
    96
            return
dz@8384
    97
        }
borja@6875
    98
dz@8384
    99
        XCTAssertEqual(verifier.loginName, login)
dz@8384
   100
        XCTAssertEqual(verifier.password, password)
dz@8384
   101
        XCTAssertEqual(verifier.serverIMAP, address)
dz@8384
   102
        XCTAssertEqual(verifier.serverSMTP, address)
dz@8384
   103
        XCTAssertEqual(verifier.portIMAP, portInt)
dz@8384
   104
        XCTAssertEqual(verifier.portSMTP, portInt)
dz@8384
   105
        XCTAssertNil(verifier.accessToken)
borja@6875
   106
    }
borja@6875
   107
borja@6875
   108
    public func testSectionIsValid() {
borja@6875
   109
        setUpViewModel()
borja@6875
   110
        //Header count in AccountSettingViewModel
borja@6875
   111
        let headerCount = 3
borja@6875
   112
        var validSection: Bool!
borja@6875
   113
        for i in 0..<headerCount {
borja@6875
   114
            validSection = viewModel.sectionIsValid(section: i)
borja@6875
   115
            XCTAssertTrue(validSection)
borja@6875
   116
        }
borja@6875
   117
borja@6875
   118
        validSection = viewModel.sectionIsValid(section: headerCount)
borja@6875
   119
        XCTAssertFalse(validSection)
borja@6875
   120
    }
borja@6875
   121
borja@6875
   122
    public func testVerified() {
borja@6875
   123
        setUpViewModel()
borja@6875
   124
borja@6875
   125
        let verifyExpectation =
borja@6875
   126
            expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
borja@6875
   127
borja@6875
   128
        let delegate = AccountVerificationResultDelegateMock()
borja@6875
   129
        delegate.expectationDidVerifyCalled = verifyExpectation
borja@6875
   130
        viewModel.delegate = delegate
borja@6875
   131
dz@8381
   132
        viewModel.didEndVerification(result: .success(()))
borja@6875
   133
borja@6875
   134
        waitForExpectations(timeout: UnitTestUtils.waitTime)
borja@6875
   135
    }
borja@6875
   136
borja@6875
   137
dz@8190
   138
    private func setUpViewModel() {
dz@8190
   139
        account.save()
dz@8834
   140
dz@8834
   141
        let theMessageModelService = MessageModelService(
dz@8834
   142
            mySelfer: DefaultMySelfer(backgrounder: nil),
dz@8834
   143
            errorPropagator: ErrorPropagator(),
dz@8886
   144
            notifyHandShakeDelegate: ErrorNotifyHandshakeDelegate())
dz@8834
   145
dz@8834
   146
        viewModel = AccountSettingsViewModel(
dz@8834
   147
            account: account,
dz@8834
   148
            messageModelService: theMessageModelService)
borja@6875
   149
    }
borja@6875
   150
}
borja@6875
   151
borja@6875
   152
class AccountVerificationResultDelegateMock: AccountVerificationResultDelegate {
borja@6875
   153
    static let DID_VERIFY_EXPECTATION = "DID_VERIFY_CALLED"
borja@6875
   154
    var expectationDidVerifyCalled: XCTestExpectation?
dz@8367
   155
    var error: Error? = nil
borja@6875
   156
dz@8490
   157
    func didVerify(result: AccountVerificationResult) {
dz@8367
   158
        switch result {
dz@8367
   159
        case .ok:
dz@8367
   160
            self.error = nil
dz@8367
   161
        case .noImapConnectData, .noSmtpConnectData:
dz@8367
   162
            let theError = NSError(
dz@8367
   163
                domain: #function,
dz@8367
   164
                code: 777,
dz@8367
   165
                userInfo: [NSLocalizedDescriptionKey: "SMTP/IMAP ERROR"])
dz@8367
   166
            self.error = theError
dz@8367
   167
        case .imapError(let error):
dz@8367
   168
            self.error = error
dz@8367
   169
        case .smtpError(let error):
dz@8367
   170
            self.error = error
dz@8367
   171
        }
dz@8367
   172
borja@6875
   173
        guard let expectation = expectationDidVerifyCalled else {
borja@6875
   174
            XCTFail()
borja@6875
   175
            return
borja@6875
   176
        }
borja@6875
   177
        expectation.fulfill()
borja@6875
   178
    }
borja@6875
   179
}