def setUp(self): self.auth = dict(username='******', password='******') self.team = Team.objects.all()[0] self.video = self.team.videos.all()[0] self.user = User.objects.all()[0] member = TeamMember(user=self.user, team=self.team, role=TeamMember.ROLE_MANAGER) member.save()
def test_application_new(self): def _get_counts(member): email_to = "%s" % (member.user.email) return Message.objects.filter(user=member.user).count() , \ len([x for x in mail.outbox if email_to in x.recipients()]) team, created = Team.objects.get_or_create(name='test', slug='test') applying_user = User.objects.all()[0] # creates dummy users: for x in xrange(0, 4): user, member = User.objects.get_or_create( username="******" % x, email="*****@*****.**" % x, notify_by_email=True, notify_by_message=True, ) tm = TeamMember(team=team, user=user) if x == 0: tm.role = TeamMember.ROLE_OWNER owner = tm elif x == 1: tm.role = TeamMember.ROLE_ADMIN admin = tm elif x == 2: tm.role = TeamMember.ROLE_MANAGER manager = tm elif x == 3: tm.role = TeamMember.ROLE_CONTRIBUTOR contributor = tm tm.save() # now make sure we count previsou messages owner_messge_count_1, owner_email_count_1 = _get_counts(owner) admin_messge_count_1, admin_email_count_1 = _get_counts(admin) manager_messge_count_1, manager_email_count_1 = _get_counts(manager) contributor_messge_count_1, contributor_email_count_1 = _get_counts( contributor) # now delete and check numers app = Application.objects.create(team=team, user=applying_user) app.save() notifier.application_sent.run(app.pk) # owner and admins should receive email + message owner_messge_count_2, owner_email_count_2 = _get_counts(owner) self.assertEqual(owner_messge_count_1 + 1, owner_messge_count_2) self.assertEqual(owner_email_count_1 + 1, owner_email_count_2) admin_messge_count_2, admin_email_count_2 = _get_counts(admin) self.assertEqual(admin_messge_count_1 + 1, admin_messge_count_2) self.assertEqual(admin_email_count_1 + 1, admin_email_count_2) # manager shoud not manager_messge_count_2, manager_email_count_2 = _get_counts(manager) self.assertEqual(manager_messge_count_1, manager_messge_count_2) self.assertEqual(manager_email_count_1, manager_email_count_2) # contributor shoud not contributor_messge_count_2, contributor_email_count_2 = _get_counts( contributor) self.assertEqual(contributor_messge_count_1, contributor_messge_count_2) self.assertEqual(contributor_email_count_1, contributor_email_count_2)
def test_right_team_filter(self): """ This is a test for story : #https://www.pivotaltracker.com/story/show/16450237 We add a video for moderation, create versions. Check we have the right number of videos to moderate on that dashboard. We then put antother video for another team under moderation. Then we check that each team's dashboard will only show it's own video on the dashboard. """ from haystack.query import SearchQuerySet from apps.teams.moderation_views import _get_moderation_results team1 = self.team other_user = User.objects.exclude(teammember__in=[x for x in team1.members.all()]).exclude(pk=self.user.pk)[0] self.assertEquals(team1.get_pending_moderation().count() , 0) tv1 = team1.teamvideo_set.all()[0] add_moderation(tv1.video, team1, self.user) self.assertEquals(team1.get_pending_moderation().count() , 0) self._create_versions( tv1.video.subtitle_language(), num_versions=2, user=other_user) _update_search_index(tv1.video) self.assertEquals(self.team.get_pending_moderation().count(), 2) form, results = _get_moderation_results(RequestMockup(self.user), tv1.team) team_video_pks = [x.team_video_pk for x in results] check_pks = [x.pk for x in team1.teamvideo_set.all()] for pk in team_video_pks: self.assertTrue(pk in check_pks) # second team should be filteres team2 = Team.objects.exclude(pk=team1.pk)[0] tv2 = team2.teamvideo_set.exclude(video=tv1.video)[0] lang, c = SubtitleLanguage.objects.get_or_create(video=tv2.video, is_original=False, language="pt") member = TeamMember(user=self.user, team=team2, role=TeamMember.ROLE_MANAGER) member.save() add_moderation(tv2.video, team2, self.user) self.assertEquals(tv2.video.moderated_by , team2) other_user = User.objects.exclude(teammember__in=[x for x in team2.members.all()]).exclude(pk=self.user.pk)[0] self.assertFalse(tv1.team == tv2.team) self.assertFalse(tv1.video == tv2.video) self.assertEquals(team2.get_pending_moderation().count() , 0) num_versions = 2 versions = self._create_versions( lang, num_versions=num_versions, user=other_user) _update_search_index(tv2.video) self.assertEquals(team2.get_pending_moderation().count(), num_versions) versions = team2.get_pending_moderation() videos_with_new = set() [videos_with_new.add(v.video) for v in versions] form, results = _get_moderation_results(RequestMockup(self.user), tv2.team) team_video_pks = [x.team_video_pk for x in results] check_pks = [x.pk for x in team2.teamvideo_set.all()] for pk in team_video_pks: self.assertTrue(pk in check_pks) self.assertEquals(results.count(), 1) self.assertEquals(results.count() , len(videos_with_new)) reset_solr()
def test_create_moderation_only_for_members(self): member = TeamMember(user=self.user, team=self.team, role=TeamMember.ROLE_MEMBER) member.save() e = None try: add_moderation(self.video, self.team, self.user) except SuspiciousOperation, e: pass
def test_rejection_activity_stream(self): member = TeamMember(user=self.user, team=self.team, role=TeamMember.ROLE_MANAGER) member.save() add_moderation(self.video, self.team, self.user) v1 = self._create_versions(self.video.subtitle_language(), num_versions=1)[0] count = Action.objects.all().count() reject_version(v1, self.team, self.user) self.assertEquals(count + 1, Action.objects.all().count()) act = Action.objects.all().order_by("-created")[0] act.action_type == Action.REJECT_VERSION
def test_new_version_will_await_moderation(self): #from apps.testhelpers.views import debug_video lang = self.video.subtitle_language() member = TeamMember(user=self.user, team=self.team, role=TeamMember.ROLE_MANAGER) member.save() v0 = self._make_subs(lang, 5) self.assertEquals(v0.moderation_status , UNMODERATED) add_moderation(self.video, self.team, self.user) v1 = self._create_versions(self.video.subtitle_language(), num_versions=1)[0] self.assertEquals(v1.moderation_status , WAITING_MODERATION)
def setUp(self): self.auth = dict(username='******', password='******') self.team = Team.objects.all()[0] self.video = self.team.videos.all()[0] l = self.video.subtitle_language() l.language = 'en' l.save() self.user = User.objects.all()[0] self.auth_user = User.objects.get(username=self.auth["username"]) member = TeamMember(user=self.user, team=self.team, role=TeamMember.ROLE_MANAGER) member.save()
def test_on_adding_we_approve_previsous_versions(self): #from apps.testhelpers.views import debug_video lang = self.video.subtitle_language() v1 = self._make_subs(lang, 5) self.assertEquals(v1.moderation_status , UNMODERATED) member = TeamMember(user=self.user, team=self.team, role=TeamMember.ROLE_MANAGER) member.save() add_moderation(self.video, self.team, self.user) v1 = SubtitleVersion.objects.get(pk=v1.pk) self.assertEquals(v1.moderation_status , APPROVED)
def test_remove__moderation_only_for_members(self): member = TeamMember(user=self.user, team=self.team, role=TeamMember.ROLE_MANAGER) member.save() add_moderation(self.video, self.team, self.user) member.role = TeamMember.ROLE_MEMBER member.save() e = None self.assertRaises(SuspiciousOperation, remove_moderation, *(self.video, self.team, self.user)) member.role = TeamMember.ROLE_MANAGER member.save() remove_moderation(self.video, self.team, self.user) self.video = Video.objects.get(pk=self.video.pk) self.assertFalse(self.video.moderated_by)
def test_promote_user(self): other_user = User.objects.exclude(pk=self.user.pk)[:1].get() user_tm = TeamMember(team=self.team, user=self.user) user_tm.save() other_user_tm = TeamMember(team=self.team, user=other_user) other_user_tm.save() self.assertEqual(other_user_tm.role, TeamMember.ROLE_CONTRIBUTOR) self.assertEqual(user_tm.role, TeamMember.ROLE_CONTRIBUTOR) response = self.rpc.promote_user(self.team.pk, other_user_tm.pk, TeamMember.ROLE_MANAGER, AnonymousUser()) if not isinstance(response, Error): self.fail('Anonymouse user is not member of team') response = self.rpc.promote_user(self.team.pk, other_user_tm.pk, TeamMember.ROLE_MANAGER, self.user) if not isinstance(response, Error): self.fail('User should be manager') user_tm.role = TeamMember.ROLE_MANAGER user_tm.save() NEW_ROLE = TeamMember.ROLE_CONTRIBUTOR response = self.rpc.promote_user(self.team.pk, other_user_tm.pk, NEW_ROLE, self.user) self.assertTrue(isinstance(response, Msg)) other_user_tm = refresh_obj(other_user_tm) self.assertEqual(other_user_tm.role, NEW_ROLE) response = self.rpc.promote_user(self.team.pk, user_tm.pk, TeamMember.ROLE_CONTRIBUTOR, self.user) if not isinstance(response, Error): self.fail('Can\'t promote yourself') response = self.rpc.promote_user(self.team.pk, other_user_tm.pk, 'undefined role 123456', self.user) if not isinstance(response, Error): self.fail('Incorrect role') response = self.rpc.promote_user(self.team.pk, 123456, TeamMember.ROLE_MANAGER, self.user) if not isinstance(response, Error): self.fail('Undefined team member') undefined_team_pk = 123456 self.assertFalse(Team.objects.filter(pk=undefined_team_pk)) response = self.rpc.promote_user(undefined_team_pk, other_user_tm.pk, TeamMember.ROLE_MANAGER, self.user) if not isinstance(response, Error): self.fail('Undefined team')
def test_promote_user(self): other_user = User.objects.exclude(pk=self.user.pk)[:1].get() user_tm = TeamMember(team=self.team, user=self.user) user_tm.save() other_user_tm = TeamMember(team=self.team, user=other_user) other_user_tm.save() self.assertEqual(other_user_tm.role, TeamMember.ROLE_MEMBER) self.assertEqual(user_tm.role, TeamMember.ROLE_MEMBER) response = self.rpc.promote_user(self.team.pk, other_user_tm.pk, TeamMember.ROLE_MANAGER, AnonymousUser()) if not isinstance(response, Error): self.fail('Anonymouse user is not member of team') response = self.rpc.promote_user(self.team.pk, other_user_tm.pk, TeamMember.ROLE_MANAGER, self.user) if not isinstance(response, Error): self.fail('User should be manager') user_tm.role = TeamMember.ROLE_MANAGER user_tm.save() NEW_ROLE = TeamMember.ROLE_CONTRIBUTOR response = self.rpc.promote_user(self.team.pk, other_user_tm.pk, NEW_ROLE, self.user) self.assertTrue(isinstance(response, Msg)) other_user_tm = refresh_obj(other_user_tm) self.assertEqual(other_user_tm.role, NEW_ROLE) response = self.rpc.promote_user(self.team.pk, user_tm.pk, TeamMember.ROLE_CONTRIBUTOR, self.user) if not isinstance(response, Error): self.fail('Can\'t promote yourself') response = self.rpc.promote_user(self.team.pk, other_user_tm.pk, 'undefined role 123456', self.user) if not isinstance(response, Error): self.fail('Incorrect role') response = self.rpc.promote_user(self.team.pk, 123456, TeamMember.ROLE_MANAGER, self.user) if not isinstance(response, Error): self.fail('Undefined team member') undefined_team_pk = 123456 self.assertFalse(Team.objects.filter(pk=undefined_team_pk)) response = self.rpc.promote_user(undefined_team_pk, other_user_tm.pk, TeamMember.ROLE_MANAGER, self.user) if not isinstance(response, Error): self.fail('Undefined team')
class TestNotification(TestCase): fixtures = ["test.json"] def setUp(self): fix_teams_roles() self.team = Team(name='test', slug='test') self.team.save() self.user = User.objects.all()[:1].get() self.user.is_active = True self.user.notify_by_email = True self.user.email = '*****@*****.**' self.user.save() self.tm = TeamMember(team=self.team, user=self.user) self.tm.save() v1 = Video.objects.all()[:1].get() self.tv1 = TeamVideo(team=self.team, video=v1, added_by=self.user) self.tv1.save() v2 = Video.objects.exclude(pk=v1.pk)[:1].get() self.tv2 = TeamVideo(team=self.team, video=v2, added_by=self.user) self.tv2.save() def test_new_team_video_notification(self): #check initial data self.assertEqual(self.team.teamvideo_set.count(), 2) self.assertEqual(self.team.users.count(), 1) #mockup for send_templated_email to test context of email import utils send_templated_email = utils.send_templated_email def send_templated_email_mockup(to, subject, body_template, body_dict, *args, **kwargs): send_templated_email_mockup.context = body_dict send_templated_email(to, subject, body_template, body_dict, *args, **kwargs) utils.send_templated_email = send_templated_email_mockup reload(tasks) #test notification about two new videos TeamVideo.objects.filter(pk__in=[self.tv1.pk, self.tv2.pk]).update(created=datetime.today()) self.assertEqual(TeamVideo.objects.filter(created__gt=self.team.last_notification_time).count(), 2) mail.outbox = [] self.user.notify_by_email = True self.user.save() tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 1) self.assertIn(self.user.email, mail.outbox[0].to[0] ) self.assertEqual(len(send_templated_email_mockup.context['team_videos']), 2) self.user.notify_by_email = False self.user.save() #test if user turn off notification self.user.is_active = False self.user.save() mail.outbox = [] tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 0) self.user.is_active = True self.user.notify_by_email = False self.user.save() mail.outbox = [] tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 0) self.tm.save() self.user.notify_by_email = True self.user.save() #test notification if one video is new created_date = self.team.last_notification_time + timedelta(seconds=10) TeamVideo.objects.filter(pk=self.tv1.pk).update(created=created_date) self.assertEqual(TeamVideo.objects.filter(created__gt=self.team.last_notification_time).count(), 1) mail.outbox = [] tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 1) self.assertEqual(len(send_templated_email_mockup.context['team_videos']), 1) self.assertEqual(send_templated_email_mockup.context['team_videos'][0], self.tv1) #test notification if all videos are already old created_date = self.team.last_notification_time - timedelta(seconds=10) TeamVideo.objects.filter(team=self.team).update(created=created_date) self.assertEqual(TeamVideo.objects.filter(created__gt=self.team.last_notification_time).count(), 0) mail.outbox = [] tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 0)
class TestNotification(TestCase): fixtures = ["test.json"] def setUp(self): self.team = Team(name='test', slug='test') self.team.save() self.user = User.objects.all()[:1].get() self.user.is_active = True self.user.changes_notification = True self.user.email = '*****@*****.**' self.user.save() self.tm = TeamMember(team=self.team, user=self.user) self.tm.save() v1 = Video.objects.all()[:1].get() self.tv1 = TeamVideo(team=self.team, video=v1, added_by=self.user) self.tv1.save() v2 = Video.objects.exclude(pk=v1.pk)[:1].get() self.tv2 = TeamVideo(team=self.team, video=v2, added_by=self.user) self.tv2.save() def test_new_team_video_notification(self): #check initial data self.assertEqual(self.team.teamvideo_set.count(), 2) self.assertEqual(self.team.users.count(), 1) #mockup for send_templated_email to test context of email import utils send_templated_email = utils.send_templated_email def send_templated_email_mockup(to, subject, body_template, body_dict, *args, **kwargs): send_templated_email_mockup.context = body_dict send_templated_email(to, subject, body_template, body_dict, *args, **kwargs) utils.send_templated_email = send_templated_email_mockup reload(tasks) #test notification about two new videos TeamVideo.objects.filter(pk__in=[self.tv1.pk, self.tv2.pk]).update(created=datetime.today()) self.assertEqual(TeamVideo.objects.filter(created__gt=self.team.last_notification_time).count(), 2) mail.outbox = [] tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.user.email]) self.assertEqual(len(send_templated_email_mockup.context['team_videos']), 2) #test if user turn off notification self.user.is_active = False self.user.save() mail.outbox = [] tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 0) self.user.is_active = True self.user.changes_notification = False self.user.save() mail.outbox = [] tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 0) self.user.changes_notification = True self.user.save() self.tm.changes_notification = False self.tm.save() mail.outbox = [] tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 0) self.tm.changes_notification = True self.tm.save() #test notification if one video is new created_date = self.team.last_notification_time + timedelta(seconds=10) TeamVideo.objects.filter(pk=self.tv1.pk).update(created=created_date) self.assertEqual(TeamVideo.objects.filter(created__gt=self.team.last_notification_time).count(), 1) mail.outbox = [] tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 1) self.assertEqual(len(send_templated_email_mockup.context['team_videos']), 1) self.assertEqual(send_templated_email_mockup.context['team_videos'][0], self.tv1) #test notification if all videos are already old created_date = self.team.last_notification_time - timedelta(seconds=10) TeamVideo.objects.filter(team=self.team).update(created=created_date) self.assertEqual(TeamVideo.objects.filter(created__gt=self.team.last_notification_time).count(), 0) mail.outbox = [] tasks.add_videos_notification.delay() self.team = Team.objects.get(pk=self.team.pk) self.assertEqual(len(mail.outbox), 0)
def test_member_join(self): def _get_counts(member): email_to = "%s" % (member.user.email) return Message.objects.filter(user=member.user).count() , \ len([x for x in mail.outbox if email_to in x.recipients()]) team, created = Team.objects.get_or_create(name='test', slug='test') # creates dummy users: for x in xrange(0, 5): user, member = User.objects.get_or_create( username="******" % x, email="*****@*****.**" % x, ) tm = TeamMember(team=team, user=user) if x == 0: tm.role = TeamMember.ROLE_OWNER owner = tm elif x == 1: tm.role = TeamMember.ROLE_ADMIN admin = tm elif x == 2: tm.role = TeamMember.ROLE_MANAGER manager = tm elif x == 3: tm.role = TeamMember.ROLE_CONTRIBUTOR contributor = tm if x < 4: # don't save the last role until we have counts tm.save() else: tm.role = TeamMember.ROLE_CONTRIBUTOR # now make sure we count previsou messages owner_messge_count_1, owner_email_count_1 = _get_counts(owner) admin_messge_count_1, admin_email_count_1 = _get_counts(admin) manager_messge_count_1, manager_email_count_1 = _get_counts(manager) contributor_messge_count_1, contributor_email_count_1 = _get_counts( contributor) # save the last team member and check that each group has appropriate counts tm.save() notifier.team_member_new(tm.pk) # owner and admins should receive email + message owner_messge_count_2, owner_email_count_2 = _get_counts(owner) self.assertEqual(owner_messge_count_1 + 1, owner_messge_count_2) self.assertEqual(owner_email_count_1 + 1, owner_email_count_2) admin_messge_count_2, admin_email_count_2 = _get_counts(admin) self.assertEqual(admin_messge_count_1 + 1, admin_messge_count_2) self.assertEqual(admin_email_count_1 + 1, admin_email_count_2) # manager shoud not manager_messge_count_2, manager_email_count_2 = _get_counts(manager) self.assertEqual(manager_messge_count_1, manager_messge_count_2) self.assertEqual(manager_email_count_1, manager_email_count_2) # contributor shoud not contributor_messge_count_2, contributor_email_count_2 = _get_counts( contributor) self.assertEqual(contributor_messge_count_1, contributor_messge_count_2) self.assertEqual(contributor_email_count_1, contributor_email_count_2) # now, this has to show up on everybody activitis fed action = Action.objects.get(team=team, user=tm.user, action_type=Action.MEMBER_JOINED) self.assertTrue( Action.objects.for_user(tm.user).filter(pk=action.pk).exists()) self.assertTrue( Action.objects.for_user(owner.user).filter(pk=action.pk).exists()) self.assertTrue( Action.objects.for_user( manager.user).filter(pk=action.pk).exists()) self.assertTrue( Action.objects.for_user( contributor.user).filter(pk=action.pk).exists()) self.assertTrue( Action.objects.for_user(admin.user).filter(pk=action.pk).exists())