<commit_before>"""
Projects are a way to quickly introduce our audience to the
topics and issues we cover and then provide them avenues for
deeper, sustained involvement with our work on those topics.
"""

from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse
from django.test import TestCase, Client

from muckrock.project.models import Project
from muckrock.project.forms import ProjectCreateForm, ProjectUpdateForm

import logging
import nose

# pylint: disable=no-member

ok_ = nose.tools.ok_
eq_ = nose.tools.eq_

test_title = u'Private Prisons'
test_description = (
    u'The prison industry is growing at an alarming rate. '
    'Even more alarming? The conditions inside prisions '
    'are growing worse while their tax-dollar derived '
    'profits are growing larger.')
test_image = SimpleUploadedFile(
    name='foo.gif',
    content=(b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,'
    '\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00'))

class TestProjectCreateView(TestCase):
    """Tests creating a project as a user."""

    fixtures = [
        'test_users.json',
        'test_profiles.json',
        'test_news.json',
        'test_foiarequests.json',
        'test_agencies.json',
        'agency_types.json',
        'jurisdictions.json',
        'holidays.json'
    ]

    def setUp(self):
        self.client = Client()
        self.url = reverse('project-create')

    def tearDown(self):
        self.client.logout()

    def test_create_project_functional(self):
        """I want to create a project."""
        # First things first I need to be logged in
        self.client.login(username='adam', password='<PASSWORD>')
        # I point my browser at the right webpage
        response = self.client.get(self.url)
        eq_(response.status_code, 200,
            'Should load page to create a new project.')
        ok_(isinstance(response.context['form'], ProjectCreateForm),
            'Should load page with a ProjectCreateForm')
        # Then I fill out a form with all the details of my project.
        project_title = test_title
        project_description = test_description
        # project_image = test_image
        new_project_form = ProjectCreateForm({
            'title': project_title,
            'description': project_description,
            # 'image': project_image
        })
        logging.debug('The form is valid: %s.', new_project_form.is_valid())
        eq_(Project.objects.count(), 0, 'There should be no projects.')
        logging.debug('Projects: %s', Project.objects.all())
        # When I submit the form, I expect the project to be made and to be redirected to it.
        response = self.client.post(self.url, new_project_form.data, follow=False)
        logging.debug('POST data: %s', new_project_form.data)
        logging.debug('POST URL: %s', self.url)
        logging.debug('POST status code: %s', response.status_code)
        logging.debug('There are %s projects.', Project.objects.count())
        logging.debug('Projects: %s', Project.objects.all())
        eq_(Project.objects.count(), 1, 'There should now be one project.')
        # An action should also be created with this project as the target
        project = Project.objects.get(pk=1)
        eq_(len(project.target_actions.filter(verb='created')), 1,
            'An activity stream action should be generated.')
        logging.debug('Projects: %s', Project.objects.all())
        eq_(response.status_code, 302,
            'Should redirect to the newly created project.')
        self.assertRedirects(response, list(Project.objects.all())[-1].get_absolute_url())

    def test_requires_login(self):
        """Logged out users cannot create projects."""
        response = self.client.get(self.url)
        redirect_url = reverse('acct-login') + '?next=' + reverse('project-create')
        self.assertRedirects(response, redirect_url)

    def test_staff_only(self):
        """For now, staff are the only ones who can create new projects."""
        staff_user = User.objects.get(username='adam')
        nonstaff_user = User.objects.get(username='bob')
        ok_(staff_user.is_staff and not nonstaff_user.is_staff)
        staff_client = Client()
        staff_client.login(username='adam', password='<PASSWORD>')
        staff_response = staff_client.get(self.url)
        ok_(staff_response.status_code is 200)
        nonstaff_client = Client()
        nonstaff_client.login(username='bob', password='<PASSWORD>')
        nonstaff_response = nonstaff_client.get(self.url)
        ok_(nonstaff_response.status_code is not 200,
            'Nonstaff users should not be able to create a new project at this time.')

    def test_creator_made_contributor(self):<commit_msg>fixes project test<commit_after>"""
Projects are a way to quickly introduce our audience to the
topics and issues we cover and then provide them avenues for
deeper, sustained involvement with our work on those topics.
"""

from django.contrib.auth.models import User
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.urlresolvers import reverse
from django.test import TestCase, Client

from muckrock.project.models import Project
from muckrock.project.forms import ProjectCreateForm, ProjectUpdateForm

import logging
import nose

# pylint: disable=no-member

ok_ = nose.tools.ok_
eq_ = nose.tools.eq_

test_title = u'Private Prisons'
test_description = (
    u'The prison industry is growing at an alarming rate. '
    'Even more alarming? The conditions inside prisions '
    'are growing worse while their tax-dollar derived '
    'profits are growing larger.')
test_image = SimpleUploadedFile(
    name='foo.gif',
    content=(b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,'
    '\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00'))

class TestProjectCreateView(TestCase):
    """Tests creating a project as a user."""

    fixtures = [
        'test_users.json',
        'test_profiles.json',
        'test_news.json',
        'test_foiarequests.json',
        'test_agencies.json',
        'agency_types.json',
        'jurisdictions.json',
        'holidays.json'
    ]

    def setUp(self):
        self.client = Client()
        self.url = reverse('project-create')

    def tearDown(self):
        self.client.logout()

    def test_create_project_functional(self):
        """I want to create a project."""
        # First things first I need to be logged in
        self.client.login(username='adam', password='<PASSWORD>')
        # I point my browser at the right webpage
        response = self.client.get(self.url)
        eq_(response.status_code, 200,
            'Should load page to create a new project.')
        ok_(isinstance(response.context['form'], ProjectCreateForm),
            'Should load page with a ProjectCreateForm')
        # Then I fill out a form with all the details of my project.
        project_title = test_title
        project_description = test_description
        # project_image = test_image
        new_project_form = ProjectCreateForm({
            'title': project_title,
            'description': project_description,
            # 'image': project_image
        })
        logging.debug('The form is valid: %s.', new_project_form.is_valid())
        eq_(Project.objects.count(), 0, 'There should be no projects.')
        logging.debug('Projects: %s', Project.objects.all())
        # When I submit the form, I expect the project to be made and to be redirected to it.
        response = self.client.post(self.url, new_project_form.data, follow=False)
        logging.debug('POST data: %s', new_project_form.data)
        logging.debug('POST URL: %s', self.url)
        logging.debug('POST status code: %s', response.status_code)
        logging.debug('There are %s projects.', Project.objects.count())
        logging.debug('Projects: %s', Project.objects.all())
        eq_(Project.objects.count(), 1, 'There should now be one project.')
        # An action should also be created with this project as the target
        project = Project.objects.first()
        eq_(len(project.target_actions.filter(verb='created')), 1,
            'An activity stream action should be generated.')
        logging.debug('Projects: %s', Project.objects.all())
        eq_(response.status_code, 302,
            'Should redirect to the newly created project.')
        self.assertRedirects(response, list(Project.objects.all())[-1].get_absolute_url())

    def test_requires_login(self):
        """Logged out users cannot create projects."""
        response = self.client.get(self.url)
        redirect_url = reverse('acct-login') + '?next=' + reverse('project-create')
        self.assertRedirects(response, redirect_url)

    def test_staff_only(self):
        """For now, staff are the only ones who can create new projects."""
        staff_user = User.objects.get(username='adam')
        nonstaff_user = User.objects.get(username='bob')
        ok_(staff_user.is_staff and not nonstaff_user.is_staff)
        staff_client = Client()
        staff_client.login(username='adam', password='<PASSWORD>')
        staff_response = staff_client.get(self.url)
        ok_(staff_response.status_code is 200)
        nonstaff_client = Client()
        nonstaff_client.login(username='bob', password='<PASSWORD>')
        nonstaff_response = nonstaff_client.get(self.url)
        ok_(nonstaff_response.status_code is not 200,
            'Nonstaff users should not be able to create a new project at this time.')

    def test_creator_made_contributor(self):
{'fraction_non_alphanumeric': 0.1080773606370876, 'fraction_numerical': 0.013341607198262488, 'mean_word_length': 3.057910197230382, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5612007', 'n_tokens_mistral': 2890, 'n_tokens_neox': 2759, 'n_words': 767}