<commit_before>from django.test import TestCase
from rest_framework import status
from rest_framework.test import APIClient

from ...utils.tests import AuthenticatedAPITestCase
from ...accounts.models import Account, AccountCollaborator


class ValidateSignupDomainsAPIViewTestCase(TestCase):
    def setUp(self):
        self.client = APIClient()

    def test_post_valid_data(self):
        """
        Tests that POST request with valid data to endpoint
        returns expected data.
        """
        data = {
            'signup_domains': ['example.com']
        }

        response = self.client.post(
            '/api/auth/signup_domains/validate/', data, format='json')

        expected_response = {
            'signup_domains': ['example.com']
        }

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, expected_response)

    def test_post_invalid_data(self):
        """
        Tests that POST request with invalid data to endpoint
        returns expected error.
        """
        response = self.client.post('/api/auth/signup_domains/validate/')

        expected_response = {
            'error': {
                'signup_domains': ['This field is required.']
            }
        }

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data, expected_response)

    def test_post_blacklisted_domain(self):
        """
        Tests that POST request with blacklisted domain to endpoint
        returns expected error.
        """
        data = {
            'signup_domains': ['gmail.com']
        }

        response = self.client.post(
            '/api/auth/signup_domains/validate/', data, format='json')

        expected_response = {
            'error': {
                'signup_domains': [
                    "gmail.com is an invalid sign-up domain."
                ]
            }
        }

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data, expected_response)


class AccountsForUserAPIViewTestCase(AuthenticatedAPITestCase):
    def setUp(self):
        super().setUp()

        self.account = Account.objects.create(name='Acme', slug='acme')
        self.collaborator = AccountCollaborator.objects.create(
            user=self.user, account=self.account)

    def test_get_accounts_without_token_should_fail(self):
        self.client = APIClient()
        response = self.client.get('/api/accounts/')

        expected_response = {
            'error': 'Authentication credentials were not provided.',
            'status_code': 401
        }

        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        self.assertEqual(response.data, expected_response)

    def test_get_account_with_token_should_work(self):
        response = self.client.get('/api/accounts/')
<commit_msg>Made super() py2 friendly<commit_after>from django.test import TestCase
from rest_framework import status
from rest_framework.test import APIClient

from ...utils.tests import AuthenticatedAPITestCase
from ...accounts.models import Account, AccountCollaborator


class ValidateSignupDomainsAPIViewTestCase(TestCase):
    def setUp(self):
        self.client = APIClient()

    def test_post_valid_data(self):
        """
        Tests that POST request with valid data to endpoint
        returns expected data.
        """
        data = {
            'signup_domains': ['example.com']
        }

        response = self.client.post(
            '/api/auth/signup_domains/validate/', data, format='json')

        expected_response = {
            'signup_domains': ['example.com']
        }

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, expected_response)

    def test_post_invalid_data(self):
        """
        Tests that POST request with invalid data to endpoint
        returns expected error.
        """
        response = self.client.post('/api/auth/signup_domains/validate/')

        expected_response = {
            'error': {
                'signup_domains': ['This field is required.']
            }
        }

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data, expected_response)

    def test_post_blacklisted_domain(self):
        """
        Tests that POST request with blacklisted domain to endpoint
        returns expected error.
        """
        data = {
            'signup_domains': ['gmail.com']
        }

        response = self.client.post(
            '/api/auth/signup_domains/validate/', data, format='json')

        expected_response = {
            'error': {
                'signup_domains': [
                    "gmail.com is an invalid sign-up domain."
                ]
            }
        }

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data, expected_response)


class AccountsForUserAPIViewTestCase(AuthenticatedAPITestCase):
    def setUp(self):
        super(AccountsForUserAPIViewTestCase, self).setUp()

        self.account = Account.objects.create(name='Acme', slug='acme')
        self.collaborator = AccountCollaborator.objects.create(
            user=self.user, account=self.account)

    def test_get_accounts_without_token_should_fail(self):
        self.client = APIClient()
        response = self.client.get('/api/accounts/')

        expected_response = {
            'error': 'Authentication credentials were not provided.',
            'status_code': 401
        }

        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        self.assertEqual(response.data, expected_response)

    def test_get_account_with_token_should_work(self):
        response = self.client.get('/api/accounts/')

{'fraction_non_alphanumeric': 0.09844120637072179, 'fraction_numerical': 0.005252456794307014, 'mean_word_length': 2.375071469411092, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3932682', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1577, 'n_words': 310}