def setup_app(self, url_base_pathname=None, skip_visit=False, sharing='private'): app = dash.Dash(__name__, url_base_pathname=url_base_pathname) app.layout = html.Div([ dcc.Input( id='input', value='initial value' ), html.Div(id='output') ]) @app.callback(Output('output', 'children'), [Input('input', 'value')]) def update_output(new_value): return new_value auth = plotly_auth.PlotlyAuth( app, 'integration-test', sharing, ( 'http://localhost:8050{}'.format(url_base_pathname) if url_base_pathname else oauth_urls ) ) self.startServer(app, skip_visit=skip_visit) return app, auth
def test_user_cookies(self): os.environ['PLOTLY_USERNAME'] = users['creator']['username'] os.environ['PLOTLY_API_KEY'] = users['creator']['api_key'] app = dash.Dash() auth = plotly_auth.PlotlyAuth( app, 'integration-test', 'private', 'http://*****:*****@app.callback(Output('username', 'children'), [Input('username', 'id')]) def _give_name(_): username = auth.get_username() return username @auth.is_authorized_hook def _is_authorized(_): perms = {'click_button': True} auth.set_user_data(perms) return True @app.callback(Output('authorized', 'children'), [Input('btn', 'n_clicks')]) def _check_perms(n_clicks): if n_clicks: perms = auth.get_user_data() perm_click_button = perms.get('click_button') if not perm_click_button: return 'unauthorized' else: return 'authorized' self.startServer(app) self._login_flow(users['creator']['username'], users['creator']['pw']) switch_windows(self.driver) self.wait_for_text_to_equal('#username', 'dash-test-user') btn = self.wait_for_element_by_css_selector('#btn') btn.click() self.wait_for_text_to_equal('#authorized', 'authorized')
def test_logout(self): os.environ['PLOTLY_USERNAME'] = users['creator']['username'] os.environ['PLOTLY_API_KEY'] = users['creator']['api_key'] app = dash.Dash() auth = plotly_auth.PlotlyAuth(app, 'integration-test', 'public', 'http://localhost:8050/') logout_label = 'Press to logout' redirect = 'https://www.google.com/' btn_style = { 'backgroundColor': 'red', 'padding': '16px', 'borderRadius': '8px', 'border': 'none' } app.layout = html.Div([ html.Div('content', id='content'), auth.create_logout_button(id='logout-btn', label=logout_label, redirect_to=redirect, style=btn_style) ], id='container') self.startServer(app) username = users['viewer']['username'] pw = users['viewer']['pw'] self._login_flow(username, pw) switch_windows(self.driver) time.sleep(1) btn = self.wait_for_element_by_css_selector('#logout-btn') padding = btn.value_of_css_property('padding') self.assertEqual(btn_style['padding'], padding) self.assertEqual(logout_label, btn.text) btn.click() time.sleep(1) self.assertEqual(redirect, self.driver.current_url)
def test_logout_url(self): os.environ['DASH_LOGOUT_URL'] = '/_logout' app = dash.Dash(__name__) auth = plotly_auth.PlotlyAuth( app, 'integration-test', 'public', ['http://*****:*****@app.server.route('/_logout', methods=['POST']) def on_logout(): rep = flask.redirect('/logged-out') rep.set_cookie('logout-cookie', '', 0) return rep app.layout = html.Div([ html.H2('Logout test'), dcc.Location(id='location'), html.Div(id='content'), ]) @app.callback(Output('content', 'children'), [Input('location', 'pathname')]) def on_location(location_path): if location_path is None: raise PreventUpdate if 'logged-out' in location_path: return 'Logged out' else: @flask.after_this_request def _insert_cookie(rep): rep.set_cookie('logout-cookie', 'logged-in') return rep return auth.create_logout_button() self.startServer(app) btn = self.wait_for_element_by_css_selector('#logout-btn') btn.click() self.wait_for_text_to_equal('#content', 'Logged out') self.assertFalse(self.driver.get_cookie('logout-cookie')) del os.environ['DASH_LOGOUT_URL']