pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
changeset 6875 4e1bdb08116f
child 6907 e733dd0499bd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift	Tue Oct 23 13:09:32 2018 +0200
     1.3 @@ -0,0 +1,146 @@
     1.4 +//
     1.5 +//  AccountSettingsViewModelTest.swift
     1.6 +//  pEpForiOSTests
     1.7 +//
     1.8 +//  Created by Borja González de Pablo on 22/10/2018.
     1.9 +//  Copyright © 2018 p≡p Security S.A. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +import XCTest
    1.13 +@testable import pEpForiOS
    1.14 +@testable import MessageModel
    1.15 +
    1.16 +class AccountSettingsViewModelTest: CoreDataDrivenTestBase {
    1.17 +
    1.18 +    var viewModel: AccountSettingsViewModel!
    1.19 +
    1.20 +    public func testEmail() {
    1.21 +        setUpViewModel()
    1.22 +
    1.23 +        let email = viewModel.email
    1.24 +
    1.25 +        XCTAssertEqual(email, account.user.address)
    1.26 +    }
    1.27 +
    1.28 +    public func testLoginName() {
    1.29 +        setUpViewModel()
    1.30 +
    1.31 +        let loginName = viewModel.loginName
    1.32 +
    1.33 +        XCTAssertEqual(loginName, account.server(with: .imap)?.credentials.loginName)
    1.34 +    }
    1.35 +
    1.36 +    public func testName() {
    1.37 +        setUpViewModel()
    1.38 +
    1.39 +        let name = viewModel.name
    1.40 +
    1.41 +        XCTAssertEqual(account.user.userName, name)
    1.42 +    }
    1.43 +
    1.44 +    public func testSmptServer() {
    1.45 +        setUpViewModel()
    1.46 +
    1.47 +        let smptServer = viewModel.smtpServer
    1.48 +
    1.49 +        XCTAssertEqual(smptServer.address, account.smtpServer?.address)
    1.50 +        let port =  account.smtpServer?.port
    1.51 +        XCTAssertNotNil(port)
    1.52 +        XCTAssertEqual(smptServer.port, "\(String(describing: port!))")
    1.53 +        XCTAssertEqual(smptServer.transport, account.smtpServer?.transport?.asString())
    1.54 +    }
    1.55 +
    1.56 +    public func testImapServer() {
    1.57 +        setUpViewModel()
    1.58 +
    1.59 +        let imapServer = viewModel.imapServer
    1.60 +
    1.61 +        XCTAssertEqual(imapServer.address, account.imapServer?.address)
    1.62 +        let port =  account.imapServer?.port
    1.63 +        XCTAssertNotNil(port)
    1.64 +        XCTAssertEqual(imapServer.port, "\(String(describing: port!))")
    1.65 +        XCTAssertEqual(imapServer.transport, account.imapServer?.transport?.asString())
    1.66 +    }
    1.67 +
    1.68 +    func testUpdate() {
    1.69 +        setUpViewModel()
    1.70 +        let server = AccountSettingsViewModel.ServerViewModel(address: "fakeAddress",
    1.71 +                                                              port: "123",
    1.72 +                                                              transport: "StartTls")
    1.73 +
    1.74 +
    1.75 +        viewModel.update(loginName: "fakelogin",
    1.76 +                         name: "fakeName",
    1.77 +                         password: "fakePassword",
    1.78 +                         imap: server,
    1.79 +                         smtp: server)
    1.80 +
    1.81 +        XCTAssertEqual(viewModel.account.smtpServer?.credentials.loginName, "fakelogin")
    1.82 +        XCTAssertEqual(viewModel.account.smtpServer?.credentials.password, "fakePassword")
    1.83 +        XCTAssertEqual(viewModel.account.imapServer?.credentials.loginName, "fakelogin")
    1.84 +        XCTAssertEqual(viewModel.account.imapServer?.credentials.password, "fakePassword")
    1.85 +
    1.86 +        XCTAssertEqual(viewModel.account.imapServer?.address, "fakeAddress")
    1.87 +        XCTAssertEqual(viewModel.account.imapServer?.port, 123)
    1.88 +        XCTAssertEqual(viewModel.account.imapServer?.transport, .startTls)
    1.89 +
    1.90 +        XCTAssertEqual(viewModel.account.smtpServer?.address, "fakeAddress")
    1.91 +        XCTAssertEqual(viewModel.account.smtpServer?.port, 123)
    1.92 +        XCTAssertEqual(viewModel.account.smtpServer?.transport, .startTls)
    1.93 +
    1.94 +    }
    1.95 +
    1.96 +    public func testSectionIsValid() {
    1.97 +        setUpViewModel()
    1.98 +        //Header count in AccountSettingViewModel
    1.99 +        let headerCount = 3
   1.100 +        var validSection: Bool!
   1.101 +        for i in 0..<headerCount {
   1.102 +            validSection = viewModel.sectionIsValid(section: i)
   1.103 +            XCTAssertTrue(validSection)
   1.104 +        }
   1.105 +
   1.106 +        validSection = viewModel.sectionIsValid(section: headerCount)
   1.107 +        XCTAssertFalse(validSection)
   1.108 +    }
   1.109 +
   1.110 +    public func testVerified() {
   1.111 +        setUpViewModel()
   1.112 +
   1.113 +        let verifyExpectation =
   1.114 +            expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
   1.115 +
   1.116 +        let delegate = AccountVerificationResultDelegateMock()
   1.117 +        delegate.expectationDidVerifyCalled = verifyExpectation
   1.118 +        viewModel.delegate = delegate
   1.119 +
   1.120 +        viewModel.verified(account: account,
   1.121 +                           service: AccountVerificationService(),
   1.122 +                           result: .ok)
   1.123 +
   1.124 +        waitForExpectations(timeout: UnitTestUtils.waitTime)
   1.125 +    }
   1.126 +
   1.127 +
   1.128 +    private func setUpViewModel(){
   1.129 +        viewModel = AccountSettingsViewModel(account: account)
   1.130 +        viewModel.messageSyncService = MessageSyncService()
   1.131 +    }
   1.132 +}
   1.133 +
   1.134 +class AccountVerificationResultDelegateMock: AccountVerificationResultDelegate {
   1.135 +    static let DID_VERIFY_EXPECTATION = "DID_VERIFY_CALLED"
   1.136 +    var expectationDidVerifyCalled: XCTestExpectation?
   1.137 +
   1.138 +    func didVerify(result: AccountVerificationResult, accountInput: AccountUserInput?) {
   1.139 +        guard let expectation = expectationDidVerifyCalled else {
   1.140 +            XCTFail()
   1.141 +            return
   1.142 +        }
   1.143 +        expectation.fulfill()
   1.144 +    }
   1.145 +
   1.146 +
   1.147 +}
   1.148 +
   1.149 +