<commit_before>import base64
import os

from django.contrib.auth.forms import (
    AuthenticationForm, PasswordChangeForm, PasswordResetForm, SetPasswordForm
)
from django.contrib.auth.tokens import default_token_generator
from django.core import mail
from django.core.urlresolvers import reverse
from django.utils.http import int_to_base36

from django_webtest import WebTest

from ..factories._accounts import UserFactory
from accounts.forms import UserCreationForm
from accounts.models import CustomUser

SMALL_GIF = base64.decodestring(
    'R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
)


class AccountsViewsTests(WebTest):
    def test_get_registration_view(self):
        """
        Simple GET to the registration view that should use the correct
        template and have form in its context
        """
        response = self.client.get(reverse('accounts:register'))

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'accounts/registration_form.html')
        self.failUnless(isinstance(response.context['form'], UserCreationForm))

    def test_post_registration_view_success(self):
        """
        POST to this view should create an user and redirect to the home page
        """
        page = self.app.get(reverse('accounts:register'))

        form = page.forms[0]
        form['email'] = '<EMAIL>'
        form['name'] = '<NAME>.'
        form['password1'] = 'password'
        form['password2'] = 'password'

        response = form.submit()

        self.assertRedirects(
            response, reverse('accounts:register_confirmation')
        )
        self.assertEqual(CustomUser.objects.count(), 1)
        self.assertEqual(len(mail.outbox), 1)
        new_user = CustomUser.objects.get()
        self.assertTrue(new_user.is_company_admin)

    def test_post_registration_view_failure(self):
        """
        POST to this view with an error in the form should display this form
        again with the error
        """
        page = self.app.get(reverse('accounts:register'))

        form = page.forms[0]
        form['email'] = '<EMAIL>'
        form['name'] = '<NAME>.'
        form['password1'] = 'password'
        form['password2'] = '<PASSWORD>'

        response = form.submit()

        self.assertEqual(response.status_code, 200)
        error = "The two password fields didn't match."
        self.assertFormError(response, 'form', 'password2', error)
        self.assertEqual(CustomUser.objects.count(), 0)
        self.assertEqual(len(mail.outbox), 0)

    def test_get_valid_activation(self):
        user = UserFactory(is_active=False, company=None)
        url = reverse('accounts:activate', args=(user.activation_key,))

        response = self.app.get(url).follow()
        user_found = CustomUser.objects.get()

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Login to get started.')
        self.assertTrue(user_found.is_active)

    def test_get_invalid_activation(self):
        """
        GET the activation page with an invalid activation key
        Should raise a 404, using client instead of app since app would fail
        """
        url = reverse('accounts:activate', args=('FAKE',))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    def test_get_login_view(self):
        """
        GET the login auth view (from django itself)
        """
        response = self.app.get(reverse('auth:login'))

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'accounts/login.html')
        self.failUnless(
            isinstance(response.context['form'], AuthenticationForm)
        )

    def test_post_login_view_success_without_company(self):
        """
        POST to the view to login
        Testing to make sure it works with email/activated user
        """
        user = UserFactory.create(company=None)<commit_msg>accounts: Fix tests for new wording<commit_after>import base64
import os

from django.contrib.auth.forms import (
    AuthenticationForm, PasswordChangeForm, PasswordResetForm, SetPasswordForm
)
from django.contrib.auth.tokens import default_token_generator
from django.core import mail
from django.core.urlresolvers import reverse
from django.utils.http import int_to_base36

from django_webtest import WebTest

from ..factories._accounts import UserFactory
from accounts.forms import UserCreationForm
from accounts.models import CustomUser

SMALL_GIF = base64.decodestring(
    'R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
)


class AccountsViewsTests(WebTest):
    def test_get_registration_view(self):
        """
        Simple GET to the registration view that should use the correct
        template and have form in its context
        """
        response = self.client.get(reverse('accounts:register'))

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'accounts/registration_form.html')
        self.failUnless(isinstance(response.context['form'], UserCreationForm))

    def test_post_registration_view_success(self):
        """
        POST to this view should create an user and redirect to the home page
        """
        page = self.app.get(reverse('accounts:register'))

        form = page.forms[0]
        form['email'] = '<EMAIL>'
        form['name'] = '<NAME>.'
        form['password1'] = 'password'
        form['password2'] = 'password'

        response = form.submit()

        self.assertRedirects(
            response, reverse('accounts:register_confirmation')
        )
        self.assertEqual(CustomUser.objects.count(), 1)
        self.assertEqual(len(mail.outbox), 1)
        new_user = CustomUser.objects.get()
        self.assertTrue(new_user.is_company_admin)

    def test_post_registration_view_failure(self):
        """
        POST to this view with an error in the form should display this form
        again with the error
        """
        page = self.app.get(reverse('accounts:register'))

        form = page.forms[0]
        form['email'] = '<EMAIL>'
        form['name'] = '<NAME>.'
        form['password1'] = 'password'
        form['password2'] = '<PASSWORD>'

        response = form.submit()

        self.assertEqual(response.status_code, 200)
        error = "The two password fields didn't match."
        self.assertFormError(response, 'form', 'password2', error)
        self.assertEqual(CustomUser.objects.count(), 0)
        self.assertEqual(len(mail.outbox), 0)

    def test_get_valid_activation(self):
        user = UserFactory(is_active=False, company=None)
        url = reverse('accounts:activate', args=(user.activation_key,))

        response = self.app.get(url).follow()
        user_found = CustomUser.objects.get()

        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'You can now login')
        self.assertTrue(user_found.is_active)

    def test_get_invalid_activation(self):
        """
        GET the activation page with an invalid activation key
        Should raise a 404, using client instead of app since app would fail
        """
        url = reverse('accounts:activate', args=('FAKE',))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    def test_get_login_view(self):
        """
        GET the login auth view (from django itself)
        """
        response = self.app.get(reverse('auth:login'))

        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'accounts/login.html')
        self.failUnless(
            isinstance(response.context['form'], AuthenticationForm)
        )

    def test_post_login_view_success_without_company(self):
        """
        POST to the view to login
        Testing to make sure it works with email/activated user
        """
        user = UserFactory.create(company=None)
{'fraction_non_alphanumeric': 0.10386352232814852, 'fraction_numerical': 0.009282488710486703, 'mean_word_length': 2.97259591429995, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3811430', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2175, 'n_words': 516}