def test_users_to_notify(): project = f.ProjectFactory.create() role1 = f.RoleFactory.create(project=project, permissions=['view_issues']) role2 = f.RoleFactory.create(project=project, permissions=[]) member1 = f.MembershipFactory.create(project=project, role=role1) member2 = f.MembershipFactory.create(project=project, role=role1) member3 = f.MembershipFactory.create(project=project, role=role1) member4 = f.MembershipFactory.create(project=project, role=role1) member5 = f.MembershipFactory.create(project=project, role=role2) issue = f.IssueFactory.create(project=project, owner=member4.user) policy_model_cls = apps.get_model("notifications", "NotifyPolicy") policy1 = policy_model_cls.objects.get(user=member1.user) policy2 = policy_model_cls.objects.get(user=member2.user) policy3 = policy_model_cls.objects.get(user=member3.user) history = MagicMock() history.owner = member2.user history.comment = "" # Test basic description modifications issue.description = "test1" issue.save() users = services.get_users_to_notify(issue) assert len(users) == 1 assert tuple(users)[0] == issue.get_owner() # Test watch notify level in one member policy1.notify_level = NotifyLevel.watch policy1.save() users = services.get_users_to_notify(issue) assert len(users) == 2 assert users == {member1.user, issue.get_owner()} # Test with watchers issue.watchers.add(member3.user) users = services.get_users_to_notify(issue) assert len(users) == 3 assert users == {member1.user, member3.user, issue.get_owner()} # Test with watchers with ignore policy policy3.notify_level = NotifyLevel.ignore policy3.save() issue.watchers.add(member3.user) users = services.get_users_to_notify(issue) assert len(users) == 2 assert users == {member1.user, issue.get_owner()} # Test with watchers without permissions issue.watchers.add(member5.user) users = services.get_users_to_notify(issue) assert len(users) == 2 assert users == {member1.user, issue.get_owner()}
def send_notifications(self, obj, history=None): """ Shortcut method for resources with special save cases on actions methods that not uses standard `post_save` hook of drf resources. """ if history is None: history = self.get_last_history() # If not history found, or it is empty. Do notthing. if not history: return obj = self.get_object_for_snapshot(obj) # Process that analizes the corresponding diff and # some text fields for extract mentions and add them # to watchers before obtain a complete list of # notifiable users. services.analize_object_for_watchers(obj, history) # Get a complete list of notifiable users for current # object and send the change notification to them. users = services.get_users_to_notify(obj, history=history) services.send_notifications(obj, history=history, users=users)
def send_notifications(self, obj, history=None): """ Shortcut method for resources with special save cases on actions methods that not uses standard `post_save` hook of drf resources. """ if history is None: history = self.get_last_history() # If not history found, or it is empty. Do notthing. if not history: return if self._not_notify: return obj = self.get_object_for_snapshot(obj) # Process that analizes the corresponding diff and # some text fields for extract mentions and add them # to watchers before obtain a complete list of # notifiable users. services.analize_object_for_watchers(obj, history) # Get a complete list of notifiable users for current # object and send the change notification to them. users = services.get_users_to_notify(obj, history=history) services.send_notifications(obj, history=history, users=users)
def test_watching_users_to_notify_on_issue_modification_2(): # If: # - the user is watching the issue # - the user is not watching the project # - the notify policy is involved # Then: # - email is sent project = f.ProjectFactory.create(anon_permissions= ["view_issues"]) issue = f.IssueFactory.create(project=project) watching_user = f.UserFactory() issue.add_watcher(watching_user) watching_user_policy = project.cached_notify_policy_for_user(watching_user) issue.description = "test1" issue.save() watching_user_policy.notify_level = NotifyLevel.involved users = services.get_users_to_notify(issue) assert users == {watching_user, issue.owner}
def test_watching_users_to_notify_on_issue_modification_2(): # If: # - the user is watching the issue # - the user is not watching the project # - the notify policy is involved # Then: # - email is sent project = f.ProjectFactory.create(anon_permissions=["view_issues"]) issue = f.IssueFactory.create(project=project) watching_user = f.UserFactory() issue.add_watcher(watching_user) watching_user_policy = project.cached_notify_policy_for_user(watching_user) issue.description = "test1" issue.save() watching_user_policy.notify_level = NotifyLevel.involved users = services.get_users_to_notify(issue) assert users == {watching_user, issue.owner}
def test_users_to_notify(): project = f.ProjectFactory.create() role1 = f.RoleFactory.create(project=project, permissions=['view_issues']) role2 = f.RoleFactory.create(project=project, permissions=[]) member1 = f.MembershipFactory.create(project=project, role=role1) policy_member1 = member1.user.notify_policies.get(project=project) policy_member1.notify_level = NotifyLevel.none policy_member1.save() member2 = f.MembershipFactory.create(project=project, role=role1) policy_member2 = member2.user.notify_policies.get(project=project) policy_member2.notify_level = NotifyLevel.none policy_member2.save() member3 = f.MembershipFactory.create(project=project, role=role1) policy_member3 = member3.user.notify_policies.get(project=project) policy_member3.notify_level = NotifyLevel.none policy_member3.save() member4 = f.MembershipFactory.create(project=project, role=role1) policy_member4 = member4.user.notify_policies.get(project=project) policy_member4.notify_level = NotifyLevel.none policy_member4.save() member5 = f.MembershipFactory.create(project=project, role=role2) policy_member5 = member5.user.notify_policies.get(project=project) policy_member5.notify_level = NotifyLevel.none policy_member5.save() inactive_member1 = f.MembershipFactory.create(project=project, role=role1) inactive_member1.user.is_active = False inactive_member1.user.save() system_member1 = f.MembershipFactory.create(project=project, role=role1) system_member1.user.is_system = True system_member1.user.save() issue = f.IssueFactory.create(project=project, owner=member4.user) policy_model_cls = apps.get_model("notifications", "NotifyPolicy") policy_inactive_member1 = policy_model_cls.objects.get(user=inactive_member1.user) policy_inactive_member1.notify_level = NotifyLevel.all policy_inactive_member1.save() policy_system_member1 = policy_model_cls.objects.get(user=system_member1.user) policy_system_member1.notify_level = NotifyLevel.all policy_system_member1.save() history = MagicMock() history.owner = member2.user history.comment = "" # Test basic description modifications issue.description = "test1" issue.save() policy_member4.notify_level = NotifyLevel.all policy_member4.save() users = services.get_users_to_notify(issue) assert len(users) == 1 assert tuple(users)[0] == issue.get_owner() # Test watch notify level in one member policy_member1.notify_level = NotifyLevel.all policy_member1.save() del project.cached_notify_policies users = services.get_users_to_notify(issue) assert len(users) == 2 assert users == {member1.user, issue.get_owner()} # Test with watchers issue.add_watcher(member3.user) policy_member3.notify_level = NotifyLevel.all policy_member3.save() del project.cached_notify_policies users = services.get_users_to_notify(issue) assert len(users) == 3 assert users == {member1.user, member3.user, issue.get_owner()} # Test with watchers with ignore policy policy_member3.notify_level = NotifyLevel.none policy_member3.save() issue.add_watcher(member3.user) del project.cached_notify_policies users = services.get_users_to_notify(issue) assert len(users) == 2 assert users == {member1.user, issue.get_owner()} # Test with watchers without permissions issue.add_watcher(member5.user) del project.cached_notify_policies users = services.get_users_to_notify(issue) assert len(users) == 2 assert users == {member1.user, issue.get_owner()} # Test with inactive user issue.add_watcher(inactive_member1.user) assert len(users) == 2 assert users == {member1.user, issue.get_owner()} # Test with system user issue.add_watcher(system_member1.user) assert len(users) == 2 assert users == {member1.user, issue.get_owner()}
def test_users_to_notify(): project = f.ProjectFactory.create() role1 = f.RoleFactory.create(project=project, permissions=['view_issues']) role2 = f.RoleFactory.create(project=project, permissions=[]) member1 = f.MembershipFactory.create(project=project, role=role1) policy_member1 = member1.user.notify_policies.get(project=project) policy_member1.notify_level = NotifyLevel.none policy_member1.save() member2 = f.MembershipFactory.create(project=project, role=role1) policy_member2 = member2.user.notify_policies.get(project=project) policy_member2.notify_level = NotifyLevel.none policy_member2.save() member3 = f.MembershipFactory.create(project=project, role=role1) policy_member3 = member3.user.notify_policies.get(project=project) policy_member3.notify_level = NotifyLevel.none policy_member3.save() member4 = f.MembershipFactory.create(project=project, role=role1) policy_member4 = member4.user.notify_policies.get(project=project) policy_member4.notify_level = NotifyLevel.none policy_member4.save() member5 = f.MembershipFactory.create(project=project, role=role2) policy_member5 = member5.user.notify_policies.get(project=project) policy_member5.notify_level = NotifyLevel.none policy_member5.save() inactive_member1 = f.MembershipFactory.create(project=project, role=role1) inactive_member1.user.is_active = False inactive_member1.user.save() system_member1 = f.MembershipFactory.create(project=project, role=role1) system_member1.user.is_system = True system_member1.user.save() issue = f.IssueFactory.create(project=project, owner=member4.user) policy_model_cls = apps.get_model("notifications", "NotifyPolicy") policy_inactive_member1 = policy_model_cls.objects.get( user=inactive_member1.user) policy_inactive_member1.notify_level = NotifyLevel.all policy_inactive_member1.save() policy_system_member1 = policy_model_cls.objects.get( user=system_member1.user) policy_system_member1.notify_level = NotifyLevel.all policy_system_member1.save() history = MagicMock() history.owner = member2.user history.comment = "" # Test basic description modifications issue.description = "test1" issue.save() policy_member4.notify_level = NotifyLevel.all policy_member4.save() users = services.get_users_to_notify(issue) assert len(users) == 1 assert tuple(users)[0] == issue.get_owner() # Test watch notify level in one member policy_member1.notify_level = NotifyLevel.all policy_member1.save() del project.cached_notify_policies users = services.get_users_to_notify(issue) assert len(users) == 2 assert users == {member1.user, issue.get_owner()} # Test with watchers issue.add_watcher(member3.user) policy_member3.notify_level = NotifyLevel.all policy_member3.save() del project.cached_notify_policies users = services.get_users_to_notify(issue) assert len(users) == 3 assert users == {member1.user, member3.user, issue.get_owner()} # Test with watchers with ignore policy policy_member3.notify_level = NotifyLevel.none policy_member3.save() issue.add_watcher(member3.user) del project.cached_notify_policies users = services.get_users_to_notify(issue) assert len(users) == 2 assert users == {member1.user, issue.get_owner()} # Test with watchers without permissions issue.add_watcher(member5.user) del project.cached_notify_policies users = services.get_users_to_notify(issue) assert len(users) == 2 assert users == {member1.user, issue.get_owner()} # Test with inactive user issue.add_watcher(inactive_member1.user) assert len(users) == 2 assert users == {member1.user, issue.get_owner()} # Test with system user issue.add_watcher(system_member1.user) assert len(users) == 2 assert users == {member1.user, issue.get_owner()}