Exemplo n.º 1
0
    def setUp(self):
        self.app = Flask(__name__)
        self.app.debug = True
        self.app.config.update(pusher_conf)
        self.pusher = Pusher()
        self.client = self.app.test_client()
        self._called = False

        @self.pusher.webhooks.client
        def c():
            self._called = True

        self.pusher.init_app(self.app)
Exemplo n.º 2
0
    def test_all_configurations(self):
        backend = mock.Mock()

        self.app.config.update({
            "PUSHER_SSL": False,
            "PUSHER_TIMEOUT": 3,
            "PUSHER_CLUSTER": "eu",
            "PUSHER_BACKEND": backend,
            "PUSHER_BACKEND_OPTIONS": {
                "anything": True
            },
            "PUSHER_NOTIFICATION_HOST": "example.com",
            "PUSHER_NOTIFICATION_SSL": True,
        })
        pusher = Pusher(self.app)
        with self.app.test_request_context():
            self.assertIsNotNone(pusher.client)

            if "ssl" in argspec.args:
                self.assertFalse(pusher.client.ssl)

            if "timeout" in argspec.args:
                self.assertEqual(3, pusher.client.timeout)

            if "cluster" in argspec.args:
                self.assertEqual("api-eu.pusher.com", pusher.client.host)

            if "backend" in argspec.args:
                self.assertTrue(backend.called)

            if argspec.keywords == "backend_options":
                self.assertTrue(backend.call_args[1]["anything"])
Exemplo n.º 3
0
 def test_configuration(self):
     self.app.config["PUSHER_HOST"] = "example.com"
     self.app.config["PUSHER_PORT"] = 8080
     pusher = Pusher(self.app)
     with self.app.test_request_context():
         self.assertIsNotNone(pusher.client)
         self.assertEqual("KEY", pusher.client.key)
         self.assertEqual("SUPERSECRET", pusher.client.secret)
         self.assertEqual("example.com", pusher.client.host)
         self.assertEqual(8080, pusher.client.port)
Exemplo n.º 4
0
    def setUp(self):
        self.app = Flask(__name__)
        self.app.debug = True
        self.app.config.update(pusher_conf)
        self.pusher = Pusher()
        self.client = self.app.test_client()
        self._called = False

        @self.pusher.webhooks.client
        def c():
            self._called = True

        self.pusher.init_app(self.app)
Exemplo n.º 5
0
    def test_json_encoder(self):
        if not _json_encoder_support:
            msg = u"JSON encoder override is not supported on pusher>=1.0,<1.1"
            self.skipTest(msg)

        self.app.json_encoder = CustomJSONEncoder
        pusher = Pusher(self.app)

        with self.app.test_request_context():
            try:
                enc = pusher.client._json_encoder
            except AttributeError:
                enc = pusher.client.encoder
        self.assertEqual(CustomJSONEncoder, enc)
Exemplo n.º 6
0
class PusherBatchAuthTest(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.debug = True
        self.app.config.update(pusher_conf)
        self.pusher = Pusher(self.app)
        self.client = self.app.test_client()

    def test_one_channel(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={
                                        "channel_name[0]": "private-a",
                                        "socket_id": SOCKET_ID
                                    })
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertEqual(1, len(data))

        data_a = data.get("private-a")
        self.assertEqual(200, data_a["status"])
        self.assertTrue(data_a["data"])

    def test_more_channels(self):
        self.pusher.auth(lambda c, s: "b" not in c)
        response = self.client.post("/pusher/auth",
                                    data={
                                        "channel_name[0]": "private-a",
                                        "channel_name[1]": "private-b",
                                        "channel_name[2]": "presence-c",
                                        "socket_id": SOCKET_ID
                                    })
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertEqual(3, len(data))

        a = data.get("private-a")
        self.assertEqual(200, a["status"])
        self.assertIn("auth", a["data"])

        b = data.get("private-b")
        self.assertEqual(403, b["status"])

        c = data.get("presence-c")
        self.assertEqual(200, c["status"])
        c_data = c["data"]
        self.assertIn("auth", c_data)
        self.assertIn("channel_data", c_data)

    def test_missing_channel(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={
                                        "channel_name[1]": "private-b",
                                        "socket_id": SOCKET_ID
                                    })
        self.assertEqual(400, response.status_code)
Exemplo n.º 7
0
class PusherBatchAuthTest(unittest.TestCase):

    def setUp(self):
        self.app = Flask(__name__)
        self.app.debug = True
        self.app.config.update(pusher_conf)
        self.pusher = Pusher(self.app)
        self.client = self.app.test_client()

    def test_one_channel(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={"channel_name[0]": "private-a",
                                          "socket_id": SOCKET_ID})
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertEqual(1, len(data))

        data_a = data.get("private-a")
        self.assertEqual(200, data_a["status"])
        self.assertTrue(data_a["data"])

    def test_more_channels(self):
        self.pusher.auth(lambda c, s: "b" not in c)
        response = self.client.post("/pusher/auth",
                                    data={"channel_name[0]": "private-a",
                                          "channel_name[1]": "private-b",
                                          "channel_name[2]": "presence-c",
                                          "socket_id": SOCKET_ID})
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertEqual(3, len(data))

        a = data.get("private-a")
        self.assertEqual(200, a["status"])
        self.assertIn("auth", a["data"])

        b = data.get("private-b")
        self.assertEqual(403, b["status"])

        c = data.get("presence-c")
        self.assertEqual(200, c["status"])
        c_data = c["data"]
        self.assertIn("auth", c_data)
        self.assertIn("channel_data", c_data)

    def test_missing_channel(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={"channel_name[1]": "private-b",
                                          "socket_id": SOCKET_ID})
        self.assertEqual(400, response.status_code)
Exemplo n.º 8
0
class PusherWebhookTest(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.debug = True
        self.app.config.update(pusher_conf)
        self.pusher = Pusher()
        self.client = self.app.test_client()
        self._called = False

        @self.pusher.webhooks.client
        def c():
            self._called = True

        self.pusher.init_app(self.app)

    def test_no_webhook(self):
        with self.app.test_request_context():
            url = url_for("pusher.presence_event")
        response = self.client.post(url)
        self.assertEqual(404, response.status_code)
        self.assertFalse(self._called)

    def test_without_key_forbidden(self):
        with self.app.test_request_context():
            url = url_for("pusher.client_event")
        response = self.client.post(url)
        self.assertEqual(403, response.status_code)
        self.assertFalse(self._called)

    def test_invalid_key_forbidden(self):
        with self.app.test_request_context():
            url = url_for("pusher.client_event")
        response = self.client.post(url,
                                    headers={
                                        "Content-Type": "application/json",
                                        "X-Pusher-Key": "meh"
                                    })
        self.assertEqual(403, response.status_code)
        self.assertFalse(self._called)

    def test_valid_key_forbidden_without_signature(self):
        with self.app.test_request_context():
            url = url_for("pusher.client_event")
        response = self.client.post(url,
                                    headers={
                                        "Content-Type": "application/json",
                                        "X-Pusher-Key": "KEY"
                                    })
        self.assertEqual(403, response.status_code)
        self.assertFalse(self._called)

    def test_invalid_signature(self):
        with self.app.test_request_context():
            url = url_for("pusher.client_event")
        response = self.client.post(url,
                                    headers={
                                        "Content-Type": "application/json",
                                        "X-Pusher-Key": "KEY",
                                        "X-Pusher-Signature": "x"
                                    })
        self.assertEqual(403, response.status_code)
        self.assertFalse(self._called)

    def test_valid_signature(self):
        data = '{"a": "b"}'
        with self.app.test_request_context():
            url = url_for("pusher.client_event")
            signature = self.pusher._sign(data)
        response = self.client.post(url,
                                    data=data,
                                    headers={
                                        "Content-Type": "application/json",
                                        "X-Pusher-Key": "KEY",
                                        "X-Pusher-Signature": signature
                                    })
        self.assertEqual(200, response.status_code)
        self.assertTrue(self._called)

    def test_hook_all_handlers(self):
        @self.pusher.webhooks.presence
        def h1():
            pass

        @self.pusher.webhooks.channel_existence
        def h2():
            pass
Exemplo n.º 9
0
 def setUp(self):
     self.app = Flask(__name__)
     self.app.debug = True
     self.app.config.update(pusher_conf)
     self.pusher = Pusher(self.app)
     self.client = self.app.test_client()
Exemplo n.º 10
0
class PusherAuthTest(unittest.TestCase):

    def setUp(self):
        self.app = Flask(__name__)
        self.app.debug = True
        self.app.config.update(pusher_conf)
        self.pusher = Pusher(self.app)
        self.client = self.app.test_client()

    def test_url_for(self):
        with self.app.test_request_context():
            url = url_for("pusher.auth")
        self.assertEqual("/pusher/auth", url)

    def test_forbidden_withuot_auth_handler(self):
        response = self.client.post("/pusher/auth")
        self.assertEqual(403, response.status_code)

    def test_auth_refused(self):
        self.pusher.auth(lambda c, s: False)
        response = self.client.post("/pusher/auth",
                                    data={"channel_name": "private-a",
                                          "socket_id": SOCKET_ID})
        self.assertEqual(403, response.status_code)

    def test_auth_accepted(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={"channel_name": "private-a",
                                          "socket_id": SOCKET_ID})
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertIn("auth", data)
        self.assertNotIn("channel_data", data)

    def test_no_channel_data_in_private_channel(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={"channel_name": "private-a",
                                          "socket_id": SOCKET_ID})
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertIn("auth", data)
        self.assertNotIn("channel_data", data)

    def test_default_channel_data_in_presence_channel(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={"channel_name": "presence-a",
                                          "socket_id": SOCKET_ID})
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertIn("auth", data)
        channel_data = json.loads(data["channel_data"])
        self.assertEqual({"user_id": SOCKET_ID}, channel_data)

    def test_channel_data_in_presence_channel(self):
        self.pusher.auth(lambda c, s: True)
        self.pusher.channel_data(lambda c, s: {"foo": "bar"})
        response = self.client.post("/pusher/auth",
                                    data={"channel_name": "presence-a",
                                          "socket_id": SOCKET_ID})
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertIn("auth", data)
        channel_data = json.loads(data["channel_data"])
        self.assertEqual(SOCKET_ID, channel_data["user_id"])
        self.assertIn("bar", channel_data["foo"])

    def test_invalid_channel(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={"channel_name": "foo",
                                          "socket_id": SOCKET_ID})
        self.assertEqual(404, response.status_code)
Exemplo n.º 11
0
 def test_lazy_init_app(self):
     pusher = Pusher()
     pusher.init_app(self.app)
     with self.app.test_request_context():
         self.assertIsNotNone(pusher.client)
Exemplo n.º 12
0
 def test_init_app(self):
     pusher = Pusher()
     pusher.init_app(self.app)
     self._assert_init(pusher)
Exemplo n.º 13
0
from flask import Flask
from config import Config
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_mail import Mail
from flask_pusher import Pusher

cors = CORS()
db = SQLAlchemy()
migrate = Migrate()
login = LoginManager()
login.login_view = 'auth.login'
mail = Mail()
pusher = Pusher()


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    cors.init_app(app, resources={r"/api/*": {"origins": "*"}})
    db.init_app(app)
    migrate.init_app(app, db)
    login.init_app(app)
    mail.init_app(app)
    pusher.init_app(app)

    from app.auth import bp as auth_bp
    app.register_blueprint(auth_bp, url_prefix='/auth')
Exemplo n.º 14
0
 def test_lazy_init_app(self):
     pusher = Pusher()
     pusher.init_app(self.app)
     with self.app.test_request_context():
         self.assertIsNotNone(pusher.client)
Exemplo n.º 15
0
class PusherWebhookTest(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.debug = True
        self.app.config.update(pusher_conf)
        self.pusher = Pusher()
        self.client = self.app.test_client()
        self._called = False

        @self.pusher.webhooks.client
        def c():
            self._called = True

        self.pusher.init_app(self.app)

    def test_no_webhook(self):
        with self.app.test_request_context():
            url = url_for("pusher.presence_event")
        response = self.client.post(url)
        self.assertEqual(404, response.status_code)
        self.assertFalse(self._called)

    def test_without_key_forbidden(self):
        with self.app.test_request_context():
            url = url_for("pusher.client_event")
        response = self.client.post(url)
        self.assertEqual(403, response.status_code)
        self.assertFalse(self._called)

    def test_invalid_key_forbidden(self):
        with self.app.test_request_context():
            url = url_for("pusher.client_event")
        response = self.client.post(url, headers={
            "Content-Type": "application/json",
            "X-Pusher-Key": "meh"
        })
        self.assertEqual(403, response.status_code)
        self.assertFalse(self._called)

    def test_valid_key_forbidden_without_signature(self):
        with self.app.test_request_context():
            url = url_for("pusher.client_event")
        response = self.client.post(url, headers={
            "Content-Type": "application/json",
            "X-Pusher-Key": "KEY"
        })
        self.assertEqual(403, response.status_code)
        self.assertFalse(self._called)

    def test_invalid_signature(self):
        with self.app.test_request_context():
            url = url_for("pusher.client_event")
        response = self.client.post(url, headers={
            "Content-Type": "application/json",
            "X-Pusher-Key": "KEY",
            "X-Pusher-Signature": "x"
        })
        self.assertEqual(403, response.status_code)
        self.assertFalse(self._called)

    def test_valid_signature(self):
        data = '{"a": "b"}'
        with self.app.test_request_context():
            url = url_for("pusher.client_event")
            signature = self.pusher._sign(data)
        response = self.client.post(url, data=data, headers={
            "Content-Type": "application/json",
            "X-Pusher-Key": "KEY",
            "X-Pusher-Signature": signature
        })
        self.assertEqual(200, response.status_code)
        self.assertTrue(self._called)

    def test_hook_all_handlers(self):
        @self.pusher.webhooks.presence
        def h1():
            pass

        @self.pusher.webhooks.channel_existence
        def h2():
            pass
Exemplo n.º 16
0
 def setUp(self):
     self.app = Flask(__name__)
     self.app.debug = True
     self.app.config.update(pusher_conf)
     self.pusher = Pusher(self.app)
     self.client = self.app.test_client()
Exemplo n.º 17
0
 def test_default_config(self):
     # fallback to Pusher globals still works
     pusher = Pusher(self.app)
     with self.app.test_request_context():
         self.assertIsNotNone(pusher.client)
Exemplo n.º 18
0
 def test_options(self):
     pusher = Pusher()
     pusher.init_app(self.app, host='eu', timeout=20)
     self.assertEqual(pusher.host, 'eu')
     self.assertEqual(pusher.timeout, 20)
Exemplo n.º 19
0
 def test_create_extensions_map(self):
     del self.app.extensions
     pusher = Pusher(self.app)
     with self.app.test_request_context():
         self.assertIsNotNone(pusher.client)
Exemplo n.º 20
0
 def test_override_config(self):
     pusher = Pusher()
     pusher.init_app(self.app, port=1337)
     self.assertEqual(pusher.port, 1337)
Exemplo n.º 21
0
 def test_pusher_key_in_template(self):
     Pusher(self.app)
     with self.app.test_request_context():
         rendered = render_template_string("{{ PUSHER_KEY }}")
         self.assertEqual("KEY", rendered)
Exemplo n.º 22
0
class PusherAuthTest(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.debug = True
        self.app.config.update(pusher_conf)
        self.pusher = Pusher(self.app)
        self.client = self.app.test_client()

    def test_url_for(self):
        with self.app.test_request_context():
            url = url_for("pusher.auth")
        self.assertEqual("/pusher/auth", url)

    def test_forbidden_withuot_auth_handler(self):
        response = self.client.post("/pusher/auth")
        self.assertEqual(403, response.status_code)

    def test_auth_refused(self):
        self.pusher.auth(lambda c, s: False)
        response = self.client.post("/pusher/auth",
                                    data={
                                        "channel_name": "private-a",
                                        "socket_id": SOCKET_ID
                                    })
        self.assertEqual(403, response.status_code)

    def test_auth_accepted(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={
                                        "channel_name": "private-a",
                                        "socket_id": SOCKET_ID
                                    })
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertIn("auth", data)
        self.assertNotIn("channel_data", data)

    def test_no_channel_data_in_private_channel(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={
                                        "channel_name": "private-a",
                                        "socket_id": SOCKET_ID
                                    })
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertIn("auth", data)
        self.assertNotIn("channel_data", data)

    def test_default_channel_data_in_presence_channel(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={
                                        "channel_name": "presence-a",
                                        "socket_id": SOCKET_ID
                                    })
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertIn("auth", data)
        channel_data = json.loads(data["channel_data"])
        self.assertEqual({"user_id": SOCKET_ID}, channel_data)

    def test_channel_data_in_presence_channel(self):
        self.pusher.auth(lambda c, s: True)
        self.pusher.channel_data(lambda c, s: {"foo": "bar"})
        response = self.client.post("/pusher/auth",
                                    data={
                                        "channel_name": "presence-a",
                                        "socket_id": SOCKET_ID
                                    })
        self.assertEqual(200, response.status_code)
        data = json.loads(response.data)
        self.assertIn("auth", data)
        channel_data = json.loads(data["channel_data"])
        self.assertEqual(SOCKET_ID, channel_data["user_id"])
        self.assertIn("bar", channel_data["foo"])

    def test_invalid_channel(self):
        self.pusher.auth(lambda c, s: True)
        response = self.client.post("/pusher/auth",
                                    data={
                                        "channel_name": "foo",
                                        "socket_id": SOCKET_ID
                                    })
        self.assertEqual(404, response.status_code)
Exemplo n.º 23
0
from contextlib import redirect_stdout
from time import sleep

import subprocess
import os
import sys  

from qa.question_answering import answer_question

app_id = "960906"
key = "d18ed2e42cf337876806"
secret = "ffd3ef254ef8617ab31a"
cluster = "us2"

app = Flask(__name__)
pusher = Pusher(app, secret = secret, app_id = app_id, key = key, cluster = cluster)


app.secret_key = 'secret key'
CORS(app)

UPLOAD_FOLDER = './data_upload'
ALLOWED_EXTENSIONS = {'json'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def write_to_pusher(message):
    
    if isinstance(message, bytes):
        message = message.decode('utf-8')
    
    messages = message.split('\n')
Exemplo n.º 24
0
#TODO: find out if this is bad
server_session.app.session_interface.db.create_all()
login_manager = LoginManager()
login_manager.init_app(app)

#pusher credentials configuration
app.config['PUSHER_APP_ID'] = os.environ['PUSHER_APP_ID']
app.config['PUSHER_KEY'] = os.environ['PUSHER_KEY']
app.config['PUSHER_CLUSTER'] = os.environ['PUSHER_CLUSTER']
app.config['PUSHER_SECRET'] = os.environ['PUSHER_SECRET']
app.config['PUSHER_SSL'] = False
#pusher server location configuration
if 'PUSHER_HOST' in os.environ and 'PUSHER_PORT' in os.environ:
    app.config['PUSHER_HOST'] = os.environ['PUSHER_HOST']
    app.config['PUSHER_PORT'] = int(os.environ['PUSHER_PORT'])

pusher = Pusher(app)


#This is here to avoid weird authentication bypasses.
#TODO: get rid of pusher-flask, use the official python library instead
@pusher.auth
def pusher_auth(channel_name, socket_id):
    return False


# initialize the api blueprint
from .api_views import api
app.register_blueprint(api, url_prefix='/api/v1/')