Esempio n. 1
0
def main(directory=False):

    config = Configuration(os.environ)

    if config.content_root:
        if directory and directory != config.content_root:
            print("Warning: Overriding CONTENT_ROOT [{}] with argument [{}].".format(
                config.content_root, directory))
        else:
            os.chdir(config.content_root)
            # print(os.getcwd())
    elif directory:
        os.chdir(directory)
        # print(os.getcwd())

    if os.path.exists(DECONST_FILE):
        config.apply_file(DECONST_FILE)

    # Ensure that the envelope and asset directories exist.
    os.makedirs(config.envelope_dir, exist_ok=True)
    os.makedirs(config.asset_dir, exist_ok=True)

    # Install pip requirements when possible.
    install_requirements()

    the_list = find_all(config)
    for path_name in the_list:
        file_name = os.path.basename(path_name)
        # html_name = file_name.replace('json', 'html')
        # base_location = os.path.join(config.envelope_dir, html_name)
        
        each_envelope = enveloper(path_name, config.envelope_dir)
        submit(each_envelope)
 def _populate_asset_offsets(self, original_asset_dir=None):
     '''
     Read stored asset offsets from the asset mapper, and then update the body.
     '''
     if original_asset_dir:
         classy = Configuration(os.environ)
         self.body, self.asset_offsets = asset_mapper.map_the_assets(
             original_asset_dir,
             classy.asset_dir,
             html_doc_path=self.originalFile)
     else:
         classy = Configuration(os.environ)
         self.body, self.asset_offsets = asset_mapper.map_the_assets(
             classy.original_asset_dir,
             classy.asset_dir,
             html_doc_path=self.originalFile)
 def test_envelope_dir_pass(self):
     '''
     Does the environment's CONTENT_ID_BASE variable get passed correctly?
     '''
     self.config_class = Configuration(os.environ)
     self.assertEqual(self.config_class.content_id_base,
                      'fake-repo/fake-dir/')
Esempio n. 4
0
def init_builder(test=False):
    '''
    deconst config initialization
    '''
    # Search the tree with os.walk and an if statement
    path_name = ""
    deconst_config = Configuration(os.environ)
    for (dirpath, dirnames, filenames) in os.walk(deconst_config.git_root):
        for filename in filenames:
            if filename.endswith(DECONST_FILE):
                for dirname in dirnames:
                    actual_path = str(Path(dirname).parents[0])[:-2]
                    path_name = path.join(dirpath, actual_path, filename)
    if path_name != "":
        deconst_config.apply_file(path_name)
    else:
        raise FileNotFoundError(
            "There's no _deconst.json file in this repo at " +
            str(deconst_config.git_root) + ". Add one!")
        deconst_config = None
    return deconst_config
 def test_find_all_pass(self):
     '''
     Does the find_all method really find all the openapi it can?
     '''
     config_file = Configuration(os.environ)
     actual_list = find_all(config_file)
     expected_list = [
         os.path.join(os.getcwd(), 'tests', 'src', 'openapi.json'),
         os.path.join(os.getcwd(), 'tests', 'src', 'small_test',
                      'openapi.json')
     ]
     self.assertEqual(actual_list, expected_list)
 def test_submit_pass(self):
     '''
     Does the submit method pass the json properly?
     '''
     config = Configuration(os.environ)
     self.maxDiff = None
     the_envelope_passed = {
         'body':
         '\n<h1>Swagger Petstore</h1>\n<div class="app-desc">No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)</div>\n<div class="app-desc">More information: <a href="https://helloreverb.com">https://helloreverb.com</a></div>\n<div class="app-desc">Contact Info: <a href="*****@*****.**">[email protected]</a></div>\n<div class="app-desc">Version: 1.0.0</div>\n<div class="app-desc">BasePath:/v1</div>\n<div class="license-info">MIT</div>\n<div class="license-url">http://apache.org/licenses/LICENSE-2.0.html</div>\n<h2>Access</h2>\n<h2><a name="__Methods">Methods</a></h2>\n  [ Jump to <a href="#__Models">Models</a> ]\n\n  <h3>Table of Contents </h3>\n<div class="method-summary"></div>\n<h4><a href="#Pets">Pets</a></h4>\n<ul>\n<li><a href="#createPets"><code><span class="http-method">post</span> /pets</code></a></li>\n<li><a href="#listPets"><code><span class="http-method">get</span> /pets</code></a></li>\n<li><a href="#showPetById"><code><span class="http-method">get</span> /pets/{petId}</code></a></li>\n</ul>\n<h1><a name="Pets">Pets</a></h1>\n<div class="method"><a name="createPets"></a>\n<div class="method-path">\n<a class="up" href="#__Methods">Up</a>\n<pre class="post"><code class="huge"><span class="http-method">post</span> /pets</code></pre></div>\n<div class="method-summary">Create a pet (<span class="nickname">createPets</span>)</div>\n<div class="method-notes"></div>\n<!--Todo: process Response Object and its headers, schema, examples -->\n<h3 class="field-label">Produces</h3>\n    This API call produces the following media types according to the <span class="header">Accept</span> request header;\n    the media type will be conveyed by the <span class="header">Content-Type</span> response header.\n    <ul>\n<li><code>application/json</code></li>\n</ul>\n<h3 class="field-label">Responses</h3>\n<h4 class="field-label">201</h4>\n    Null response\n        <a href="#"></a>\n<h4 class="field-label">default</h4>\n    unexpected error\n        <a href="#Error">Error</a>\n</div> <!-- method -->\n<hr/>\n<div class="method"><a name="listPets"></a>\n<div class="method-path">\n<a class="up" href="#__Methods">Up</a>\n<pre class="get"><code class="huge"><span class="http-method">get</span> /pets</code></pre></div>\n<div class="method-summary">List all pets (<span class="nickname">listPets</span>)</div>\n<div class="method-notes"></div>\n<h3 class="field-label">Query parameters</h3>\n<div class="field-items">\n<div class="param">limit (optional)</div>\n<div class="param-desc"><span class="param-type">Query Parameter</span> — How many items to return at one time max 100 format: int32</div>\n</div> <!-- field-items -->\n<h3 class="field-label">Return type</h3>\n<div class="return-type">\n<a href="#Pets">Pets</a>\n</div>\n<!--Todo: process Response Object and its headers, schema, examples -->\n<h3 class="field-label">Example data</h3>\n<div class="example-data-content-type">Content-Type: application/json</div>\n<pre class="example"><code>null</code></pre>\n<h3 class="field-label">Produces</h3>\n    This API call produces the following media types according to the <span class="header">Accept</span> request header;\n    the media type will be conveyed by the <span class="header">Content-Type</span> response header.\n    <ul>\n<li><code>application/json</code></li>\n</ul>\n<h3 class="field-label">Responses</h3>\n<h4 class="field-label">200</h4>\n    An paged array of pets\n        <a href="#Pets">Pets</a>\n<h4 class="field-label">default</h4>\n    unexpected error\n        <a href="#Error">Error</a>\n</div> <!-- method -->\n<hr/>\n<div class="method"><a name="showPetById"></a>\n<div class="method-path">\n<a class="up" href="#__Methods">Up</a>\n<pre class="get"><code class="huge"><span class="http-method">get</span> /pets/{petId}</code></pre></div>\n<div class="method-summary">Info for a specific pet (<span class="nickname">showPetById</span>)</div>\n<div class="method-notes"></div>\n<h3 class="field-label">Path parameters</h3>\n<div class="field-items">\n<div class="param">petId (required)</div>\n<div class="param-desc"><span class="param-type">Path Parameter</span> — The id of the pet to retrieve </div>\n</div> <!-- field-items -->\n<h3 class="field-label">Return type</h3>\n<div class="return-type">\n<a href="#Pets">Pets</a>\n</div>\n<!--Todo: process Response Object and its headers, schema, examples -->\n<h3 class="field-label">Example data</h3>\n<div class="example-data-content-type">Content-Type: application/json</div>\n<pre class="example"><code>null</code></pre>\n<h3 class="field-label">Produces</h3>\n    This API call produces the following media types according to the <span class="header">Accept</span> request header;\n    the media type will be conveyed by the <span class="header">Content-Type</span> response header.\n    <ul>\n<li><code>application/json</code></li>\n</ul>\n<h3 class="field-label">Responses</h3>\n<h4 class="field-label">200</h4>\n    Expected response to a valid request\n        <a href="#Pets">Pets</a>\n<h4 class="field-label">default</h4>\n    unexpected error\n        <a href="#Error">Error</a>\n</div> <!-- method -->\n<hr/>\n<h2><a name="__Models">Models</a></h2>\n  [ Jump to <a href="#__Methods">Methods</a> ]\n\n  <h3>Table of Contents</h3>\n<ol>\n<li><a href="#Error"><code>Error</code> - </a></li>\n<li><a href="#Pet"><code>Pet</code> - </a></li>\n<li><a href="#Pets"><code>Pets</code> - </a></li>\n</ol>\n<div class="model">\n<h3><a name="Error"><code>Error</code> - </a> <a class="up" href="#__Models">Up</a></h3>\n<div class="model-description"></div>\n<div class="field-items">\n<div class="param">code </div><div class="param-desc"><span class="param-type"><a href="#integer">Integer</a></span>  format: int32</div>\n<div class="param">message </div><div class="param-desc"><span class="param-type"><a href="#string">String</a></span> </div>\n</div> <!-- field-items -->\n</div>\n<div class="model">\n<h3><a name="Pet"><code>Pet</code> - </a> <a class="up" href="#__Models">Up</a></h3>\n<div class="model-description"></div>\n<div class="field-items">\n<div class="param">id </div><div class="param-desc"><span class="param-type"><a href="#long">Long</a></span>  format: int64</div>\n<div class="param">name </div><div class="param-desc"><span class="param-type"><a href="#string">String</a></span> </div>\n<div class="param">tag (optional)</div><div class="param-desc"><span class="param-type"><a href="#string">String</a></span> </div>\n</div> <!-- field-items -->\n</div>\n<div class="model">\n<h3><a name="Pets"><code>Pets</code> - </a> <a class="up" href="#__Models">Up</a></h3>\n<div class="model-description"></div>\n<div class="field-items">\n</div> <!-- field-items -->\n</div>\n',
         'docname': "fake_content_id",
         'title': 'Swagger Petstore',
         'toc':
         '<ul><li><a href="#Access">Access</a></li><li><a href="#Methods">Methods</a></li><ul><li><a href="#TableofContents">Table of Contents </a></li><ul><li><a href="#Pets">Pets</a></li></ul></ul><ul><li><a href="#201">201</a></li><li><a href="#default">default</a></li></ul><ul><li><a href="#200">200</a></li><li><a href="#default">default</a></li></ul><ul><li><a href="#200">200</a></li><li><a href="#default">default</a></li></ul><li><a href="#Models">Models</a></li><ul><li><a href="#TableofContents">Table of Contents</a></li></ul><ul><li><a href="#Error----Up"></a></li></ul><ul><li><a href="#Pet----Up"></a></li></ul><ul><li><a href="#Pets----Up"></a></li></ul></ul>',
         'unsearchable': None,
         'content_id': "fake_content_id",
         'meta': {
             'someKey':
             'someValue',
             'preferGithubIssues':
             True,
             'github_issues_url':
             'https://github.com/deconst/fake-repo/issues',
             'github_edit_url':
             'https://github.com/deconst/fake-repo/edit/master/tests/dest/index.html'
         },
         'asset_offsets': {},
         'addenda': None,
         'per_page_meta': {}
     }
     dest_path = submit(the_envelope_passed)
     with open(dest_path, 'r') as written_file:
         actual_file = json.load(written_file)
     fake_json_envelope = os.path.join(os.getcwd(), 'tests', 'src',
                                       'input_file_writeout.json')
     with open(fake_json_envelope, 'r') as fake_envelope:
         expected_json_output = json.load(fake_envelope)
     self.assertEqual(actual_file, expected_json_output)
Esempio n. 7
0
to pass to the deconst submitter module.
'''

import sys
import os
import re
import shutil
import urllib.parse
from pathlib import Path

sys.path.insert(0, os.getcwd())

import openapipreparer.builders.envelope_writer as envelope_writer
from openapipreparer.config import Configuration

config = Configuration(os.environ)


def enveloper(the_json, the_location):
    '''
    Use the openapi-generator-cli-3.1.1.jar file to generate HTML, and then wrap the
    submission in the deconst envelope schema.
    '''
    the_html = envelope_writer.make_it_html(the_json, the_location)
    the_envelope = envelope_writer.parsing_html(the_html)
    return the_envelope


def submit(the_envelope):
    '''
    Pass the envelopes to the submitter.
 def setUp(self):
     '''
     Instantiate the class.
     '''
     self.config_class = Configuration(os.environ)
     return self.config_class
class ConfigurationTestCase(unittest.TestCase):
    '''
    Tests for class methods in the Configuration class
    '''
    def setUp(self):
        '''
        Instantiate the class.
        '''
        self.config_class = Configuration(os.environ)
        return self.config_class

    def tearDown(self):
        self.config_class = None

    def test_apply_file_pass(self):
        '''
        Does the apply_file method correctly parse the deconst json file?
        '''
        deconstjson = path.join(os.getcwd(), 'tests', 'src', '_deconst.json')
        self.config_class.apply_file(deconstjson)
        test_deconst_result = {}
        test_deconst_result[
            'contentIDBase'] = self.config_class.content_id_base
        test_deconst_result['meta'] = self.config_class.meta
        test_deconst_result['githubUrl'] = self.config_class.github_url
        expected_deconst_result = {}
        expected_deconst_result[
            'contentIDBase'] = 'https://github.com/deconst/fake-repo/'
        expected_deconst_result['meta'] = {
            'github_issues_url': 'https://github.com/deconst/fake-repo/issues',
            "someKey": "someValue",
            "preferGithubIssues": True
        }
        expected_deconst_result[
            'githubUrl'] = 'https://github.com/deconst/fake-repo/'
        self.assertEqual(expected_deconst_result, test_deconst_result)

    def test_apply_minimal_file_pass(self):
        '''
        Does the apply_file method correctly parse the deconst json file?
        '''
        deconstjson = path.join(os.getcwd(), 'tests', 'src',
                                '_deconst_minimal.json')
        self.config_class.content_id_base = '/random/path/'
        self.config_class.apply_file(deconstjson)
        test_deconst_result = {}
        test_deconst_result[
            'contentIDBase'] = self.config_class.content_id_base
        expected_deconst_result = {}
        expected_deconst_result['contentIDBase'] = '/random/path/'
        self.assertEqual(expected_deconst_result, test_deconst_result)

    def test_apply_minimal_file_check_error_message_pass(self):
        '''
        Does the apply_file method correctly parse the deconst json file?
        '''
        expected_message = ("Using environment variable CONTENT_ID_BASE=" +
                            "[/random/path/] instead of _deconst.json " +
                            "setting [https://github.com/deconst/fake-repo/]" +
                            ".\n")
        deconstjson = path.join(os.getcwd(), 'tests', 'src',
                                '_deconst_errorcheck.json')
        self.config_class.content_id_base = '/random/path/'
        with mock.patch('sys.stdout', new_callable=io.StringIO) as object_out:
            self.config_class.apply_file(deconstjson)
            self.assertEqual(expected_message, object_out.getvalue())

    def test__get_git_root_pass(self):
        '''
        Question?
        '''
        actual_git_root = self.config_class._get_git_root()
        expected_git_root = os.getcwd()
        self.assertEqual(actual_git_root, expected_git_root)

    def test_missing_values_pass(self):
        '''
        Does a missing content_id_base raise the correct reason for failure?
        '''
        self.maxDiff = None
        self.config_class.content_id_base = None
        actual_output = self.config_class.missing_values()
        self.assertEqual(actual_output, [
            'CONTENT_ID_BASE is missing. It should be the base URL used to generate IDs for content within this repository.'
        ])
 def test_envelope_dir_pass(self):
     '''
     Does the environment's ASSET_DIR variable get passed correctly?
     '''
     self.config_class = Configuration(os.environ)
     self.assertEqual(self.config_class.asset_dir, 'fake-repo/fake-dir/')
 def test_content_root_pass(self):
     '''
     Does the environment's CONTENT_ROOT variable get passed correctly?
     '''
     self.config_class = Configuration(os.environ)
     self.assertEqual(self.config_class.content_root, 'fake-repo/fake-dir/')