pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
author Dirk Zimmermann <dz@pep.security>
Wed, 24 Apr 2019 12:43:18 +0200
branchIOS-1542
changeset 8354 1b6ad54afb11
parent 8327 8a65e73fcc04
child 8365 3ee18a0d2630
permissions -rw-r--r--
IOS-1542 AccountSettingsViewModel: Get rid of the account copy.
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
borja@6907
    77
        viewModel.update(loginName: login,
borja@6907
    78
                         name: name,
borja@6907
    79
                         password: password,
borja@6875
    80
                         imap: server,
borja@6875
    81
                         smtp: server)
dz@8354
    82
        // TODO: What to test here?
dz@8354
    83
        /*
borja@6907
    84
        let smtp = viewModel.account.smtpServer
borja@6907
    85
        let imap = viewModel.account.imapServer
borja@6875
    86
borja@6907
    87
        XCTAssertEqual(smtp?.credentials.loginName, login)
borja@6907
    88
        XCTAssertEqual(smtp?.credentials.password, password)
borja@6907
    89
        XCTAssertEqual(imap?.credentials.loginName, login)
borja@6907
    90
        XCTAssertEqual(imap?.credentials.password, password)
borja@6875
    91
borja@6907
    92
        XCTAssertEqual(imap?.address, address)
borja@6907
    93
        XCTAssertEqual(imap?.port, 123)
borja@6907
    94
        XCTAssertEqual(imap?.transport, .startTls)
borja@6875
    95
borja@6907
    96
        XCTAssertEqual(smtp?.address, address)
borja@6907
    97
        XCTAssertEqual(smtp?.port, 123)
borja@6907
    98
        XCTAssertEqual(smtp?.transport, .startTls)
dz@8354
    99
         */
borja@6875
   100
    }
borja@6875
   101
borja@6875
   102
    public func testSectionIsValid() {
borja@6875
   103
        setUpViewModel()
borja@6875
   104
        //Header count in AccountSettingViewModel
borja@6875
   105
        let headerCount = 3
borja@6875
   106
        var validSection: Bool!
borja@6875
   107
        for i in 0..<headerCount {
borja@6875
   108
            validSection = viewModel.sectionIsValid(section: i)
borja@6875
   109
            XCTAssertTrue(validSection)
borja@6875
   110
        }
borja@6875
   111
borja@6875
   112
        validSection = viewModel.sectionIsValid(section: headerCount)
borja@6875
   113
        XCTAssertFalse(validSection)
borja@6875
   114
    }
borja@6875
   115
borja@6875
   116
    public func testVerified() {
borja@6875
   117
        setUpViewModel()
borja@6875
   118
borja@6875
   119
        let verifyExpectation =
borja@6875
   120
            expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
borja@6875
   121
borja@6875
   122
        let delegate = AccountVerificationResultDelegateMock()
borja@6875
   123
        delegate.expectationDidVerifyCalled = verifyExpectation
borja@6875
   124
        viewModel.delegate = delegate
borja@6875
   125
borja@6875
   126
        viewModel.verified(account: account,
borja@6875
   127
                           service: AccountVerificationService(),
borja@6875
   128
                           result: .ok)
borja@6875
   129
borja@6875
   130
        waitForExpectations(timeout: UnitTestUtils.waitTime)
borja@6875
   131
    }
borja@6875
   132
borja@6875
   133
dz@8190
   134
    private func setUpViewModel() {
dz@8190
   135
        account.save()
borja@6875
   136
        viewModel = AccountSettingsViewModel(account: account)
agp@8142
   137
        viewModel.verificationService = VerificationService()
borja@6875
   138
    }
borja@6875
   139
}
borja@6875
   140
borja@6875
   141
class AccountVerificationResultDelegateMock: AccountVerificationResultDelegate {
borja@6875
   142
    static let DID_VERIFY_EXPECTATION = "DID_VERIFY_CALLED"
borja@6875
   143
    var expectationDidVerifyCalled: XCTestExpectation?
borja@6875
   144
dz@8327
   145
    func didVerify(result: AccountVerificationResult, accountInput: VerifiableAccountProtocol?) {
borja@6875
   146
        guard let expectation = expectationDidVerifyCalled else {
borja@6875
   147
            XCTFail()
borja@6875
   148
            return
borja@6875
   149
        }
borja@6875
   150
        expectation.fulfill()
borja@6875
   151
    }
borja@6875
   152
borja@6875
   153
borja@6875
   154
}
borja@6875
   155
borja@6875
   156