Example #1
0
    def test_missing_static_dir(self):
        fake_dir = self.TEST_DIR / 'this' / 'directory' / 'does' / 'not' / 'exist'

        with self.assertRaises(SystemExit) as context:
            make_app(fake_dir)

        assert context.exception.code == -1  # pylint: disable=no-member
Example #2
0
def select_app(input_str):
    if input_str == "myapp":
        return make_app()
    elif input_str == "altdemo":
        return make_altdemo()
    elif input_str == "image":
        return make_imageapp()
    elif input_str == "quotes":
        return make_quotesapp()
    elif input_str == "chat":
        return make_chatapp()
    else:
        # assume my app by default
        return make_app() 
Example #3
0
def main():
    s = socket.socket() # Create a socket object.
    host = socket.getfqdn() # Get local machine name.

    parser = argparse.ArgumentParser() #creating a parser 
    parser.add_argument("-A", choices=['image', 'altdemo', 'myapp', 'quotes', 'chat', 'cookie'],
            help='Choose which app you would like to run')
    parser.add_argument("-p", type=int, help="Choose the port you would like to run on.")
    args = parser.parse_args()

    #Check to see if a port is specified
    if args.p == None:
        port = random.randint(8000, 9999) #Creating WSGI app
        s.bind((host, port))
    else:
        port = args.p
        s.bind((host, port))
    if args.A == 'myapp':
        wsgi_app = make_app()
    elif args.A == "image":
        imageapp.setup()
        p = imageapp.create_publisher()
        wsgi_app = quixote.get_wsgi_app()
    elif args.A == "altdemo":
        p = create_publisher()
        wsgi_app = quixote.get_wsgi_app()
    elif args.A == "quotes":
        directory_path = './quotes/'
        wsgi_app = quotes.create_quotes_app(directory_path + 'quotes.txt', directory_path + 'html')
    elif args.A == "chat":
        wsgi_app = chat.create_chat_app('./chat/html')
    elif args.A == "cookie":
        wsgi_app = make_cookie_app()
    else:
        wsgi_app = make_app() #In the event that no argument is passed just make my_app

    print 'Starting server on', host, port
    print 'The Web server URL for this would be http://%s:%d/' % (host, port)

    s.listen(5) #wait for client connection

    print 'Entering infinite loop; hit CTRL+C to exit'
    while True:
        #Establish a connection with the client
        c, (client_host, client_port) = s.accept()
        print 'Got connection from', client_host, client_port
        handle_connection(c, wsgi_app)
        #handle connection(c, validate_app)
    return
Example #4
0
def test_make_app():
    app = make_app({
        'DEBUG': 'YES',
        'REDIS_URL': 'NO_URL'
    })

    assert app.config['CELERY_ALWAYS_EAGER']
Example #5
0
def test_it():
    app = make_app()
    client = test.TestClient(app)
    response = client.get("/")

    assert response.status_code == 200
    assert response.json() == {"message": "Welcome to API Star!"}
Example #6
0
    def setUp(self):
        """Defines the initialization variables for the class
        """
        self.app = make_app(config_name="testing")
        self.client = self.app.test_client
        self.categories = {'category_name': 'New_Category'}

        # binds the app to the current context
        with self.app.app_context():
            # create all tables
            db.create_all()

        #register a user
        user_details = json.dumps(dict({
            "email": "*****@*****.**",
            "password": "******",
            "username": "******",
            "secret_word": "TOP SECRET"
        }))
        self.client().post(base_url + 'auth/register', data=user_details,
                           content_type="application/json")

        # Login  a test user
        login_details = json.dumps(dict({
            "email": "*****@*****.**",
            "password": "******"
        }))

        self.login_data = self.client().post(base_url + 'auth/login', data=user_details,
                                             content_type="application/json")

        # login a user and obtain the token
        self.access_token = json.loads(
            self.login_data.data.decode())['access_token']
def test_handle_connection_GET_404():
    conn = FakeConnection("GET /404 HTTP/1.0\r\n\r\n")
    expected_return = 'HTTP/1.0 404 Not Found'
    
    server.handle_connection(conn, 80, app.make_app())

    assert conn.sent.startswith(expected_return), 'Got: %s' % (repr(conn.sent),)
Example #8
0
def test_submit_post_multipart():
    conn = FakeConnection("POST /submit HTTP/1.0\r\n" + \
                          "Content-Length: 374\r\n" + \
                          "Content-Type: multipart/form-data; " + \
                          "boundary=32452685f36942178a5f36fd94e34b63\r\n\r\n" + \
                          "--32452685f36942178a5f36fd94e34b63\r\n" + \
                          "Content-Disposition: form-data; name=\"lastname\";" + \
                          " filename=\"lastname\"\r\n\r\n" + \
                          "taylor\r\n" + \
                          "--32452685f36942178a5f36fd94e34b63\r\n" + \
                          "Content-Disposition: form-data; name=\"firstname\";" + \
                          " filename=\"firstname\"\r\n\r\n" + \
                          "ben\r\n" + \
                          "--32452685f36942178a5f36fd94e34b63\r\n" + \
                          "Content-Disposition: form-data; name=\"key\";" + \
                          " filename=\"key\"\r\n\r\n" + \
                          "value\r\n" + \
                          "--32452685f36942178a5f36fd94e34b63--\r\n"
                    )
    fname = 'ben'
    lname = 'taylor'
    er = 'HTTP/1.0 200 OK\r\n'

    app = make_app()
    server.handle_connection(conn, 80, app)

    assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
Example #9
0
    def test_permalinks_work(self):
        db = InMemoryDemoDatabase()
        application = make_app(build_dir=self.TEST_DIR, demo_db=db)
        predictor = CountingPredictor()
        application.predictors = {"counting": predictor}
        application.max_request_lengths["counting"] = 100
        application.testing = True
        client = application.test_client()

        def post(endpoint: str, data: JsonDict) -> Response:
            return client.post(endpoint,
                               content_type="application/json",
                               data=json.dumps(data))

        data = {"some": "input"}
        response = post("/predict/counting", data=data)

        assert response.status_code == 200
        result = json.loads(response.get_data())
        slug = result.get("slug")
        assert slug is not None

        response = post("/permadata", data={"slug": "not the right slug"})
        assert response.status_code == 400

        response = post("/permadata", data={"slug": slug})
        assert response.status_code == 200
        result2 = json.loads(response.get_data())
        assert set(
            result2.keys()) == {"modelName", "requestData", "responseData"}
        assert result2["modelName"] == "counting"
        assert result2["requestData"] == data
        assert result2["responseData"] == result
Example #10
0
def test_handle_not_found():
    conn = FakeConnection("GET /fake HTTP/1.0\r\n\r\n")

    app = make_app()
    server.handle_connection(conn, app)
    assert 'HTTP/1.0 404' in conn.sent , \
    'Got: %s' % (repr(conn.sent),)
Example #11
0
    def test_microservice(self):
        models = {
            'reading-comprehension': DemoModel(TEST_ARCHIVE_FILES['reading-comprehension'],
                                               'machine-comprehension',
                                               LIMITS['reading-comprehension'])
        }

        app = make_app(build_dir=self.TEST_DIR, models=models)
        app.testing = True


        client = app.test_client()

        # Should have only one model
        response = client.get("/models")
        data = json.loads(response.get_data())
        assert data["models"] == ["reading-comprehension"]

        # Should return results for that model
        response = client.post("/predict/reading-comprehension",
                               content_type="application/json",
                               data="""{"passage": "the super bowl was played in seattle",
                                        "question": "where was the super bowl played?"}""")
        assert response.status_code == 200
        results = json.loads(response.data)
        assert "best_span" in results

        # Other models should be unknown
        response = client.post("/predict/textual-entailment",
                               content_type="application/json",
                               data="""{"premise": "the super bowl was played in seattle",
                                        "hypothesis": "the super bowl was played in ohio"}""")
        assert response.status_code == 400
        data = response.get_data()
        assert b"unknown model" in data and b"textual-entailment" in data
Example #12
0
    def test_disable_caching(self):
        predictor = CountingPredictor()
        application = make_app(build_dir=self.TEST_DIR,
                               models={},
                               cache_size=0)
        application.predictors = {"counting": predictor}
        application.max_request_lengths["counting"] = 100
        application.testing = True
        client = application.test_client()

        data = {"input1": "this is input 1", "input2": 10}
        key = json.dumps(data)

        assert not predictor.calls

        for i in range(5):
            response = client.post("/predict/counting",
                                   content_type="application/json",
                                   data=json.dumps(data))
            assert response.status_code == 200
            assert json.loads(response.get_data()) == data

            # cache is disabled, so call count should keep incrementing
            assert predictor.calls[key] == i + 1
            assert len(predictor.calls) == 1
Example #13
0
    def setUp(self):
        self.app = make_app(config_name="testing")
        self.client = self.app.test_client
        self.user_details = {
            'email': '*****@*****.**',
            'password': '******',
            'username': '******',
            'secret_word': 'TOP SECRET'
        }
        self.unathorized_user_details = {
            'email': '*****@*****.**',
            'password': '******',
            'username': '******',
            'secret_word': 'TOP SECRET'
        }
        self.password_reset_user_details = {
            'email': '*****@*****.**',
            'password': '******',
            'secret_word': 'TOP SECRET'
        }

        with self.app.app_context():
            db.session.close()
            db.drop_all()
            db.create_all()
Example #14
0
    def test_db_resilient_to_prediction_failure(self):
        db = InMemoryDemoDatabase()
        application = make_app(build_dir=self.TEST_DIR, demo_db=db)
        predictor = FailingPredictor()
        application.predictors = {"failing": predictor}
        application.max_request_lengths["failing"] = 100
        # Keep error handling as it would be in the actual application.
        application.testing = False
        client = application.test_client()

        def post(endpoint: str, data: JsonDict) -> Response:
            return client.post(endpoint,
                               content_type="application/json",
                               data=json.dumps(data))

        data = {"some": "very nasty input that will cause a failure"}
        response = post("/predict/failing", data=data)
        assert response.status_code == 500

        # This won't be returned when the server errors out, but the necessary information is still
        # in the database for subsequent analysis.
        slug = app.int_to_slug(0)

        response = post("/permadata", data={"slug": slug})
        assert response.status_code == 200
        result = json.loads(response.get_data())
        assert set(
            result.keys()) == {"modelName", "requestData", "responseData"}
        assert result["modelName"] == "failing"
        assert result["requestData"] == data
        assert result["responseData"] == {}
Example #15
0
 def setUp(self) -> None:
     self.app = make_app(TestConfig)
     self.app_context = self.app.app_context()
     self.app_context.push()
     User.is_chat_between.cache_clear()
     User.get_chat_id_by_users_ids.cache_clear()
     db.create_all()
Example #16
0
def main():
    tornado_app = app.make_app()

    server = tornado.httpserver.HTTPServer(tornado_app)
    server.listen(options.port, options.host)

    tornado.ioloop.IOLoop.instance().start()
Example #17
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-p", "--port", type=int, default=0)
    parser.add_argument("-A", "--app", default="simple")

    args = parser.parse_args()
    port = args.port
    appname = args.app

    ###

    if appname == "simple":
        from app import make_app

        the_wsgi_app = make_app()
    elif appname == "imageapp":
        import imageapp, quixote

        imageapp.setup()

        p = imageapp.create_publisher()
        the_wsgi_app = quixote.get_wsgi_app()
    elif appname == "cookie":
        import cookieapp

        the_wsgi_app = cookieapp.wsgi_app

    host = socket.getfqdn()  # Get local machine name
    if port == 0:
        port = random.randint(8000, 9999)
    httpd = make_server("", port, the_wsgi_app)
    print "Serving at http://%s:%d/..." % (host, port)
    httpd.serve_forever()
Example #18
0
def handle_connection(conn, port):
    request = conn.recv(1)
    
    if not request:
        print 'Error, remote client closed connection without sending anything'
        return

    count = 0
    env = {}
    while request[-4:] != '\r\n\r\n':
         request += conn.recv(1)

    request, data = request.split('\r\n',1)
    headers = {}
    for line in data.split('\r\n')[:-2]:
        k, v = line.split(': ', 1)
        headers[k.lower()] = v

    path = urlparse(request.split(' ', 3)[1])
    env['REQUEST_METHOD'] = 'GET'
    env['PATH_INFO'] = path[2]
    env['QUERY_STRING'] = path[4]
    env['CONTENT_TYPE'] = 'text/html'
    env['CONTENT_LENGTH'] = str(0)
    env['SCRIPT_NAME'] = ''
    env['SERVER_NAME'] = socket.getfqdn()
    env['SERVER_PORT'] = str(port)
    env['wsgi.version'] = (1, 0)
    env['wsgi.errors'] = stderr
    env['wsgi.multithread'] = False
    env['wsgi.multiprocess'] = False
    env['wsgi.run_once'] = False
    env['wsgi.url_scheme'] = 'http'
    env['HTTP_COOKIE'] = headers['cookie'] if 'cookie' in headers.keys() else ''

    body = ''
    if request.startswith('POST '):
        env['REQUEST_METHOD'] = 'POST'
        env['CONTENT_LENGTH'] = headers['content-length']
        env['CONTENT_TYPE'] = headers['content-type']
        while len(body) < int(headers['content-length']):
            body += conn.recv(1)

    def start_response(status, response_headers):
        conn.send('HTTP/1.0 ')
        conn.send(status)
        conn.send('\r\n')
        for pair in response_headers:
            key, header = pair
            conn.send(key + ': ' + header + '\r\n')
        conn.send('\r\n')

    env['wsgi.input'] = StringIO(body)
    my_app = make_app()
    validator_app = validator(my_app)
    result = my_app(env, start_response)
    for data in result:
        conn.send(data)
    conn.close()
Example #19
0
def test_404():
    conn = FakeConnection("GET /asdf HTTP/1.0\r\n\r\n")
    er = 'HTTP/1.0 404 Not Found\r\n'
    
    app = make_app()
    server.handle_connection(conn, 80, app)

    assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
Example #20
0
def test_handle_connection_file():
    conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")

    app = make_app()
    server.handle_connection(conn, app)

    assert 'HTTP/1.0 200' in conn.sent and 'text/plain' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
Example #21
0
def test_make_app_async():
    app = make_app({
        'DEBUG': 'YES',
        'ASYNC_TRANSLATION': 'YES',
        'REDIS_URL': 'NO_URL'
    })

    assert not app.config['CELERY_ALWAYS_EAGER']
Example #22
0
def test_handle_connection_post():
    conn = FakeConnection("POST / HTTP/1.0\r\n" + \
      "Content-length: 0\r\n\r\n")

    app = make_app()
    server.handle_connection(conn, app)
    assert 'HTTP/1.0 200' in conn.sent and 'form' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
Example #23
0
def test_make_app_async():
    app = make_app({
        'DEBUG': 'YES',
        'ASYNC_TRANSLATION': 'YES',
        'REDIS_URL': 'NO_URL'
    })

    assert not app.config['CELERY_ALWAYS_EAGER']
Example #24
0
def test_handle_connection_content():
    conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")

    app = make_app()
    server.handle_connection(conn, app)

    assert 'HTTP/1.0 200' in conn.sent and 'content' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
Example #25
0
def test_get_form():
    conn = FakeConnection("GET /form HTTP/1.0\r\n\r\n")
    er = 'HTTP/1.0 200 OK\r\n'

    app = make_app()
    server.handle_connection(conn, 80, app)

    assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
Example #26
0
def test_handle_connection_image():
    conn = FakeConnection("GET /image HTTP/1.0\r\n\r\n")

    app = make_app()
    server.handle_connection(conn, app)
    print conn.sent
    assert 'HTTP/1.0 200' in conn.sent and 'image/jpeg' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
Example #27
0
 def test_instance_config(self):
     with open(os.path.join(self.app.instance_path, 'production_config.py'),
               'w') as file:
         file.write("TEST_VAR = 666\nSECRET_KEY = 'blablabla'")
     temp_app = make_app()
     self.assertEqual(temp_app.config['TEST_VAR'], 666)
     self.assertEqual(temp_app.config['SECRET_KEY'], 'blablabla')
     os.remove(os.path.join(self.app.instance_path, 'production_config.py'))
def test_handle_connection_form():
    conn = FakeConnection("GET /form HTTP/1.0\r\n\r\n")
    expected_return = 'HTTP/1.0 200 OK\r\n' + \
                      'Content-type: text/html\r\n\r\n'

    server.handle_connection(conn, 80, app.make_app())

    assert conn.sent.startswith(expected_return), 'Got: %s' % (repr(conn.sent),)
Example #29
0
def test_handle_empty_request():
  conn = FakeConnection("\r\n\r\n")

  app = make_app()
  server.handle_connection(conn, app)

  assert 'HTTP/1.0 404' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
Example #30
0
def test_handle_connection_image():
    conn = FakeConnection("GET /image HTTP/1.0\r\n\r\n")
    app = make_app()
    expected_return = 'HTTP/1.0 200 OK\r\n' + \
                      'Content-type: image/jpeg\r\n\r\n'

    server.handle_connection(conn, app)

    assert conn.sent.startswith(expected_return), 'Got: %s' % (repr(conn.sent),)
Example #31
0
def test_handle_submit_no_last_name():
    conn = FakeConnection("GET /submit?firstname=George&lastname=" + \
                          " HTTP/1.1\r\n\r\n")

    app = make_app()
    server.handle_connection(conn, app)

    assert 'html' in conn.sent and "George" in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
    def setUp(self):
        self.app = app.make_app()
        self.client = self.app.test_client()

        self.app.config.update(validation_token=None,)

        with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_data", "webhook.json")) as f:
            self.webhook_payload = f.read()
            self.alt_paylod = f.read()
Example #33
0
    def get_app(self):
        conn = psycopg2.connect(host="localhost", user="******")
        cursor = conn.cursor()
        cursor.execute("DROP SCHEMA IF EXISTS gandalf CASCADE")
        conn.commit()

        app = make_app(GandalfConfiguration('localhost:8889', PostgresAdapter(), 'localhost', mode=WEBSOCKET))
        app.listen(8888)
        return app
Example #34
0
    def __init__(self,
                 addr,
                 confdir=CONFDIR,
                 ssl=False,
                 ssloptions=None,
                 craftanchor="/p/",
                 staticdir=None,
                 anchors=None,
                 sizelimit=None,
                 noweb=False,
                 nocraft=False,
                 noapi=False,
                 nohang=False,
                 timeout=None,
                 logreq=False,
                 logresp=False,
                 explain=False,
                 hexdump=False):
        """
            addr: (address, port) tuple. If port is 0, a free port will be
            automatically chosen.
            ssloptions: an SSLOptions object.
            craftanchor: string specifying the path under which to anchor response generation.
            staticdir: path to a directory of static resources, or None.
            anchors: A list of (regex, spec) tuples, or None.
            sizelimit: Limit size of served data.
            nocraft: Disable response crafting.
            noapi: Disable the API.
            nohang: Disable pauses.
        """
        tcp.TCPServer.__init__(self, addr)
        self.ssl = ssl
        self.ssloptions = ssloptions or SSLOptions()
        self.staticdir = staticdir
        self.craftanchor = craftanchor
        self.sizelimit = sizelimit
        self.noweb, self.nocraft, self.noapi, self.nohang = noweb, nocraft, noapi, nohang
        self.timeout, self.logreq, self.logresp, self.hexdump = timeout, logreq, logresp, hexdump
        self.explain = explain

        self.app = app.make_app(noapi)
        self.app.config["pathod"] = self
        self.log = []
        self.logid = 0
        self.anchors = []
        if anchors:
            for i in anchors:
                try:
                    arex = re.compile(i[0])
                except re.error:
                    raise PathodError("Invalid regex in anchor: %s" % i[0])
                try:
                    language.parse_response(self.request_settings, i[1])
                except language.ParseException, v:
                    raise PathodError("Invalid page spec in anchor: '%s', %s" %
                                      (i[1], str(v)))
                self.anchors.append((arex, i[1]))
def test_handle_connection_POST_404():
    conn = FakeConnection("POST /404 HTTP/1.0\r\n" + \
                          "Content-Type: application/x-www-form-urlencoded \r\n"+\
                          "Content-Length: 0\r\n\r\n")
    expected_return = 'HTTP/1.0 404 Not Found\r\n'
    
    server.handle_connection(conn, 80, app.make_app())

    assert conn.sent.startswith(expected_return), 'Got: %s' % (repr(conn.sent),)
Example #36
0
def test_handle_not_found_post():
    conn = FakeConnection("POST /butts HTTP/1.1\r\n" + \
                          "Content-Length: 32\r\n\r\n" + \
                          "firstname=George&lastname=Strait")

    app = make_app()
    server.handle_connection(conn, app)
    assert 'HTTP/1.0 404' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
Example #37
0
    def setUp(self):
        super().setUp()

        if self.client is None:

            self.app = make_app(models={})
            self.app.predictors = PREDICTORS
            self.app.max_request_lengths = LIMITS
            self.app.testing = True
            self.client = self.app.test_client()
Example #38
0
def test_handle_connection_file():
    conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")
    app = make_app()
    expected_return = 'HTTP/1.0 200 OK\r\n' + \
                      'Content-type: text/plain\r\n\r\n' + \
                      'This is Jeff Johnson\'s text file!\n'

    server.handle_connection(conn, app)

    assert conn.sent == expected_return, 'Got: %s' % (repr(conn.sent),)
Example #39
0
def test_handle_submit_post():
    conn = FakeConnection("POST /submit HTTP/1.1\r\n" + \
                          "Content-Length: 31\r\n\r\n" + \
                          "firstname=George&lastname=Strait")

    app = make_app()
    server.handle_connection(conn, app)
    
    assert 'HTTP/1.0 200' in conn.sent and "Hello" in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
Example #40
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument( '-A', '--runApp', help = 'Application to run (image/altdemo/myapp)' )
    parser.add_argument( '-p', '--portNumb', help = 'Specified port number', type=int )
    
    args = parser.parse_args()

    # Handle port input (if there)
    if args.portNumb:
        port = args.portNumb
    else:
        port = random.randint(8000, 9999)

	# Determine what app to create
    if args.runApp == 'myapp':
	    wsgi_app = make_app()
    elif args.runApp == 'image':
        imageapp.setup()
        p        = imageapp.create_publisher()
        wsgi_app = quixote.get_wsgi_app()
    elif args.runApp == 'altdemo':
    	p        = create_publisher()
    	wsgi_app = quixote.get_wsgi_app()
    elif args.runApp == 'quotes':
    	import quotes
    	wsgi_app = quotes.setup()
    elif args.runApp == 'chat':
    	import chat
    	wsgi_app = chat.setup()
    elif args.runApp == 'cookie':
    	import cookieapp
        wsgi_app = cookieapp.wsgi_app
    else:
		print 'Invalid Application...'
		return
    	
    s = socket.socket()         # Create a socket object
    host = socket.getfqdn()     # Get local machine name
    s.bind((host, port))        # Bind to the port

    print 'Starting server on', host, port
    print 'The Web server URL for this would be http://%s:%d/' % (host, port)

    s.listen(5)                 # Now wait for client connection.

    print 'Entering infinite loop; hit CTRL-C to exit'
    while True:
        # Establish connection with client.
        c, (client_host, client_port) = s.accept()
        print 'Got connection from', client_host, client_port
        try:
            handle_connection(c, port, wsgi_app)
        finally:
            imageapp.teardown()
Example #41
0
    def setUp(self):
        """method to define varibles to be used in the tests
        """
        self.app = make_app(config_name="testing")
        self.client = self.app.test_client
        self.recipes = {
            'recipe_name': 'New_Recipes',
            'recipe_ingredients': 'milk',
            'recipe_methods': 'boil to heat'
        }

        self.other_recipes = {
            'recipe_name': 'Another_New_Recipes',
            'recipe_ingredients': 'Water, Milk',
            'recipe_methods': 'Heat till Warm'
        }

        self.categories = {'category_name': 'New_Category'}

        #binds app to the current context
        with self.app.app_context():
            #creates all the tables
            db.create_all()

        #register a user
        user_details = json.dumps(
            dict({
                "email": "*****@*****.**",
                "password": "******",
                "username": "******",
                "secret_word": "TOP SECRET"
            }))
        self.client().post(base_url + '/auth/register',
                           data=user_details,
                           content_type="application/json")

        # Login  a test user
        login_details = json.dumps(
            dict({
                "email": "*****@*****.**",
                "password": "******"
            }))

        self.login_data = self.client().post(base_url + '/auth/login',
                                             data=login_details,
                                             content_type="application/json")

        # login a user and obtain the token
        self.access_token = json.loads(
            self.login_data.data.decode())['access_token']

        # create a category
        self.client().post(base_url + '/categories/',
                           headers=dict(Authorization=self.access_token),
                           data=self.categories)
Example #42
0
def test_submit_get():
    fname = "Ben"
    lname = "Taylor"
    conn = FakeConnection("GET /submit?firstname={0}&lastname={1} \
                           HTTP/1.0\r\n\r\n".format(fname, lname))
    er = 'HTTP/1.0 200 OK\r\n'

    app = make_app()
    server.handle_connection(conn, 80, app)

    assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)  
Example #43
0
def test_handle_long_request():
    firstname = lastname = "asdfasdfasdfasdfasdf" * 100
    conn = FakeConnection("POST /submit HTTP/1.1\r\n" + \
                          "Content-Length: 4020\r\n\r\n" + \
                          "firstname=%s&lastname=%s" % (firstname, lastname))

    app = make_app()
    server.handle_connection(conn, app)
    
    assert 'HTTP/1.0 200' in conn.sent and "Hello" in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
Example #44
0
def main():
    import app

    server = tornado.httpserver.HTTPServer(app.make_app())
    server.listen(options.port)
    message = "Listening server at http://{0}:{1}"
    print(message.format(options.host, options.port))

    try:
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        print("\nStopping server.")
Example #45
0
def main():
    import app

    server = tornado.httpserver.HTTPServer(app.make_app())
    server.listen(options.port)
    message = "Listening server at http://{0}:{1}"
    print(message.format(options.host, options.port))

    try:
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        print("\nStopping server.")
Example #46
0
    def setUp(self):
        super().setUp()
        self.TEST_DIR = pathlib.Path(tempfile.mkdtemp())
        # Create index.html in TEST_DIR
        (self.TEST_DIR / 'index.html').touch()  # pylint: disable=no-member

        if self.client is None:

            self.app = make_app(build_dir=self.TEST_DIR)
            self.app.predictors = PREDICTORS
            self.app.testing = True
            self.client = self.app.test_client()
Example #47
0
def main():
    curr_app = make_app()
    with curr_app.app_context():
        try:
            db.drop_all()
        except:
            print("db does not exist")
        else:
            print("creating db")
            db.create_all()
        finally:
            print("initdb done")
Example #48
0
def test_handle_connection_submit_get():
    conn = FakeConnection("GET /submit-get?firstname=Jeff&lastname=Johnson HTTP/1.0\r\n\r\n")
    app = make_app()
    expected_return = 'HTTP/1.0 200 OK\r\n' + \
                      'Content-type: text/html\r\n\r\n' + \
                      '<html>\n    <body>\n        \n<title>Hello!</title>' + \
                      '\n<h1>Hello Mr. Jeff Johnson.</h1>\n\n    </body>\n' + \
                      '</html>'

    server.handle_connection(conn, app)

    assert conn.sent == expected_return, 'Got: %s' % (repr(conn.sent),)
Example #49
0
def test_handle_connection_content():
    conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")
    app = make_app()
    expected_return = 'HTTP/1.0 200 OK\r\n' + \
                      'Content-type: text/html\r\n\r\n' + \
                      '<html>\n    <body>\n        \n<title>Content</title' + \
                      '>\n<h1>This is john3209\'s content!</h1>\n\n    </b' + \
                      'ody>\n</html>'

    server.handle_connection(conn, app)

    assert conn.sent == expected_return, 'Got: %s' % (repr(conn.sent),)
Example #50
0
def client():
    app = make_app(TestConfig)

    db = Database(app)
    db.crete_tables()
    app.config['TESTING'] = True
    client = app.test_client()
    client.db = db
    client.app = app
    yield client

    db.delete_db()
Example #51
0
def refresh(clear_tables=True):
    logger.info('Refresh invoked')
    with app.make_app().app_context():
        rlog_id = RefreshLog.start()
        logger.info('refresh log id {} started'.format(rlog_id))
        try:
            aws.fetch_bucket(clear_tables=clear_tables)
            logger.debug('bucket fetched')
            utils.refresh_tables()
            logger.info('refresh complete')
            refresh_log.stop(rlog_id)
        except Exception as e:
            refresh_log.stop(rlog_id, err_msg=str(e))
Example #52
0
def parselogs():
    """This is a log parser"""
    current_app = make_app()
    log_gen = log_generator()
    temp_list = []
    for log_entry in log_gen:
        processed_entry = process_entry(log_entry)
        temp_list.append(processed_entry)
        if len(temp_list) > 10000:
            with current_app.app_context():
                insert_log_entries(temp_list)
            temp_list = []
    else:
        with current_app.app_context():
            insert_log_entries(temp_list)
Example #53
0
def app():
    db_fd, db_path = tempfile.mkstemp()

    app = make_app({
        'TESTING': True,
        'DATABASE': db_path,
    })

    with app.app_context():
        init_db()
        get_db().executescript(_data_sql)

    yield app

    os.close(db_fd)
    os.unlink(db_path)
Example #54
0
def test_app_context():
    a = app.make_app()
    a.config['TESTING'] = True
    a.app_context().push()

    g._database = get_redis_database_connection(db_number=0, redis_client=fakeredis.FakeRedis)

    pipe = g._database.pipeline()

    pipe.set(consts.KEY_CONFIGURATION_PRIME_COST, 300, nx=True)
    pipe.set(consts.KEY_API_VERSION, '1.2', nx=True)
    pipe.set(consts.KEY_API_VERSION_SUPPORTED, '1.1', nx=True)
    pipe.hset(consts.KEY_CONFIGURATION_ORDERS, consts.HASH_KEY_ORDER_TICK_DELAY, '60000')
    pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MTIME_START, 23)
    pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MTIME_LENGTH, 3600)
    pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MTIME_PREABMLE, 7200)
    pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MTIME_NEXT, 0)
    pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MTIME_SET, "false")
    pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_BUY_PRICE_MULTIPLIER, "0.20")
    pipe.hmset(consts.KEY_API_MAINTENANCE_WINDOW, {"Start": "1551049200", "Stop": "1551052800"})

    pipe.hset(consts.KEY_GLITTERBOT_DATA,
              consts.KEY_GLITTERBOT_IGNORE_THINGS, json.dumps(
                ['8697f432058b914ba2b20c5bd6f0678548126e21', 'cdf9187a28bcb1b219a3a4aeaf3c99a65e7eb882'])
              )
    pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_SELL_PRICE_MULTIPLIER, "0.75")
    pipe.hsetnx(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_MIN_SELL_PRICE_MULTIPLIER, "0.2")
    breakpoints = [
        {'PriceStart': 0, 'PriceStop': 5, 'StockMin': 1000, 'StockMax': 100000, 'CapBuyPrice': 2.5},
        {'PriceStart': 5, 'PriceStop': 25, 'StockMin': 200, 'StockMax': 250, 'CapBuyPrice': 2.75},
        {'PriceStart': 25, 'PriceStop': 50, 'StockMin': 100, 'StockMax': 150, 'CapBuyPrice': 3.0},
        {'PriceStart': 50, 'PriceStop': 100, 'StockMin': 10, 'StockMax': 15, 'CapBuyPrice': 3.5},
        {'PriceStart': 100, 'PriceStop': 100000, 'StockMin': 1, 'StockMax': 15, 'CapBuyPrice': 4.0},
        {'PriceStart': 100000, 'PriceStop': 200000, 'StockMin': 1, 'StockMax': 5, 'CapBuyPrice': 4.5}
    ]
    pipe.hset(consts.KEY_GLITTERBOT_DATA, consts.KEY_GLITTERBOT_PRICEBREAKS, json.dumps(breakpoints))

    thing = Thing("Silver")
    thing.save_to_database(pipe)

    pipe.execute()

    yield a
Example #55
0
    def test_permalinks_fail_gracefully_with_no_database(self):
        application = make_app(build_dir=self.TEST_DIR, models={})
        predictor = CountingPredictor()
        application.predictors = {"counting": predictor}
        application.max_request_lengths["counting"] = 100
        application.testing = True
        client = application.test_client()

        # Make a prediction, no permalinks.
        data = {"some": "input"}
        response = client.post("/predict/counting", content_type="application/json", data=json.dumps(data))

        assert response.status_code == 200

        # With permalinks not enabled, the result shouldn't contain a slug.
        result = json.loads(response.get_data())
        assert "slug" not in result

        # With permalinks not enabled, a post to the /permadata endpoint should be a 400.
        response = self.client.post("/permadata", data="""{"slug": "someslug"}""")
        assert response.status_code == 400
Example #56
0
def main(*args):
    parser = argparse.ArgumentParser()
    sub = parser.add_subparsers(dest='cmd')

    m2_parser = sub.add_parser('mongrel2')
    m2_parser.add_argument('--send')
    m2_parser.add_argument('--recv')

    gevent_parser = sub.add_parser('gevent')
    gevent_parser.add_argument('--host', default='0.0.0.0')
    gevent_parser.add_argument('-p', '--port', type=int, default=8080)

    opts = parser.parse_args(args)

    settings = {}
    app = make_app({}, **settings)

    if opts.cmd == 'gevent':
        serve_gevent(app, opts.host, opts.port)
    elif opts.cmd == 'mongrel2':
        serve_mongrel2(app, opts.send, opts.recv)
Example #57
0
 def create_app(self):
     app = make_app(config="test_config.py")
     db.init_app(app)
     Migrate(app, db)
     return app
Example #58
0
from app.config import HTTP

tornado_logging.access_log.setLevel(logging.DEBUG)
tornado_logging.app_log.setLevel(logging.DEBUG)
tornado_logging.gen_log.setLevel(logging.DEBUG)

if __name__ == "__main__":
    host = os.getenv("GANDALF_PROXIED_HOST")
    secret = os.getenv("GANDALF_SIGNING_SECRET", "")

    if os.getenv("GANDALF_WEBSOCKET_MODE", "False").lower() == "true":
        mode = WEBSOCKET
        print("Running in WEBSOCKET mode.")
    else:
        mode = HTTP
        print("Running in HTTP mode.")

    internal_hosts = os.getenv("GANDALF_ALLOWED_HOSTS", "").strip()
    if len(secret) == 0:
        print(
            "GANDALF_SIGNING_SECRET is not set. *DO NOT RUN THIS IN PRODUCTION!!!*"
        )
    app = make_app(
        GandalfConfiguration(host,
                             PostgresAdapter(),
                             internal_hosts,
                             signing_secret=secret,
                             mode=mode))
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()
Example #59
0
def serve():
    app.make_app().run(host='0.0.0.0')
Example #60
0
 def get_app(self):
   return app.make_app()