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