def test_add_user_to_group_adds_user(self): """Test adding a user to a group.""" user = self.create_user() group = mommy.make(Group) add_user_to_group.delay(user.pk, group.pk) self.assertTrue(Subscription.objects.filter(user=user, group=group).exists()) self.assertIn(group, user.groups_joined)
def test_add_user_to_group_adds_user(self): """Test adding a user to a group.""" user = self.create_user() group = mommy.make(Group) add_user_to_group.delay(user.pk, group.pk) self.assertTrue( Subscription.objects.filter(user=user, group=group).exists()) self.assertIn(group, user.groups_joined)
def test_add_user_to_group_does_not_notify_opted_out_owners(self, mock): """Owners who have opted out of join notifications shouldn't get one.""" group = Group.objects.create(name="test") # group = mommy.make('groups.Group') owner = self.create_superuser(receive_group_join_notifications=False) group.owners.add(owner) add_user_to_group.delay(self.user.pk, group.pk) self.assertEqual(mock.delay.call_count, 0)
def test_reactivates_deleted_userthreads(self): """Re-joining a group should reactivate old UserThreads.""" user = self.thread.recipients.first() ut = UserThread.objects.get(user=user, thread=self.thread) ut_id = ut.pk remove_user_from_group(user, self.group) add_user_to_group.delay(user.pk, self.group.pk) self.assertTrue(UserThread.objects.filter(pk=ut_id).exists()) self.assertEqual(UserThread.objects.filter(user=user, thread=self.thread).count(), 1)
def test_bails_out_if_user_is_already_member(self): """Test that nothing happens if a user is already a member.""" Subscription.objects.filter(user_id=self.user.pk, group=self.group).delete() with patch.object(tasks, 'send_system_message') as mock: add_user_to_group.delay(self.user.pk, self.group.pk, notification=('test', 'test')) self.assertEqual(mock.delay.call_count, 0)
def test_add_user_to_group_does_not_notify_opted_out_owners(self, mock): """Owners who have opted out of join notifications shouldn't get one.""" group = Group.objects.create(name='test') # group = mommy.make('groups.Group') owner = self.create_superuser(receive_group_join_notifications=False) group.owners.add(owner) add_user_to_group.delay(self.user.pk, group.pk) self.assertEqual(mock.delay.call_count, 0)
def test_user_sees_existing_threads(self): """Test that a user added to a group can see the existing threads.""" user = mommy.make(User) self.assertFalse(UserThread.objects.filter(user=user).exists()) add_user_to_group.delay(user.pk, self.group.pk) user_threads = UserThread.objects.filter(user=user) self.assertTrue(user_threads.exists()) self.assertTrue(user_threads.filter(thread=self.thread, subscribed_email=True).exists())
def test_add_user_to_group_with_notification(self, mock): """Test adding to a group with a notification""" group = mommy.make(Group) subject = "This is a subject" message = "This is a message" add_user_to_group.delay(self.user.pk, group.pk, notification=(subject, message)) self.assertIn(group, self.user.groups_joined) self.assertIn(self.user, group.get_members()) mock.delay.assert_called_with(self.user.pk, "This is a subject", "This is a message")
def save(self, *args, **kwargs): """Save the form""" self.instance.tos_accepted_at = now() result = super(GroupForm, self).save(*args, **kwargs) # For each group owner, added them to the group if self.instance.pk: for owner in self.cleaned_data['owners']: add_user_to_group.delay(owner.pk, self.instance.pk) return result
def test_user_sees_existing_threads(self): """Test that a user added to a group can see the existing threads.""" user = mommy.make(User) self.assertFalse(UserThread.objects.filter(user=user).exists()) add_user_to_group.delay(user.pk, self.group.pk) user_threads = UserThread.objects.filter(user=user) self.assertTrue(user_threads.exists()) self.assertTrue( user_threads.filter(thread=self.thread, subscribed_email=True).exists())
def test_reactivates_deleted_userthreads(self): """Re-joining a group should reactivate old UserThreads.""" user = self.thread.recipients.first() ut = UserThread.objects.get(user=user, thread=self.thread) ut_id = ut.pk remove_user_from_group(user, self.group) add_user_to_group.delay(user.pk, self.group.pk) self.assertTrue(UserThread.objects.filter(pk=ut_id).exists()) self.assertEqual( UserThread.objects.filter(user=user, thread=self.thread).count(), 1)
def test_bails_out_with_existing_subscription(self): """Test that nothing happens if a user has an existing subscription""" user = mommy.make(User) group = mommy.make(Group) mommy.make(Subscription, user=user, group=group) thread = self.create_thread(recipient=user) UserThread.objects.filter(thread=thread, user=user).update(status="deleted") add_user_to_group.delay(user.pk, group.pk) with patch.object(tasks, "send_system_message") as mock: add_user_to_group.delay(user.pk, group.pk, notification=("test", "test")) self.assertEqual(mock.delay.call_count, 0)
def test_add_user_to_group_notifies_owners(self, mock): """Adding a user to a group should notify the owners of the group.""" group = Group.objects.create(name="test") # group = mommy.make('groups.Group') owner = self.create_superuser() group.owners.add(owner) add_user_to_group.delay(self.user.pk, group.pk) call_args = mock.delay.call_args_list[0][1] self.assertEqual(call_args["recipient"], owner.pk) self.assertEqual(call_args["subject"], u"Your group test has a new member.") self.assertIn(self.user.full_url, call_args["message_content"]) self.assertIn(group.full_url, call_args["message_content"]) self.assertEqual(mock.delay.call_count, 1)
def test_add_user_to_group_notifies_owners(self, mock): """Adding a user to a group should notify the owners of the group.""" group = Group.objects.create(name='test') # group = mommy.make('groups.Group') owner = self.create_superuser() group.owners.add(owner) add_user_to_group.delay(self.user.pk, group.pk) call_args = mock.delay.call_args_list[0][1] self.assertEqual(call_args['recipient'], owner.pk) self.assertEqual(call_args['subject'], u'Your group test has a new member.') self.assertIn(self.user.full_url, call_args['message_content']) self.assertIn(group.full_url, call_args['message_content']) self.assertEqual(mock.delay.call_count, 1)
def test_add_user_to_group_with_notification(self, mock): """Test adding to a group with a notification""" group = mommy.make(Group) subject = 'This is a subject' message = 'This is a message' add_user_to_group.delay(self.user.pk, group.pk, notification=(subject, message)) self.assertIn(group, self.user.groups_joined) self.assertIn(self.user, group.get_members()) mock.delay.assert_called_with(self.user.pk, 'This is a subject', 'This is a message')
def test_bails_out_with_existing_subscription(self): """Test that nothing happens if a user has an existing subscription""" user = mommy.make(User) group = mommy.make(Group) mommy.make(Subscription, user=user, group=group) thread = self.create_thread(recipient=user) UserThread.objects.filter(thread=thread, user=user).update(status='deleted') add_user_to_group.delay(user.pk, group.pk) with patch.object(tasks, 'send_system_message') as mock: add_user_to_group.delay(user.pk, group.pk, notification=('test', 'test')) self.assertEqual(mock.delay.call_count, 0)
def group_quick_user_add(request, group_id): """A view that takes a list of user IDs and adds them to groups""" group = get_object_or_404(Group, pk=group_id) # If there is no users in the POST or the users post is empty, bail out if not 'users' in request.POST or not request.POST['users']: messages.warning(request, 'No Users Entered') else: subject = 'You\'ve been added to {group}'.format(group=str(group)) message = render_to_string( 'groups/notifications/added_to_group_notification.html', {'group': group} ) total_processed = 0 for user_id in request.POST.getlist('users'): if user_id.isdigit(): add_user_to_group.delay( user_id, group_id, notification=(subject, message)) total_processed += 1 plural = '' if total_processed == 1 else 's' messages.success(request, '{total} User{plural} Added'.format( total=total_processed, plural=plural)) return redirect(reverse('group_details', args=[group_id]))
def test_creates_with_default_notification_preference(self): """Test adding users to groups when they have an email preference""" user_immediate = mommy.make(User, group_notification_period="immediate") user_daily = mommy.make(User, group_notification_period="daily") user_override = mommy.make(User, group_notification_period="daily") add_user_to_group.delay(user_immediate.pk, self.group.pk) add_user_to_group.delay(user_daily.pk, self.group.pk) add_user_to_group.delay(user_override.pk, self.group.pk, period="none") self.assertTrue(Subscription.objects.filter(user=user_immediate, group=self.group, period="immediate")) self.assertTrue(Subscription.objects.filter(user=user_daily, group=self.group, period="daily")) self.assertTrue(Subscription.objects.filter(user=user_override, group=self.group, period="none"))
def test_creates_with_default_notification_preference(self): """Test adding users to groups when they have an email preference""" user_immediate = mommy.make(User, group_notification_period='immediate') user_daily = mommy.make(User, group_notification_period='daily') user_override = mommy.make(User, group_notification_period='daily') add_user_to_group.delay(user_immediate.pk, self.group.pk) add_user_to_group.delay(user_daily.pk, self.group.pk) add_user_to_group.delay(user_override.pk, self.group.pk, period='none') self.assertTrue( Subscription.objects.filter(user=user_immediate, group=self.group, period='immediate')) self.assertTrue( Subscription.objects.filter(user=user_daily, group=self.group, period='daily')) self.assertTrue( Subscription.objects.filter(user=user_override, group=self.group, period='none'))
def test_bails_out_if_user_is_already_member(self): """Test that nothing happens if a user is already a member.""" Subscription.objects.filter(user_id=self.user.pk, group=self.group).delete() with patch.object(tasks, "send_system_message") as mock: add_user_to_group.delay(self.user.pk, self.group.pk, notification=("test", "test")) self.assertEqual(mock.delay.call_count, 0)