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