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