pEpForiOSTests/AccountVerificationServiceTests.swift
author Xavier Algarra <xavier@pep-project.org>
Tue, 06 Jun 2017 07:49:23 +0200
changeset 2248 75013a586bfd
parent 2238 7cf70731b53f
child 2473 a71fb5209169
permissions -rw-r--r--
IOS-137 add multiple accounts option with the new login
     1 //
     2 //  AccountVerificationServiceTests.swift
     3 //  pEpForiOS
     4 //
     5 //  Created by Dirk Zimmermann on 24.05.17.
     6 //  Copyright © 2017 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import XCTest
    10 
    11 @testable import MessageModel
    12 @testable import pEpForiOS
    13 
    14 class AccountVerificationTestDelegate: AccountVerificationServiceDelegate {
    15     let expVerified: XCTestExpectation?
    16     var verificationResult: AccountVerificationResult?
    17     var verifiedAccount: Account?
    18 
    19     init(expVerified: XCTestExpectation? = nil) {
    20         self.expVerified = expVerified
    21     }
    22 
    23     func verified(account: Account, service: AccountVerificationServiceProtocol,
    24                   result: AccountVerificationResult) {
    25         verifiedAccount = account
    26         verificationResult = result
    27         expVerified?.fulfill()
    28     }
    29 }
    30 
    31 class AccountVerificationServiceTests: XCTestCase {
    32     var persistentSetup: PersistentSetup!
    33 
    34     override func setUp() {
    35         persistentSetup = PersistentSetup()
    36     }
    37     
    38     override func tearDown() {
    39         persistentSetup = nil
    40     }
    41 
    42     func testVerification(account: Account, expectedResult: AccountVerificationResult,
    43                           testDirectly: Bool) {
    44         let expVerified = expectation(description: "account verified")
    45         let delegate = AccountVerificationTestDelegate(expVerified: expVerified)
    46 
    47         let asService = AccountVerificationService()
    48         let msService = MessageSyncService()
    49 
    50         if testDirectly {
    51             asService.delegate = delegate
    52             asService.verify(account: account)
    53         } else {
    54             msService.requestVerification(account: account, delegate: delegate)
    55         }
    56 
    57         waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
    58             XCTAssertNil(error)
    59             guard let result = delegate.verificationResult else {
    60                 XCTFail()
    61                 return
    62             }
    63             XCTAssertEqual(result, expectedResult)
    64         })
    65     }
    66 
    67     func testDirectlySuccess() {
    68         testVerification(account: TestData().createWorkingAccount(),
    69                          expectedResult: AccountVerificationResult.ok,
    70                          testDirectly: true)
    71     }
    72 
    73     func testDirectlyFailures() {
    74         testVerification(
    75             account: TestData().createImapTimeOutAccount(),
    76             expectedResult: AccountVerificationResult.imapError(
    77                 .connectionTimedOut("connectionTimedOut(_:notification:)")),
    78             testDirectly: true)
    79         testVerification(
    80             account: TestData().createSmtpTimeOutAccount(),
    81             expectedResult: AccountVerificationResult.smtpError(
    82                 .connectionTimedOut("connectionTimedOut(_:theNotification:)")),
    83             testDirectly: true)
    84     }
    85 
    86     func testMessageSyncServiceSuccess() {
    87         testVerification(account: TestData().createWorkingAccount(),
    88                          expectedResult: AccountVerificationResult.ok,
    89                          testDirectly: false)
    90     }
    91 
    92     func testMessageSyncServiceFailures() {
    93         testVerification(
    94             account: TestData().createImapTimeOutAccount(),
    95             expectedResult: AccountVerificationResult.imapError(
    96                 .connectionTimedOut("connectionTimedOut(_:notification:)")),
    97             testDirectly: false)
    98         testVerification(
    99             account: TestData().createSmtpTimeOutAccount(),
   100             expectedResult: AccountVerificationResult.smtpError(
   101                 .connectionTimedOut("connectionTimedOut(_:theNotification:)")),
   102             testDirectly: false)
   103     }
   104 }