def _create_team_videos(team, videos, users): for video in videos: shuffle(users) team_video = TeamVideo(team=team, video=video) member, created = CustomUser.objects.get_or_create(user_ptr=users[0]) team_video.added_by = member team_video.save()
def test_cache_has_right_value(self): video_url = VideoUrl.objects.all()[0] tv = TeamVideo(video=video_url.video, team=self.team,added_by=self.user) tv.save() res = self._get_widget_moderation_status(video_url.url) self.assertFalse(res["is_moderated"]) video, created = Video.get_or_create_for_url(video_url.url) add_moderation(video, self.team, self.user) res = self._get_widget_moderation_status(video_url.url) self.assertTrue(res["is_moderated"])
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 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()
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)
def setUp(self): self.tv = TeamVideo.objects.all()[0] self.sl = SubtitleLanguage.objects.exclude(language='')[0] self.team = Team.objects.all()[0] tv = TeamVideo(team=self.team, video=self.sl.video, added_by=self.team.users.all()[:1].get()) tv.save()
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_moderated_notifies_only_when_published(self): # TODO: should this use the new visibility settings instead of the old # moderation stuff? """ Set up a public team, add new video and new version. Notification should be sent. Setup a team with moderated videos """ from teams.moderation_const import WAITING_MODERATION def video_with_two_followers(): v, c = Video.get_or_create_for_url( "http://blip.tv/file/get/Miropcf-AboutUniversalSubtitles847.ogv" ) f1 = User.objects.all()[0] f2 = User.objects.all()[1] f1.notify_by_email = f2.notify_by_email = True f1.save() f2.save() v.followers.add(f1, f2) return v def new_version(v): subs = [(0, 1000, 'Hello', {}), (2000, 5000, 'world.', {})] add_subtitles(v, 'en', subs, author=self.author, committer=self.author) subs = [(0, 1000, 'Hello', {}), (3000, 5000, 'world.', {})] return add_subtitles(v, 'en', subs, author=self.author, committer=self.author) v = video_with_two_followers() mail.outbox = [] from videos.tasks import video_changed_tasks v = video_with_two_followers() sv = new_version(v) video_changed_tasks(v.pk, sv.pk) # notifications are only sent on the second version of a video # as optimization sv = new_version(v) video_changed_tasks(v.pk, sv.pk) # video is public , followers should be notified self.assertEquals(len(mail.outbox), 2) mail.outbox = [] # add to a moderated video team = Team.objects.create(slug='my-team', name='myteam', workflow_enabled=True) workflow = Workflow(team=team, review_allowed=20, approve_allowed=20) workflow.save() tv = TeamVideo(team=team, video=v, added_by=User.objects.all()[2]) tv.save() sv = new_version(v) # with the widget, this would set up correctly sv.moderation_status = WAITING_MODERATION sv.save() video_changed_tasks(v.pk, sv.pk) sv = sub_models.SubtitleVersion.objects.get(pk=sv.pk) self.assertFalse(sv.is_public()) # no emails should be sent before the video is approved self.assertEqual(len(mail.outbox), 0) # approve video t = Task(type=40, approved=20, team_video=tv, team=team, language='en', new_subtitle_version=sv, assignee=self.author) t.save() t.complete() self.assertTrue(sv.is_public()) video_changed_tasks(v.pk, sv.pk) # Once the video is approved, we should send out the # team-task-approved-published.html email and the # email_notification_non_editors.html to the author self.assertEqual(len(mail.outbox), 2)
def test_moderated_subs_approve_one(self): video = Video.objects.get(pk=4) tv = TeamVideo(video=video, team=self.team,added_by=self.user) tv.save() url = reverse("teams:detail", kwargs={"slug": self.team.slug}) response = self.client.get(url) add_moderation(video, self.team, self.user) self._login(is_moderator=True) self.client.get("\en\faq") lang = video.subtitle_language() [ self._make_subs(lang, 5) for x in xrange(0, 3)] self.assertEquals(self.team.get_pending_moderation().count(), 3) lang = SubtitleLanguage(video=video, language="pt", title="a", standard_language=lang) lang.save() [ self._make_subs(lang, 5) for x in xrange(0, 3)] # we can see the unmoderated sub on the website, response = self.client.get(reverse("videos:translation_history", kwargs={ 'video_id':video.video_id, 'lang':lang.language, 'lang_id':lang.id })) versions = SubtitleVersion.objects.filter(language=lang) latest_version = lang.latest_version(public_only=False) #lang.subtitleversion_set.all()[0] subs = response.context['last_version'].subtitle_set.all() self.assertEquals(latest_version, response.context['last_version']) self.assertTrue(subs[0].subtitle_text.startswith("vno:2 Sub 0 ")) self.assertTrue(len(subs)) self.assertEquals(self.team.get_pending_moderation().count(), 6) versions = self.team.get_pending_moderation() version = versions[0] # after moderation it should not be visible on the widget subs = self._call_rpc_method("fetch_subtitles", lang.video.video_id, lang.pk) self.assertFalse(subs) url = reverse("moderation:revision-approve", kwargs={ "team_id":self.team.id, "version_id":version.pk} ) response = self.client.post(url, {},follow=True, HTTP_X_REQUESTED_WITH='XMLHttpRequest') self.assertEquals(response.status_code, 200) data = json.loads(response.content) self.assertTrue(data["success"]) self.team = refresh_obj(self.team) self.assertEquals(self.team.get_pending_moderation().count(), 5) version = SubtitleVersion.objects.get(pk=version.pk) self.assertEquals(version.moderation_status,APPROVED) response = self.client.get(reverse("videos:translation_history", kwargs={ 'video_id':video.video_id, 'lang':lang.language, 'lang_id':lang.id })) sub_1 = response.context['last_version'].subtitle_set.all() self.assertTrue(len(sub_1)) widget_res = self._call_rpc_method("fetch_subtitles", version.video.video_id, version.language.pk) self.assertTrue(widget_res) self.assertTrue(widget_res["subtitles"]) sub = widget_res["subtitles"][0] self.assertTrue(sub["text"].startswith("vno:2 Sub 0 "))