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_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')
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())