コード例 #1
0
ファイル: test_api.py プロジェクト: Cadasta/ckanext-project
    def test_all_no_parameters_fail(self):
        for action, cadasta_endpoint in post_files_api_map.items():
            print "testing {action}".format(action=action),
            # read our expected json output as <action_name>.json
            filepath = os.path.join(self.data_dir, ".".join([action, "json"]))
            body = open(filepath).read()

            # add the expected parameters (everything is a 1)
            api_url = cadasta_endpoint.url
            url_args = dict([(a[1], 1) for a in string.Formatter().parse(api_url) if a[1]])

            # if this endpoint needs no parameters, quit early, test does not
            # apply
            if not url_args:
                return

            # make sure the point parameters are filled out
            endpoint = urljoin(self.test_api, api_url).format(**url_args)

            # fake out our response
            responses.add(responses.POST, endpoint, body=body, content_type="application/json")

            # call our action with no arguments
            assert_raises(toolkit.ValidationError, helpers.call_action, action)
            print "\t[OK]"
コード例 #2
0
ファイル: test_api.py プロジェクト: okfn/ckanext-cadastaroles
    def test_all_no_parameters_fail(self):
        for action, cadasta_endpoint in post_files_api_map.items():
            print 'testing {action}'.format(action=action),
            # read our expected json output as <action_name>.json
            filepath = os.path.join(self.data_dir, '.'.join([action, 'json']))
            body = open(filepath).read()

            # add the expected parameters (everything is a 1)
            api_url = cadasta_endpoint.url
            url_args = dict([(a[1], 1)
                             for a in string.Formatter().parse(api_url)
                             if a[1]])

            # if this endpoint needs no parameters, quit early, test does not
            # apply
            if not url_args:
                return

            # make sure the point parameters are filled out
            endpoint = urljoin(self.test_api, api_url).format(**url_args)

            # fake out our response
            responses.add(responses.POST,
                          endpoint,
                          body=body,
                          content_type="application/json")

            # call our action with no arguments
            assert_raises(toolkit.ValidationError, helpers.call_action, action)
            print '\t[OK]'
コード例 #3
0
ファイル: test_api.py プロジェクト: Cadasta/ckanext-project
    def test_all_post_actions_success(self):
        for action, cadasta_endpoint in post_files_api_map.items():
            print "testing {action}".format(action=action),
            # read our expected json output as <action_name>.json
            filepath = os.path.join(self.data_dir, ".".join([action, "json"]))
            body = open(filepath).read()
            expected = json.loads(body)

            # add the expected parameters (everything is a 1)
            api_url = cadasta_endpoint.url
            url_args = dict([(a[1], 1) for a in string.Formatter().parse(api_url) if a[1]])

            # make sure the point parameters are filled out
            endpoint = urljoin(self.test_api, api_url).format(**url_args)

            # fake out our response
            responses.add(responses.POST, endpoint, body=body, content_type="application/json")

            if cadasta_endpoint.upload_fields:
                for upload_field in cadasta_endpoint.upload_fields:
                    url_args[upload_field] = {"file": StringIO("test file"), "filename": "testfile.doc"}

            # call our action with the same arguments passed
            result = helpers.call_action(action, **url_args)

            # check a file upload was sent
            # assert_in(
            #     'Content-Disposition: form-data; name="filedata"; '
            #     'filename="filedata"\r\n\r\ntest file',
            #     responses.calls[0].request.body
            # )
            assert_equal(expected, result)

            print "\t[OK]"
コード例 #4
0
ファイル: test_api.py プロジェクト: okfn/ckanext-cadastaroles
    def test_all_post_actions_success(self):
        for action, cadasta_endpoint in post_files_api_map.items():
            print 'testing {action}'.format(action=action),
            # read our expected json output as <action_name>.json
            filepath = os.path.join(self.data_dir, '.'.join([action, 'json']))
            body = open(filepath).read()
            expected = json.loads(body)

            # add the expected parameters (everything is a 1)
            api_url = cadasta_endpoint.url
            url_args = dict([(a[1], 1)
                             for a in string.Formatter().parse(api_url)
                             if a[1]])

            # make sure the point parameters are filled out
            endpoint = urljoin(self.test_api, api_url).format(**url_args)

            # fake out our response
            responses.add(responses.POST,
                          endpoint,
                          body=body,
                          content_type="application/json")

            if cadasta_endpoint.upload_fields:
                for upload_field in cadasta_endpoint.upload_fields:
                    url_args[upload_field] = StringIO('test file')

            # call our action with the same arguments passed
            result = helpers.call_action(action, **url_args)

            # check a file upload was sent
            assert_in(
                'Content-Disposition: form-data; name="filedata"; '
                'filename="filedata"\r\n\r\ntest file',
                responses.calls[0].request.body)
            assert_equal(expected, result)

            print '\t[OK]'