IOS-1518 Move SpecialUseMailboxesTest. IOS-1518
authorDirk Zimmermann <dz@pep.security>
Tue, 14 May 2019 16:19:30 +0200
branchIOS-1518
changeset 8603b2a9c31628ad
parent 8602 736f8c97aedd
child 8604 c1a6caacbdcf
IOS-1518 Move SpecialUseMailboxesTest.
pEpForiOS.xcodeproj/project.pbxproj
pEpForiOSTests/SpecialUseMailboxesTest.swift
     1.1 --- a/pEpForiOS.xcodeproj/project.pbxproj	Tue May 14 16:19:30 2019 +0200
     1.2 +++ b/pEpForiOS.xcodeproj/project.pbxproj	Tue May 14 16:19:30 2019 +0200
     1.3 @@ -156,7 +156,6 @@
     1.4  		15A536902155136800CF6204 /* PEPUtilTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15A5368F2155136800CF6204 /* PEPUtilTest.swift */; };
     1.5  		15B220501FBF5D6E00CA52BA /* InfoPlist.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15B2204F1FBF5D6E00CA52BA /* InfoPlist.swift */; };
     1.6  		15B220521FC2DAE200CA52BA /* InfoPlist+pEpStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15B220511FC2DAE200CA52BA /* InfoPlist+pEpStrings.swift */; };
     1.7 -		15B483DB1F28E2FC000FB2CF /* SpecialUseMailboxesTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15B483DA1F28E2FC000FB2CF /* SpecialUseMailboxesTest.swift */; };
     1.8  		15BA536E20A095410090F126 /* UnifiedInboxTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15BA536D20A095410090F126 /* UnifiedInboxTest.swift */; };
     1.9  		15BBBC6B1FD0527200B9DCC8 /* DisplayUserErrorTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15BBBC6A1FD0527200B9DCC8 /* DisplayUserErrorTest.swift */; };
    1.10  		15BBBC6C1FD05F4300B9DCC8 /* DisplayUserError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 152130531FD00B7A00688DF2 /* DisplayUserError.swift */; };
    1.11 @@ -626,7 +625,6 @@
    1.12  		15A5368F2155136800CF6204 /* PEPUtilTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PEPUtilTest.swift; sourceTree = "<group>"; };
    1.13  		15B2204F1FBF5D6E00CA52BA /* InfoPlist.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoPlist.swift; sourceTree = "<group>"; };
    1.14  		15B220511FC2DAE200CA52BA /* InfoPlist+pEpStrings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "InfoPlist+pEpStrings.swift"; sourceTree = "<group>"; };
    1.15 -		15B483DA1F28E2FC000FB2CF /* SpecialUseMailboxesTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpecialUseMailboxesTest.swift; sourceTree = "<group>"; };
    1.16  		15BA536D20A095410090F126 /* UnifiedInboxTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnifiedInboxTest.swift; sourceTree = "<group>"; };
    1.17  		15BA537B20A1F5F50090F126 /* MoveToAccountViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoveToAccountViewController.swift; sourceTree = "<group>"; };
    1.18  		15BBBC6A1FD0527200B9DCC8 /* DisplayUserErrorTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayUserErrorTest.swift; sourceTree = "<group>"; };
    1.19 @@ -1780,7 +1778,6 @@
    1.20  				43C3B15F2003851100ED48A4 /* DecryptImportedMessagesTests.swift */,
    1.21  				431E58FB1ED5926B00EFA77F /* AccountVerificationServiceTests.swift */,
    1.22  				436795F71EE98B9A00B03E23 /* MessageReevalutionTests.swift */,
    1.23 -				15B483DA1F28E2FC000FB2CF /* SpecialUseMailboxesTest.swift */,
    1.24  				43F7F0791F6AD44600BDF151 /* HandshakeTests.swift */,
    1.25  				431F987E1F6FD3E300A1E4D2 /* HandshakePartnerTableViewCellViewModelTests.swift */,
    1.26  				1541D7EF1FC81ED900D52A5D /* URL+ExtensionsTest.swift */,
    1.27 @@ -3004,7 +3001,6 @@
    1.28  				00172F462163C2EE0079B189 /* SettingsSectionViewModelTest.swift in Sources */,
    1.29  				00172F432163AC110079B189 /* SettingsViewModelTest.swift in Sources */,
    1.30  				438281831E891B7E00087343 /* DateTests.swift in Sources */,
    1.31 -				15B483DB1F28E2FC000FB2CF /* SpecialUseMailboxesTest.swift in Sources */,
    1.32  				151F71FE202A06760057C74D /* CdMessage+TestUtils.swift in Sources */,
    1.33  				151F71F9202A06760057C74D /* ReplicationServiceObserver.swift in Sources */,
    1.34  				150E18C0219D8C6C009E9048 /* ComposeViewModelSectionTest.swift in Sources */,
     2.1 --- a/pEpForiOSTests/SpecialUseMailboxesTest.swift	Tue May 14 16:19:30 2019 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,218 +0,0 @@
     2.4 -//
     2.5 -//  SpecialUseMailboxesTest.swift
     2.6 -//  pEpForiOS
     2.7 -//
     2.8 -//  Created by buff on 26.07.17.
     2.9 -//  Copyright © 2017 p≡p Security S.A. All rights reserved.
    2.10 -//
    2.11 -
    2.12 -import XCTest
    2.13 -import CoreData
    2.14 -import MessageModel
    2.15 -@testable import pEpForiOS
    2.16 -
    2.17 -class SpecialUseMailboxesTest: CoreDataDrivenTestBase {
    2.18 -    /// This test makes sense only for a Yahoo account as special-use
    2.19 -    /// mailbox names and purposes will differ using other providers. 
    2.20 -    /// Or Special-Use Mailboxes are not even supported by the server.
    2.21 -    func testSpecialMailboxesAndRequiredFolders() {
    2.22 -        let imapSyncData = ImapSyncData(connectInfo: imapConnectInfo)
    2.23 -        let errorContainer = ErrorContainer()
    2.24 -
    2.25 -        let imapLogin = LoginImapOperation(parentName: #function,
    2.26 -                                           errorContainer: errorContainer,
    2.27 -                                           imapSyncData: imapSyncData)
    2.28 -        imapLogin.completionBlock = {
    2.29 -            imapLogin.completionBlock = nil
    2.30 -            XCTAssertNotNil(imapSyncData.sync)
    2.31 -        }
    2.32 -
    2.33 -        let expFoldersFetched = expectation(description: "expFoldersFetched")
    2.34 -        guard let syncFoldersOp = SyncFoldersFromServerOperation(parentName: #function,
    2.35 -                                                                 imapSyncData: imapSyncData)
    2.36 -            else {
    2.37 -                XCTFail()
    2.38 -                return
    2.39 -        }
    2.40 -        syncFoldersOp.addDependency(imapLogin)
    2.41 -        syncFoldersOp.completionBlock = {
    2.42 -            syncFoldersOp.completionBlock = nil
    2.43 -            guard let allFolders = CdFolder.all() as? [CdFolder] else {
    2.44 -                XCTFail("No folders?")
    2.45 -                return
    2.46 -            }
    2.47 -            // triggers only for Yahoo accounts
    2.48 -            self.assertYahooFolderTypes(for: allFolders)
    2.49 -
    2.50 -            expFoldersFetched.fulfill()
    2.51 -        }
    2.52 -
    2.53 -        let expFoldersCreated = expectation(description: "expFoldersCreated")
    2.54 -        let createRequiredFoldersOp = CreateRequiredFoldersOperation(parentName: #function, imapSyncData: imapSyncData)
    2.55 -        createRequiredFoldersOp.addDependency(syncFoldersOp)
    2.56 -        createRequiredFoldersOp.completionBlock = {
    2.57 -            guard let allFolders = CdFolder.all() as? [CdFolder] else {
    2.58 -                XCTFail("No folders?")
    2.59 -                return
    2.60 -            }
    2.61 -            // triggers only for Yahoo accounts
    2.62 -            self.assert(yahooFolders: allFolders)
    2.63 -
    2.64 -            expFoldersCreated.fulfill()
    2.65 -        }
    2.66 -
    2.67 -        let queue = OperationQueue()
    2.68 -        queue.addOperation(imapLogin)
    2.69 -        queue.addOperation(syncFoldersOp)
    2.70 -        queue.addOperation(createRequiredFoldersOp)
    2.71 -
    2.72 -        waitForExpectations(timeout: TestUtil.waitTime, handler: { error in
    2.73 -            XCTAssertNil(error)
    2.74 -            XCTAssertFalse(imapLogin.hasErrors())
    2.75 -            XCTAssertFalse(syncFoldersOp.hasErrors())
    2.76 -            XCTAssertFalse(createRequiredFoldersOp.hasErrors())
    2.77 -            XCTAssertTrue(self.existsFolderForEveryRequiredFolderType(in: self.cdAccount))
    2.78 -
    2.79 -            guard let allFolders = CdFolder.all() as? [CdFolder] else {
    2.80 -                XCTFail("No folders?")
    2.81 -                return
    2.82 -            }
    2.83 -            // triggers only for Yahoo accounts
    2.84 -            self.assert(yahooFolders: allFolders)
    2.85 -        })
    2.86 -    }
    2.87 -
    2.88 -    //MARK: - HELPER
    2.89 -
    2.90 -    //MARK: Only for Yahoo Accoounts
    2.91 -
    2.92 -
    2.93 -    /// Runs all Yahoo specific assertions on:
    2.94 -    ///
    2.95 -    /// - Parameter folders: folders fetched from a Yahoo account and created in pEp DB
    2.96 -    func assert(yahooFolders folders: [CdFolder]) {
    2.97 -        assertYahooFolderTypes(for: folders)
    2.98 -        assertOnlyRequiredFoldersCreated(for: folders)
    2.99 -    }
   2.100 -
   2.101 -    private let yahooFolderInfo = ["Bulk Mail":FolderType.spam,
   2.102 -                                   "Archive":.archive,
   2.103 -                                   "Draft":.drafts,
   2.104 -                                   "Inbox":.inbox,
   2.105 -                                   "Sent":.sent,
   2.106 -                                   "Trash":.trash]
   2.107 -
   2.108 -    /// Asserts all Yahoo folders have been created.
   2.109 -    ///
   2.110 -    /// - Parameter yahooFolders: folders fetched from a Yahoo account and created in pEp DB
   2.111 -    private func assertAllYahooFolderNamesExist(in yahooFolders:[CdFolder]) {
   2.112 -        guard let first = yahooFolders.first, let moc = first.managedObjectContext else {
   2.113 -            XCTFail("No folders")
   2.114 -            return
   2.115 -        }
   2.116 -        moc.performAndWait {
   2.117 -            guard let account = yahooFolders.first?.account,
   2.118 -                isYahooAccount(account:account)
   2.119 -                else {
   2.120 -                    return
   2.121 -            }
   2.122 -            let folderNames: [String] = yahooFolders.map { $0.name! }
   2.123 -            let yahooFolderNames = yahooFolderInfo.map { $0.0 }
   2.124 -            for yahooName in yahooFolderNames {
   2.125 -                XCTAssertTrue(folderNames.contains(yahooName))
   2.126 -            }
   2.127 -        }
   2.128 -    }
   2.129 -
   2.130 -    ///
   2.131 -    /// Assures all folders fetched from Yahoo exist and are saved with the correct folder type.
   2.132 -    ///
   2.133 -    /// - Parameter yahooFolders: folders fetched from a Yahoo account and created in pEp DB
   2.134 -    private func assertYahooFolderTypes(for yahooFolders:[CdFolder]) {
   2.135 -        guard let first = yahooFolders.first, let moc = first.managedObjectContext else {
   2.136 -            XCTFail("No folders")
   2.137 -            return
   2.138 -        }
   2.139 -        moc.performAndWait {
   2.140 -            guard let account = yahooFolders.first?.account,
   2.141 -                isYahooAccount(account:account)
   2.142 -                else {
   2.143 -                    return
   2.144 -            }
   2.145 -            assertAllYahooFolderNamesExist(in: yahooFolders)
   2.146 -            for folder in yahooFolders {
   2.147 -                if folder.name! == "Bulk Mail" {
   2.148 -                    XCTAssertTrue(folder.folderType == .spam)
   2.149 -                } else if folder.name! == "Archive" {
   2.150 -                    XCTAssertTrue(folder.folderType == .archive)
   2.151 -                } else if folder.name! == "Draft" {
   2.152 -                    XCTAssertTrue(folder.folderType == .drafts)
   2.153 -                } else if folder.name! == "Inbox" {
   2.154 -                    XCTAssertTrue(folder.folderType == .inbox)
   2.155 -                } else if folder.name! == "Sent" {
   2.156 -                    XCTAssertTrue(folder.folderType == .sent)
   2.157 -                } else if folder.name! == "Trash" {
   2.158 -                    XCTAssertTrue(folder.folderType == .trash)
   2.159 -                }
   2.160 -            }
   2.161 -        }
   2.162 -    }
   2.163 -
   2.164 -    /// Some folders should not be created. Yahoo has a folder "Draft" with special
   2.165 -    /// use purpose \Drafts, so the pEp required folder "Drafts" (leading "s") should not be created.
   2.166 -    /// Folders are:
   2.167 -    ///  pep:    Drafts, Spam
   2.168 -    /// yahooo:  Draft,  Bulkmail
   2.169 -    ///
   2.170 -    /// - Parameter yahooFolders: folders fetched from a Yahoo account and created in pEp DB
   2.171 -    private func assertOnlyRequiredFoldersCreated(for yahooFolders:[CdFolder]) {
   2.172 -        guard let first = yahooFolders.first, let moc = first.managedObjectContext else {
   2.173 -            XCTFail("No folders")
   2.174 -            return
   2.175 -        }
   2.176 -        moc.performAndWait {
   2.177 -            // return if we are not dealing with a Yahoo account
   2.178 -            guard let account = yahooFolders.first?.account,
   2.179 -                isYahooAccount(account:account)
   2.180 -                else {
   2.181 -                    return
   2.182 -            }
   2.183 -            for folder in yahooFolders {
   2.184 -                let pEpNamesThatDifferFromYahooNames = ["Drafts", "Spam"]
   2.185 -                let nonRequiredFoldersHaveBeenCreated = pEpNamesThatDifferFromYahooNames.contains(folder.name!)
   2.186 -                XCTAssertFalse(nonRequiredFoldersHaveBeenCreated)
   2.187 -            }
   2.188 -        }
   2.189 -    }
   2.190 -
   2.191 -    private func isYahooAccount(account: CdAccount) -> Bool {
   2.192 -        if (account.identity?.address?.contains("yahoo"))! {
   2.193 -            return true
   2.194 -        }
   2.195 -        return false
   2.196 -    }
   2.197 -
   2.198 -    private func existsFolderForEveryRequiredFolderType(in account: CdAccount) -> Bool {
   2.199 -        for type in FolderType.requiredTypes {
   2.200 -            if !existsFolder(for: type, in: account) {
   2.201 -                return false
   2.202 -            }
   2.203 -        }
   2.204 -        return true
   2.205 -    }
   2.206 -
   2.207 -    private func existsFolder(`for` folderType: FolderType, in account: CdAccount) -> Bool{
   2.208 -        guard let origFolders = account.folders,
   2.209 -        let folders = Array(origFolders) as? [CdFolder]
   2.210 -            else {
   2.211 -            return false
   2.212 -        }
   2.213 -        for folder in folders {
   2.214 -            if folder.folderType == folderType {
   2.215 -                return true
   2.216 -            }
   2.217 -        }
   2.218 -        return false
   2.219 -    }
   2.220 -
   2.221 -}