pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
author Alejandro Gelos <agp@pep.security>
Fri, 12 Apr 2019 11:35:32 +0200
branchIOS-1541
changeset 8221 d77a4e242296
parent 8190 f5f02da9ea67
permissions -rw-r--r--
IOS-1541 point from VerificationService to AccountVS
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)
borja@6907
    82
        let smtp = viewModel.account.smtpServer
borja@6907
    83
        let imap = viewModel.account.imapServer
borja@6875
    84
borja@6907
    85
        XCTAssertEqual(smtp?.credentials.loginName, login)
borja@6907
    86
        XCTAssertEqual(smtp?.credentials.password, password)
borja@6907
    87
        XCTAssertEqual(imap?.credentials.loginName, login)
borja@6907
    88
        XCTAssertEqual(imap?.credentials.password, password)
borja@6875
    89
borja@6907
    90
        XCTAssertEqual(imap?.address, address)
borja@6907
    91
        XCTAssertEqual(imap?.port, 123)
borja@6907
    92
        XCTAssertEqual(imap?.transport, .startTls)
borja@6875
    93
borja@6907
    94
        XCTAssertEqual(smtp?.address, address)
borja@6907
    95
        XCTAssertEqual(smtp?.port, 123)
borja@6907
    96
        XCTAssertEqual(smtp?.transport, .startTls)
borja@6875
    97
borja@6875
    98
    }
borja@6875
    99
borja@6875
   100
    public func testSectionIsValid() {
borja@6875
   101
        setUpViewModel()
borja@6875
   102
        //Header count in AccountSettingViewModel
borja@6875
   103
        let headerCount = 3
borja@6875
   104
        var validSection: Bool!
borja@6875
   105
        for i in 0..<headerCount {
borja@6875
   106
            validSection = viewModel.sectionIsValid(section: i)
borja@6875
   107
            XCTAssertTrue(validSection)
borja@6875
   108
        }
borja@6875
   109
borja@6875
   110
        validSection = viewModel.sectionIsValid(section: headerCount)
borja@6875
   111
        XCTAssertFalse(validSection)
borja@6875
   112
    }
borja@6875
   113
borja@6875
   114
    public func testVerified() {
borja@6875
   115
        setUpViewModel()
borja@6875
   116
borja@6875
   117
        let verifyExpectation =
borja@6875
   118
            expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
borja@6875
   119
borja@6875
   120
        let delegate = AccountVerificationResultDelegateMock()
borja@6875
   121
        delegate.expectationDidVerifyCalled = verifyExpectation
borja@6875
   122
        viewModel.delegate = delegate
borja@6875
   123
borja@6875
   124
        viewModel.verified(account: account,
borja@6875
   125
                           service: AccountVerificationService(),
borja@6875
   126
                           result: .ok)
borja@6875
   127
borja@6875
   128
        waitForExpectations(timeout: UnitTestUtils.waitTime)
borja@6875
   129
    }
borja@6875
   130
borja@6875
   131
dz@8190
   132
    private func setUpViewModel() {
dz@8190
   133
        account.save()
borja@6875
   134
        viewModel = AccountSettingsViewModel(account: account)
agp@8221
   135
        viewModel.verificationService = AccountVerificationService(delegate: viewModel)
borja@6875
   136
    }
borja@6875
   137
}
borja@6875
   138
borja@6875
   139
class AccountVerificationResultDelegateMock: AccountVerificationResultDelegate {
borja@6875
   140
    static let DID_VERIFY_EXPECTATION = "DID_VERIFY_CALLED"
borja@6875
   141
    var expectationDidVerifyCalled: XCTestExpectation?
borja@6875
   142
borja@6875
   143
    func didVerify(result: AccountVerificationResult, accountInput: AccountUserInput?) {
borja@6875
   144
        guard let expectation = expectationDidVerifyCalled else {
borja@6875
   145
            XCTFail()
borja@6875
   146
            return
borja@6875
   147
        }
borja@6875
   148
        expectation.fulfill()
borja@6875
   149
    }
borja@6875
   150
borja@6875
   151
borja@6875
   152
}
borja@6875
   153
borja@6875
   154