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