def test_add_new_group_Passes_correct_data_to_modify_groups_file( self, mock_modify_groups_file, mock_read_groups_file): crud = CRUD() mock_read_groups_file.return_value = self.groups_data crud.add_new_group("test", "50", ["test"]) mock_modify_groups_file.assert_called_once_with(self.groups_data) """Description: il faut utiliser les mocks des fonctions "read_groups_file",
def test_add_new_group_Passes_correct_data_to_modify_groups_file( self, mock_modify_groups_file, mock_read_groups_file, mock_read_users_file): mock_read_users_file.return_value = self.users_data mock_read_groups_file.return_value = self.groups_data crud = CRUD() crud.add_new_group("funsies", 50, ["*****@*****.**"]) mock_modify_groups_file.assert_called_once_with(self.groups_data) """Description: il faut utiliser les mocks des fonctions "read_groups_file",
def test_add_new_group_Passes_correct_data_to_modify_groups_file( self, mock_modify_groups_file, mock_read_groups_file ): mock_read_groups_file.return_value=self.groups_data crud=CRUD() name="roommates" Trust=100 List_of_members= [] crud.add_new_group(name,Trust, List_of_members) mock_modify_groups_file.assert_called_once_with(self.groups_data)
def test_add_new_group_Exception_Raised_to_modify_groups_file( self, mock_read_groups_file): crud = CRUD() mock_read_groups_file.return_value = None self.assertFalse(crud.add_new_group("test", "50", ["test"]))
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 = "1000-mails.json" self.crud = CRUD() self.e_mail = EmailAnalyzer() def calculate_user_trust(self, user_id): ''' Description: fonction a implementer pour la deuxieme partie du deuxieme travail pratique. Permet de calculer le trust d'un utilisateur specifique Sortie: int; Retourne la valeur du trust de l'utilisateur ''' # calculation for Trust1 nb_spam = self.crud.get_user_data(user_id, "SpamN") nb_ham = self.crud.get_user_data(user_id, "HamN") time_first_seen_message = self.crud.get_user_data( user_id, "Date_of_first_seen_message") time_last_seen_message = self.crud.get_user_data( user_id, "Date_of_last_seen_message") if (time_first_seen_message * (nb_ham + nb_spam)) == 0: print("trust1 not possible, division by zero") return trust1 = ((time_last_seen_message * nb_ham) / (time_first_seen_message * (nb_ham + nb_spam))) # calculation for Trust2 group_list = self.crud.read_groups_file() # in order to determine if user is part of a group, we need their email user_email = self.crud.get_user_data(user_id, "name") # number of groups that the user is in nb_group = 0 user_trust_total = 0 for group in group_list.values(): if user_email in group["List_of_members"]: nb_group += 1 user_trust_total += group["Trust"] trust2 = user_trust_total / nb_group # determining user's final trust value if trust2 < 50: return trust2 elif trust1 > 100: return 100 final_trust = (trust1 + trust2) / 2 if final_trust < 0: final_trust = 0 elif final_trust > 100: final_trust = 100 return final_trust def classify_emails(self, spam_definition_mode): ''' 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(), spam_definition_mode) return True except Exception as e: print("Error!", e.__class__, "occurred.") return False def process_email(self, new_emails, mode): ''' 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. ''' list_of_email_addresses = self.get_user_email_list() for user in list_of_email_addresses: # date de defaut est l'epoch linux self.crud.add_new_user(user, "1970-01-01") self.crud.add_new_group('default', 50, list_of_email_addresses) is_spam = '' for email in new_emails["dataset"]: individual_email = email["mail"] email_address = individual_email["From"] date = time.mktime( datetime.datetime.strptime(individual_email["Date"], "%Y-%m-%d").timetuple()) if mode == 0: is_spam = individual_email["Spam"] elif mode == 1: is_spam = self.is_spam1(email) elif mode == 2: is_spam = self.is_spam2(email) self.update_user_info(email_address, date, is_spam) self.update_group_info(list_of_email_addresses, 'default') def is_spam1(self, email): ''' Description: fonction qui calcule la probabilité de spam selon l'équation 1 de l'énoncé, soit S = P ∗ (H ∗ T1 + T2) + H ∗ T2 ∗ ¬T3 ''' user_id = self.crud.get_user_id(email["From"]) trust = self.crud.get_user_data(user_id, "Trust") first = self.crud.get_user_data(user_id, "Date_of_first_seen_message") last = self.crud.get_user_data(user_id, "Date_of_last_seen_message") p = email["Spam"] == "true" h = (float(last) - float(first)) / (60 * 60 * 24) > 31 t1 = trust < 60 t2 = trust < 70 t3 = trust > 75 return (p and ((h and t1) or t2)) or (h and t2 and not t3) def is_spam2(self, email): ''' Description: fonction qui calcule la probabilité de spam selon l'équation 2 de l'énoncé, soit S = P + ¬T3 ∗ T2 ''' user_id = self.crud.get_user_id(email["From"]) trust = self.crud.get_user_data(user_id, "Trust") p = email["Spam"] == "true" t2 = trust < 70 t3 = trust > 75 return p or (not t3 and t2) 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. ''' user_id = self.crud.get_user_id(new_user_email) date_first_seen_message = self.crud.get_user_data( user_id, "Date_of_first_seen_message") date_last_seen_message = self.crud.get_user_data( user_id, "Date_of_last_seen_message") try: if new_user_date < date_first_seen_message: self.crud.update_users(user_id, "Date_of_first_seen_message", new_user_date) elif new_user_date > date_last_seen_message: self.crud.update_users(user_id, "Date_of_last_seen_message", new_user_date) # Mise a jour des spamN/hamN et trust des utilisateurs user_spamN = self.crud.get_user_data(user_id, "SpamN") user_hamN = self.crud.get_user_data(user_id, "HamN") if new_email_spam == "true": user_spamN = user_spamN + 1 self.crud.update_users(user_id, "SpamN", user_spamN) else: user_hamN = user_hamN + 1 self.crud.update_users(user_id, "HamN", user_hamN) total_msg = user_spamN + user_hamN self.crud.update_users(user_id, "Trust", (user_hamN / total_msg) * 100) return True except: return False def update_group_info(self, user_group_list, group_name): ''' 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. ''' try: trust_sum = 0 for user in user_group_list: user_id = self.crud.get_user_id(user) trust_sum += self.crud.get_user_data(user_id, "Trust") # Calcul de la nouvelle moyenne de trust des membres group_trust = round(trust_sum / len(user_group_list), 0) group_id = self.crud.get_group_id(group_name) self.crud.update_groups(group_id, "Trust", group_trust) return True except: 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 ''' email_dict = self.get_email() existing_email_address = set() unique_emails = [] for email in email_dict["dataset"]: individual_email = email["mail"] email_address = individual_email["From"] if email_address in existing_email_address: continue existing_email_address.add(email_address) unique_emails.append(email_address) return unique_emails 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 email_file: return json.load(email_file)
class TestCRUD(unittest.TestCase): def setUp(self): self.crud = CRUD() # c'est un exemple de données "mock" à utiliser comme "return value" de read_users_file self.users_data = { "1": { "name": "*****@*****.**", "Trust": 100, "SpamN": 0, "HamN": 20, "Date_of_first_seen_message": 1596848266.0, "Date_of_last_seen_message": 1596848266.0, "Groups": ["default"], }, "2": { "name": "*****@*****.**", "Trust": 65.45454, "SpamN": 171, "HamN": 324, "Date_of_first_seen_message": 1596855166.0, "Date_of_last_seen_message": 1596855166.0, "Groups": ["default"], }, } # c'est un exemple de données "mock" à utiliser comme "return value" de read_groups_file self.groups_data = { "1": { "name": "default", "Trust": 50, "List_of_members": ["*****@*****.**", "*****@*****.**"], }, "2": { "name": "friends", "Trust": 90, "List_of_members": ["*****@*****.**"], }, } def tearDown(self): pass @patch("crud.CRUD.read_users_file") def test_add_new_user_returns_false_if_username_and_date_are_not_given( self, mock_read_users_file ): """Description: il faut utiliser les mocks des fonctions "read_users_file", pour vérifier que le nom d'utilisateur est donné à la fonction """ mock_read_users_file.return_value = self.users_data add_new_user_return_value_two_none = self.crud.add_new_user(None, None) self.assertFalse(add_new_user_return_value_two_none) @patch("crud.CRUD.read_users_file") def test_add_new_user_returns_false_if_only_date_is_given( self, mock_read_users_file ): """Description: il faut utiliser les mocks des fonctions "read_users_file", pour vérifier que le nom d'utilisateur est donné à la fonction """ mock_read_users_file.return_value = self.users_data add_new_user_return_value_one_none = self.crud.add_new_user(None, '2020-01-01') self.assertFalse(add_new_user_return_value_one_none) @patch("crud.CRUD.read_users_file") def test_add_new_user_returns_false_if_only_name_is_given( self, mock_read_users_file ): """Description: il faut utiliser les mocks des fonctions "read_users_file", pour vérifier que le nom d'utilisateur est donné à la fonction """ mock_read_users_file.return_value = self.users_data add_new_user_return_value_one_none = self.crud.add_new_user('*****@*****.**', None) self.assertFalse(add_new_user_return_value_one_none) @patch("crud.CRUD.read_users_file") def test_add_new_user_returns_true_if_name_and_date_are_given( self, mock_read_users_file ): """Description: il faut utiliser les mocks des fonctions "read_users_file", pour vérifier que le nom d'utilisateur est donné à la fonction """ mock_read_users_file.return_value = self.users_data add_new_user_return_value_correct_vals = self.crud.add_new_user('*****@*****.**', '2020-01-01') self.assertTrue(add_new_user_return_value_correct_vals) @patch("crud.CRUD.read_groups_file") def test_add_new_group_returns_false_if_name_is_too_short( self, mock_read_groups_file ): """Description: il faut utiliser les mocks des fonctions "read_groups_file",pour tester que l'information a ajouter pour le groupe a étée formée correctement """ mock_read_groups_file.return_value = self.groups_data add_new_group_no_name = self.crud.add_new_group("", 50, ['dummyUser1', 'dummyUser2']) self.assertFalse(add_new_group_no_name) @patch("crud.CRUD.read_groups_file") def test_add_new_group_returns_false_if_name_is_too_long( self, mock_read_groups_file ): """Description: il faut utiliser les mocks des fonctions "read_groups_file",pour tester que l'information a ajouter pour le groupe a étée formée correctement """ dummy_long_name = ("a" * 65) mock_read_groups_file.return_value = self.groups_data add_new_group_long_name = self.crud.add_new_group(dummy_long_name, 50, ['dummyUser1', 'dummyUser2']) self.assertFalse(add_new_group_long_name) @patch("crud.CRUD.read_groups_file") def test_add_new_group_returns_false_if_trust_is_more_than_100( self, mock_read_groups_file ): """Description: il faut utiliser les mocks des fonctions "read_groups_file",pour tester que l'information a ajouter pour le groupe a étée formée correctement """ mock_read_groups_file.return_value = self.groups_data add_new_group_tooBigTrust = self.crud.add_new_group("dummyName", 101, ['dummyUser1', 'dummyUser2']) self.assertFalse(add_new_group_tooBigTrust) @patch("crud.CRUD.read_groups_file") def test_add_new_group_returns_false_if_trust_is_less_than_0( self, mock_read_groups_file ): """Description: il faut utiliser les mocks des fonctions "read_groups_file",pour tester que l'information a ajouter pour le groupe a étée formée correctement """ mock_read_groups_file.return_value = self.groups_data add_new_group_too_small_trust = self.crud.add_new_group("dummyName", -1, ['dummyUser1', 'dummyUser2']) self.assertFalse(add_new_group_too_small_trust) @patch("crud.CRUD.read_groups_file") def test_add_new_group_returns_false_if_member_list_is_not_a_list( self, mock_read_groups_file ): """Description: il faut utiliser les mocks des fonctions "read_groups_file",pour tester que l'information a ajouter pour le groupe a étée formée correctement """ mock_read_groups_file.return_value = self.groups_data add_new_group_not_good_member_list = self.crud.add_new_group("dummyName", 50, 'not a list') self.assertFalse(add_new_group_not_good_member_list) @patch("crud.CRUD.read_groups_file") def test_add_new_group_returns_true_if_values_are_correct( self, mock_read_groups_file ): """Description: il faut utiliser les mocks des fonctions "read_groups_file",pour tester que l'information a ajouter pour le groupe a étée formée correctement """ mock_read_groups_file.return_value = self.groups_data add_new_group_correct_vals = self.crud.add_new_group("dummyName", 50, ['dummyUser1', 'dummyUser2']) self.assertTrue(add_new_group_correct_vals) @patch("crud.CRUD.read_users_file") def test_get_user_data_Returns_false_for_invalid_id(self, mock_read_users_file): """Description: il faut utiliser le mock de fonction "read_groups_file", (ou selon votre realisation) pour tester que false (ou bien une excepton) est returnee par la fonction si ID non-existant est utilisée il faut utiliser ".assertEqual()" ou ".assertFalse()" """ dummyBadId = 10 mock_read_users_file.return_value = self.users_data self.crud.number_of_users = 2 get_user_data_return_value = self.crud.get_user_data(dummyBadId, 'name') self.assertIsNone(get_user_data_return_value) @patch("crud.CRUD.read_users_file") def test_get_user_data_Returns_false_for_invalid_field(self, mock_read_users_file): """Description: il faut utiliser le mock de fonction "read_groups_file", (ou selon votre realisation) pour tester que false (ou bien une excepton) est returnee par la fonction si champ non-existant est utilisée il faut utiliser ".assertEqual()" ou ".assertFalse()" """ mock_read_users_file.return_value = self.users_data self.crud.number_of_users = 2 get_user_data_return_value = self.crud.get_user_data(1, 'badField') self.assertIsNone(get_user_data_return_value) @patch("crud.CRUD.read_users_file") def test_get_user_data_Returns_correct_value_if_field_and_id_are_valid( self, mock_read_users_file ): """Description: il faut utiliser le mock de fonction "read_groups_file", (ou selon votre realisation) pour tester que une bonne valeur est fournie si champ est id valide sont utilisee il faut utiliser ".assertEqual()" """ mock_read_users_file.return_value = self.users_data self.crud.number_of_users = 2 get_user_data_return_value = self.crud.get_user_data(1, 'name') self.assertEqual(get_user_data_return_value, "*****@*****.**") @patch("crud.CRUD.read_groups_file") def test_get_group_data_Returns_false_for_invalid_id(self, mock_read_groups_file): """""" dummyBadId = 10 mock_read_groups_file.return_value = self.groups_data self.crud.number_of_groups = 2 get_group_return_value = self.crud.get_group_data(dummyBadId, 'name') self.assertIsNone(get_group_return_value) @patch("crud.CRUD.read_groups_file") def test_get_group_data_Returns_false_for_invalid_field( self, mock_read_groups_file ): """""" mock_read_groups_file.return_value = self.groups_data self.crud.number_of_groups = 2 get_group_data_return_value = self.crud.get_group_data(1, 'badField') self.assertIsNone(get_group_data_return_value) @patch("crud.CRUD.read_groups_file") def test_get_group_data_Returns_correct_value_if_field_and_id_are_valid( self, mock_read_groups_file ): """""" mock_read_groups_file.return_value = self.groups_data self.crud.number_of_groups = 2 get_group_data_return_value = self.crud.get_group_data(1, 'name') self.assertEqual(get_group_data_return_value, 'default') @patch("crud.CRUD.read_users_file") def test_get_user_id_Returns_None_for_invalid_user_name( self, mock_read_users_file ): mock_read_users_file.return_value = self.users_data get_user_id_return_value = self.crud.get_user_id("dummyName") self.assertIsNone(get_user_id_return_value) @patch("crud.CRUD.read_users_file") def test_get_user_id_Returns_id_for_valid_user_name(self, mock_read_users_file): mock_read_users_file.return_value = self.users_data get_user_id_return_value = self.crud.get_user_id("*****@*****.**") self.assertEqual('1', get_user_id_return_value) @patch("crud.CRUD.read_groups_file") def test_get_group_id_Returns_false_for_invalid_group_name( self, mock_read_groups_file ): mock_read_groups_file.return_value = self.groups_data get_group_id_return_value = self.crud.get_group_id("dummyName") self.assertIsNone(get_group_id_return_value) @patch("crud.CRUD.read_groups_file") def test_get_group_id_Returns_id_for_valid_group_name(self, mock_read_groups_file): mock_read_groups_file.return_value = self.groups_data get_group_id_return_value = self.crud.get_group_id("default") self.assertEqual(get_group_id_return_value, '1') @patch("crud.CRUD.read_users_file") def test_update_users_Returns_false_for_invalid_id( self, mock_read_users_file ): mock_read_users_file.return_value = self.users_data update_users_ret_val = self.crud.update_users('10', 'name', 'dummyName') self.assertFalse(update_users_ret_val) @patch("crud.CRUD.read_users_file") def test_update_users_Returns_false_for_invalid_field( self, mock_read_users_file ): mock_read_users_file.return_value = self.users_data update_users_ret_val = self.crud.update_users('1', 'invalid', 'dummyInvalid') self.assertFalse(update_users_ret_val) @patch("crud.CRUD.modify_users_file") @patch("crud.CRUD.read_users_file") def test_update_users_Passes_correct_data_to_modify_users_file( self, mock_read_users_file, mock_modify_users_file ): """Il faut utiliser les mocks pour 'read_users_file' et 'modify_users_file' (ou selon votre realisation) Il faut utiliser ".assert_called_once_with(expected_data)" """ mock_read_users_file.return_value = self.users_data new_data = self.users_data new_data['1']['name'] = 'dummyName' self.crud.update_users('1', 'name', 'dummyName') mock_modify_users_file.assert_called_once_with(new_data) @patch("crud.CRUD.read_groups_file") def test_update_groups_Returns_false_for_invalid_id( self, mock_read_groups_file ): """Il faut utiliser les mocks pour 'read_groups_file' et 'modify_groups_file' (ou selon votre realisation) """ mock_read_groups_file.return_value = self.groups_data update_groups_ret_val = self.crud.update_groups('10', 'name', 'dummyName') self.assertFalse(update_groups_ret_val) @patch("crud.CRUD.read_groups_file") def test_update_groups_Returns_false_for_invalid_field( self, mock_read_groups_file ): mock_read_groups_file.return_value = self.groups_data update_groups_ret_val = self.crud.update_groups('1', 'invalidField', 'dummyName') self.assertFalse(update_groups_ret_val) @patch("crud.CRUD.modify_groups_file") @patch("crud.CRUD.read_groups_file") def test_update_groups_Passes_correct_data_to_modify_groups_file( self, mock_read_groups_file, mock_modify_groups_file ): """Il faut utiliser les mocks pour 'read_groups_file' et 'modify_groups_file' (ou selon votre realisation) Il faut utiliser ".assert_called_once_with(expected_data)" """ mock_read_groups_file.return_value = self.groups_data new_data = self.groups_data new_data['1']['name'] = 'dummyName' self.crud.update_groups('1', 'name', 'dummyName') mock_modify_groups_file.assert_called_once_with(new_data) @patch("crud.CRUD.read_users_file") def test_remove_user_Returns_false_for_invalid_id( self, mock_read_users_file ): mock_read_users_file.return_value = self.users_data remove_user_ret_val = self.crud.remove_user('10') self.assertFalse(remove_user_ret_val) @patch("crud.CRUD.modify_users_file") @patch("crud.CRUD.read_users_file") def test_remove_user_Passes_correct_value_to_modify_users_file( self, mock_read_users_file, mock_modify_users_file ): mock_read_users_file.return_value = self.users_data new_data = {"2": { "name": "*****@*****.**", "Trust": 65.45454, "SpamN": 171, "HamN": 324, "Date_of_first_seen_message": 1596855166.0, "Date_of_last_seen_message": 1596855166.0, "Groups": ["default"], }, } self.crud.remove_user('1') mock_modify_users_file.assert_called_once_with(new_data) @patch("crud.CRUD.read_users_file") def test_remove_user_group_Returns_false_for_invalid_id( self, mock_read_users_file, ): mock_read_users_file.return_value = self.users_data remove_user_group_ret_val = self.crud.remove_user_group('10', 'default') self.assertFalse(remove_user_group_ret_val) @patch("crud.CRUD.read_users_file") def test_remove_user_group_Returns_false_for_invalid_group( self, mock_read_users_file ): mock_read_users_file.return_value = self.users_data remove_user_group_ret_val = self.crud.remove_user_group('1', 'notValidGroup') self.assertFalse(remove_user_group_ret_val) @patch("crud.CRUD.modify_users_file") @patch("crud.CRUD.read_users_file") def test_remove_user_group_Passes_correct_value_to_modify_users_file( self, mock_read_users_file, mock_modify_users_file ): mock_read_users_file.return_value = self.users_data new_data = { "1": { "name": "*****@*****.**", "Trust": 100, "SpamN": 0, "HamN": 20, "Date_of_first_seen_message": 1596848266.0, "Date_of_last_seen_message": 1596848266.0, "Groups": [], }, "2": { "name": "*****@*****.**", "Trust": 65.45454, "SpamN": 171, "HamN": 324, "Date_of_first_seen_message": 1596855166.0, "Date_of_last_seen_message": 1596855166.0, "Groups": ["default"], }, } self.crud.remove_user_group('1', 'default') mock_modify_users_file.assert_called_once_with(new_data) @patch("crud.CRUD.read_groups_file") def test_remove_group_Returns_false_for_invalid_id( self, mock_read_groups_file ): mock_read_groups_file.return_value = self.groups_data remove_group_ret_val = self.crud.remove_group('10') self.assertFalse(remove_group_ret_val) @patch("crud.CRUD.modify_groups_file") @patch("crud.CRUD.read_groups_file") def test_remove_group_Passes_correct_value_to_modify_groups_file( self, mock_read_groups_file, mock_modify_groups_file ): mock_read_groups_file.return_value = self.groups_data new_data = {"2": { "name": "friends", "Trust": 90, "List_of_members": ["*****@*****.**"], }, } self.crud.remove_group('1') mock_modify_groups_file.assert_called_once_with(new_data) @patch("crud.CRUD.read_groups_file") def test_remove_group_member_Returns_false_for_invalid_id( self, mock_read_groups_file ): mock_read_groups_file.return_value = self.groups_data remove_group_member_ret_val = self.crud.remove_group_member('10', '*****@*****.**') self.assertFalse(remove_group_member_ret_val) @patch("crud.CRUD.read_groups_file") def test_remove_group_member_Returns_false_for_invalid_group_member( self, mock_read_groups_file ): mock_read_groups_file.return_value = self.groups_data remove_group_member_ret_val = self.crud.remove_group_member('1', 'invalidMember') self.assertFalse(remove_group_member_ret_val) @patch("crud.CRUD.modify_groups_file") @patch("crud.CRUD.read_groups_file") def test_remove_group_member_Passes_correct_value_to_modify_groups_file( self, mock_read_groups_file, mock_modify_groups_file ): mock_read_groups_file.return_value = self.groups_data self.crud.remove_group_member('1', '*****@*****.**') new_data = { "1": { "name": "default", "Trust": 50, "List_of_members": ["*****@*****.**"], }, "2": { "name": "friends", "Trust": 90, "List_of_members": ["*****@*****.**"], }, } mock_modify_groups_file.assert_called_once_with(new_data)