<commit_before># coding: utf-8
from django.test import TestCase, override_settings
from django.test import Client
from django.core import mail
from django.contrib.auth.models import User, Group
from django.utils import timezone
from django.utils.translation import get_language
from django.urls import reverse
from django.conf import settings

from mock import patch, Mock
from freezegun import freeze_time

from studygroups.models import Course
from studygroups.models import StudyGroup
from studygroups.models import Meeting
from studygroups.models import Application
from studygroups.models import Rsvp
from studygroups.models import Reminder
from studygroups.models import Team
from studygroups.models import TeamMembership
from studygroups.models import TeamInvitation
from studygroups.models import Feedback
from studygroups.models import generate_all_meetings
from studygroups.utils import gen_rsvp_querystring
from studygroups.tasks import generate_reminder
from studygroups.tasks import send_reminders
from custom_registration.models import create_user
from custom_registration.models import confirm_user_email

import datetime
import urllib.request, urllib.parse, urllib.error
import json



"""
Tests for when facilitators interact with the system
"""
class TestFacilitatorViews(TestCase):

    fixtures = ['test_courses.json', 'test_studygroups.json']

    APPLICATION_DATA = {
        'study_group': '1',
        'name': '<NAME>',
        'email': '<EMAIL>',
        'mobile': '',
        'goals': 'Social reasons',
        'support': 'thinking how to?',
        'computer_access': 'Both',
        'consent': True,
        'use_internet': '2'
    }

    STUDY_GROUP_DATA = {
        'course': '1',
        'venue_name': 'мой дом',
        'venue_details': 'Garrage at my house',
        'venue_address': 'Rosemary Street 6',
        'city': 'Johannesburg',
        'country': 'South Africa',
        'country_en': 'South Africa',
        'region': 'Gauteng',
        'latitude': -26.205,
        'longitude': 28.0497,
        'place_id': '342432',
        'language': 'en',
        'description': 'We will complete the course about motorcycle maintenance together',
        'start_date': '2018-07-25',
        'weeks': '6',
        'meeting_time': '19:00',
        'duration': '90',
        'timezone': 'Africa/Johannesburg',
        'venue_website': 'http://venue.com',
    }

    def setUp(self):
        user = User.objects.create_user('admin', '<EMAIL>', 'password')
        user.is_superuser = True
        user.is_staff = True
        user.save()


    def test_user_forbidden(self):
        user = User.objects.create_user('bob', '<EMAIL>', 'password')
        c = Client()
        c.login(username='bob', password='password')
        def assertForbidden(url):
            resp = c.get(url)
            self.assertEqual(resp.status_code, 403)
        assertForbidden('/en/organize/')
        assertForbidden('/en/report/weekly/')
        assertForbidden('/en/studygroup/1/')
        assertForbidden('/en/studygroup/1/edit/')
        assertForbidden('/en/studygroup/1/message/compose/')
        assertForbidden('/en/studygroup/1/message/edit/1/')
        assertForbidden('/en/studygroup/1/member/add/')
        assertForbidden('/en/studygroup/1/member/1/delete/')
        assertForbidden('/en/studygroup/1/meeting/create/')
        assertForbidden('/en/studygroup/1/meeting/2/edit/')
        assertForbidden('/en/studygroup/1/meeting/2/delete/')
        assertForbidden('/en/studygroup/1/meeting/2/feedback/create/')


    def test_facilitator_access(self):
        User.objects.create_user('bob123', '<EMAIL>', 'password')
        user = User.objects.get(username='bob123')
        sg = StudyGroup.objects.get(pk=1)
        sg.facilitator = user
        sg.save()
        c = Client()
        c.login(username='bob123', password='password')
        def assertAllowed(url):
            resp = c.get(url)
            #TODO not sure if it's a good idea to include 404 here!
            self.assertIn(resp.status_code, [200, 301, 302])

        def assertStatus(url, status):
            resp = c.get(url)
            self.assertEqual(resp.status_code, status)
        assertAllowed('/en/studygroup/1/')
        assertAllowed('/en/studygroup/1/edit/')
        assertAllowed('/en/studygroup/1/message/compose/')
        assertStatus('/en/studygroup/1/message/edit/1/', 404)
        assertAllowed('/en/studygroup/1/member/add/')
        assertStatus('/en/studygroup/1/member/2/delete/', 404)
        assertAllowed('/en/studygroup/1/meeting/create/')
        assertStatus('/en/studygroup/1/meeting/2/edit/', 404)
        assertStatus('/en/studygroup/1/meeting/2/delete/', 404)
        assertStatus('/en/studygroup/1/meeting/2/feedback/create/', 404)


    def test_create_study_group(self):
        user = User.objects.create_user('bob123', '<EMAIL>', 'password')
        c = Client()
        c.login(username='bob123', password='password')
        data = self.STUDY_GROUP_DATA.copy()
        data['start_date'] = '07/25/2018',
        data['meeting_time'] = '07:00 PM',
        with freeze_time('2018-07-20'):
            resp = c.post('/en/studygroup/create/legacy/', data)
            sg = StudyGroup.objects.last()
            self.assertRedirects(resp, '/en/studygroup/{}/'.format(sg.pk))
        study_groups = StudyGroup.objects.filter(facilitator=user)
        self.assertEquals(study_groups.count(), 1)
        lc = study_groups.first()
        self.assertEquals(study_groups.first().meeting_set.count(), 0)
        self.assertEquals(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Your “{}” learning circle in {} has been created! What next?'.format(lc.course.title, lc.city))
        self.assertIn('<EMAIL>', mail.outbox[0].to)
        self.assertIn('community@localhost', mail.outbox[0].cc)


    @patch('custom_registration.signals.handle_new_facilitator')
    def test_publish_study_group(self, handle_new_facilitator):
        user = create_user('<EMAIL>', 'bob', 'test', 'password', False)
        confirm_user_email(user)
        c = Client()
        c.login(username='<EMAIL>', password='password')

        with freeze_time('2018-07-20'):
            resp = c.post('/api/learning-circle/', data=json.dumps(self.STUDY_GROUP_DATA), content_type='application/json')
            self.assertEqual(resp.json()['status'], 'created')
        study_groups = StudyGroup.objects.filter(facilitator=user)
        self.assertEqual(study_groups.count(), 1)
        lc = study_groups.first()
        self.assertEqual(lc.meeting_set.count(), 0)
        resp = c.post('/en/studygroup/{0}/publish/'.format(lc.pk))
        self.assertRedirects(resp, '/en/studygroup/{0}/'.format(lc.pk))<commit_msg>Fix test<commit_after># coding: utf-8
from django.test import TestCase, override_settings
from django.test import Client
from django.core import mail
from django.contrib.auth.models import User, Group
from django.utils import timezone
from django.utils.translation import get_language
from django.urls import reverse
from django.conf import settings

from mock import patch, Mock
from freezegun import freeze_time

from studygroups.models import Course
from studygroups.models import StudyGroup
from studygroups.models import Meeting
from studygroups.models import Application
from studygroups.models import Rsvp
from studygroups.models import Reminder
from studygroups.models import Team
from studygroups.models import TeamMembership
from studygroups.models import TeamInvitation
from studygroups.models import Feedback
from studygroups.models import generate_all_meetings
from studygroups.utils import gen_rsvp_querystring
from studygroups.tasks import generate_reminder
from studygroups.tasks import send_reminders
from custom_registration.models import create_user
from custom_registration.models import confirm_user_email

import datetime
import urllib.request, urllib.parse, urllib.error
import json



"""
Tests for when facilitators interact with the system
"""
class TestFacilitatorViews(TestCase):

    fixtures = ['test_courses.json', 'test_studygroups.json']

    APPLICATION_DATA = {
        'study_group': '1',
        'name': '<NAME>',
        'email': '<EMAIL>',
        'mobile': '',
        'goals': 'Social reasons',
        'support': 'thinking how to?',
        'computer_access': 'Both',
        'consent': True,
        'use_internet': '2'
    }

    STUDY_GROUP_DATA = {
        'course': '1',
        'venue_name': 'мой дом',
        'venue_details': 'Garrage at my house',
        'venue_address': 'Rosemary Street 6',
        'city': 'Johannesburg',
        'country': 'South Africa',
        'country_en': 'South Africa',
        'region': 'Gauteng',
        'latitude': -26.205,
        'longitude': 28.0497,
        'place_id': '342432',
        'language': 'en',
        'description': 'We will complete the course about motorcycle maintenance together',
        'start_date': '2018-07-25',
        'weeks': '6',
        'meeting_time': '19:00',
        'duration': '90',
        'timezone': 'Africa/Johannesburg',
        'facilitator_concerns': 'how do i market',
        'venue_website': 'http://venue.<EMAIL>',
    }

    def setUp(self):
        user = User.objects.create_user('admin', '<EMAIL>', 'password')
        user.is_superuser = True
        user.is_staff = True
        user.save()


    def test_user_forbidden(self):
        user = User.objects.create_user('bob', '<EMAIL>', 'password')
        c = Client()
        c.login(username='bob', password='password')
        def assertForbidden(url):
            resp = c.get(url)
            self.assertEqual(resp.status_code, 403)
        assertForbidden('/en/organize/')
        assertForbidden('/en/report/weekly/')
        assertForbidden('/en/studygroup/1/')
        assertForbidden('/en/studygroup/1/edit/')
        assertForbidden('/en/studygroup/1/message/compose/')
        assertForbidden('/en/studygroup/1/message/edit/1/')
        assertForbidden('/en/studygroup/1/member/add/')
        assertForbidden('/en/studygroup/1/member/1/delete/')
        assertForbidden('/en/studygroup/1/meeting/create/')
        assertForbidden('/en/studygroup/1/meeting/2/edit/')
        assertForbidden('/en/studygroup/1/meeting/2/delete/')
        assertForbidden('/en/studygroup/1/meeting/2/feedback/create/')


    def test_facilitator_access(self):
        User.objects.create_user('bob123', '<EMAIL>', 'password')
        user = User.objects.get(username='bob123')
        sg = StudyGroup.objects.get(pk=1)
        sg.facilitator = user
        sg.save()
        c = Client()
        c.login(username='bob123', password='password')
        def assertAllowed(url):
            resp = c.get(url)
            #TODO not sure if it's a good idea to include 404 here!
            self.assertIn(resp.status_code, [200, 301, 302])

        def assertStatus(url, status):
            resp = c.get(url)
            self.assertEqual(resp.status_code, status)
        assertAllowed('/en/studygroup/1/')
        assertAllowed('/en/studygroup/1/edit/')
        assertAllowed('/en/studygroup/1/message/compose/')
        assertStatus('/en/studygroup/1/message/edit/1/', 404)
        assertAllowed('/en/studygroup/1/member/add/')
        assertStatus('/en/studygroup/1/member/2/delete/', 404)
        assertAllowed('/en/studygroup/1/meeting/create/')
        assertStatus('/en/studygroup/1/meeting/2/edit/', 404)
        assertStatus('/en/studygroup/1/meeting/2/delete/', 404)
        assertStatus('/en/studygroup/1/meeting/2/feedback/create/', 404)


    def test_create_study_group(self):
        user = User.objects.create_user('bob123', '<EMAIL>', 'password')
        c = Client()
        c.login(username='bob123', password='password')
        data = self.STUDY_GROUP_DATA.copy()
        data['start_date'] = '07/25/2018',
        data['meeting_time'] = '07:00 PM',
        with freeze_time('2018-07-20'):
            resp = c.post('/en/studygroup/create/legacy/', data)
            sg = StudyGroup.objects.last()
            self.assertRedirects(resp, '/en/studygroup/{}/'.format(sg.pk))
        study_groups = StudyGroup.objects.filter(facilitator=user)
        self.assertEquals(study_groups.count(), 1)
        lc = study_groups.first()
        self.assertEquals(study_groups.first().meeting_set.count(), 0)
        self.assertEquals(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, 'Your “{}” learning circle in {} has been created! What next?'.format(lc.course.title, lc.city))
        self.assertIn('<EMAIL>', mail.outbox[0].to)
        self.assertIn('<EMAIL>', mail.outbox[0].cc)
        self.assertIn('community@localhost', mail.outbox[0].cc)


    @patch('custom_registration.signals.handle_new_facilitator')
    def test_publish_study_group(self, handle_new_facilitator):
        user = create_user('<EMAIL>', 'bob', 'test', 'password', False)
        confirm_user_email(user)
        c = Client()
        c.login(username='<EMAIL>', password='password')

        with freeze_time('2018-07-20'):
            resp = c.post('/api/learning-circle/', data=json.dumps(self.STUDY_GROUP_DATA), content_type='application/json')
            self.assertEqual(resp.json()['status'], 'created')
        study_groups = StudyGroup.objects.filter(facilitator=user)
        self.assertEqual(study_groups.count(), 1)
        lc = study_groups.first()
        self.assertEqual(lc.meeting_set.count(), 0)
        resp = c.post('/en/studygroup/{0}/publish/'.format(lc.pk))
        self.assertRedirects(resp, '/en/studygroup/{0}/'.format(lc.pk))
{'fraction_non_alphanumeric': 0.1400147383935151, 'fraction_numerical': 0.022328666175386883, 'mean_word_length': 3.0365853658536586, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2756242', 'n_tokens_mistral': 4406, 'n_tokens_neox': 4154, 'n_words': 787}