def notify_whitelist_change(auth_user, old_signature: Signature, new_signature: Signature): company_name = new_signature.get_signature_reference_name() project = cla.utils.get_project_instance() project.load(new_signature.get_signature_project_id()) project_name = project.get_project_name() changes = [] domain_msg_added = 'The domain {} was added to the domain approval list.' domain_msg_deleted = 'The domain {} was removed from the domain approval list.' domain_changes, _, _ = change_in_list( old_list=old_signature.get_domain_whitelist(), new_list=new_signature.get_domain_whitelist(), msg_added=domain_msg_added, msg_deleted=domain_msg_deleted) changes = changes + domain_changes email_msg_added = 'The email address {} was added to the email approval list.' email_msg_deleted = 'The email address {} was removed from the email approval list.' email_changes, email_added, email_deleted = change_in_list( old_list=old_signature.get_email_whitelist(), new_list=new_signature.get_email_whitelist(), msg_added=email_msg_added, msg_deleted=email_msg_deleted) changes = changes + email_changes github_msg_added = 'The GitHub user {} was added to the GitHub approval list.' github_msg_deleted = 'The GitHub user {} was removed from the github approval list.' github_changes, github_added, github_deleted = change_in_list( old_list=old_signature.get_github_whitelist(), new_list=new_signature.get_github_whitelist(), msg_added=github_msg_added, msg_deleted=github_msg_deleted) changes = changes + github_changes github_org_msg_added = 'The GitHub organization {} was added to the GitHub organization approval list.' github_org_msg_deleted = 'The GitHub organization {} was removed from the GitHub organization approval list.' github_org_changes, _, _ = change_in_list( old_list=old_signature.get_github_org_whitelist(), new_list=new_signature.get_github_org_whitelist(), msg_added=github_org_msg_added, msg_deleted=github_org_msg_deleted) changes = changes + github_org_changes if len(changes) > 0: # send email to cla managers about change cla_managers = new_signature.get_managers() subject, body, recipients = approval_list_change_email_content( project, company_name, project_name, cla_managers, changes) if len(recipients) > 0: get_email_service().send(subject, body, recipients) cla_manager_name = auth_user.name # send email to contributors notify_whitelist_change_to_contributors( project=project, email_added=email_added, email_removed=email_deleted, github_users_added=github_added, github_users_removed=github_deleted, company_name=company_name, project_name=project_name, cla_manager_name=cla_manager_name) event_data = " ,".join(changes) Event.create_event( event_data=event_data, event_summary=event_data, event_type=EventType.NotifyWLChange, event_company_name=company_name, event_project_name=project_name, contains_pii=True, )
def test_notify_whitelist_change(self): old_sig = Signature() new_sig = Signature() new_sig.set_signature_reference_name('Company') new_sig.set_signature_project_id('projectID') cla_manager = CLAUser({'name': 'CLA Manager'}) old_sig.set_domain_whitelist(['a.com', 'b.com']) new_sig.set_domain_whitelist(['b.com', 'd.com']) old_sig.set_github_whitelist([]) new_sig.set_github_whitelist(['githubuser']) old_sig.set_email_whitelist(['*****@*****.**']) new_sig.set_email_whitelist([]) old_sig.set_github_org_whitelist(['githuborg']) new_sig.set_github_org_whitelist(['githuborg']) snsClient = MockSNS() cla.controllers.signature.get_email_service = Mock() cla.controllers.signature.get_email_service.return_value = snsClient new_sig.get_managers = Mock(side_effect=mock_get_managers) cla.models.dynamo_models.Project.load = Mock(side_effect=mock_project) cla.models.dynamo_models.Project.get_project_name = Mock() cla.models.dynamo_models.Project.get_project_name.return_value = 'Project' cla.models.dynamo_models.User.get_user_by_github_username = Mock( side_effect=mock_get_user_by_github_username) notify_whitelist_change(cla_manager, old_sig, new_sig) self.assertEqual(len(snsClient.emails_sent), 3) # check email to cla manager msg = snsClient.emails_sent[0] msg = json.loads(msg) self.assertEqual(msg['data']['subject'], 'EasyCLA: Allow List Update for Project') self.assertEqual(msg['data']['recipients'], ['*****@*****.**', '*****@*****.**']) body = msg['data']['body'] self.assertIn('a.com', body) self.assertNotIn('b.com', body) self.assertIn('d.com', body) self.assertIn('githubuser', body) self.assertIn('*****@*****.**', body) self.assertNotIn('githuborg', body) # check email sent to contributor - removed email msg = snsClient.emails_sent[1] msg = json.loads(msg) self.assertEqual(msg['data']['subject'], 'EasyCLA: Allow List Update for Project') self.assertEqual(msg['data']['recipients'], ['*****@*****.**']) body = msg['data']['body'] self.assertIn('deleted', body) self.assertIn('Company', body) self.assertIn('Project', body) self.assertIn('CLA Manager', body) # check email sent to contributor - added github user msg = snsClient.emails_sent[2] msg = json.loads(msg) self.assertEqual(msg['data']['subject'], 'EasyCLA: Allow List Update for Project') self.assertEqual(msg['data']['recipients'], ['*****@*****.**']) body = msg['data']['body'] self.assertIn('added', body) self.assertIn('Company', body) self.assertIn('Project', body) self.assertIn('CLA Manager', body)