<commit_before># -*- coding: utf-8 -*-
# Universal Subtitles, universalsubtitles.org
#
# Copyright (C) 2010 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.

import json

from django.test import TestCase
from apps.auth.models import CustomUser as User
from apps.auth.models import EmailConfirmation
from django.core import mail

from apps.messages.models import Message
from apps.messages import tasks as notifier
from teams.models import Team, TeamMember, Application
from teams.forms import InviteForm
from videos.models import Action
from utils import send_templated_email

class MessageTest(TestCase):

    def setUp(self):
        self.author = User.objects.all()[:1].get()
        self.subject = "Let's talk"
        self.body = "Will you please help me out with Portuguese trans?"
        self.user = User.objects.exclude(pk=self.author.pk)[:1].get()

    def _create_message(self, to_user):
        self.message = Message(user=to_user,
                           author=self.author,
                           subject=self.subject,
                          content=self.body)
        self.message.save()

    def _send_email(self, to_user):
        send_templated_email(to_user, "test email", "messages/email/email_confirmed.html", {})

    def test_send_email_to_allowed_user(self):
        self.user.notify_by_email = True
        self.user.save()
        assert self.user.is_active and self.user.email
        
        self._send_email(self.user)
        self.assertEqual(len(mail.outbox), 1)

    def test_send_email_to_optout_user(self):
        self.user.notify_by_email = False
        self.user.save()
        assert self.user.is_active and self.user.email
                
        self._send_email(self.user)
        self.assertEquals(len(mail.outbox), 0)

    def test_message_to_optout_user(self):
        self.user.notify_by_message = False
        self.user.notify_by_email = False
        self.user.save()
        self._send_email(self.user)
        self._create_message(self.user)
        self.assertEquals(len(mail.outbox), 0)
        self.assertEquals(Message.objects.unread().filter(user=self.user).count(), 0)
        self.assertEquals(Message.objects.filter(user=self.user).count(), 1)
        
    def test_member_join(self):
        def _get_counts(member):
            email_to = "%s <%s>" %(member.user.username, 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="test%s" % x,
                email = "<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()
        # 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)<commit_msg>Fixed team member join notification test<commit_after># -*- coding: utf-8 -*-
# Universal Subtitles, universalsubtitles.org
#
# Copyright (C) 2010 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see
# http://www.gnu.org/licenses/agpl-3.0.html.

import json

from django.test import TestCase
from apps.auth.models import CustomUser as User
from apps.auth.models import EmailConfirmation
from django.core import mail

from apps.messages.models import Message
from apps.messages import tasks as notifier
from teams.models import Team, TeamMember, Application
from teams.forms import InviteForm
from videos.models import Action
from utils import send_templated_email

class MessageTest(TestCase):

    def setUp(self):
        self.author = User.objects.all()[:1].get()
        self.subject = "Let's talk"
        self.body = "Will you please help me out with Portuguese trans?"
        self.user = User.objects.exclude(pk=self.author.pk)[:1].get()

    def _create_message(self, to_user):
        self.message = Message(user=to_user,
                           author=self.author,
                           subject=self.subject,
                          content=self.body)
        self.message.save()

    def _send_email(self, to_user):
        send_templated_email(to_user, "test email", "messages/email/email_confirmed.html", {})

    def test_send_email_to_allowed_user(self):
        self.user.notify_by_email = True
        self.user.save()
        assert self.user.is_active and self.user.email
        
        self._send_email(self.user)
        self.assertEqual(len(mail.outbox), 1)

    def test_send_email_to_optout_user(self):
        self.user.notify_by_email = False
        self.user.save()
        assert self.user.is_active and self.user.email
                
        self._send_email(self.user)
        self.assertEquals(len(mail.outbox), 0)

    def test_message_to_optout_user(self):
        self.user.notify_by_message = False
        self.user.notify_by_email = False
        self.user.save()
        self._send_email(self.user)
        self._create_message(self.user)
        self.assertEquals(len(mail.outbox), 0)
        self.assertEquals(Message.objects.unread().filter(user=self.user).count(), 0)
        self.assertEquals(Message.objects.filter(user=self.user).count(), 1)
        
    def test_member_join(self):
        def _get_counts(member):
            email_to = "%s <%s>" %(member.user.username, 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="test%s" % x,
                email = "<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)
{'fraction_non_alphanumeric': 0.08265213442325159, 'fraction_numerical': 0.009809264305177113, 'mean_word_length': 2.5382390745501286, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5682845', 'n_tokens_mistral': 3311, 'n_tokens_neox': 3195, 'n_words': 801}