pEpForiOSTests/GrandOperatorTests.swift
author Xavier Algarra <xavier@pep-project.org>
Tue, 06 Jun 2017 07:49:23 +0200
changeset 2248 75013a586bfd
parent 819 1e2af6f3d05b
permissions -rw-r--r--
IOS-137 add multiple accounts option with the new login
dirk@70
     1
//
dirk@70
     2
//  GrandOperatorTests.swift
dirk@70
     3
//  pEpForiOS
dirk@70
     4
//
dirk@70
     5
//  Created by Dirk Zimmermann on 26/04/16.
dirk@70
     6
//  Copyright © 2016 p≡p Security S.A. All rights reserved.
dirk@70
     7
//
dirk@70
     8
dirk@70
     9
import XCTest
dirk@70
    10
import CoreData
dirk@70
    11
dirk@70
    12
import pEpForiOS
dirk@70
    13
dirk@70
    14
class GrandOperatorTests: XCTestCase {
dirk@92
    15
    let comp = "GrandOperatorTests"
dirk@92
    16
dirk@100
    17
    let correct = TestData.connectInfo
dirk@139
    18
    var persistentSetup: PersistentSetup!
dirk@70
    19
dirk@70
    20
    override func setUp() {
dirk@70
    21
        super.setUp()
dirk@233
    22
        persistentSetup = PersistentSetup.init()
dirk@70
    23
    }
dirk@244
    24
dirk@262
    25
    override func tearDown() {
dirk@262
    26
        persistentSetup = nil
dirk@262
    27
        super.tearDown()
dirk@262
    28
    }
dirk@262
    29
dirk@70
    30
    /**
dirk@70
    31
     Proof of concept for using managed object context in unit tests.
dirk@70
    32
     */
dirk@70
    33
    func testNewMessage() {
dirk@784
    34
        let message = NSEntityDescription.insertNewObject(
dirk@802
    35
            forEntityName: CdMessage.entityName(),
dirk@784
    36
            into:
dirk@802
    37
            persistentSetup.grandOperator.coreDataUtil.managedObjectContext) as? CdMessage
dirk@70
    38
        XCTAssertNotNil(message)
dirk@70
    39
        message!.subject = "Subject"
dirk@70
    40
        XCTAssertNotNil(message?.subject)
dirk@70
    41
    }
dirk@86
    42
dirk@139
    43
    func testFetchFolders() {
dirk@784
    44
        let exp = expectation(description: "foldersFetched")
dirk@775
    45
        persistentSetup.grandOperator.fetchFolders(
dirk@775
    46
            persistentSetup.connectionInfo, completionBlock: { error in
dirk@775
    47
                XCTAssertNil(error)
dirk@775
    48
                exp.fulfill()
dirk@139
    49
        })
dirk@625
    50
dirk@784
    51
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@139
    52
            XCTAssertNil(error)
dirk@139
    53
        })
dirk@625
    54
dirk@625
    55
        let p = NSPredicate.init(value: true)
dirk@625
    56
        let model = self.persistentSetup.model
dirk@625
    57
        XCTAssertGreaterThan(
dirk@625
    58
            model.folderCountByPredicate(p), 0)
dirk@775
    59
        XCTAssertEqual(model.folderByType(
dirk@790
    60
            .inbox, email: self.persistentSetup.accountEmail)?.name.lowercased(),
dirk@784
    61
                       ImapSync.defaultImapInboxName.lowercased())
dirk@139
    62
    }
dirk@193
    63
dirk@802
    64
    func createMail() -> CdMessage {
dirk@790
    65
        let msg = persistentSetup.model.insertNewMessage()
dirk@234
    66
        msg.subject = "Subject"
dirk@234
    67
        msg.longMessage = "Message body"
dirk@234
    68
        let from = persistentSetup.model.insertOrUpdateContactEmail(
dirk@790
    69
            persistentSetup.accountEmail, name: persistentSetup.accountEmail)
dirk@235
    70
        msg.from = from
dirk@234
    71
        let to = persistentSetup.model.insertOrUpdateContactEmail(
dirk@790
    72
            "unittest.ios.3@peptest.ch", name: "UnitTestiOS 3")
dirk@790
    73
        msg.addToObject(value: to)
dirk@235
    74
        return msg
dirk@235
    75
    }
dirk@235
    76
dirk@373
    77
    func testChainFolderFetching() {
dirk@819
    78
        let account = persistentSetup.model.insertAccountFromImapSmtpConnectInfo(
dirk@446
    79
            persistentSetup.connectionInfo)
dirk@446
    80
        var callbackNumber = 0
dirk@446
    81
        let op1 = CreateLocalSpecialFoldersOperation.init(
dirk@446
    82
            coreDataUtil: persistentSetup.grandOperator.coreDataUtil,
dirk@446
    83
            accountEmail: account.email)
dirk@624
    84
        let op2 = FetchFoldersOperation.init(
dirk@624
    85
            connectInfo: persistentSetup.connectionInfo,
dirk@624
    86
            coreDataUtil: persistentSetup.grandOperator.coreDataUtil,
dirk@624
    87
            connectionManager: persistentSetup.grandOperator.connectionManager)
dirk@784
    88
        let expFoldersFetched = expectation(description: "expFoldersFetched")
dirk@446
    89
        persistentSetup.grandOperator.chainOperations(
dirk@446
    90
            [op1, op2],
dirk@446
    91
            completionBlock: { error in
dirk@373
    92
                XCTAssertNil(error)
dirk@446
    93
                XCTAssertEqual(callbackNumber, 0)
dirk@446
    94
                callbackNumber += 1
dirk@446
    95
                expFoldersFetched.fulfill()
dirk@446
    96
        })
dirk@784
    97
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@446
    98
            XCTAssertNil(error)
dirk@446
    99
            if let folders = self.persistentSetup.model.foldersByPredicate(
dirk@446
   100
                NSPredicate.init(value: true), sortDescriptors: nil) {
dirk@446
   101
                XCTAssertGreaterThan(folders.count, FolderType.allValuesToCreate.count)
dirk@446
   102
            } else {
dirk@446
   103
                XCTAssertTrue(false, "Expected folders created")
dirk@446
   104
            }
dirk@579
   105
            let folder = self.persistentSetup.model.folderByType(
dirk@784
   106
                FolderType.inbox, email: self.persistentSetup.connectionInfo.email)
dirk@446
   107
            XCTAssertNotNil(folder)
dirk@446
   108
        })
dirk@373
   109
    }
dirk@373
   110
dirk@802
   111
    func createSpecialFolders(_ account: CdAccount) {
dirk@784
   112
        let expSpecialFoldersCreated = expectation(description: "expSpecialFoldersCreated")
dirk@689
   113
        persistentSetup.grandOperator.createSpecialLocalFolders(
dirk@689
   114
            account.email, completionBlock: { error in
dirk@689
   115
                XCTAssertNil(error)
dirk@689
   116
                expSpecialFoldersCreated.fulfill()
dirk@689
   117
        })
dirk@689
   118
dirk@784
   119
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@689
   120
            XCTAssertNil(error)
dirk@689
   121
        })
dirk@689
   122
    }
dirk@689
   123
dirk@235
   124
    func testSendMail() {
dirk@628
   125
        testFetchFolders()
dirk@819
   126
        let account = persistentSetup.model.insertAccountFromImapSmtpConnectInfo(
dirk@790
   127
            persistentSetup.connectionInfo)
dirk@689
   128
dirk@689
   129
        createSpecialFolders(account)
dirk@689
   130
dirk@690
   131
        guard let outFolder = persistentSetup.model.folderByType(
dirk@784
   132
            .localOutbox, email:account.email) else {
dirk@690
   133
                XCTAssertTrue(false)
dirk@690
   134
                return
dirk@690
   135
        }
dirk@690
   136
        XCTAssertEqual(outFolder.messages.count, 0)
dirk@690
   137
dirk@235
   138
        let msg = createMail()
dirk@784
   139
        let exp = expectation(description: "mailSent")
dirk@689
   140
        persistentSetup.grandOperator.sendMail(msg, account: account, completionBlock: { error in
dirk@234
   141
            XCTAssertNil(error)
dirk@234
   142
            exp.fulfill()
dirk@234
   143
        })
dirk@784
   144
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@234
   145
            XCTAssertNil(error)
dirk@234
   146
        })
dirk@690
   147
        XCTAssertEqual(outFolder.messages.count, 0)
dirk@234
   148
    }
dirk@235
   149
dirk@689
   150
    func testSendMailFail() {
dirk@689
   151
        testFetchFolders()
dirk@689
   152
dirk@819
   153
        let account = persistentSetup.model.insertAccountFromImapSmtpConnectInfo(
dirk@790
   154
            TestData.connectInfoWrongPassword)
dirk@689
   155
dirk@689
   156
        createSpecialFolders(account)
dirk@689
   157
dirk@689
   158
        guard let outFolder = persistentSetup.model.folderByType(
dirk@784
   159
            .localOutbox, email:account.email) else {
dirk@689
   160
                XCTAssertTrue(false)
dirk@689
   161
                return
dirk@689
   162
        }
dirk@689
   163
        XCTAssertEqual(outFolder.messages.count, 0)
dirk@689
   164
dirk@689
   165
        let msg = createMail()
dirk@784
   166
        let exp = expectation(description: "mailSent")
dirk@689
   167
        persistentSetup.grandOperator.sendMail(msg, account: account, completionBlock: { error in
dirk@689
   168
            XCTAssertNotNil(error)
dirk@689
   169
            exp.fulfill()
dirk@689
   170
        })
dirk@784
   171
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@689
   172
            XCTAssertNil(error)
dirk@689
   173
        })
dirk@689
   174
dirk@689
   175
        XCTAssertEqual(outFolder.messages.count, 1)
dirk@689
   176
    }
dirk@689
   177
dirk@235
   178
    func testSaveDraft() {
dirk@819
   179
        let account = persistentSetup.model.insertAccountFromImapSmtpConnectInfo(
dirk@790
   180
            TestData.connectInfo)
dirk@692
   181
dirk@784
   182
        let expFoldersFetched = expectation(description: "foldersFetched")
dirk@692
   183
        persistentSetup.grandOperator.fetchFolders(correct, completionBlock: { error in
dirk@235
   184
            XCTAssertNil(error)
dirk@692
   185
            expFoldersFetched.fulfill()
dirk@235
   186
        })
dirk@784
   187
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@235
   188
            XCTAssertNil(error)
dirk@235
   189
        })
dirk@692
   190
dirk@692
   191
        guard let draftsFolder = persistentSetup.model.folderByType(
dirk@784
   192
            .drafts, email: account.email) else {
dirk@692
   193
                XCTAssertTrue(false)
dirk@692
   194
                return
dirk@692
   195
        }
dirk@692
   196
dirk@784
   197
        let expDraftsFetched = expectation(description: "expDraftsFetched")
dirk@819
   198
        persistentSetup.grandOperator.fetchEmailsAndDecryptImapSmtp(connectInfos: 
dirk@692
   199
            [account.connectInfo], folderName: draftsFolder.name, completionBlock: { error in
dirk@692
   200
                XCTAssertNil(error)
dirk@692
   201
                expDraftsFetched.fulfill()
dirk@692
   202
        })
dirk@784
   203
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@692
   204
            XCTAssertNil(error)
dirk@692
   205
        })
dirk@692
   206
dirk@692
   207
        let numDrafts = draftsFolder.messages.count
dirk@692
   208
dirk@692
   209
        let msg = createMail()
dirk@784
   210
        let expDraftSaved = expectation(description: "expDraftSaved")
dirk@692
   211
        persistentSetup.grandOperator.saveDraftMail(
dirk@692
   212
            msg, account: account, completionBlock: { error in
dirk@692
   213
                XCTAssertNil(error)
dirk@692
   214
                expDraftSaved.fulfill()
dirk@692
   215
        })
dirk@784
   216
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@692
   217
            XCTAssertNil(error)
dirk@692
   218
        })
dirk@692
   219
dirk@784
   220
        let expDraftsFetched2 = expectation(description: "expDraftsFetched2")
dirk@819
   221
        persistentSetup.grandOperator.fetchEmailsAndDecryptImapSmtp(connectInfos: 
dirk@692
   222
            [account.connectInfo], folderName: draftsFolder.name, completionBlock: { error in
dirk@692
   223
                XCTAssertNil(error)
dirk@692
   224
                expDraftsFetched2.fulfill()
dirk@692
   225
        })
dirk@784
   226
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@692
   227
            XCTAssertNil(error)
dirk@692
   228
        })
dirk@692
   229
dirk@692
   230
        XCTAssertGreaterThan(draftsFolder.messages.count, numDrafts)
dirk@235
   231
    }
dirk@678
   232
dirk@678
   233
    func testSyncFlags() {
dirk@784
   234
        let emailsFetched = expectation(description: "emailsFetched")
dirk@819
   235
        persistentSetup.grandOperator.fetchEmailsAndDecryptImapSmtp(connectInfos: 
dirk@680
   236
            [persistentSetup.connectionInfo], folderName: nil, completionBlock: { error in
dirk@680
   237
                XCTAssertNil(error)
dirk@680
   238
                emailsFetched.fulfill()
dirk@680
   239
        })
dirk@784
   240
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@680
   241
            XCTAssertNil(error)
dirk@680
   242
        })
dirk@678
   243
dirk@678
   244
        guard let inbox = persistentSetup.model.folderByType(
dirk@784
   245
            .inbox, email: persistentSetup.accountEmail) else {
dirk@678
   246
                XCTAssertTrue(false)
dirk@678
   247
                return
dirk@678
   248
        }
dirk@678
   249
dirk@776
   250
        XCTAssertGreaterThan(inbox.messages.count, 0)
dirk@776
   251
dirk@680
   252
        var expectations = [XCTestExpectation]()
dirk@801
   253
        var counter = 5
dirk@678
   254
        for elm in inbox.messages {
dirk@801
   255
            guard counter > 0 else {
dirk@801
   256
                break
dirk@801
   257
            }
dirk@802
   258
            guard let m = elm as? CdMessage else {
dirk@678
   259
                XCTAssertTrue(false)
dirk@678
   260
                break
dirk@678
   261
            }
dirk@784
   262
            m.flagFlagged = NSNumber.init(value: !m.flagFlagged.boolValue as Bool)
dirk@678
   263
            m.updateFlags()
dirk@680
   264
dirk@790
   265
            let exp = expectation(description: "flagsSynced\(counter)")
dirk@680
   266
            expectations.append(exp)
dirk@801
   267
            counter -= 1
dirk@680
   268
            persistentSetup.grandOperator.syncFlagsToServerForFolder(
dirk@680
   269
                m.folder, completionBlock: { error in
dirk@680
   270
                    XCTAssertNil(error)
dirk@680
   271
                    exp.fulfill()
dirk@680
   272
            })
dirk@678
   273
        }
dirk@784
   274
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@678
   275
            XCTAssertNil(error)
dirk@678
   276
        })
dirk@678
   277
    }
dirk@737
   278
dirk@737
   279
    func testDeleteFolder() {
dirk@740
   280
        testFetchFolders()
dirk@737
   281
dirk@784
   282
        let expFolderCreated = expectation(description: "expFolderCreated")
dirk@737
   283
        persistentSetup.grandOperator.createFolderOfType(
dirk@784
   284
        persistentSetup.account, folderType: .drafts) { error in
dirk@737
   285
            XCTAssertNil(error)
dirk@737
   286
            expFolderCreated.fulfill()
dirk@737
   287
        }
dirk@784
   288
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@737
   289
            XCTAssertNil(error)
dirk@737
   290
        })
dirk@737
   291
dirk@737
   292
        guard let draftsFolder = persistentSetup.model.folderByType(
dirk@784
   293
            .drafts, email: persistentSetup.accountEmail) else {
dirk@737
   294
                XCTAssertTrue(false)
dirk@737
   295
                return
dirk@737
   296
        }
dirk@737
   297
dirk@784
   298
        let expFolderDeleted = expectation(description: "expFolderDeleted")
dirk@737
   299
        persistentSetup.grandOperator.deleteFolder( draftsFolder) { error in
dirk@737
   300
            XCTAssertNil(error)
dirk@737
   301
            expFolderDeleted.fulfill()
dirk@737
   302
        }
dirk@784
   303
        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
dirk@737
   304
            XCTAssertNil(error)
dirk@737
   305
        })
dirk@737
   306
dirk@784
   307
        XCTAssertNil(persistentSetup.model.folderByType(.drafts,
dirk@737
   308
            email: persistentSetup.accountEmail))
dirk@740
   309
dirk@740
   310
        testFetchFolders()
dirk@740
   311
dirk@784
   312
        XCTAssertNil(persistentSetup.model.folderByType(.drafts,
dirk@740
   313
            email: persistentSetup.accountEmail))
dirk@737
   314
    }
dirk@784
   315
}