<commit_before>import re
import time
from unittest.mock import Mock
from urllib.parse import urlparse

import os
from django.contrib.auth.models import User
from django.core.urlresolvers import resolve
from django.test.testcases import TestCase
from hamcrest import matches_regexp, match_equality

from osmaxx.api_client import ConversionApiClient, API_client
from osmaxx.excerptexport.models import Excerpt, ExtractionOrder, ExtractionOrderState, BBoxBoundingGeometry
from osmaxx.job_progress.views import tracker
from tests.test_helpers import vcr_explicit_path as vcr, absolute_cassette_lib_path


class ConversionApiClientAuthTestCase(TestCase):
    @vcr.use_cassette('fixtures/vcr/conversion_api-test_successful_login.yml')
    def test_successful_login(self):
        api_client = ConversionApiClient()

        self.assertIsNone(api_client.token)

        api_client.login()

        self.assertIsNotNone(api_client.token)

    @vcr.use_cassette('fixtures/vcr/conversion_api-test_failed_login.yml')
    def test_failed_login(self):
        api_client = ConversionApiClient()
        api_client.password = '<PASSWORD>'

        self.assertEqual(api_client.password, '<PASSWORD>')
        self.assertIsNone(api_client.token)

        expected_msg = \
            "400 Client Error: BAD REQUEST for url: http://localhost:8901/api/token-auth/"
        with self.assertRaisesRegex(API_client.HTTPError, "^{}$".format(re.escape(expected_msg))) as cm:
            api_client.login()

        self.assertEqual({'non_field_errors': ['Unable to login with provided credentials.']}, API_client.errors(cm.exception))
        self.assertIsNone(api_client.token)


class ConversionApiClientTestCase(TestCase):
    def setUp(self):
        self.request = Mock()
        self.request.build_absolute_uri.return_value = 'http://the-host.example.com/job_progress/tracker/1/'
        self.user = User.objects.create_user('user', '<EMAIL>', 'pw')
        self.bounding_box = BBoxBoundingGeometry.create_from_bounding_box_coordinates(
            40.77739734768811, 29.528980851173397, 40.77546776498174, 29.525547623634335
        )
        self.excerpt = Excerpt.objects.create(
            name='Neverland', is_active=True, is_public=True, owner=self.user, bounding_geometry=self.bounding_box
        )
        self.extraction_order = ExtractionOrder.objects.create(excerpt=self.excerpt, orderer=self.user)
        self.extraction_order.extraction_configuration = {
            'gis_formats': ['fgdb', 'spatialite'],
            'gis_options': {
                'coordinate_reference_system': 'WGS_84',
                'detail_level': 1
            }
        }
        self.api_client = ConversionApiClient()

    @vcr.use_cassette('fixtures/vcr/conversion_api-test_create_job.yml')
    def test_create_job(self):
        self.api_client.login()
        self.assertIsNone(self.extraction_order.process_id)

        response = self.api_client.create_job(self.extraction_order, request=self.request)<commit_msg>refactoring: switch arguments of equality assertion<commit_after>import re
import time
from unittest.mock import Mock
from urllib.parse import urlparse

import os
from django.contrib.auth.models import User
from django.core.urlresolvers import resolve
from django.test.testcases import TestCase
from hamcrest import matches_regexp, match_equality

from osmaxx.api_client import ConversionApiClient, API_client
from osmaxx.excerptexport.models import Excerpt, ExtractionOrder, ExtractionOrderState, BBoxBoundingGeometry
from osmaxx.job_progress.views import tracker
from tests.test_helpers import vcr_explicit_path as vcr, absolute_cassette_lib_path


class ConversionApiClientAuthTestCase(TestCase):
    @vcr.use_cassette('fixtures/vcr/conversion_api-test_successful_login.yml')
    def test_successful_login(self):
        api_client = ConversionApiClient()

        self.assertIsNone(api_client.token)

        api_client.login()

        self.assertIsNotNone(api_client.token)

    @vcr.use_cassette('fixtures/vcr/conversion_api-test_failed_login.yml')
    def test_failed_login(self):
        api_client = ConversionApiClient()
        api_client.password = '<PASSWORD>'

        self.assertEqual(api_client.password, '<PASSWORD>')
        self.assertIsNone(api_client.token)

        expected_msg = \
            "400 Client Error: BAD REQUEST for url: http://localhost:8901/api/token-auth/"
        with self.assertRaisesRegex(API_client.HTTPError, "^{}$".format(re.escape(expected_msg))) as cm:
            api_client.login()

        self.assertEqual(
            API_client.errors(cm.exception),
            {'non_field_errors': ['Unable to login with provided credentials.']}
        )
        self.assertIsNone(api_client.token)


class ConversionApiClientTestCase(TestCase):
    def setUp(self):
        self.request = Mock()
        self.request.build_absolute_uri.return_value = 'http://the-host.example.com/job_progress/tracker/1/'
        self.user = User.objects.create_user('user', '<EMAIL>', 'pw')
        self.bounding_box = BBoxBoundingGeometry.create_from_bounding_box_coordinates(
            40.77739734768811, 29.528980851173397, 40.77546776498174, 29.525547623634335
        )
        self.excerpt = Excerpt.objects.create(
            name='Neverland', is_active=True, is_public=True, owner=self.user, bounding_geometry=self.bounding_box
        )
        self.extraction_order = ExtractionOrder.objects.create(excerpt=self.excerpt, orderer=self.user)
        self.extraction_order.extraction_configuration = {
            'gis_formats': ['fgdb', 'spatialite'],
            'gis_options': {
                'coordinate_reference_system': 'WGS_84',
                'detail_level': 1
            }
        }
        self.api_client = ConversionApiClient()

    @vcr.use_cassette('fixtures/vcr/conversion_api-test_create_job.yml')
    def test_create_job(self):
        self.api_client.login()
        self.assertIsNone(self.extraction_order.process_id)

        response = self.api_client.create_job(self.extraction_order, request=self.request)
{'fraction_non_alphanumeric': 0.09742828882294757, 'fraction_numerical': 0.025387405209363667, 'mean_word_length': 3.6454823889739663, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3428300', 'n_tokens_mistral': 1999, 'n_tokens_neox': 1849, 'n_words': 295}