<commit_before># coding:utf-8
"""
Use this module to write functional tests for the view-functions, only!
"""
from django_webtest import WebTest
from django.core.urlresolvers import reverse
from django_factory_boy import auth

from journalmanager.tests import modelfactories


HASH_FOR_123 = 'sha1$93d45$5f366b56ce0444bfea0f5634c7ce8248508c9799'


class LoginForm(WebTest):
    fixtures = ['groups.json',]

    def test_the_user_must_provide_his_credentials(self):
        form = self.app.get(reverse('journalmanager.user_login')).forms[0]
        form['username'] = ''
        form['password'] = ''
        response = form.submit()

        self.assertTrue('error' in response.body)
        self.assertTemplateUsed(response, 'registration/login.html')

    def test_right_username_and_wrong_password(self):
        user = auth.UserF(username='foo',
                          password=<PASSWORD>,
                          is_active=True)

        form = self.app.get(reverse('journalmanager.user_login')).forms[0]
        form['username'] = 'foo'
        form['password'] = '<PASSWORD>'
        response = form.submit()

        self.assertTrue('error' in response.body)
        self.assertTemplateUsed(response, 'registration/login.html')

    def test_wrong_username_and_right_password(self):
        user = auth.UserF(username='foo',
                          password=<PASSWORD>,
                          is_active=True)

        form = self.app.get(reverse('journalmanager.user_login')).forms[0]
        form['username'] = 'fuu'
        form['password'] = '<PASSWORD>'
        response = form.submit()

        self.assertTrue('error' in response.body)
        self.assertTemplateUsed(response, 'registration/login.html')

    def test_right_username_and_right_password(self):
        user = auth.UserF(username='foo',
                          password=<PASSWORD>23,
                          is_active=True)

        collection = modelfactories.CollectionFactory.create()
        collection.add_user(user)

        form = self.app.get(reverse('journalmanager.user_login')).forms[0]
        form['username'] = 'foo'
        form['password'] = '<PASSWORD>'

        response = form.submit().follow()

        self.assertTemplateUsed(response, 'journalmanager/home_journal.html')

    def test_right_username_and_right_password_for_inactive_user(self):
        user = auth.UserF(username='foo',
                          password=<PASSWORD>,
                          is_active=False)

        collection = modelfactories.CollectionFactory.create()
        collection.add_user(user)

        form = self.app.get(reverse('journalmanager.user_login')).forms[0]
        form['username'] = 'foo'
        form['password'] = '<PASSWORD>'

        response = form.submit()

        self.assertTrue('error' in response.body)
        self.assertTemplateUsed(response, 'registration/login.html')

    def test_redirect_to_restricted_page_after_successful_login(self):
        user = auth.UserF(is_active=True)
        # perm = auth.PermissionF(name='journalmanager.list_journal')
        # user.user_permissions.add(perm)

        collection = modelfactories.CollectionFactory.create()
        collection.add_user(user)

        page = self.app.get(reverse('journal.index'), user=user).follow().follow()

        page.mustcontain('no items')<commit_msg>Resolvido o problema na implementação do login que estava dificultando a elaboração de testes automatizados envolvendo acesso a views com permissões<commit_after># coding:utf-8
"""
Use this module to write functional tests for the view-functions, only!
"""
from django_webtest import WebTest
from django.core.urlresolvers import reverse
from django_factory_boy import auth

from journalmanager.tests import modelfactories


HASH_FOR_123 = 'sha1$93d45$5f366b56ce0444b<PASSWORD>f5634c<PASSWORD>2485<PASSWORD>9'


class LoginForm(WebTest):

    def _makePermission(self, perm, model, app_label='journalmanager'):
        """
        Retrieves a ContentType according to the given model and app_label.
        """
        from django.contrib.contenttypes import models
        from django.contrib.auth import models as auth_models

        ct = models.ContentType.objects.get(model=model,
                                            app_label=app_label)
        return auth_models.Permission.objects.get(codename=perm, content_type=ct)

    def test_the_user_must_provide_his_credentials(self):
        form = self.app.get(reverse('journalmanager.user_login')).forms[0]
        form['username'] = ''
        form['password'] = ''
        response = form.submit()

        self.assertTrue('error' in response.body)
        self.assertTemplateUsed(response, 'registration/login.html')

    def test_right_username_and_wrong_password(self):
        user = auth.UserF(username='foo',
                          password=<PASSWORD>,
                          is_active=True)

        form = self.app.get(reverse('journalmanager.user_login')).forms[0]
        form['username'] = 'foo'
        form['password'] = '<PASSWORD>'
        response = form.submit()

        self.assertTrue('error' in response.body)
        self.assertTemplateUsed(response, 'registration/login.html')

    def test_wrong_username_and_right_password(self):
        user = auth.UserF(username='foo',
                          password=<PASSWORD>,
                          is_active=True)

        form = self.app.get(reverse('journalmanager.user_login')).forms[0]
        form['username'] = 'fuu'
        form['password'] = '<PASSWORD>'
        response = form.submit()

        self.assertTrue('error' in response.body)
        self.assertTemplateUsed(response, 'registration/login.html')

    def test_right_username_and_right_password(self):
        user = auth.UserF(username='foo',
                          password=<PASSWORD>,
                          is_active=True)

        collection = modelfactories.CollectionFactory.create()
        collection.add_user(user)

        form = self.app.get(reverse('journalmanager.user_login')).forms[0]
        form['username'] = 'foo'
        form['password'] = '<PASSWORD>'

        response = form.submit().follow()

        self.assertTemplateUsed(response, 'journalmanager/home_journal.html')

    def test_right_username_and_right_password_for_inactive_user(self):
        user = auth.UserF(username='foo',
                          password=<PASSWORD>,
                          is_active=False)

        collection = modelfactories.CollectionFactory.create()
        collection.add_user(user)

        form = self.app.get(reverse('journalmanager.user_login')).forms[0]
        form['username'] = 'foo'
        form['password'] = '<PASSWORD>'

        response = form.submit()

        self.assertTrue('error' in response.body)
        self.assertTemplateUsed(response, 'registration/login.html')

    def test_redirect_to_restricted_page_after_successful_login(self):
        user = auth.UserF(is_active=True)
        perm = self._makePermission(perm='list_journal', model='journal')
        user.user_permissions.add(perm)

        collection = modelfactories.CollectionFactory.create()
        collection.add_user(user)

        page = self.app.get(reverse('journal.index'), user=user)

        page.mustcontain('no items')
{'fraction_non_alphanumeric': 0.11365505895256375, 'fraction_numerical': 0.010419522895530574, 'mean_word_length': 2.8273871983210914, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '359428', 'n_tokens_mistral': 2111, 'n_tokens_neox': 2031, 'n_words': 333}