コード例 #1
0
ファイル: test_requests.py プロジェクト: mcepl/py-autodoc
    def setUp(self):
        self.root_path = root_path
        if os.path.exists(var_path):
            shutil.rmtree(var_path)
            os.mkdir(var_path)
            with open('{0}/.gitkeep'.format(var_path), 'w') as f:
                f.write('')

        self.autodoc = Autodoc()
コード例 #2
0
    def setUp(self):
        self.root_path = root_path
        if os.path.exists(var_path):
            shutil.rmtree(var_path)
            os.mkdir(var_path)
            with open('{0}/.gitkeep'.format(var_path), 'w') as f:
                f.write('')

        self.autodoc = Autodoc()
コード例 #3
0
from flask import Blueprint
from autodoc import Autodoc

from const import API_PREFIX

document = Blueprint('document', __name__)
autodoc = Autodoc()


@document.route(API_PREFIX + '/documents')
def document_get():
    return autodoc.html(groups=['public'], title="LiveCloud API Documentation")


@document.route(API_PREFIX + '/documents/networks')
def network_document_get():
    return autodoc.html(groups=['network_app'],
                        title="LiveCloud API Documentation")


@document.route(API_PREFIX + '/documents/subnets')
def subnet_document_get():
    return autodoc.html(groups=['subnet_app'],
                        title="LiveCloud API Documentation")


@document.route(API_PREFIX + '/documents/ports')
def port_document_get():
    return autodoc.html(groups=['port_app'],
                        title="LiveCloud API Documentation")
コード例 #4
0
class TestRequestsResponse(TestCase):
    def create_request(self, url, method, data=None, headers=None):
        request = Request(method, url, data=data, headers=headers)

        return request

    @patch('requests.sessions.Session.send')
    def send(self, request, params, file_path, m):
        dummy_response(m, request, params, file_path)

        session = Session()
        res = session.send(request.prepare())

        return res

    def setUp(self):
        self.root_path = root_path
        if os.path.exists(var_path):
            shutil.rmtree(var_path)
            os.mkdir(var_path)
            with open('{0}/.gitkeep'.format(var_path), 'w') as f:
                f.write('')

        self.autodoc = Autodoc()

    def test_parse_response(self):
        """ Should parse requests response. """
        params = {'message': 'foo'}
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='POST',
                                  data=params,
                                  headers=headers)

        res = self.send(req, params, 'data/post.json')
        self.autodoc.parse('POST /', res)

        var = {
            'describe': 'POST /',
            'describe_separators': '======',
            'target_url': 'http://localhost:5000/',
            'status_code': 200,
            'request': 'POST /',
            'response_body': '{\n  "response": "create"\n}',
            'response_content_type': 'application/json',
            'params': '{\n  "message": "foo"\n}'
        }
        for k, v in iteritems(self.autodoc.vars[0]):
            self.assertEqual(v, var[k])

        self.autodoc.clear()

    def test_parse_responses(self):
        """ Should stack responses. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)

        res = self.send(req, '', 'data/get.json')
        self.autodoc.parse('GET /', res)
        self.autodoc.parse('GET /', res)
        var = {
            'response_content_type': 'application/json',
            'response_body': '{\n  "response": "index"\n}',
            'describe': 'GET /',
            'request': 'GET /',
            'params': '',
            'status_code': 200,
            'target_url': 'http://localhost:5000/',
            'describe_separators': '====='
        }
        vars = [var, var]
        self.assertEqual(self.autodoc.vars, vars)
        self.autodoc.clear()

    def test_clear_responses(self):
        """ Should clear stacked WebTest responses. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)

        res = self.send(req, '', 'data/get.json')

        self.autodoc.parse('GET /', res)
        self.autodoc.parse('GET /', res)
        self.autodoc.clear()
        self.assertEqual(self.autodoc.vars, [])

    def test_create_document(self):
        """ Should create reST document. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)

        res = self.send(req, '', 'data/get.json')

        self.autodoc.parse('GET /', res)
        self.autodoc.create_document(os.path.join(self.root_path,
                                                  'var/test_autodoc.rst'))
        self.assertTrue(os.path.exists(os.path.join(self.root_path,
                                                    'var/test_autodoc.rst')))
        self.autodoc.clear()

    def test_create_markdown_document(self):
        """ Should create markdown document. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)
        res = self.send(req, '', 'data/get.json')

        self.autodoc.parse('GET /', res)
        self.autodoc.template_path = os.path.join(self.root_path,
                                                  'templates/markdown.md')
        output = os.path.join(self.root_path, 'var/test_autodoc.md')
        self.autodoc.create_document(output)
        ret = os.path.exists(output)
        self.assertTrue(ret)
        self.autodoc.clear()

    def test_should_change_separators(self):
        """ Should change separators. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)
        res = self.send(req, '', 'data/get.json')

        self.autodoc.separators = '*'
        self.autodoc.parse('GET /', res)
        var = {
            'response_content_type': 'application/json',
            'response_body': '{\n  "response": "index"\n}',
            'describe': 'GET /',
            'request': 'GET /',
            'params': '',
            'status_code': 200,
            'target_url': 'http://localhost:5000/',
            'describe_separators': '*****'
        }
        for k, v in iteritems(self.autodoc.vars[0]):
            self.assertEqual(v, var[k])

        self.autodoc.clear()
コード例 #5
0
import sys
from io import BytesIO

from flask import Flask, render_template, request, redirect, flash, Response
from flask_sqlalchemy import SQLAlchemy
from geoip2.errors import AddressNotFoundError
from sqlalchemy import and_

from config import load_config, DefaultFlaskConfig
from crawler import init_geoip, connect
from models import *
import pandas as pd
from autodoc import Autodoc

app = Flask(__name__)
auto = Autodoc(app)
app.config.from_object(DefaultFlaskConfig())
app.config.from_object('flask_config')
db = SQLAlchemy(app)

CONF = load_config()
COUNTRY, CITY, ASN = init_geoip()


@app.route('/')
@app.route('/networks/<network_name>', methods=['GET'])
def network_dashboard(network_name=None):
    if not network_name in ("okcash", "testnet", None):
        flash("Invalid network")
        return redirect("/")
コード例 #6
0
ファイル: test_requests.py プロジェクト: mcepl/py-autodoc
class TestRequestsResponse(TestCase):
    def create_request(self, url, method, data=None, headers=None):
        request = Request(method, url, data=data, headers=headers)

        return request

    @patch('requests.sessions.Session.send')
    def send(self, request, params, file_path, m):
        dummy_response(m, request, params, file_path)

        session = Session()
        res = session.send(request.prepare())

        return res

    def setUp(self):
        self.root_path = root_path
        if os.path.exists(var_path):
            shutil.rmtree(var_path)
            os.mkdir(var_path)
            with open('{0}/.gitkeep'.format(var_path), 'w') as f:
                f.write('')

        self.autodoc = Autodoc()

    def test_parse_response(self):
        """ Should parse requests response. """
        params = {'message': 'foo'}
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='POST',
                                  data=params,
                                  headers=headers)

        res = self.send(req, params, 'data/post.json')
        self.autodoc.parse('POST /', res)

        var = {
            'describe': 'POST /',
            'describe_separators': '======',
            'target_url': 'http://localhost:5000/',
            'status_code': 200,
            'request': 'POST /',
            'response_body': '{\n  "response": "create"\n}',
            'response_content_type': 'application/json',
            'params': '{\n  "message": "foo"\n}'
        }
        for k, v in iteritems(self.autodoc.vars[0]):
            self.assertEqual(v, var[k])

        self.autodoc.clear()

    def test_parse_responses(self):
        """ Should stack responses. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)

        res = self.send(req, '', 'data/get.json')
        self.autodoc.parse('GET /', res)
        self.autodoc.parse('GET /', res)
        var = {
            'response_content_type': 'application/json',
            'response_body': '{\n  "response": "index"\n}',
            'describe': 'GET /',
            'request': 'GET /',
            'params': '',
            'status_code': 200,
            'target_url': 'http://localhost:5000/',
            'describe_separators': '====='
        }
        vars = [var, var]
        self.assertEqual(self.autodoc.vars, vars)
        self.autodoc.clear()

    def test_clear_responses(self):
        """ Should clear stacked WebTest responses. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)

        res = self.send(req, '', 'data/get.json')

        self.autodoc.parse('GET /', res)
        self.autodoc.parse('GET /', res)
        self.autodoc.clear()
        self.assertEqual(self.autodoc.vars, [])

    def test_create_document(self):
        """ Should create reST document. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)

        res = self.send(req, '', 'data/get.json')

        self.autodoc.parse('GET /', res)
        self.autodoc.create_document(
            os.path.join(self.root_path, 'var/test_autodoc.rst'))
        self.assertTrue(
            os.path.exists(os.path.join(self.root_path,
                                        'var/test_autodoc.rst')))
        self.autodoc.clear()

    def test_create_markdown_document(self):
        """ Should create markdown document. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)
        res = self.send(req, '', 'data/get.json')

        self.autodoc.parse('GET /', res)
        self.autodoc.template_path = os.path.join(self.root_path,
                                                  'templates/markdown.md')
        output = os.path.join(self.root_path, 'var/test_autodoc.md')
        self.autodoc.create_document(output)
        ret = os.path.exists(output)
        self.assertTrue(ret)
        self.autodoc.clear()

    def test_should_change_separators(self):
        """ Should change separators. """
        headers = {'content-type': 'application/json'}
        req = self.create_request(url='http://localhost:5000/',
                                  method='GET',
                                  headers=headers)
        res = self.send(req, '', 'data/get.json')

        self.autodoc.separators = '*'
        self.autodoc.parse('GET /', res)
        var = {
            'response_content_type': 'application/json',
            'response_body': '{\n  "response": "index"\n}',
            'describe': 'GET /',
            'request': 'GET /',
            'params': '',
            'status_code': 200,
            'target_url': 'http://localhost:5000/',
            'describe_separators': '*****'
        }
        for k, v in iteritems(self.autodoc.vars[0]):
            self.assertEqual(v, var[k])

        self.autodoc.clear()