<commit_before>from urllib import urlencode

import responses

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase

from bellman.models import SlackAccount


class TestAccountViews(TestCase):

    def setUp(self):
        self.user = User.objects.create_user(
            'username', '<EMAIL>', 'password')

    def test_login_view(self):
        response = self.client.get(reverse('accounts:login'))
        self.assertContains(response, 'Login with Google')

    def test_logout_view(self):
        self.client.login(username='username', password='password')
        self.assertEqual(
            self.client.get(reverse('accounts:profile')).status_code,
            200)
        self.assertRedirects(
            self.client.get(reverse('accounts:logout')),
            reverse('accounts:login'))

    def test_new_profile(self):
        self.client.login(username='username', password='password')
        response = self.client.get(reverse('accounts:profile'))
        self.assertTemplateUsed(response, 'account/new_profile.html')

    def test_existing_profile(self):
        self.client.login(username='username', password='password')
        SlackAccount.objects.create(user=self.user)
        response = self.client.get(reverse('accounts:profile'))
        self.assertTemplateUsed(response, 'account/profile.html')

    @responses.activate
    def test_authorize(self):
        responses.add(
            responses.POST, 'https://slack.com/api/oauth.access',
            json={
                'team_id': 'the-team-id',
                'access_token': 'the-access-token',
                'scope': 'the-scope',
                'team_name': 'the-team-name',
                'incoming_webhook': {
                    'url': 'http://incoming-webhook-url/',
                    'channel': 'incoming-webhook-channel',
                    'configuration_url': 'incoming-webhook-configuration-url',
                },
                'bot': {
                    'bot_user_id': 'bot-user-id',
                    'bot_access_token': 'bot-access-token',
                }
            })

        self.client.login(username='username', password='password')
        session = self.client.session
        session['authorize_state'] = 'authorize_state'
        session['authorize_request_uri'] = 'http://testserver/'
        session.save()

        self.assertFalse(self.user.slackaccount_set.exists())
        response = self.client.get('%s?state=authorize_state&code=code' % (
            reverse('accounts:authorize'),))
        [slackaccount] = self.user.slackaccount_set.all()
        self.assertEqual(slackaccount.access_token, 'the-access-token')
        self.assertEqual(slackaccount.bot_user_id, 'bot-user-id')
        self.assertEqual(slackaccount.bot_access_token, 'bot-access-token')<commit_msg>fix flake8<commit_after>import responses

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase

from bellman.models import SlackAccount


class TestAccountViews(TestCase):

    def setUp(self):
        self.user = User.objects.create_user(
            'username', '<EMAIL>', 'password')

    def test_login_view(self):
        response = self.client.get(reverse('accounts:login'))
        self.assertContains(response, 'Login with Google')

    def test_logout_view(self):
        self.client.login(username='username', password='password')
        self.assertEqual(
            self.client.get(reverse('accounts:profile')).status_code,
            200)
        self.assertRedirects(
            self.client.get(reverse('accounts:logout')),
            reverse('accounts:login'))

    def test_new_profile(self):
        self.client.login(username='username', password='password')
        response = self.client.get(reverse('accounts:profile'))
        self.assertTemplateUsed(response, 'account/new_profile.html')

    def test_existing_profile(self):
        self.client.login(username='username', password='password')
        SlackAccount.objects.create(user=self.user)
        response = self.client.get(reverse('accounts:profile'))
        self.assertTemplateUsed(response, 'account/profile.html')

    @responses.activate
    def test_authorize(self):
        responses.add(
            responses.POST, 'https://slack.com/api/oauth.access',
            json={
                'team_id': 'the-team-id',
                'access_token': 'the-access-token',
                'scope': 'the-scope',
                'team_name': 'the-team-name',
                'incoming_webhook': {
                    'url': 'http://incoming-webhook-url/',
                    'channel': 'incoming-webhook-channel',
                    'configuration_url': 'incoming-webhook-configuration-url',
                },
                'bot': {
                    'bot_user_id': 'bot-user-id',
                    'bot_access_token': 'bot-access-token',
                }
            })

        self.client.login(username='username', password='password')
        session = self.client.session
        session['authorize_state'] = 'authorize_state'
        session['authorize_request_uri'] = 'http://testserver/'
        session.save()

        self.assertFalse(self.user.slackaccount_set.exists())
        response = self.client.get('%s?state=authorize_state&code=code' % (
            reverse('accounts:authorize'),))
        self.assertRedirects(response, reverse('accounts:profile'))
        [slackaccount] = self.user.slackaccount_set.all()
        self.assertEqual(slackaccount.access_token, 'the-access-token')
        self.assertEqual(slackaccount.bot_user_id, 'bot-user-id')
        self.assertEqual(slackaccount.bot_access_token, 'bot-access-token')
{'fraction_non_alphanumeric': 0.13168368229797542, 'fraction_numerical': 0.001211282228759301, 'mean_word_length': 2.5767326732673266, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '4210004', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1609, 'n_words': 226}