pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
author Alejandro Gelos <agp@pep.security>
Mon, 19 Aug 2019 13:42:48 +0200
branchIOS-1740
changeset 9723 2da4ad6faab3
parent 9172 7de6361090b9
child 9725 efdbdfc23000
permissions -rw-r--r--
IOS-1740 add test for account password not updating in keychain
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!
agp@9172
    16
    var keySyncServiceHandshakeDelegateMoc: KeySyncServiceHandshakeDelegateMoc!
borja@6875
    17
borja@6875
    18
    public func testEmail() {
borja@6875
    19
        setUpViewModel()
borja@6875
    20
borja@6875
    21
        let email = viewModel.email
borja@6875
    22
borja@6875
    23
        XCTAssertEqual(email, account.user.address)
borja@6875
    24
    }
borja@6875
    25
borja@6875
    26
    public func testLoginName() {
borja@6875
    27
        setUpViewModel()
borja@6875
    28
borja@6875
    29
        let loginName = viewModel.loginName
borja@6875
    30
andreas@8460
    31
        XCTAssertEqual(loginName, account.imapServer?.credentials.loginName)
borja@6875
    32
    }
borja@6875
    33
borja@6875
    34
    public func testName() {
borja@6875
    35
        setUpViewModel()
borja@6875
    36
borja@6875
    37
        let name = viewModel.name
borja@6875
    38
borja@6875
    39
        XCTAssertEqual(account.user.userName, name)
borja@6875
    40
    }
borja@6875
    41
borja@6875
    42
    public func testSmptServer() {
borja@6875
    43
        setUpViewModel()
borja@6875
    44
borja@6875
    45
        let smptServer = viewModel.smtpServer
borja@6875
    46
borja@6875
    47
        XCTAssertEqual(smptServer.address, account.smtpServer?.address)
borja@6875
    48
        let port =  account.smtpServer?.port
borja@6875
    49
        XCTAssertNotNil(port)
borja@6875
    50
        XCTAssertEqual(smptServer.port, "\(String(describing: port!))")
andreas@8460
    51
        XCTAssertEqual(smptServer.transport, account.smtpServer?.transport.asString())
borja@6875
    52
    }
borja@6875
    53
borja@6875
    54
    public func testImapServer() {
borja@6875
    55
        setUpViewModel()
borja@6875
    56
borja@6875
    57
        let imapServer = viewModel.imapServer
borja@6875
    58
borja@6875
    59
        XCTAssertEqual(imapServer.address, account.imapServer?.address)
borja@6875
    60
        let port =  account.imapServer?.port
borja@6875
    61
        XCTAssertNotNil(port)
borja@6875
    62
        XCTAssertEqual(imapServer.port, "\(String(describing: port!))")
andreas@8460
    63
        XCTAssertEqual(imapServer.transport, account.imapServer?.transport.asString())
borja@6875
    64
    }
borja@6875
    65
borja@6875
    66
    func testUpdate() {
dz@8384
    67
        let address = "localhost"
borja@6907
    68
        let login = "fakelogin"
borja@6907
    69
        let name = "fakeName"
borja@6907
    70
        let password = "fakePassword"
dz@8384
    71
        let portString = "1"
dz@8384
    72
        let portInt = UInt16(portString)!
borja@6907
    73
borja@6875
    74
        setUpViewModel()
borja@6907
    75
borja@6907
    76
        let server = AccountSettingsViewModel.ServerViewModel(address: address,
dz@8384
    77
                                                              port: portString,
borja@6875
    78
                                                              transport: "StartTls")
borja@6875
    79
dz@8367
    80
        let verifyExpectation =
dz@8367
    81
            expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
dz@8367
    82
dz@8367
    83
        let delegate = AccountVerificationResultDelegateMock()
dz@8367
    84
        delegate.expectationDidVerifyCalled = verifyExpectation
dz@8367
    85
        viewModel.delegate = delegate
dz@8367
    86
borja@6907
    87
        viewModel.update(loginName: login,
borja@6907
    88
                         name: name,
borja@6907
    89
                         password: password,
borja@6875
    90
                         imap: server,
borja@6875
    91
                         smtp: server)
dz@8367
    92
dz@8368
    93
        waitForExpectations(timeout: UnitTestUtils.asyncWaitTime)
dz@8367
    94
dz@8384
    95
        guard let verifier = viewModel.verifiableAccount else {
dz@8384
    96
            XCTFail()
dz@8384
    97
            return
dz@8384
    98
        }
borja@6875
    99
dz@8384
   100
        XCTAssertEqual(verifier.loginName, login)
dz@8384
   101
        XCTAssertEqual(verifier.password, password)
dz@8384
   102
        XCTAssertEqual(verifier.serverIMAP, address)
dz@8384
   103
        XCTAssertEqual(verifier.serverSMTP, address)
dz@8384
   104
        XCTAssertEqual(verifier.portIMAP, portInt)
dz@8384
   105
        XCTAssertEqual(verifier.portSMTP, portInt)
dz@8384
   106
        XCTAssertNil(verifier.accessToken)
borja@6875
   107
    }
borja@6875
   108
borja@6875
   109
    public func testSectionIsValid() {
borja@6875
   110
        setUpViewModel()
borja@6875
   111
        //Header count in AccountSettingViewModel
borja@6875
   112
        let headerCount = 3
borja@6875
   113
        var validSection: Bool!
borja@6875
   114
        for i in 0..<headerCount {
borja@6875
   115
            validSection = viewModel.sectionIsValid(section: i)
borja@6875
   116
            XCTAssertTrue(validSection)
borja@6875
   117
        }
borja@6875
   118
borja@6875
   119
        validSection = viewModel.sectionIsValid(section: headerCount)
borja@6875
   120
        XCTAssertFalse(validSection)
borja@6875
   121
    }
borja@6875
   122
borja@6875
   123
    public func testVerified() {
borja@6875
   124
        setUpViewModel()
borja@6875
   125
borja@6875
   126
        let verifyExpectation =
borja@6875
   127
            expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
borja@6875
   128
borja@6875
   129
        let delegate = AccountVerificationResultDelegateMock()
borja@6875
   130
        delegate.expectationDidVerifyCalled = verifyExpectation
borja@6875
   131
        viewModel.delegate = delegate
borja@6875
   132
dz@8381
   133
        viewModel.didEndVerification(result: .success(()))
borja@6875
   134
borja@6875
   135
        waitForExpectations(timeout: UnitTestUtils.waitTime)
borja@6875
   136
    }
borja@6875
   137
agp@9723
   138
    public func testSavePasswordAfterEndVerification() {
agp@9723
   139
        // GIVEN
agp@9723
   140
        setUpViewModel()
agp@9723
   141
        let successResult: Result<Void, Error> = .success(())
agp@9723
   142
        let expectedPassword = "passwordChanged"
agp@9723
   143
        let imap = AccountSettingsViewModel.ServerViewModel(address: nil, port: nil, transport: nil)
agp@9723
   144
        let smtp = AccountSettingsViewModel.ServerViewModel(address: nil, port: nil, transport: nil)
agp@9723
   145
        viewModel.update(loginName: "", name: "", password: expectedPassword, imap: imap, smtp: smtp)
agp@9723
   146
agp@9723
   147
        // WHEN
agp@9723
   148
        viewModel.didEndVerification(result: successResult)
agp@9723
   149
agp@9723
   150
        // THEN
agp@9723
   151
        let connectionInfo = account.cdObject.imapConnectInfo
agp@9723
   152
        let actualPassword = connectionInfo?.loginPassword
agp@9723
   153
        XCTAssertEqual(actualPassword, expectedPassword)
agp@9723
   154
    }
agp@9723
   155
borja@6875
   156
dz@8190
   157
    private func setUpViewModel() {
dz@8190
   158
        account.save()
agp@9172
   159
        keySyncServiceHandshakeDelegateMoc = KeySyncServiceHandshakeDelegateMoc()
dz@8834
   160
        let theMessageModelService = MessageModelService(
dz@8834
   161
            errorPropagator: ErrorPropagator(),
agp@9172
   162
            keySyncServiceDelegate: keySyncServiceHandshakeDelegateMoc, keySyncEnabled: false)
dz@8834
   163
dz@8834
   164
        viewModel = AccountSettingsViewModel(
dz@8834
   165
            account: account,
dz@8834
   166
            messageModelService: theMessageModelService)
borja@6875
   167
    }
borja@6875
   168
}
borja@6875
   169
borja@6875
   170
class AccountVerificationResultDelegateMock: AccountVerificationResultDelegate {
borja@6875
   171
    static let DID_VERIFY_EXPECTATION = "DID_VERIFY_CALLED"
borja@6875
   172
    var expectationDidVerifyCalled: XCTestExpectation?
dz@8367
   173
    var error: Error? = nil
borja@6875
   174
dz@8490
   175
    func didVerify(result: AccountVerificationResult) {
dz@8367
   176
        switch result {
dz@8367
   177
        case .ok:
dz@8367
   178
            self.error = nil
dz@8367
   179
        case .noImapConnectData, .noSmtpConnectData:
dz@8367
   180
            let theError = NSError(
dz@8367
   181
                domain: #function,
dz@8367
   182
                code: 777,
dz@8367
   183
                userInfo: [NSLocalizedDescriptionKey: "SMTP/IMAP ERROR"])
dz@8367
   184
            self.error = theError
dz@8367
   185
        case .imapError(let error):
dz@8367
   186
            self.error = error
dz@8367
   187
        case .smtpError(let error):
dz@8367
   188
            self.error = error
dz@8367
   189
        }
dz@8367
   190
borja@6875
   191
        guard let expectation = expectationDidVerifyCalled else {
borja@6875
   192
            XCTFail()
borja@6875
   193
            return
borja@6875
   194
        }
borja@6875
   195
        expectation.fulfill()
borja@6875
   196
    }
borja@6875
   197
}