Example #1
0
    def test_app_two_weblabs_same_app(self):
        app = Flask(__name__)
        app.config.update({
            'WEBLAB_USERNAME': '******',
            'WEBLAB_PASSWORD': '******',
            'WEBLAB_BASE_URL': '/foo',
            'SERVER_NAME': 'localhost:5000',
        })
        weblab1 = weblablib.WebLab(app)
        with self.assertRaises(ValueError) as cm:
            weblab2 = weblablib.WebLab(app)

        self.assertIn('already installed', str(cm.exception))

        weblab1._cleanup()
Example #2
0
    def _check_error(self, config, error_class, message):
        with self.assertRaises(error_class) as cm:
            self.weblab = weblablib.WebLab()
            self.app = Flask(__name__)
            self.app.config.update(config)
            self.weblab.init_app(self.app)

        self.assertIn(message, str(cm.exception))
Example #3
0
 def _create_weblab(self):
     self.app = Flask(__name__)
     self.app.config.update({
         'WEBLAB_USERNAME': '******',
         'WEBLAB_PASSWORD': '******',
         'SERVER_NAME': 'localhost:5000',
     })
     self.weblab = weblablib.WebLab(self.app)
     self.weblab.init_app(self.app) # No problem
Example #4
0
 def test_app_twice(self):
     app = Flask(__name__)
     app.config.update({
         'WEBLAB_USERNAME': '******',
         'WEBLAB_PASSWORD': '******',
         'SERVER_NAME': 'localhost:5000',
     })
     weblab = weblablib.WebLab(app)
     weblab.init_app(app) # No problem
     weblab._cleanup()
Example #5
0
 def test_app_trailing_slashes(self):
     app = Flask(__name__)
     app.config.update({
         'WEBLAB_BASE_URL': '/mylab/',
         'WEBLAB_CALLBACK_URL': '/mylab/callback/',
         'WEBLAB_USERNAME': '******',
         'WEBLAB_PASSWORD': '******',
         'SERVER_NAME': 'localhost:5000',
     })
     with StdWrap():
         weblab = weblablib.WebLab(app)
     weblab._cleanup()
Example #6
0
    def test_app_twice_different_apps(self):
        app1 = Flask(__name__)
        app1.config.update({
            'WEBLAB_USERNAME': '******',
            'WEBLAB_PASSWORD': '******',
            'SERVER_NAME': 'localhost:5000',
        })
        app2 = Flask(__name__)
        weblab = weblablib.WebLab(app1)
        with self.assertRaises(ValueError) as cm:
            weblab.init_app(app2)

        self.assertIn('different app', str(cm.exception))
        weblab._cleanup()
Example #7
0
 def test_missing_server_name(self):
     app = Flask(__name__)
     app.config.update({
         'WEBLAB_USERNAME': '******',
         'WEBLAB_PASSWORD': '******',
     })
     with StdWrap():
         sysargv = sys.argv
         sys.argv = list(sys.argv) + [ 'weblab', 'fake', 'new', '--dont-open-browser']
         try:
             weblab = weblablib.WebLab(app)
         finally:
             sys.argv = sysargv
     weblab._cleanup()
Example #8
0
    def test_app_no_thread_and_task_threads(self):
        app = Flask(__name__)
        app.config.update({
            'WEBLAB_USERNAME': '******',
            'WEBLAB_PASSWORD': '******',
            'SERVER_NAME': 'localhost:5000',
            'WEBLAB_NO_THREAD': True,
            'WEBLAB_TASK_THREADS_PROCESS': 5,
        })
        weblab = weblablib.WebLab()
        with self.assertRaises(ValueError) as cm:
            weblab.init_app(app)

        self.assertIn('incompatible with WEBLAB_TASK_THREADS_PROCESS', str(cm.exception))
        weblab._cleanup()
Example #9
0
    def test_app_twice_different_config(self):
        app = Flask(__name__)
        app.config.update({
            'WEBLAB_CALLBACK_URL': '/mylab/callback',
            'WEBLAB_USERNAME': '******',
            'WEBLAB_PASSWORD': '******',
            'SERVER_NAME': 'localhost:5000',
        })
        weblab = weblablib.WebLab(app)
        app.config.update({
            'WEBLAB_CALLBACK_URL': '/mylab/callback2',
        })

        with self.assertRaises(ValueError) as cm:
            weblab.init_app(app)

        self.assertIn('different config', str(cm.exception))
        weblablib._cleanup_all()
        weblab._cleanup()
Example #10
0
    def create_weblab(self):
        self.weblab = weblablib.WebLab()
        self.app = Flask(__name__)
        flask_cli.locate_app = lambda *args: self.app
        self.server_name = 'localhost:5000'
        self.app.config.update(self.get_config())
        self.auth_headers = {
            'Authorization': 'Basic ' + base64.encodestring(b'weblabdeusto:password').decode('utf8').strip(),
        }
        self.wrong_auth_headers = {
            'Authorization': 'Basic ' + base64.encodestring(b'wrong_weblabdeusto:wrong_password').decode('utf8').strip(),
        }

        @self.weblab.task(ensure_unique=True)
        def task_before_init():
            return True

        self.weblab.init_app(self.app)
        self.weblab._redis_manager.client.flushall()

        @self.weblab.on_start
        def on_start(client_data, server_data):
            self.on_start(client_data, server_data)

        @self.weblab.on_dispose
        def on_dispose():
            self.on_dispose()

        @self.weblab.initial_url
        def initial_url():
            return url_for('lab')

        @self.app.route('/lab/')
        @weblablib.requires_login
        def lab():
            return self.lab()

        @self.app.route('/lab/active')
        @weblablib.requires_active
        def lab_active():
            return self.lab()

        @self.app.route('/logout')
        @weblablib.requires_active
        def logout():
            weblablib.logout()
            return "logout"

        @self.app.route('/poll')
        @weblablib.requires_active
        def poll():
            weblablib.poll()
            weblablib.poll() # Twice so as to test g.poll_requested
            return "poll"

        @self.weblab.task()
        def task():
            return self.task()

        with self.assertRaises(ValueError) as cm:
            @self.weblab.task()
            def task():
                self.task()

        self.assertIn("same name", str(cm.exception))

        with self.assertRaises(ValueError) as cm:
            # 43 characters is the length of create_token()
            self.assertEquals(43, len(weblablib._create_token()))

            # Therefore, having a task with that name length is forbidden
            # to avoid any potential issue in get_task
            @self.weblab.task()
            def abc0123456789012345678901234567890123456789():
                pass
           
        self.assertIn("number of characters", str(cm.exception))

        self.current_task = task
Example #11
0
    def test_empty_app(self):
        with self.assertRaises(ValueError) as cm:
            weblablib.WebLab().init_app(None)

        self.assertIn("Flask app", str(cm.exception))