|
12 | 12 | from mock import Mock, patch
|
13 | 13 |
|
14 | 14 | from flask import escape
|
| 15 | +from flask_appbuilder.security.sqla import models as ab_models |
15 | 16 |
|
16 | 17 | import caravel
|
17 | 18 | from caravel import app, db, models, utils, appbuilder
|
@@ -63,17 +64,38 @@ def login_gamma(self):
|
63 | 64 | follow_redirects=True)
|
64 | 65 | assert 'Welcome' in resp.data.decode('utf-8')
|
65 | 66 |
|
| 67 | + def setup_public_access_for_dashboard(self, dashboard_name): |
| 68 | + public_role = appbuilder.sm.find_role('Public') |
| 69 | + perms = db.session.query(ab_models.PermissionView).all() |
| 70 | + for perm in perms: |
| 71 | + if perm.permission.name not in ( |
| 72 | + 'can_list', |
| 73 | + 'can_dashboard', |
| 74 | + 'can_explore', |
| 75 | + 'datasource_access'): |
| 76 | + continue |
| 77 | + if not perm.view_menu: |
| 78 | + continue |
| 79 | + if perm.view_menu.name not in ( |
| 80 | + 'SliceModelView', |
| 81 | + 'DashboardModelView', |
| 82 | + 'Caravel') and dashboard_name not in perm.view_menu.name: |
| 83 | + continue |
| 84 | + appbuilder.sm.add_permission_role(public_role, perm) |
| 85 | + |
66 | 86 |
|
67 | 87 | class CoreTests(CaravelTestCase):
|
68 | 88 |
|
69 | 89 | def __init__(self, *args, **kwargs):
|
| 90 | + # Load examples first, so that we setup proper permission-view relations |
| 91 | + # for all example data sources. |
| 92 | + self.load_examples() |
70 | 93 | super(CoreTests, self).__init__(*args, **kwargs)
|
71 | 94 | self.table_ids = {tbl.table_name: tbl.id for tbl in (
|
72 | 95 | db.session
|
73 | 96 | .query(models.SqlaTable)
|
74 | 97 | .all()
|
75 | 98 | )}
|
76 |
| - self.load_examples() |
77 | 99 |
|
78 | 100 | def setUp(self):
|
79 | 101 | pass
|
@@ -162,6 +184,52 @@ def test_gamma(self):
|
162 | 184 | resp = self.client.get('/dashboardmodelview/list/')
|
163 | 185 | assert "List Dashboard" in resp.data.decode('utf-8')
|
164 | 186 |
|
| 187 | + def test_public_user_dashboard_access(self): |
| 188 | + # Try access before adding appropriate permissions. |
| 189 | + resp = self.client.get('/slicemodelview/list/') |
| 190 | + data = resp.data.decode('utf-8') |
| 191 | + assert '<a href="/tablemodelview/edit/3">birth_names</a>' not in data |
| 192 | + |
| 193 | + resp = self.client.get('/dashboardmodelview/list/') |
| 194 | + data = resp.data.decode('utf-8') |
| 195 | + assert '<a href="/caravel/dashboard/births/">' not in data |
| 196 | + |
| 197 | + resp = self.client.get('/caravel/dashboard/births/') |
| 198 | + data = resp.data.decode('utf-8') |
| 199 | + assert '[dashboard] Births' not in data |
| 200 | + |
| 201 | + self.setup_public_access_for_dashboard('birth_names') |
| 202 | + |
| 203 | + # Try access after adding appropriate permissions. |
| 204 | + resp = self.client.get('/slicemodelview/list/') |
| 205 | + data = resp.data.decode('utf-8') |
| 206 | + assert '<a href="/tablemodelview/edit/3">birth_names</a>' in data |
| 207 | + |
| 208 | + resp = self.client.get('/dashboardmodelview/list/') |
| 209 | + data = resp.data.decode('utf-8') |
| 210 | + assert '<a href="/caravel/dashboard/births/">' in data |
| 211 | + |
| 212 | + resp = self.client.get('/caravel/dashboard/births/') |
| 213 | + data = resp.data.decode('utf-8') |
| 214 | + assert '[dashboard] Births' in data |
| 215 | + |
| 216 | + resp = self.client.get('/caravel/explore/table/3/') |
| 217 | + data = resp.data.decode('utf-8') |
| 218 | + assert '[explore] birth_names' in data |
| 219 | + |
| 220 | + # Confirm that public doesn't have access to other datasets. |
| 221 | + resp = self.client.get('/slicemodelview/list/') |
| 222 | + data = resp.data.decode('utf-8') |
| 223 | + assert '<a href="/tablemodelview/edit/2">wb_health_population</a>' not in data |
| 224 | + |
| 225 | + resp = self.client.get('/dashboardmodelview/list/') |
| 226 | + data = resp.data.decode('utf-8') |
| 227 | + assert '<a href="/caravel/dashboard/world_health/">' not in data |
| 228 | + |
| 229 | + resp = self.client.get('/caravel/explore/table/2/', follow_redirects=True) |
| 230 | + data = resp.data.decode('utf-8') |
| 231 | + assert "You don't seem to have access to this datasource" in data |
| 232 | + |
165 | 233 |
|
166 | 234 | SEGMENT_METADATA = [{
|
167 | 235 | "id": "some_id",
|
|
0 commit comments