Skip to content
Snippets Groups Projects
Commit 17907009 authored by Viktoria's avatar Viktoria
Browse files

add resetAddressBook()

parent 9f9dbb52
No related branches found
No related tags found
No related merge requests found
......@@ -301,7 +301,7 @@ class DataHandler {
self.deleteNum("PersistentMail", type: "uid", search: uid)
}
private func removeAll(entity: String) {
func removeAll(entity: String) {
let DelAllReqVar = NSBatchDeleteRequest(fetchRequest: NSFetchRequest<NSFetchRequestResult>(entityName: entity))
do {
try managedObjectContext.execute(DelAllReqVar)
......
// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationModel.swift at 2020-03-26 15:41:47 +0000
// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationModel.swift at 2020-03-30 07:41:15 +0000
//
// AuthenticationModel.swift
......@@ -653,7 +653,7 @@ import Foundation
}
// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationViewModel.swift at 2020-03-26 15:41:47 +0000
// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationViewModel.swift at 2020-03-30 07:41:15 +0000
//
// AuthenticationViewModel.swift
......
......@@ -2,19 +2,20 @@
//
import XCTest
import Contacts
//import Contacts
@testable import enzevalos_iphone
class MailComparisonTests: XCTestCase {
let datahandler = DataHandler.handler
let userAdr = "alice@example.com"
let userName = "alice"
var user: MCOAddress = MCOAddress.init(mailbox: "alice@example.com")
var user: MCOAddress = MCOAddress.init(displayName: "alice", mailbox: "alice@example.com")
let userAdr_1 = "bob@enzevalos.de"
let userName_1 = "bob"
var user_1: MCOAddress = MCOAddress.init(mailbox: "bob@enzevalos.de")
var user_1: MCOAddress = MCOAddress.init(displayName: "bob", mailbox: "bob@enzevalos.de")
let userAdr_2 = "carl@test.com"
let userName_2 = "carl"
var user_2: MCOAddress = MCOAddress.init(displayName: "Carl", mailbox: "carl@test.com")
override func setUp() {
super.setUp()
datahandler.reset()
......@@ -46,71 +47,106 @@ class MailComparisonTests: XCTestCase {
Code
******************/
@NSManaged public var eContact: EnzevalosContact
public func compareSenderToContacts(_ mail: PersistentMail? ) -> String {
let senderAddress = mail?.from.mailAddress
// 1. ich will wissen, ob der sender bekannt ist, deshalb erst einmal equal test mit sender und kontaktbuch durch cncontact
let enzContacts = datahandler.getContacts()
for contact in enzContacts {
if contact.isAddress(mailadr: senderAddress!){
return "is a contact"
func countMatches(_ mailAddr: String, _ inboxMails: [PersistentMail]) -> Int {
var senders: [String] = []
for sender in inboxMails{
let sender: String = sender.from.mailAddress
senders.append(sender)
}
/*
for cont in contacts{
let addr = mail?.from.contact?.cnContact?.getMailAddresses()
if addr != nil{
return "is a contact"
}
if cont.isAddress(mailadr: senderAddress!){
return "send mails before"
var numberOfFoundMatches = 0
for addr in senders{
if addr == mailAddr{
numberOfFoundMatches += 1
if numberOfFoundMatches == 2{
return numberOfFoundMatches
}
}
}
// 2. ich will wissen, ob der sender schon eine mail geschickt hat, wenn nicht im kontaktbuch, deshalb vergleich mit contact
let knownMailAddresses = contact.getMailAddresses()
for knownAddress in knownMailAddresses{
if knownAddress.mailAddress == senderAddress!{
return "has alrady send an email"
return numberOfFoundMatches
}
public func compareSenderToContacts(_ mail: PersistentMail? ) -> String {
let senderAddress = mail?.from.mailAddress
// Case 1: Is the address of the sender in the address book?
let enzContacts = datahandler.getContacts()
for contact in enzContacts {
let contMailAddr = contact.cnContact?.getMailAddresses()
if contMailAddr != nil{
for mAddr in contMailAddr!{
if mAddr.mailAddress == senderAddress {
return "is a contact"
}
}
}
*/
}
/*
if mail?.from.contact?.getMailAddresses() == senderAddress{
// Case 2: Is the address of the sender known through previous mails?
let inboxMails = datahandler.getAllPersistentMails()
if countMatches(senderAddress!, inboxMails) > 1{
return "has alrady send an email"
}*/
// 3. ich will wissen, ob wenn der sender unbekannt, eine ähnliche mailaddresse schon einmal ankam
}
// Case 3: The identity of an mail address is known, but not the (sub-)domain
return "false"
return "not known"
}
/**************
End Code
****************/
func createAddressBook(){
datahandler.reset()
_ = datahandler.getContactByMCOAddress(address: user)
datahandler.save(during: "createAddressBook")
@NSManaged public var addresses: NSSet
private func createAddressBook(_ senders: [MCOAddress]) {
for sender in senders {
let con = CNMutableContact()
let value = sender.mailbox! as AnyObject
let name = sender.displayName
let nameArray = name!.split(separator: " ").map(String.init)
con.givenName = nameArray.first!
con.emailAddresses.append(CNLabeledValue(label: CNLabelOther, value: value as! NSString))
// Saving the newly created contact
let store = CNContactStore()
let saveRequest = CNSaveRequest()
saveRequest.add(con, toContainerWithIdentifier:nil)
try! store.execute(saveRequest)
}
}
private func resetAddressBook(_ senders: [MCOAddress]) {
for sender in senders {
do {
let name = sender.displayName!
let predicate = CNContact.predicateForContacts(matchingName: name)
let store = CNContactStore()
let contacts = try store.unifiedContacts(matching: predicate,keysToFetch: [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactImageDataKey as CNKeyDescriptor])
guard contacts.count > 0, let selectedContact = contacts.first else { throw Error.self as! Error }
let request = CNSaveRequest()
let mutableContact = selectedContact.mutableCopy() as! CNMutableContact
request.delete(mutableContact)
try store.execute(request)
} catch { }
}
}
func testCompareSenderToContacts () {
createAddressBook()
let mail_1 = testMail(from: user, to: [], cc: [], bcc: [])
let mail_2 = testMail(from: user_1, to: [], cc: [], bcc: [])
let mail_3 = testMail(from: user_2, to: [], cc: [], bcc: [])
let mail_4 = testMail(from: user_2, to: [], cc: [], bcc: [])
let mailsInInbox: [PersistentMail] = [mail_1!, mail_2!, mail_3!, mail_4!]
createAddressBook([user])
XCTAssertEqual("is a contact", compareSenderToContacts(mailsInInbox[0]))
XCTAssertEqual("false", compareSenderToContacts(mailsInInbox[1]))
// XCTAssertEqual("has alrady send an email", compareSenderToContacts(mailsInInbox[2]))
// XCTAssertEqual("false", compareSenderToContacts(mail_2))
XCTAssertNotEqual(testMail(from: user, to: [], cc: [], bcc: [])?.from.contact?.cnContact?.getMailAddresses().count, 0 )
XCTAssertTrue((testMail(from: user, to: [], cc: [], bcc: [])?.from.contact?.isAddress(mailadr: userAdr))!)
XCTAssertTrue((testMail(from: user_1, to: [], cc: [], bcc: [])?.from.contact?.isAddress(mailadr: userAdr_1))!)
XCTAssertEqual("not known", compareSenderToContacts(mailsInInbox[1]))
XCTAssertEqual("has alrady send an email", compareSenderToContacts(mailsInInbox[2]))
// The created user has to be deleted after the test
resetAddressBook([user])
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment