pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
author borja <borja@helm.cat>
Tue, 23 Oct 2018 13:09:32 +0200
changeset 6875 4e1bdb08116f
child 6907 e733dd0499bd
permissions -rw-r--r--
IOS-1377 Add test for AccountSettingsViewModel
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@6875
    66
        setUpViewModel()
borja@6875
    67
        let server = AccountSettingsViewModel.ServerViewModel(address: "fakeAddress",
borja@6875
    68
                                                              port: "123",
borja@6875
    69
                                                              transport: "StartTls")
borja@6875
    70
borja@6875
    71
borja@6875
    72
        viewModel.update(loginName: "fakelogin",
borja@6875
    73
                         name: "fakeName",
borja@6875
    74
                         password: "fakePassword",
borja@6875
    75
                         imap: server,
borja@6875
    76
                         smtp: server)
borja@6875
    77
borja@6875
    78
        XCTAssertEqual(viewModel.account.smtpServer?.credentials.loginName, "fakelogin")
borja@6875
    79
        XCTAssertEqual(viewModel.account.smtpServer?.credentials.password, "fakePassword")
borja@6875
    80
        XCTAssertEqual(viewModel.account.imapServer?.credentials.loginName, "fakelogin")
borja@6875
    81
        XCTAssertEqual(viewModel.account.imapServer?.credentials.password, "fakePassword")
borja@6875
    82
borja@6875
    83
        XCTAssertEqual(viewModel.account.imapServer?.address, "fakeAddress")
borja@6875
    84
        XCTAssertEqual(viewModel.account.imapServer?.port, 123)
borja@6875
    85
        XCTAssertEqual(viewModel.account.imapServer?.transport, .startTls)
borja@6875
    86
borja@6875
    87
        XCTAssertEqual(viewModel.account.smtpServer?.address, "fakeAddress")
borja@6875
    88
        XCTAssertEqual(viewModel.account.smtpServer?.port, 123)
borja@6875
    89
        XCTAssertEqual(viewModel.account.smtpServer?.transport, .startTls)
borja@6875
    90
borja@6875
    91
    }
borja@6875
    92
borja@6875
    93
    public func testSectionIsValid() {
borja@6875
    94
        setUpViewModel()
borja@6875
    95
        //Header count in AccountSettingViewModel
borja@6875
    96
        let headerCount = 3
borja@6875
    97
        var validSection: Bool!
borja@6875
    98
        for i in 0..<headerCount {
borja@6875
    99
            validSection = viewModel.sectionIsValid(section: i)
borja@6875
   100
            XCTAssertTrue(validSection)
borja@6875
   101
        }
borja@6875
   102
borja@6875
   103
        validSection = viewModel.sectionIsValid(section: headerCount)
borja@6875
   104
        XCTAssertFalse(validSection)
borja@6875
   105
    }
borja@6875
   106
borja@6875
   107
    public func testVerified() {
borja@6875
   108
        setUpViewModel()
borja@6875
   109
borja@6875
   110
        let verifyExpectation =
borja@6875
   111
            expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
borja@6875
   112
borja@6875
   113
        let delegate = AccountVerificationResultDelegateMock()
borja@6875
   114
        delegate.expectationDidVerifyCalled = verifyExpectation
borja@6875
   115
        viewModel.delegate = delegate
borja@6875
   116
borja@6875
   117
        viewModel.verified(account: account,
borja@6875
   118
                           service: AccountVerificationService(),
borja@6875
   119
                           result: .ok)
borja@6875
   120
borja@6875
   121
        waitForExpectations(timeout: UnitTestUtils.waitTime)
borja@6875
   122
    }
borja@6875
   123
borja@6875
   124
borja@6875
   125
    private func setUpViewModel(){
borja@6875
   126
        viewModel = AccountSettingsViewModel(account: account)
borja@6875
   127
        viewModel.messageSyncService = MessageSyncService()
borja@6875
   128
    }
borja@6875
   129
}
borja@6875
   130
borja@6875
   131
class AccountVerificationResultDelegateMock: AccountVerificationResultDelegate {
borja@6875
   132
    static let DID_VERIFY_EXPECTATION = "DID_VERIFY_CALLED"
borja@6875
   133
    var expectationDidVerifyCalled: XCTestExpectation?
borja@6875
   134
borja@6875
   135
    func didVerify(result: AccountVerificationResult, accountInput: AccountUserInput?) {
borja@6875
   136
        guard let expectation = expectationDidVerifyCalled else {
borja@6875
   137
            XCTFail()
borja@6875
   138
            return
borja@6875
   139
        }
borja@6875
   140
        expectation.fulfill()
borja@6875
   141
    }
borja@6875
   142
borja@6875
   143
borja@6875
   144
}
borja@6875
   145
borja@6875
   146