<commit_before>"""Unit tests for the Gerrit hosting service."""

from __future__ import unicode_literals

import base64
import sys

from django.utils.six.moves.urllib.request import (HTTPDigestAuthHandler,
                                                   OpenerDirector)

from reviewboard.hostingsvcs.errors import (AuthorizationError,
                                            HostingServiceAPIError,
                                            HostingServiceError,
                                            RepositoryError)
from reviewboard.hostingsvcs.gerrit import GerritForm
from reviewboard.hostingsvcs.testing import HostingServiceTestCase
from reviewboard.scmtools.core import Branch, Commit
from reviewboard.scmtools.crypto_utils import encrypt_password
from reviewboard.scmtools.errors import FileNotFoundError


class GerritTestCase(HostingServiceTestCase):
    """Base class for Gerrit unit tests."""

    service_name = 'gerrit'

    default_account_data = {
        'authorized': False,
        'gerrit_http_password': <PASSWORD>_password('<PASSWORD>'),
        'gerrit_ssh_port': 1234,
    }

    default_repository_tool_name = 'Git'

    default_repository_extra_data = {
        'gerrit_url': 'http://gerrit.example.com/',
        'gerrit_project_name': 'Project',
    }


class GerritFormTests(GerritTestCase):
    """Unit tests for GerritForm."""

    def test_clean(self):
        """Testing GerritForm.clean"""
        form = GerritForm({
            'gerrit_project_name': 'test-project',
            'gerrit_ssh_port': 12345,
            'gerrit_url': 'http://gerrit.example.com:8080',
        })

        self.assertTrue(form.is_valid())
        self.assertEqual(
            form.cleaned_data,
            {
                'gerrit_domain': 'gerrit.example.com',
                'gerrit_project_name': 'test-project',
                'gerrit_ssh_port': 12345,
                'gerrit_url': 'http://gerrit.example.com:8080/',
            })

    def test_clean_with_errors(self):
        """Testing GerritForm.clean with errors"""
        form = GerritForm({
            'gerrit_url': 'invalid',
        })

        self.assertFalse(form.is_valid())
        self.assertEqual(form.cleaned_data, {})
        self.assertIn('gerrit_project_name', form.errors)
        self.assertIn('gerrit_ssh_port', form.errors)
        self.assertIn('gerrit_url', form.errors)
        self.assertEqual(form.errors['gerrit_project_name'],
                         ['This field is required.'])
        self.assertEqual(form.errors['gerrit_ssh_port'],
                         ['This field is required.'])
        self.assertEqual(form.errors['gerrit_url'],
                         ['Enter a valid URL.'])


class GerritClientTests(GerritTestCase):
    """Unit tests for GerritClient."""

    def setUp(self):
        super(GerritClientTests, self).setUp()
<commit_msg>Update the GerritForm.clean() tests for cross-Django compatibility.<commit_after>"""Unit tests for the Gerrit hosting service."""

from __future__ import unicode_literals

import base64
import sys

from django.utils.six.moves.urllib.request import (HTTPDigestAuthHandler,
                                                   OpenerDirector)

from reviewboard.hostingsvcs.errors import (AuthorizationError,
                                            HostingServiceAPIError,
                                            HostingServiceError,
                                            RepositoryError)
from reviewboard.hostingsvcs.gerrit import GerritForm
from reviewboard.hostingsvcs.testing import HostingServiceTestCase
from reviewboard.scmtools.core import Branch, Commit
from reviewboard.scmtools.crypto_utils import encrypt_password
from reviewboard.scmtools.errors import FileNotFoundError


class GerritTestCase(HostingServiceTestCase):
    """Base class for Gerrit unit tests."""

    service_name = 'gerrit'

    default_account_data = {
        'authorized': False,
        'gerrit_http_password': encrypt_password('<PASSWORD>'),
        'gerrit_ssh_port': 1234,
    }

    default_repository_tool_name = 'Git'

    default_repository_extra_data = {
        'gerrit_url': 'http://gerrit.example.com/',
        'gerrit_project_name': 'Project',
    }


class GerritFormTests(GerritTestCase):
    """Unit tests for GerritForm."""

    def test_clean_sets_gerrit_domain(self):
        """Testing GerritForm.clean sets gerrit_domain"""
        form = GerritForm({
            'gerrit_project_name': 'test-project',
            'gerrit_ssh_port': 12345,
            'gerrit_url': 'http://gerrit.example.com:8080',
        })

        self.assertTrue(form.is_valid())
        self.assertIn('gerrit_domain', form.cleaned_data)
        self.assertEqual(form.cleaned_data['gerrit_domain'],
                         'gerrit.example.com')

    def test_clean_with_errors(self):
        """Testing GerritForm.clean with errors"""
        form = GerritForm({
            'gerrit_url': 'invalid',
        })

        self.assertFalse(form.is_valid())
        self.assertEqual(form.cleaned_data, {})
        self.assertIn('gerrit_project_name', form.errors)
        self.assertIn('gerrit_ssh_port', form.errors)
        self.assertIn('gerrit_url', form.errors)
        self.assertEqual(form.errors['gerrit_project_name'],
                         ['This field is required.'])
        self.assertEqual(form.errors['gerrit_ssh_port'],
                         ['This field is required.'])
        self.assertEqual(form.errors['gerrit_url'],
                         ['Enter a valid URL.'])


class GerritClientTests(GerritTestCase):
    """Unit tests for GerritClient."""

    def setUp(self):
        super(GerritClientTests, self).setUp()

{'fraction_non_alphanumeric': 0.10402802101576182, 'fraction_numerical': 0.006830122591943958, 'mean_word_length': 2.401429422275164, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4659791', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1530, 'n_words': 290}