def test_deprecated_message_in_retvalue(self):

        id_, params = build_data(self.apikey, 'show_ip')
        response = api_call(self.app, params)

        expected = {
            'id':
            id_,
            'error':
            None,
            'result':
            json.loads(response.body)['result'],
            'DEPRECATION_WARNING':
            'DEPRECATED METHOD Please use method `get_ip` instead.'
        }
        assert expected == json.loads(response.body)
 def test_file_source_history(self, backend):
     response = self.app.get(
         url(
             controller='files', action='history',
             repo_name=backend.repo_name,
             revision='tip',
             f_path='vcs/nodes.py'),
         extra_environ={'HTTP_X_PARTIAL_XHR': '1'})
     assert NODE_HISTORY[backend.alias] == json.loads(response.body)
Esempio n. 3
0
    def get_update_data(update_url):
        """Return the JSON update data."""
        ver = rhodecode.__version__
        log.debug('Checking for upgrade on `%s` server', update_url)
        opener = urllib2.build_opener()
        opener.addheaders = [('User-agent', 'RhodeCode-SCM/%s' % ver)]
        response = opener.open(update_url)
        response_data = response.read()
        data = json.loads(response_data)

        return data
    def test_file_source_history_svn(self, backend_svn):
        simple_repo = backend_svn['svn-simple-layout']
        response = self.app.get(
            url(
                controller='files', action='history',
                repo_name=simple_repo.repo_name,
                revision='tip',
                f_path='trunk/example.py'),
            extra_environ={'HTTP_X_PARTIAL_XHR': '1'})

        expected_data = json.loads(
            fixture.load_resource('svn_node_history_branches.json'))
        assert expected_data == response.json
    def _request(self,
                 url,
                 body=None,
                 headers=None,
                 method=None,
                 noformat=False,
                 empty_response_ok=False):
        _headers = {
            "Content-type": "application/json",
            "Accept": "application/json"
        }
        if self.user and self.passwd:
            authstring = base64.b64encode("%s:%s" % (self.user, self.passwd))
            _headers["Authorization"] = "Basic %s" % authstring
        if headers:
            _headers.update(headers)
        log.debug("Sent crowd: \n%s" % (formatted_json({
            "url": url,
            "body": body,
            "headers": _headers
        })))
        request = urllib2.Request(url, body, _headers)
        if method:
            request.get_method = lambda: method

        global msg
        msg = ""
        try:
            rdoc = self.opener.open(request)
            msg = "".join(rdoc.readlines())
            if not msg and empty_response_ok:
                rval = {}
                rval["status"] = True
                rval["error"] = "Response body was empty"
            elif not noformat:
                rval = json.loads(msg)
                rval["status"] = True
            else:
                rval = "".join(rdoc.readlines())
        except Exception as e:
            if not noformat:
                rval = {
                    "status": False,
                    "body": body,
                    "error": str(e) + "\n" + msg
                }
            else:
                rval = None
        return rval
Esempio n. 6
0
def api_call(apikey, apihost, format, method=None, **kw):
    """
    Api_call wrapper for RhodeCode

    :param apikey:
    :param apihost:
    :param format: formatting, pretty means prints and pprint of json
     json returns unparsed json
    :param method:
    """
    def _build_data(random_id):
        """
        Builds API data with given random ID

        :param random_id:
        :type random_id:
        """
        return {
            "id": random_id,
            "api_key": apikey,
            "method": method,
            "args": kw
        }

    if not method:
        raise Exception('please specify method name !')
    id_ = random.randrange(1, 9999)
    req = urllib2.Request('%s/_admin/api' % apihost,
                          data=json.dumps(_build_data(id_)),
                          headers={'content-type': 'text/plain'})
    if format == FORMAT_PRETTY:
        sys.stdout.write('calling %s to %s \n' % (req.get_data(), apihost))
    ret = urllib2.urlopen(req)
    raw_json = ret.read()
    json_data = json.loads(raw_json)
    id_ret = json_data['id']
    _formatted_json = pprint.pformat(json_data)
    if id_ret == id_:
        if format == FORMAT_JSON:
            sys.stdout.write(str(raw_json))
        else:
            sys.stdout.write('rhodecode returned:\n%s\n' % (_formatted_json))

    else:
        raise Exception('something went wrong. '
                        'ID mismatch got %s, expected %s | %s' %
                        (id_ret, id_, _formatted_json))
Esempio n. 7
0
def api_call(apikey, apihost, format, method=None, **kw):
    """
    Api_call wrapper for RhodeCode

    :param apikey:
    :param apihost:
    :param format: formatting, pretty means prints and pprint of json
     json returns unparsed json
    :param method:
    """
    def _build_data(random_id):
        """
        Builds API data with given random ID

        :param random_id:
        :type random_id:
        """
        return {
            "id": random_id,
            "api_key": apikey,
            "method": method,
            "args": kw
        }

    if not method:
        raise Exception('please specify method name !')
    id_ = random.randrange(1, 9999)
    req = urllib2.Request('%s/_admin/api' % apihost,
                      data=json.dumps(_build_data(id_)),
                      headers={'content-type': 'text/plain'})
    if format == FORMAT_PRETTY:
        sys.stdout.write('calling %s to %s \n' % (req.get_data(), apihost))
    ret = urllib2.urlopen(req)
    raw_json = ret.read()
    json_data = json.loads(raw_json)
    id_ret = json_data['id']
    _formatted_json = pprint.pformat(json_data)
    if id_ret == id_:
        if format == FORMAT_JSON:
            sys.stdout.write(str(raw_json))
        else:
            sys.stdout.write('rhodecode returned:\n%s\n' % (_formatted_json))

    else:
        raise Exception('something went wrong. '
                        'ID mismatch got %s, expected %s | %s' % (
                                            id_ret, id_, _formatted_json))
Esempio n. 8
0
def api_call(apikey, apihost, method=None, **kw):
    """
    Api_call wrapper for RhodeCode.

    :param apikey:
    :param apihost:
    :param format: formatting, pretty means prints and pprint of json
     json returns unparsed json
    :param method:
    :returns: json response from server
    """
    def _build_data(random_id):
        """
        Builds API data with given random ID

        :param random_id:
        """
        return {
            "id": random_id,
            "api_key": apikey,
            "method": method,
            "args": kw
        }

    if not method:
        raise Exception('please specify method name !')

    id_ = random.randrange(1, 9999)
    req = urllib2.Request('%s/_admin/api' % apihost,
                          data=json.dumps(_build_data(id_)),
                          headers={'content-type': 'text/plain'})
    ret = urllib2.urlopen(req)
    raw_json = ret.read()
    json_data = json.loads(raw_json)
    id_ret = json_data['id']
    if id_ret == id_:
        return json_data

    else:
        _formatted_json = pprint.pformat(json_data)
        raise Exception('something went wrong. '
                        'ID mismatch got %s, expected %s | %s' %
                        (id_ret, id_, _formatted_json))
Esempio n. 9
0
def api_call(apikey, apihost, method=None, **kw):
    """
    Api_call wrapper for RhodeCode.

    :param apikey:
    :param apihost:
    :param format: formatting, pretty means prints and pprint of json
     json returns unparsed json
    :param method:
    :returns: json response from server
    """
    def _build_data(random_id):
        """
        Builds API data with given random ID

        :param random_id:
        """
        return {
            "id": random_id,
            "api_key": apikey,
            "method": method,
            "args": kw
        }

    if not method:
        raise Exception('please specify method name !')

    id_ = random.randrange(1, 9999)
    req = urllib2.Request('%s/_admin/api' % apihost,
                      data=json.dumps(_build_data(id_)),
                      headers={'content-type': 'text/plain'})
    ret = urllib2.urlopen(req)
    raw_json = ret.read()
    json_data = json.loads(raw_json)
    id_ret = json_data['id']
    if id_ret == id_:
        return json_data

    else:
        _formatted_json = pprint.pformat(json_data)
        raise Exception('something went wrong. '
                        'ID mismatch got %s, expected %s | %s' % (
                                            id_ret, id_, _formatted_json))
Esempio n. 10
0
def setup_request(request):
    """
    Parse a JSON-RPC request body. It's used inside the predicates method
    to validate and bootstrap requests for usage in rpc calls.

    We need to raise JSONRPCError here if we want to return some errors back to
    user.
    """
    log.debug('Executing setup request: %r', request)
    request.rpc_ip_addr = get_ip_addr(request.environ)
    # TODO: marcink, deprecate GET at some point
    if request.method not in ['POST', 'GET']:
        log.debug('unsupported request method "%s"', request.method)
        raise JSONRPCError('unsupported request method "%s". Please use POST' %
                           request.method)

    if 'CONTENT_LENGTH' not in request.environ:
        log.debug("No Content-Length")
        raise JSONRPCError("Empty body, No Content-Length in request")

    else:
        length = request.environ['CONTENT_LENGTH']
        log.debug('Content-Length: %s', length)

        if length == 0:
            log.debug("Content-Length is 0")
            raise JSONRPCError("Content-Length is 0")

    raw_body = request.body
    try:
        json_body = json.loads(raw_body)
    except ValueError as e:
        # catch JSON errors Here
        raise JSONRPCError("JSON parse error ERR:%s RAW:%r" % (e, raw_body))

    request.rpc_id = json_body.get('id')
    request.rpc_method = json_body.get('method')

    # check required base parameters
    try:
        api_key = json_body.get('api_key')
        if not api_key:
            api_key = json_body.get('auth_token')

        if not api_key:
            raise KeyError('api_key or auth_token')

        request.rpc_api_key = api_key
        request.rpc_id = json_body['id']
        request.rpc_method = json_body['method']
        request.rpc_params = json_body['args'] \
            if isinstance(json_body['args'], dict) else {}

        log.debug('method: %s, params: %s' %
                  (request.rpc_method, request.rpc_params))
    except KeyError as e:
        raise JSONRPCError('Incorrect JSON data. Missing %s' % e)

    log.debug(
        'setup complete, now handling method:%s rpcid:%s',
        request.rpc_method,
        request.rpc_id,
    )
Esempio n. 11
0
def jsonify(obj):
    return json.loads(json.dumps(obj))
Esempio n. 12
0
def assert_error(id_, expected, given):
    expected = jsonify({'id': id_, 'error': expected, 'result': None})
    given = json.loads(given)
    assert expected == given
Esempio n. 13
0
def test_dumps_set():
    result = json.dumps(set((1, 2, 3)))
    # We cannot infer what the order of result is going to be
    result = json.loads(result)
    assert isinstance(result, list)
    assert [1, 2, 3] == sorted(result)
from rhodecode.controllers.files import FilesController
from rhodecode.lib import helpers as h
from rhodecode.lib.compat import OrderedDict
from rhodecode.lib.ext_json import json
from rhodecode.lib.vcs import nodes
from rhodecode.lib.vcs.conf import settings
from rhodecode.tests import (
    url, assert_session_flash, assert_not_in_session_flash)
from rhodecode.tests.fixture import Fixture
from rhodecode.tests.utils import AssertResponse

fixture = Fixture()

NODE_HISTORY = {
    'hg': json.loads(fixture.load_resource('hg_node_history_response.json')),
    'git': json.loads(fixture.load_resource('git_node_history_response.json')),
    'svn': json.loads(fixture.load_resource('svn_node_history_response.json')),
}


@pytest.mark.usefixtures("app")
class TestFilesController:

    def test_index(self, backend):
        response = self.app.get(url(
            controller='files', action='index',
            repo_name=backend.repo_name, revision='tip', f_path='/'))
        commit = backend.repo.get_commit()

        params = {
Esempio n. 15
0
    def index(self, repo_name):
        username = ''
        if c.rhodecode_user.username != User.DEFAULT_USER:
            username = safe_str(c.rhodecode_user.username)

        _def_clone_uri = _def_clone_uri_by_id = c.clone_uri_tmpl
        if '{repo}' in _def_clone_uri:
            _def_clone_uri_by_id = _def_clone_uri.replace(
                '{repo}', '_{repoid}')
        elif '{repoid}' in _def_clone_uri:
            _def_clone_uri_by_id = _def_clone_uri.replace(
                '_{repoid}', '{repo}')

        c.clone_repo_url = c.rhodecode_db_repo.clone_url(
            user=username, uri_tmpl=_def_clone_uri)
        c.clone_repo_url_id = c.rhodecode_db_repo.clone_url(
            user=username, uri_tmpl=_def_clone_uri_by_id)

        c.show_stats = bool(c.rhodecode_db_repo.enable_statistics)

        stats = self.sa.query(Statistics)\
            .filter(Statistics.repository == c.rhodecode_db_repo)\
            .scalar()

        c.stats_percentage = 0

        if stats and stats.languages:
            c.no_data = False is c.rhodecode_db_repo.enable_statistics
            lang_stats_d = json.loads(stats.languages)

            # Sort first by decreasing count and second by the file extension,
            # so we have a consistent output.
            lang_stats_items = sorted(lang_stats_d.iteritems(),
                                      key=lambda k: (-k[1], k[0]))[:10]
            lang_stats = [(x, {
                "count": y,
                "desc": LANGUAGES_EXTENSIONS_MAP.get(x)
            }) for x, y in lang_stats_items]

            c.trending_languages = json.dumps(lang_stats)
        else:
            c.no_data = True
            c.trending_languages = json.dumps({})

        c.enable_downloads = c.rhodecode_db_repo.enable_downloads
        c.repository_followers = self.scm_model.get_followers(
            c.rhodecode_db_repo)
        c.repository_forks = self.scm_model.get_forks(c.rhodecode_db_repo)
        c.repository_is_user_following = self.scm_model.is_following_repo(
            c.repo_name, c.rhodecode_user.user_id)

        if c.repository_requirements_missing:
            return render('summary/missing_requirements.html')

        c.readme_data, c.readme_file = \
            self.__get_readme_data(c.rhodecode_db_repo)

        _load_changelog_summary()

        if request.is_xhr:
            return render('changelog/changelog_summary_data.html')

        return render('summary/summary.html')