def test_crud_init_add_new_user(self): # _INIT_ crud = CRUD(True) self.assertEqual(crud.read_users_file(), {}) # add_new_user self.assertEqual(crud.add_new_user("*****@*****.**", "2020-11-16"), True) self.assertEqual(crud.read_users_file().get('1'), self.user_2)
def test_crud_add_new_user_add_new_user(self): crud = CRUD(True) # add_new_user self.assertEqual(crud.add_new_user("*****@*****.**", "2020-11-16"), True) self.assertEqual(crud.read_users_file().get('1'), self.user_2) # add_new_user self.assertEqual(crud.add_new_user("*****@*****.**", "2020-11-16"), True) self.assertEqual(crud.read_users_file().get('2'), self.user)
def test_crud_add_new_user_remove_user(self): crud = CRUD(True) # add_new_user crud.add_new_user("*****@*****.**", "2020-11-16") self.assertEqual(crud.read_users_file().get('1'), self.user_2) # remove_user self.assertEqual(crud.remove_user(1), True) self.assertEqual(crud.read_users_file(), {})
def test_crud_add_new_user_2(self): crud = CRUD(False) crud.add_new_user("*****@*****.**", "2020-11-16") crud.update_users('2', 'Groups', 'friends') crud.remove_user(1) crud.remove_user_group('2', 'default') expected_user = self.user_2 expected_user["Groups"].append('friends') expected_user["Groups"].remove('default') print(crud.read_users_file()) self.assertEqual(crud.read_users_file(), {'2': expected_user})
def test_crud_update_users_6(self): crud = CRUD(False) crud.update_users('1', 'SpamN', 5) crud.remove_user(1) crud.remove_user_group('1', 'default') crud.add_new_user('*****@*****.**', '2020-11-16') self.assertEqual(crud.read_users_file(), {'1': self.user_2})
def test_crud_update_users_3(self): crud = CRUD(False) crud.update_users('1', 'Trust', 15) crud.remove_user_group('1', 'colleague') crud.remove_user(1) crud.add_new_user('*****@*****.**', '2020-11-16') self.assertEqual(crud.read_users_file(), {'1': self.user_2})
def test_crud_remove_user_group_4(self): crud = CRUD(False) crud.remove_user_group('1', 'colleagues') crud.remove_user(1) crud.update_users('1', 'Trust', 10) crud.add_new_user("*****@*****.**", "2020-11-16") expected_user = self.user_2 self.assertEqual(crud.read_users_file(), {'1': expected_user})
def test_crud_update_users_5(self): crud = CRUD(False) crud.update_users('1', 'HamN', 2) crud.remove_user(1) crud.add_new_user('*****@*****.**', '2020-11-16') crud.remove_user_group('1', 'default') expected_user = self.user_2 expected_user['Groups'].remove('default') self.assertEqual(crud.read_users_file(), {'1': expected_user})
def test_crud_remove_user_3(self): crud = CRUD(False) crud.remove_user(1) crud.remove_user_group('1', 'default') crud.add_new_user('*****@*****.**', '2020-11-16') crud.update_users('1', 'Trust', 15) expected_user = self.user_2 expected_user['Trust'] = 15 self.assertEqual(crud.read_users_file(), {'1': expected_user})
def test_crud_add_new_user_5(self): crud = CRUD(False) crud.add_new_user("*****@*****.**", "2020-11-16") crud.remove_user_group('1', 'default') crud.update_users('2', 'SpamN', 1) crud.remove_user(1) expected_user = self.user_2 expected_user["SpamN"] = 1 self.assertEqual(crud.read_users_file(), {'2': expected_user})
def test_crud_add_new_user_get_user_data(self): crud = CRUD(True) # add_new_user crud.add_new_user("*****@*****.**", "2020-11-16") self.assertEqual(crud.read_users_file().get('1'), self.user_2) # get_user_data trust1 = crud.get_user_data(1, 'Trust') self.assertEqual(trust1, 50)
def test_crud_add_new_user_1(self): crud = CRUD(False) crud.add_new_user("*****@*****.**", "2020-11-16") crud.update_users('2', "Trust", 60) crud.remove_user_group('2', 'default') crud.remove_user(1) modified_user = self.user_2 modified_user["Groups"].remove('default') modified_user["Trust"] = 60 self.assertEqual(crud.read_users_file(), {'2': modified_user})
def test_crud_update_users_4(self): crud = CRUD(False) crud.update_users('1', 'Trust', 20) crud.remove_user_group('1', 'colleagues') crud.add_new_user('*****@*****.**', '2020-11-16') crud.remove_user(2) expected_user = self.user expected_user['Trust'] = 20 expected_user['Groups'].remove('colleagues') self.assertEqual(crud.read_users_file(), {'1': expected_user})
def test_crud_update_users_2(self): crud = CRUD(False) crud.update_users('1', 'Date_of_last_seen_message', 1605762353) crud.add_new_user('*****@*****.**', '2020-11-16') crud.remove_user_group('1', 'default') crud.remove_user(2) expected_user = self.user expected_user['Date_of_last_seen_message'] = 1605762353 expected_user['Groups'].remove('default') self.assertEqual(crud.read_users_file(), {'1': expected_user})
def test_crud_remove_user_2(self): crud = CRUD(False) crud.remove_user(1) crud.add_new_user("*****@*****.**", "2020-11-16") crud.remove_user_group('1', 'default') crud.update_users('1', 'Trust', 95) expected_user = self.user_2 expected_user['Trust'] = 95 expected_user['Groups'].remove('default') self.assertEqual(crud.read_users_file(), {'1': expected_user})
def test_crud_add_new_user_3(self): crud = CRUD(False) crud.add_new_user("*****@*****.**", "2020-11-16") crud.remove_user(2) crud.update_users('1', 'Trust', 75) crud.remove_user_group('1', 'default') expected_user = self.user expected_user["Groups"].remove('default') expected_user["Trust"] = 75 self.assertEqual(crud.read_users_file(), {'1': expected_user})
def test_crud_update_users_remove_user(self): crud = CRUD(True) crud.add_new_user("*****@*****.**", "2020-11-16") crud.update_users(1, "Trust", 0) self.assertEqual(crud.get_user_data(1, 'Trust'), 0) # remove_user self.assertEqual(crud.remove_user(1), True) self.assertEqual(crud.read_users_file(), {})
def test_crud_add_new_user_update_users(self): crud = CRUD(True) # add_new_user crud.add_new_user("*****@*****.**", "2020-11-16") self.assertEqual(crud.read_users_file().get('1'), self.user_2) # update_users crud.update_users(1, "Trust", 0) self.assertEqual(crud.get_user_data(1, 'Trust'), 0)
def test_crud_add_new_user_remove_user_group(self): crud = CRUD(True) # add_new_user crud.add_new_user("*****@*****.**", "2020-11-16") self.assertEqual(crud.read_users_file().get('1'), self.user_2) # remove_user_group crud.remove_user_group(1, 'default') self.assertEqual(crud.get_user_data(1, 'Groups'), [])
def test_crud_remove_user_group_remove_user(self): crud = CRUD(True) crud.add_new_user("*****@*****.**", "2020-11-16") # remove_user_group crud.remove_user_group(1, 'default') self.assertEqual(crud.get_user_data(1, 'Groups'), []) # remove_user self.assertEqual(crud.remove_user(1), True) self.assertEqual(crud.read_users_file(), {})
def test_crud_get_user_data_remove_user(self): crud = CRUD(True) crud.add_new_user("*****@*****.**", "2020-11-16") # get_user_data trust1 = crud.get_user_data(1, 'Trust') self.assertEqual(trust1, 50) # remove_user self.assertEqual(crud.remove_user(1), True) self.assertEqual(crud.read_users_file(), {})
def test_crud_remove_user_group_add_new_user(self): crud = CRUD(True) crud.add_new_user("*****@*****.**", "2020-11-16") # remove_user_group crud.remove_user_group(1, 'default') self.assertEqual(crud.get_user_data(1, 'Groups'), []) # add_new_user self.assertEqual(crud.add_new_user("*****@*****.**", "2020-11-16"), True) self.assertEqual(crud.read_users_file().get('2'), self.user)
def test_crud_get_user_data_add_new_user(self): crud = CRUD(True) crud.add_new_user("*****@*****.**", "2020-11-16") # get_user_data trust1 = crud.get_user_data(1, 'Trust') self.assertEqual(trust1, 50) # add_new_user self.assertEqual(crud.add_new_user("*****@*****.**", "2020-11-16"), True) self.assertEqual(crud.read_users_file().get('1'), self.user_2)
class RENEGE: """Class pour realiser le filtrage du spam en utilisant vocabular.json file et CRUD et EmalAnalyze classes""" def __init__(self): self.email_file = "800-mails.json" self.crud = CRUD() self.e_mail = EmailAnalyzer() def calculate_user_trust(self, user_id): #extracting json data date_of_first_seen_message = self.crud.get_user_data(user_id, "Date_of_first_seen_message") date_of_last_seen_message = self.crud.get_user_data(user_id, "Date_of_last_seen_message") n_ham = self.crud.get_user_data(user_id, "HamN") n_spam = self.crud.get_user_data(user_id, "SpamN") groups = self.crud.get_user_data(user_id, "Groups") #calculate the sum of trust values of all groups sum_trust = 0 for group in groups : group_id = self.crud.get_group_id(group) sum_trust += self.crud.get_group_data(group_id, 'Trust') #now that we have all the needed vars, calculate trust1, trust2 and trust trust1 = (date_of_last_seen_message * n_ham) / (date_of_first_seen_message * (n_ham + n_spam)) trust2 = sum_trust / len(groups) trust = (trust1 + trust2) / 2 if trust2 < 50: trust = trust2 if trust1 > 100: trust = 100 #before returning a value, check if trust is between 0 and 100 if trust < 0: trust = 0 elif trust > 100: trust = 100 return trust def classify_emails(self): ''' fonction deja implemente Description: fonction pour commencer l'analyse des e-mails. Sortie: bool, 'True' pour succes, 'False' dans le cas de failure. ''' try: self.process_email(self.get_email()) return True except Exception as e: raise Exception return False def process_email(self, new_emails): ''' Description: fonction pour analyser chaque nouvel e-mail dans le dictionare. Elle gere l'ajout des nouveux utilisateurs et/ou modification de l'information existante sur les utilisateurs et groupes. Sortie: bool, 'True' pour succes, 'False' dans le cas de failure. ''' print(type(new_emails)) emails = new_emails["dataset"] for email in emails: email_adr = email['mail']['From'] date = email['mail']['Date'] spam = email['mail']['Spam'] spam = spam == 'true' and True or False if self.crud.get_user_id(email_adr): self.update_user_info(email_adr, date, spam) else: self.add_user_info(email_adr, date) return True def update_user_info(self, new_user_email, new_user_date, new_email_spam): ''' Description: fonction pour modifier l'information de utilisateur (date de dernier message arrive, numero de spam/ham, trust level, etc). Sortie: bool, 'True' pour succes, 'False' dans le cas de failure. ''' users_dict = self.crud.read_users_file() for key in users_dict: user = users_dict.get(key) if user.get("name") == new_user_email: self.crud.update_users(key, 'Date_of_first_seen_message', new_user_date) if new_email_spam: spamN = user.get('SpamN') spamN += 1 trust = user.get('Trust') if trust > 0: trust -= 1 self.crud.update_users(key, 'SpamN', spamN) self.crud.update_users(key, 'Trust', trust) else: hamN = user.get('HamN') hamN += 1 trust = user.get('Trust') if trust < 100: trust += 1 self.crud.update_users(key, 'HamN', hamN) self.crud.update_users(key, 'HamN', trust) break return True def add_user_info(self, user_email, user_date): self.crud.add_new_user(user_email, user_date) def update_group_info(self, user_group_list): ''' Description: fonction pour modifier l'information de groupe dans lequel l'utilisater est present (trust level, etc). Sortie: bool, 'True' pour succes, 'False' dans le cas de failure. ''' groups = self.crud.read_groups_file() for group in groups: if group['List_of_members'] == user_group_list: group['Trust'] = self.crud.get_user_data(user_group_list[1], 'Trust') return True return False def get_user_email_list(self): ''' Description: fonction pour creer le liste des e-mails (noms) des utilisateurs uniques. Sortie: liste des uniques e-mails des utilisateurs ''' emails = self.get_email() list_of_email_addresses = [] for mail in emails['dataset']: list_of_email_addresses.append((mail['mail']['From'])) return list_of_email_addresses def get_email(self): ''' Description: fonction pour lire le ficher json avec les mails et extraire les donees necessaire. Sortie: dictionare de e-mails formate selon le JSON. ''' with open(self.email_file) as file: emails = json.load(file) return emails
def test_get_users_data(self): crud = CRUD(False) crud.modify_users_file(self.users_data) self.assertEqual(crud.read_users_file(), self.users_data)
def test_crud_constructor(self): crud = CRUD(False) self.assertEqual(crud.read_users_file(), {"1": self.user}) self.assertEqual(crud.read_groups_file(), {})