<commit_before>from mock import (
    patch,
    MagicMock,
    mock_open,
)
from nose import with_setup
from nose.tools import raises
import requests
import os
import json
import hashlib
from imboclient import client as imbo
from imboclient.url import image, images, status, user, accesstoken, metadata
import sys


class TestClient:
    def setup(self):
        self._client = imbo.Client(['http://imbo.local'], 'public', 'private');

    def teardown(self):
        self._client = None

    def test_server_urls_generic(self):
        self._client = imbo.Client(['imbo.local'], 'public', 'private');
        assert self._client.server_urls[0] == 'http://imbo.local'

    def test_server_urls_http(self):
        self._client = imbo.Client(['http://imbo.local'], 'public', 'private');
        assert self._client.server_urls[0] == 'http://imbo.local'

    def test_server_urls_https(self):
        self._client = imbo.Client(['https://imbo.local'], 'public', 'private');
        assert self._client.server_urls[0] == 'https://imbo.local'

    def test_server_urls_port_normal(self):
        self._client = imbo.Client(['http://imbo.local'], 'public', 'private');
        assert self._client.server_urls[0] == 'http://imbo.local'

    def test_server_urls_port_normal_explicit(self):
        self._client = imbo.Client(['http://imbo.local:80'], 'public', 'private');
        assert self._client.server_urls[0] == 'http://imbo.local'

    def test_server_urls_port_ssl(self):
        self._client = imbo.Client(['https://imbo.local:443'], 'public', 'private');
        assert self._client.server_urls[0] == 'https://imbo.local'

    def test_server_urls_port_explicit_without_protocol(self):
        self._client = imbo.Client(['imbo.local:8000'], 'public', 'private');
        assert self._client.server_urls[0] == 'http://imbo.local:8000'

    @patch('imboclient.url.status.UrlStatus')
    def test_status_url(self, mocked_url_status):
        status_url = self._client.status_url()
        mocked_url_status.assert_called_once_with('http://imbo.local', 'public', 'private')
        assert status_url == mocked_url_status()

    @patch('imboclient.url.user.UrlUser')
    def test_user_url(self, mocked_url_user):
        user_url = self._client.user_url()
        mocked_url_user.assert_called_once_with('http://imbo.local', 'public', 'private')
        assert user_url == mocked_url_user()

    @patch('imboclient.url.images.UrlImages')
    def test_images_url(self, mocked_url_images):
        images_url = self._client.images_url()
        mocked_url_images.assert_called_once_with('http://imbo.local', 'public', 'private')
        assert images_url == mocked_url_images()

    @patch('imboclient.url.image.UrlImage')
    def test_image_url(self, mocked_url_image):
        image_url = self._client.image_url('ff')
        mocked_url_image.assert_called_once_with('http://imbo.local', 'public', 'private', 'ff')
        assert image_url == mocked_url_image()

    @patch('imboclient.url.metadata.UrlMetadata')
    def test_metadata_url(self, mocked_url_metadata):
        metadata_url = self._client.metadata_url('ff')
        mocked_url_metadata.assert_called_once_with('http://imbo.local', 'public', 'private', 'ff')
        assert metadata_url == mocked_url_metadata()

    @patch('imboclient.header.authenticate.Authenticate.headers')
    @patch('imboclient.url.images.UrlImages.url')
    @patch('requests.post')
    @patch('os.path.isfile')
    @patch('os.path.getsize')
    def test_add_image(self, mocked_os_path_getsize, mocked_os_path_isfile, mocked_requests_post, mocked_url, mocked_headers):
        mocked_os_path_isfile.return_value = True
        mocked_os_path_getsize.return_value = 7

        response_mock = MagicMock()
        response_mock.status_code = 201

        mocked_requests_post.return_value = response_mock

        mocked_url.return_value = 'url'
        mocked_headers.return_value = {'Accept': 'application/json'}

        content = 'content'
        m = '__builtin__'

        if sys.version_info >= (3,):
            m = 'builtins'

        mocked_open = mock_open(read_data=content)

        with patch(m + '.open', mocked_open):
            result = self._client.add_image('/mocked/image/path.jpg')
            mocked_requests_post.assert_called_once_with('url', data='content', headers={'Accept': 'application/json'})


    @patch('imboclient.header.authenticate.Authenticate.headers')
    @patch('requests.post')
    @patch('imboclient.url.images.UrlImages.url')
    def test_add_image_from_string(self, mock_image_url, mock_requests_post, mock_headers):
        mock_image_url.return_value = 'imageurl'
        mock_headers.return_value = {'Accept': 'application/json'}

        response_mock = MagicMock()
        response_mock.status_code = 201

        mock_requests_post.return_value = response_mock

        result = self._client.add_image_from_string('imagestring')

        mock_requests_post.assert_called_once_with('imageurl', data='imagestring', headers={'Accept': 'application/json'})
        assert result

    @raises(imbo.Client.ImboTransportError)
    def test_wrap_result_transport_failure(self):
        def fails(self):
            raise requests.exceptions.RequestException('Fail')

        self._client._wrap_result(fails, [200], 'error')

    @raises(imbo.Client.ImboInternalError)
    def test_wrap_result_internal_failure(self):
        def fails(self):
            class Response(object):
                pass

            response = Response()
            response.status_code = 400
            response.text = 'err'

            return response

        self._client._wrap_result(fails, [200], 'error')

    @patch('imboclient.client.Client.add_image_from_string')
    @patch('requests.get')
    def test_add_image_from_url(self, mock_requests_get, mock_add_image_from_string):
        response_mock = MagicMock()
        response_mock.status_code = 200
        response_mock.content = 'imagedatafromhttp'

        mock_requests_get.return_value = response_mock

        self._client.add_image_from_url('validimageurl')
        mock_requests_get.assert_called_once_with('validimageurl')
        mock_add_image_from_string.assert_called_once_with('imagedatafromhttp')

    @patch('imboclient.client.Client.image_identifier_exists')
    def test_image_exists(self, mock_image_identifier_exists):
        mock_image_identifier_exists.return_value = True

        image_exists = self._client.image_exists('identifier')

        assert image_exists is True
        mock_image_identifier_exists.assert_called_once_with('identifier')

    @patch('requests.head')
    @patch('imboclient.url.image.UrlImage')
    def test_image_identifier_exists_true(self, mocked_url_image, mocked_requests_head):
        mocked_requests_head.return_value = self._valid_requests_response_stub_ok()
        mocked_url_image_instance = mocked_url_image.return_value
        mocked_url_image_instance.url.return_value = 'http://imbo.local/users/public/ff?accessToken=aa'

        assert self._client.image_identifier_exists('ff') is True
        mocked_requests_head.assert_called_once_with('http://imbo.local/users/public/ff?accessToken=aa')
        mocked_url_image.assert_called_once_with('http://imbo.local', 'public', 'private', 'ff')

    @patch('requests.head')
    @patch('imboclient.url.image.UrlImage')
    def test_image_identifier_exists_false(self, mocked_url_image, mocked_requests_get):
        mocked_requests_get.return_value = self._invalid_requests_response_stub_not_found()
        mocked_url_image_instance = mocked_url_image.return_value
        mocked_url_image_instance.url.return_value = 'http://imbo.local/users/public/ffa?accessToken=aaf'

        assert self._client.image_identifier_exists('ffa') is False
        mocked_requests_get.assert_called_once_with('http://imbo.local/users/public/ffa?accessToken=aaf')
        mocked_url_image.assert_called_once_with('http://imbo.local', 'public', 'private', 'ffa')

    @patch('imboclient.url.image.UrlImage.url')
    @patch('requests.head')
    def test_head_image(self, mock_requests_head, mock_image_url):
        class StubResponse:
            status_code = 200

        mock_image_url.return_value = "imageurl"
        mock_requests_head.return_value = StubResponse()

        self._client.head_image("ff")
        mock_image_url.assert_called_once_with()
        mock_requests_head.assert_called_once_with("imageurl")

    @patch('imboclient.header.authenticate.Authenticate.headers')
    @patch('imboclient.url.image.UrlImage.url')
    @patch('requests.delete')
    def test_delete_image(self, mock_requests_delete, mock_image_url, mock_headers):
        class StubResponse:
            status_code = 200
            text = '{}'

            def json(self):
                return {}

        mock_image_url.return_value = "imageurl"
        mock_headers.return_value = {}<commit_msg>Update mocks to expect the user= parameter, although empty<commit_after>from mock import (
    patch,
    MagicMock,
    mock_open,
)
from nose import with_setup
from nose.tools import raises
import requests
import os
import json
import hashlib
from imboclient import client as imbo
from imboclient.url import image, images, status, user, accesstoken, metadata
import sys


class TestClient:
    def setup(self):
        self._client = imbo.Client(['http://imbo.local'], 'public', 'private');

    def teardown(self):
        self._client = None

    def test_server_urls_generic(self):
        self._client = imbo.Client(['imbo.local'], 'public', 'private');
        assert self._client.server_urls[0] == 'http://imbo.local'

    def test_server_urls_http(self):
        self._client = imbo.Client(['http://imbo.local'], 'public', 'private');
        assert self._client.server_urls[0] == 'http://imbo.local'

    def test_server_urls_https(self):
        self._client = imbo.Client(['https://imbo.local'], 'public', 'private');
        assert self._client.server_urls[0] == 'https://imbo.local'

    def test_server_urls_port_normal(self):
        self._client = imbo.Client(['http://imbo.local'], 'public', 'private');
        assert self._client.server_urls[0] == 'http://imbo.local'

    def test_server_urls_port_normal_explicit(self):
        self._client = imbo.Client(['http://imbo.local:80'], 'public', 'private');
        assert self._client.server_urls[0] == 'http://imbo.local'

    def test_server_urls_port_ssl(self):
        self._client = imbo.Client(['https://imbo.local:443'], 'public', 'private');
        assert self._client.server_urls[0] == 'https://imbo.local'

    def test_server_urls_port_explicit_without_protocol(self):
        self._client = imbo.Client(['imbo.local:8000'], 'public', 'private');
        assert self._client.server_urls[0] == 'http://imbo.local:8000'

    @patch('imboclient.url.status.UrlStatus')
    def test_status_url(self, mocked_url_status):
        status_url = self._client.status_url()
        mocked_url_status.assert_called_once_with('http://imbo.local', 'public', 'private')
        assert status_url == mocked_url_status()

    @patch('imboclient.url.user.UrlUser')
    def test_user_url(self, mocked_url_user):
        user_url = self._client.user_url()
        mocked_url_user.assert_called_once_with('http://imbo.local', 'public', 'private')
        assert user_url == mocked_url_user()

    @patch('imboclient.url.images.UrlImages')
    def test_images_url(self, mocked_url_images):
        images_url = self._client.images_url()
        mocked_url_images.assert_called_once_with('http://imbo.local', 'public', 'private', user=None)
        assert images_url == mocked_url_images()

    @patch('imboclient.url.image.UrlImage')
    def test_image_url(self, mocked_url_image):
        image_url = self._client.image_url('ff')
        mocked_url_image.assert_called_once_with('http://imbo.local', 'public', 'private', 'ff', user=None)
        assert image_url == mocked_url_image()

    @patch('imboclient.url.metadata.UrlMetadata')
    def test_metadata_url(self, mocked_url_metadata):
        metadata_url = self._client.metadata_url('ff')
        mocked_url_metadata.assert_called_once_with('http://imbo.local', 'public', 'private', 'ff', user=None)
        assert metadata_url == mocked_url_metadata()

    @patch('imboclient.header.authenticate.Authenticate.headers')
    @patch('imboclient.url.images.UrlImages.url')
    @patch('requests.post')
    @patch('os.path.isfile')
    @patch('os.path.getsize')
    def test_add_image(self, mocked_os_path_getsize, mocked_os_path_isfile, mocked_requests_post, mocked_url, mocked_headers):
        mocked_os_path_isfile.return_value = True
        mocked_os_path_getsize.return_value = 7

        response_mock = MagicMock()
        response_mock.status_code = 201

        mocked_requests_post.return_value = response_mock

        mocked_url.return_value = 'url'
        mocked_headers.return_value = {'Accept': 'application/json'}

        content = 'content'
        m = '__builtin__'

        if sys.version_info >= (3,):
            m = 'builtins'

        mocked_open = mock_open(read_data=content)

        with patch(m + '.open', mocked_open):
            result = self._client.add_image('/mocked/image/path.jpg')
            mocked_requests_post.assert_called_once_with('url', data='content', headers={'Accept': 'application/json'})


    @patch('imboclient.header.authenticate.Authenticate.headers')
    @patch('requests.post')
    @patch('imboclient.url.images.UrlImages.url')
    def test_add_image_from_string(self, mock_image_url, mock_requests_post, mock_headers):
        mock_image_url.return_value = 'imageurl'
        mock_headers.return_value = {'Accept': 'application/json'}

        response_mock = MagicMock()
        response_mock.status_code = 201

        mock_requests_post.return_value = response_mock

        result = self._client.add_image_from_string('imagestring')

        mock_requests_post.assert_called_once_with('imageurl', data='imagestring', headers={'Accept': 'application/json'})
        assert result

    @raises(imbo.Client.ImboTransportError)
    def test_wrap_result_transport_failure(self):
        def fails(self):
            raise requests.exceptions.RequestException('Fail')

        self._client._wrap_result(fails, [200], 'error')

    @raises(imbo.Client.ImboInternalError)
    def test_wrap_result_internal_failure(self):
        def fails(self):
            class Response(object):
                pass

            response = Response()
            response.status_code = 400
            response.text = 'err'

            return response

        self._client._wrap_result(fails, [200], 'error')

    @patch('imboclient.client.Client.add_image_from_string')
    @patch('requests.get')
    def test_add_image_from_url(self, mock_requests_get, mock_add_image_from_string):
        response_mock = MagicMock()
        response_mock.status_code = 200
        response_mock.content = 'imagedatafromhttp'

        mock_requests_get.return_value = response_mock

        self._client.add_image_from_url('validimageurl')
        mock_requests_get.assert_called_once_with('validimageurl')
        mock_add_image_from_string.assert_called_once_with('imagedatafromhttp')

    @patch('imboclient.client.Client.image_identifier_exists')
    def test_image_exists(self, mock_image_identifier_exists):
        mock_image_identifier_exists.return_value = True

        image_exists = self._client.image_exists('identifier')

        assert image_exists is True
        mock_image_identifier_exists.assert_called_once_with('identifier')

    @patch('requests.head')
    @patch('imboclient.url.image.UrlImage')
    def test_image_identifier_exists_true(self, mocked_url_image, mocked_requests_head):
        mocked_requests_head.return_value = self._valid_requests_response_stub_ok()
        mocked_url_image_instance = mocked_url_image.return_value
        mocked_url_image_instance.url.return_value = 'http://imbo.local/users/public/ff?accessToken=aa'

        assert self._client.image_identifier_exists('ff') is True
        mocked_requests_head.assert_called_once_with('http://imbo.local/users/public/ff?accessToken=aa')

    @patch('requests.head')
    @patch('imboclient.url.image.UrlImage')
    def test_image_identifier_exists_false(self, mocked_url_image, mocked_requests_get):
        mocked_requests_get.return_value = self._invalid_requests_response_stub_not_found()
        mocked_url_image_instance = mocked_url_image.return_value
        mocked_url_image_instance.url.return_value = 'http://imbo.local/users/public/ffa?accessToken=aaf'

        assert self._client.image_identifier_exists('ffa') is False
        mocked_requests_get.assert_called_once_with('http://imbo.local/users/public/ffa?accessToken=aaf')
        mocked_url_image.assert_called_once_with('http://imbo.local', 'public', 'private', 'ffa', user=None)

    @patch('imboclient.url.image.UrlImage.url')
    @patch('requests.head')
    def test_head_image(self, mock_requests_head, mock_image_url):
        class StubResponse:
            status_code = 200

        mock_image_url.return_value = "imageurl"
        mock_requests_head.return_value = StubResponse()

        self._client.head_image("ff")
        mock_image_url.assert_called_once_with()
        mock_requests_head.assert_called_once_with("imageurl")

    @patch('imboclient.header.authenticate.Authenticate.headers')
    @patch('imboclient.url.image.UrlImage.url')
    @patch('requests.delete')
    def test_delete_image(self, mock_requests_delete, mock_image_url, mock_headers):
        class StubResponse:
            status_code = 200
            text = '{}'

            def json(self):
                return {}

        mock_image_url.return_value = "imageurl"
        mock_headers.return_value = {}
{'fraction_non_alphanumeric': 0.11914654688377316, 'fraction_numerical': 0.005165637282425604, 'mean_word_length': 3.725656672857522, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 8, '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': '3086286', 'n_tokens_mistral': 5735, 'n_tokens_neox': 5562, 'n_words': 784}