OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DashboardTest.java
Go to the documentation of this file.
1 /*
2  * Copyright 2015 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.mapd.dashboard;
17 
18 import static com.mapd.tests.HeavyDBAsserts.shouldThrowException;
19 
22 
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 
26 import java.util.Arrays;
27 import java.util.List;
28 
29 import ai.heavy.thrift.server.TDBObject;
30 import ai.heavy.thrift.server.TDBObjectType;
31 
32 public class DashboardTest {
33  final static Logger logger = LoggerFactory.getLogger(DashboardTest.class);
34 
35  public static void main(String[] args) throws Exception {
36  logger.info("Hello, World");
37 
38  DashboardTest x = new DashboardTest();
39  x.testUserRoles();
40  x.testDashboards();
41  x.testDbLevelDashboardPermissions();
42  }
43 
44  void testUserRoles() throws Exception {
45  logger.info("testUserRoles()");
46  HeavyDBTestClient su = HeavyDBTestClient.getClient(
47  "localhost", 6274, "omnisci", "admin", "HyperInteractive");
48 
49  su.runSql("CREATE USER dba (password = 'password', is_super = 'true');");
50  su.runSql("CREATE USER jason (password = 'password', is_super = 'false');");
51  su.runSql("CREATE USER bob (password = 'password', is_super = 'false');");
52 
53  su.runSql("CREATE ROLE salesDept;");
54  su.runSql("CREATE USER foo (password = 'password', is_super = 'false');");
55 
56  su.runSql("CREATE DATABASE db1;");
57  su.runSql("CREATE DATABASE db2;");
58  HeavyDBTestClient dba1 =
59  HeavyDBTestClient.getClient("localhost", 6274, "db1", "bob", "password");
60  HeavyDBTestClient dba2 =
61  HeavyDBTestClient.getClient("localhost", 6274, "db2", "foo", "password");
62  HeavyDBTestClient dba =
63  HeavyDBTestClient.getClient("localhost", 6274, "db2", "dba", "password");
64  HeavyDBAsserts.assertEqual(0, dba1.get_users().size());
65  HeavyDBAsserts.assertEqual(0, dba1.get_roles().size());
66  HeavyDBAsserts.assertEqual(0, dba2.get_users().size());
67  HeavyDBAsserts.assertEqual(0, dba2.get_roles().size());
68  HeavyDBAsserts.assertEqual(5, dba.get_users().size());
69  HeavyDBAsserts.assertEqual(1, dba.get_roles().size());
70 
71  su.runSql("GRANT create dashboard on database db1 to jason;");
72  HeavyDBAsserts.assertEqual(Arrays.asList("jason"), dba1.get_users());
73  HeavyDBAsserts.assertEqual(0, dba1.get_roles().size());
74  HeavyDBAsserts.assertEqual(0, dba2.get_users().size());
75  HeavyDBAsserts.assertEqual(0, dba2.get_roles().size());
76  HeavyDBAsserts.assertEqual(5, dba.get_users().size());
77  HeavyDBAsserts.assertEqual(1, dba.get_roles().size());
78 
79  su.runSql("GRANT create dashboard on database db1 to salesDept;");
80  HeavyDBAsserts.assertEqual(Arrays.asList("jason"), dba1.get_users());
81  HeavyDBAsserts.assertEqual(Arrays.asList("salesDept"), dba1.get_roles());
82  HeavyDBAsserts.assertEqual(0, dba2.get_users().size());
83  HeavyDBAsserts.assertEqual(0, dba2.get_roles().size());
84  HeavyDBAsserts.assertEqual(5, dba.get_users().size());
85  HeavyDBAsserts.assertEqual(1, dba.get_roles().size());
86 
87  su.runSql("DROP DATABASE db1;");
88  su.runSql("DROP DATABASE db2;");
89  su.runSql("DROP USER foo;");
90  su.runSql("DROP ROLE salesDept;");
91  su.runSql("DROP USER bob;");
92  su.runSql("DROP USER jason;");
93  su.runSql("DROP USER dba;");
94  }
95 
96  void testDbLevelDashboardPermissions() throws Exception {
97  logger.info("testDbLevelDashboardPermissions()");
98 
99  HeavyDBTestClient su = HeavyDBTestClient.getClient(
100  "localhost", 6274, "omnisci", "admin", "HyperInteractive");
101 
102  su.runSql("CREATE USER dba (password = 'password', is_super = 'true');");
103  su.runSql("CREATE USER jason (password = 'password', is_super = 'false');");
104 
105  su.runSql("CREATE ROLE salesDept;");
106  su.runSql("CREATE USER foo (password = 'password', is_super = 'false');");
107 
108  su.runSql("GRANT salesDept TO foo;");
109 
110  HeavyDBTestClient dba =
111  HeavyDBTestClient.getClient("localhost", 6274, "omnisci", "dba", "password");
112  HeavyDBTestClient jason = HeavyDBTestClient.getClient(
113  "localhost", 6274, "omnisci", "jason", "password");
114  HeavyDBTestClient foo =
115  HeavyDBTestClient.getClient("localhost", 6274, "omnisci", "foo", "password");
116 
117  HeavyDBAsserts.assertEqual(
118  0, jason.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).size());
119  HeavyDBAsserts.assertEqual(
120  0, foo.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).size());
121 
122  su.runSql("GRANT CREATE DASHBOARD ON DATABASE omnisci TO jason;");
123  HeavyDBAsserts.assertEqual(
124  1, jason.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).size());
125  HeavyDBAsserts.assertEqual(
126  0, foo.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).size());
127 
128  TDBObject obj =
129  jason.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).get(0);
130  HeavyDBAsserts.assertEqual(Arrays.asList(true, false, false, false), obj.getPrivs());
131 
132  su.runSql("GRANT EDIT DASHBOARD ON DATABASE omnisci TO jason;");
133  obj = jason.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).get(0);
134  HeavyDBAsserts.assertEqual(Arrays.asList(true, false, false, true), obj.getPrivs());
135 
136  su.runSql("GRANT VIEW DASHBOARD ON DATABASE omnisci TO jason;");
137  obj = jason.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).get(0);
138  HeavyDBAsserts.assertEqual(Arrays.asList(true, false, true, true), obj.getPrivs());
139 
140  su.runSql("GRANT DELETE DASHBOARD ON DATABASE omnisci TO jason;");
141  obj = jason.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).get(0);
142  HeavyDBAsserts.assertEqual(Arrays.asList(true, true, true, true), obj.getPrivs());
143 
144  su.runSql("GRANT CREATE DASHBOARD ON DATABASE omnisci TO salesDept;");
145  HeavyDBAsserts.assertEqual(
146  1, jason.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).size());
147  HeavyDBAsserts.assertEqual(
148  1, foo.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).size());
149 
150  obj = foo.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).get(0);
151  HeavyDBAsserts.assertEqual(Arrays.asList(true, false, false, false), obj.getPrivs());
152 
153  su.runSql("GRANT EDIT DASHBOARD ON DATABASE omnisci TO salesDept;");
154  obj = foo.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).get(0);
155  HeavyDBAsserts.assertEqual(Arrays.asList(true, false, false, true), obj.getPrivs());
156 
157  su.runSql("GRANT VIEW DASHBOARD ON DATABASE omnisci TO salesDept;");
158  obj = foo.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).get(0);
159  HeavyDBAsserts.assertEqual(Arrays.asList(true, false, true, true), obj.getPrivs());
160 
161  su.runSql("GRANT DELETE DASHBOARD ON DATABASE omnisci TO salesDept;");
162  obj = foo.get_db_object_privs("", TDBObjectType.DashboardDBObjectType).get(0);
163  HeavyDBAsserts.assertEqual(Arrays.asList(true, true, true, true), obj.getPrivs());
164 
165  su.runSql("DROP USER foo;");
166  su.runSql("DROP ROLE salesDept;");
167  su.runSql("DROP USER jason;");
168  su.runSql("DROP USER dba;");
169  }
170 
171  void testDashboards() throws Exception {
172  logger.info("testDashboards()");
173 
174  HeavyDBTestClient su = HeavyDBTestClient.getClient(
175  "localhost", 6274, "omnisci", "admin", "HyperInteractive");
176 
177  List<String> users = su.get_users();
178 
179  su.runSql("CREATE USER dba (password = 'password', is_super = 'true');");
180  su.runSql("CREATE USER jason (password = 'password', is_super = 'false');");
181  su.runSql("CREATE USER bob (password = 'password', is_super = 'false');");
182 
183  su.runSql("CREATE ROLE salesDept;");
184  su.runSql("CREATE USER foo (password = 'password', is_super = 'false');");
185 
186  su.runSql("GRANT salesDept TO foo;");
187 
188  HeavyDBTestClient dba =
189  HeavyDBTestClient.getClient("localhost", 6274, "omnisci", "dba", "password");
190  HeavyDBTestClient jason = HeavyDBTestClient.getClient(
191  "localhost", 6274, "omnisci", "jason", "password");
192  HeavyDBTestClient bob =
193  HeavyDBTestClient.getClient("localhost", 6274, "omnisci", "bob", "password");
194  HeavyDBTestClient foo =
195  HeavyDBTestClient.getClient("localhost", 6274, "omnisci", "foo", "password");
196 
197  su.runSql("GRANT CREATE DASHBOARD ON DATABASE omnisci TO jason;");
198 
199  shouldThrowException("bob should not be able to create dashboards",
200  () -> bob.create_dashboard("for_bob"));
201  shouldThrowException("foo should not be able to create dashboards",
202  () -> foo.create_dashboard("for_bob"));
203 
204  int for_bob = jason.create_dashboard("for_bob");
205  int for_sales = jason.create_dashboard("for_sales");
206  int for_all = jason.create_dashboard("for_all");
207 
208  HeavyDBAsserts.assertEqual(0, bob.get_dashboards().size());
209  HeavyDBAsserts.assertEqual(0, foo.get_dashboards().size());
210 
211  HeavyDBTestClient granter = jason;
212  granter.runSql("GRANT VIEW ON DASHBOARD " + for_bob + " TO bob;");
213  granter.runSql("GRANT VIEW ON DASHBOARD " + for_sales + " TO salesDept;");
214  granter.runSql("GRANT VIEW ON DASHBOARD " + for_all + " TO bob;");
215  granter.runSql("GRANT VIEW ON DASHBOARD " + for_all + " TO salesDept;");
216 
217  HeavyDBAsserts.assertEqual(2, bob.get_dashboards().size());
218  HeavyDBAsserts.assertEqual(2, foo.get_dashboards().size());
219 
220  shouldThrowException("bob should not be able to access for_sales",
221  () -> bob.get_dashboard(for_sales));
222  shouldThrowException(
223  "foo should not be able to access for_bob", () -> foo.get_dashboard(for_bob));
224 
225  HeavyDBAsserts.assertEqual("for_bob", bob.get_dashboard(for_bob));
226  HeavyDBAsserts.assertEqual("for_all", bob.get_dashboard(for_all));
227 
228  HeavyDBAsserts.assertEqual("for_sales", foo.get_dashboard(for_sales));
229  HeavyDBAsserts.assertEqual("for_all", foo.get_dashboard(for_all));
230 
231  // check update
232  shouldThrowException("bob can not edit for_bob",
233  () -> bob.replace_dashboard(for_bob, "for_bob2", "jason"));
234  shouldThrowException("foo can not edit for_bob",
235  () -> foo.replace_dashboard(for_bob, "for_bob2", "jason"));
236  shouldThrowException("bob can not edit for_sales",
237  () -> bob.replace_dashboard(for_sales, "for_sales2", "jason"));
238  shouldThrowException("foo can not edit for_sales",
239  () -> foo.replace_dashboard(for_sales, "for_sales2", "jason"));
240 
241  jason.runSql("GRANT EDIT ON DASHBOARD " + for_bob + " TO bob;");
242  jason.runSql("GRANT EDIT ON DASHBOARD " + for_sales + " TO salesDept;");
243  shouldThrowException("foo can not edit for_bob",
244  () -> foo.replace_dashboard(for_bob, "for_bob2", "jason"));
245  shouldThrowException("bob can not edit for_sales",
246  () -> bob.replace_dashboard(for_sales, "for_sales2", "jason"));
247 
248  jason.replace_dashboard(for_all, "for_all2", "jason");
249  bob.replace_dashboard(for_bob, "for_bob2", "jason");
250  foo.replace_dashboard(for_sales, "for_sales2", "jason");
251 
252  HeavyDBAsserts.assertEqual("for_bob2", bob.get_dashboard(for_bob));
253  HeavyDBAsserts.assertEqual("for_all2", bob.get_dashboard(for_all));
254 
255  HeavyDBAsserts.assertEqual("for_sales2", foo.get_dashboard(for_sales));
256  HeavyDBAsserts.assertEqual("for_all2", foo.get_dashboard(for_all));
257 
258  shouldThrowException(
259  "foo can not delete for_bob", () -> foo.delete_dashboard(for_bob));
260  shouldThrowException(
261  "foo can not delete for_sales", () -> foo.delete_dashboard(for_sales));
262  shouldThrowException(
263  "foo can not delete for_all", () -> foo.delete_dashboard(for_all));
264 
265  shouldThrowException(
266  "bob can not delete for_bob", () -> bob.delete_dashboard(for_bob));
267  shouldThrowException(
268  "bob can not delete for_sales", () -> bob.delete_dashboard(for_sales));
269  shouldThrowException(
270  "bob can not delete for_all", () -> bob.delete_dashboard(for_all));
271 
272  jason.delete_dashboard(for_bob);
273 
274  HeavyDBAsserts.assertEqual(1, bob.get_dashboards().size());
275  HeavyDBAsserts.assertEqual(2, foo.get_dashboards().size());
276  HeavyDBAsserts.assertEqual("for_all2", bob.get_dashboard(for_all));
277  HeavyDBAsserts.assertEqual("for_sales2", foo.get_dashboard(for_sales));
278  HeavyDBAsserts.assertEqual("for_all2", foo.get_dashboard(for_all));
279 
280  jason.delete_dashboard(for_all);
281 
282  HeavyDBAsserts.assertEqual(0, bob.get_dashboards().size());
283  HeavyDBAsserts.assertEqual(1, foo.get_dashboards().size());
284  HeavyDBAsserts.assertEqual("for_sales2", foo.get_dashboard(for_sales));
285 
286  jason.delete_dashboard(for_sales);
287 
288  HeavyDBAsserts.assertEqual(0, bob.get_dashboards().size());
289  HeavyDBAsserts.assertEqual(0, foo.get_dashboards().size());
290 
291  su.runSql("DROP USER foo;");
292  su.runSql("DROP ROLE salesDept;");
293  su.runSql("DROP USER bob;");
294  su.runSql("DROP USER jason;");
295  su.runSql("DROP USER dba;");
296  }
297 }
static void main(String[] args)