IOS-1377 Add test for AccountSettingsViewModel
authorborja <borja@helm.cat>
Tue, 23 Oct 2018 13:09:32 +0200
changeset 68754e1bdb08116f
parent 6874 82333ea3f7b3
child 6876 be3cbe7e152b
child 6890 6f958f44ab77
IOS-1377 Add test for AccountSettingsViewModel
pEpForiOS.xcodeproj/project.pbxproj
pEpForiOS/UI/Settings/Setting/AccountSettings/ViewModel/AccountSettingsViewModel.swift
pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift
     1.1 --- a/pEpForiOS.xcodeproj/project.pbxproj	Mon Oct 22 12:49:53 2018 +0200
     1.2 +++ b/pEpForiOS.xcodeproj/project.pbxproj	Tue Oct 23 13:09:32 2018 +0200
     1.3 @@ -20,6 +20,7 @@
     1.4  		002375D420DCF59D00663961 /* MoveToAccountViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15BA537B20A1F5F50090F126 /* MoveToAccountViewController.swift */; };
     1.5  		002375D620DD185100663961 /* TreadedEmailViewModel+DisplayedMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 002375D520DD185100663961 /* TreadedEmailViewModel+DisplayedMessage.swift */; };
     1.6  		00262056217DCCB200DDAFB5 /* TrustedServerSettingsViewModelTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00262055217DCCB200DDAFB5 /* TrustedServerSettingsViewModelTest.swift */; };
     1.7 +		00262058217DE97100DDAFB5 /* AccountSettingsViewModelTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00262057217DE97100DDAFB5 /* AccountSettingsViewModelTest.swift */; };
     1.8  		0033C07F20D7EBD500224E61 /* ThreadedEmailViewModel+UpdateThreadDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0033C07E20D7EBD500224E61 /* ThreadedEmailViewModel+UpdateThreadDelegate.swift */; };
     1.9  		0033C08120D7F3C600224E61 /* ThreadViewController+ViewModelDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0033C08020D7F3C600224E61 /* ThreadViewController+ViewModelDelegate.swift */; };
    1.10  		0033C08320D7F41600224E61 /* ThreadedEmailViewModelDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0033C08220D7F41600224E61 /* ThreadedEmailViewModelDelegate.swift */; };
    1.11 @@ -589,6 +590,7 @@
    1.12  		002375D220DCDC7300663961 /* FullMessageCell+SecureWebViewControllerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FullMessageCell+SecureWebViewControllerDelegate.swift"; sourceTree = "<group>"; };
    1.13  		002375D520DD185100663961 /* TreadedEmailViewModel+DisplayedMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TreadedEmailViewModel+DisplayedMessage.swift"; sourceTree = "<group>"; };
    1.14  		00262055217DCCB200DDAFB5 /* TrustedServerSettingsViewModelTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrustedServerSettingsViewModelTest.swift; sourceTree = "<group>"; };
    1.15 +		00262057217DE97100DDAFB5 /* AccountSettingsViewModelTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountSettingsViewModelTest.swift; sourceTree = "<group>"; };
    1.16  		0033C07E20D7EBD500224E61 /* ThreadedEmailViewModel+UpdateThreadDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ThreadedEmailViewModel+UpdateThreadDelegate.swift"; sourceTree = "<group>"; };
    1.17  		0033C08020D7F3C600224E61 /* ThreadViewController+ViewModelDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ThreadViewController+ViewModelDelegate.swift"; sourceTree = "<group>"; };
    1.18  		0033C08220D7F41600224E61 /* ThreadedEmailViewModelDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadedEmailViewModelDelegate.swift; sourceTree = "<group>"; };
    1.19 @@ -1189,6 +1191,7 @@
    1.20  				004422C92179ECD600BDF6DF /* PassiveModeViewModelTest.swift */,
    1.21  				004422D8217A25AD00BDF6DF /* UnecryptedSubjectViewModelTest.swift */,
    1.22  				00262055217DCCB200DDAFB5 /* TrustedServerSettingsViewModelTest.swift */,
    1.23 +				00262057217DE97100DDAFB5 /* AccountSettingsViewModelTest.swift */,
    1.24  			);
    1.25  			path = Settings;
    1.26  			sourceTree = "<group>";
    1.27 @@ -3319,6 +3322,7 @@
    1.28  				151F71F9202A06760057C74D /* NetworkServiceObserver.swift in Sources */,
    1.29  				43F7F07A1F6AD44600BDF151 /* HandshakeTests.swift in Sources */,
    1.30  				1569AEA520E14DC3002102A0 /* ReUploadTest.swift in Sources */,
    1.31 +				00262058217DE97100DDAFB5 /* AccountSettingsViewModelTest.swift in Sources */,
    1.32  				004422C82179E3C500BDF6DF /* SettingsCellViewModelTest.swift in Sources */,
    1.33  				150707DC21006CD000AA213F /* ComposeUtilTest.swift in Sources */,
    1.34  				1574D07D2114696B00FEDC93 /* URL+MailToTest.swift in Sources */,
     2.1 --- a/pEpForiOS/UI/Settings/Setting/AccountSettings/ViewModel/AccountSettingsViewModel.swift	Mon Oct 22 12:49:53 2018 +0200
     2.2 +++ b/pEpForiOS/UI/Settings/Setting/AccountSettings/ViewModel/AccountSettingsViewModel.swift	Tue Oct 23 13:09:32 2018 +0200
     2.3 @@ -31,7 +31,7 @@
     2.4          }
     2.5      }
     2.6  
     2.7 -    private var account: Account
     2.8 +    private (set) var account: Account
     2.9      private let headers = [
    2.10          NSLocalizedString("Account", comment: "Account settings"),
    2.11          NSLocalizedString("IMAP Settings", comment: "Account settings title IMAP"),
    2.12 @@ -136,7 +136,7 @@
    2.13      }
    2.14  
    2.15      func sectionIsValid(section: Int) -> Bool {
    2.16 -        return section >= 0 && section <= headers.count
    2.17 +        return section >= 0 && section < headers.count
    2.18      }
    2.19  
    2.20      var count: Int {
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/pEpForiOSTests/Models/Settings/AccountSettingsViewModelTest.swift	Tue Oct 23 13:09:32 2018 +0200
     3.3 @@ -0,0 +1,146 @@
     3.4 +//
     3.5 +//  AccountSettingsViewModelTest.swift
     3.6 +//  pEpForiOSTests
     3.7 +//
     3.8 +//  Created by Borja González de Pablo on 22/10/2018.
     3.9 +//  Copyright © 2018 p≡p Security S.A. All rights reserved.
    3.10 +//
    3.11 +
    3.12 +import XCTest
    3.13 +@testable import pEpForiOS
    3.14 +@testable import MessageModel
    3.15 +
    3.16 +class AccountSettingsViewModelTest: CoreDataDrivenTestBase {
    3.17 +
    3.18 +    var viewModel: AccountSettingsViewModel!
    3.19 +
    3.20 +    public func testEmail() {
    3.21 +        setUpViewModel()
    3.22 +
    3.23 +        let email = viewModel.email
    3.24 +
    3.25 +        XCTAssertEqual(email, account.user.address)
    3.26 +    }
    3.27 +
    3.28 +    public func testLoginName() {
    3.29 +        setUpViewModel()
    3.30 +
    3.31 +        let loginName = viewModel.loginName
    3.32 +
    3.33 +        XCTAssertEqual(loginName, account.server(with: .imap)?.credentials.loginName)
    3.34 +    }
    3.35 +
    3.36 +    public func testName() {
    3.37 +        setUpViewModel()
    3.38 +
    3.39 +        let name = viewModel.name
    3.40 +
    3.41 +        XCTAssertEqual(account.user.userName, name)
    3.42 +    }
    3.43 +
    3.44 +    public func testSmptServer() {
    3.45 +        setUpViewModel()
    3.46 +
    3.47 +        let smptServer = viewModel.smtpServer
    3.48 +
    3.49 +        XCTAssertEqual(smptServer.address, account.smtpServer?.address)
    3.50 +        let port =  account.smtpServer?.port
    3.51 +        XCTAssertNotNil(port)
    3.52 +        XCTAssertEqual(smptServer.port, "\(String(describing: port!))")
    3.53 +        XCTAssertEqual(smptServer.transport, account.smtpServer?.transport?.asString())
    3.54 +    }
    3.55 +
    3.56 +    public func testImapServer() {
    3.57 +        setUpViewModel()
    3.58 +
    3.59 +        let imapServer = viewModel.imapServer
    3.60 +
    3.61 +        XCTAssertEqual(imapServer.address, account.imapServer?.address)
    3.62 +        let port =  account.imapServer?.port
    3.63 +        XCTAssertNotNil(port)
    3.64 +        XCTAssertEqual(imapServer.port, "\(String(describing: port!))")
    3.65 +        XCTAssertEqual(imapServer.transport, account.imapServer?.transport?.asString())
    3.66 +    }
    3.67 +
    3.68 +    func testUpdate() {
    3.69 +        setUpViewModel()
    3.70 +        let server = AccountSettingsViewModel.ServerViewModel(address: "fakeAddress",
    3.71 +                                                              port: "123",
    3.72 +                                                              transport: "StartTls")
    3.73 +
    3.74 +
    3.75 +        viewModel.update(loginName: "fakelogin",
    3.76 +                         name: "fakeName",
    3.77 +                         password: "fakePassword",
    3.78 +                         imap: server,
    3.79 +                         smtp: server)
    3.80 +
    3.81 +        XCTAssertEqual(viewModel.account.smtpServer?.credentials.loginName, "fakelogin")
    3.82 +        XCTAssertEqual(viewModel.account.smtpServer?.credentials.password, "fakePassword")
    3.83 +        XCTAssertEqual(viewModel.account.imapServer?.credentials.loginName, "fakelogin")
    3.84 +        XCTAssertEqual(viewModel.account.imapServer?.credentials.password, "fakePassword")
    3.85 +
    3.86 +        XCTAssertEqual(viewModel.account.imapServer?.address, "fakeAddress")
    3.87 +        XCTAssertEqual(viewModel.account.imapServer?.port, 123)
    3.88 +        XCTAssertEqual(viewModel.account.imapServer?.transport, .startTls)
    3.89 +
    3.90 +        XCTAssertEqual(viewModel.account.smtpServer?.address, "fakeAddress")
    3.91 +        XCTAssertEqual(viewModel.account.smtpServer?.port, 123)
    3.92 +        XCTAssertEqual(viewModel.account.smtpServer?.transport, .startTls)
    3.93 +
    3.94 +    }
    3.95 +
    3.96 +    public func testSectionIsValid() {
    3.97 +        setUpViewModel()
    3.98 +        //Header count in AccountSettingViewModel
    3.99 +        let headerCount = 3
   3.100 +        var validSection: Bool!
   3.101 +        for i in 0..<headerCount {
   3.102 +            validSection = viewModel.sectionIsValid(section: i)
   3.103 +            XCTAssertTrue(validSection)
   3.104 +        }
   3.105 +
   3.106 +        validSection = viewModel.sectionIsValid(section: headerCount)
   3.107 +        XCTAssertFalse(validSection)
   3.108 +    }
   3.109 +
   3.110 +    public func testVerified() {
   3.111 +        setUpViewModel()
   3.112 +
   3.113 +        let verifyExpectation =
   3.114 +            expectation(description: AccountVerificationResultDelegateMock.DID_VERIFY_EXPECTATION)
   3.115 +
   3.116 +        let delegate = AccountVerificationResultDelegateMock()
   3.117 +        delegate.expectationDidVerifyCalled = verifyExpectation
   3.118 +        viewModel.delegate = delegate
   3.119 +
   3.120 +        viewModel.verified(account: account,
   3.121 +                           service: AccountVerificationService(),
   3.122 +                           result: .ok)
   3.123 +
   3.124 +        waitForExpectations(timeout: UnitTestUtils.waitTime)
   3.125 +    }
   3.126 +
   3.127 +
   3.128 +    private func setUpViewModel(){
   3.129 +        viewModel = AccountSettingsViewModel(account: account)
   3.130 +        viewModel.messageSyncService = MessageSyncService()
   3.131 +    }
   3.132 +}
   3.133 +
   3.134 +class AccountVerificationResultDelegateMock: AccountVerificationResultDelegate {
   3.135 +    static let DID_VERIFY_EXPECTATION = "DID_VERIFY_CALLED"
   3.136 +    var expectationDidVerifyCalled: XCTestExpectation?
   3.137 +
   3.138 +    func didVerify(result: AccountVerificationResult, accountInput: AccountUserInput?) {
   3.139 +        guard let expectation = expectationDidVerifyCalled else {
   3.140 +            XCTFail()
   3.141 +            return
   3.142 +        }
   3.143 +        expectation.fulfill()
   3.144 +    }
   3.145 +
   3.146 +
   3.147 +}
   3.148 +
   3.149 +