system sh/stop_dnodes.sh
system sh/deploy.sh -n dnode1 -i 1
system sh/exec.sh -n dnode1 -s start
sql connect

print =============== create db
sql create database d1 vgroups 1;
sql use d1
sql create table d1_stb (ts timestamp, i int) tags (j int)
sql create topic d1_topic_1 as select ts, i from d1_stb

sql create database d2 vgroups 1;
sql create database d3 vgroups 1;
sql select * from information_schema.ins_databases
if $rows != 5 then 
  return -1
endi

print =============== create users
sql create user user1 PASS 'user1'
sql create user user2 PASS 'user2'
sql select * from information_schema.ins_users
if $rows != 3 then 
  return -1
endi

print =============== test read 
sql_error GRANT read ON d1.* to a;
sql_error GRANT read ON d0.* to user1;

sql GRANT read ON d1.* to user1;
sql GRANT read ON d2.* to user1;
sql GRANT read ON *.* to user1;

sql REVOKE read ON d1.* from user1;
sql REVOKE read ON d2.* from user1;
sql REVOKE read ON *.* from user1;

print =============== test write 
sql_error GRANT write ON d1.* to a;
sql_error GRANT write ON d0.* to user1;

sql GRANT write ON d1.* to user1;
sql GRANT write ON d2.* to user1;
sql GRANT write ON *.* to user1;

sql REVOKE write ON d1.* from user1;
sql REVOKE write ON d2.* from user1;
sql REVOKE write ON *.* from user1;

print =============== test all 
sql_error GRANT all ON d1.* to a;
sql_error GRANT all ON d0.* to user1;

sql GRANT all ON d1.* to user1;
sql GRANT all ON d2.* to user1;
sql GRANT all ON *.* to user1;

sql REVOKE all ON d1.* from user1;
sql REVOKE all ON d2.* from user1;
sql REVOKE all ON *.* from user1;

print =============== test read write 
sql_error GRANT read,write ON d1.* to a;
sql_error GRANT read,write ON d0.* to user1;

sql GRANT read,write ON d1.* to user1;
sql GRANT read,write ON d2.* to user1;
sql GRANT read,write ON *.* to user1;

sql REVOKE read,write ON d1.* from user1;
sql REVOKE read,write ON d2.* from user1;
sql REVOKE read,write ON *.* from user1;

print =============== create users
sql create user u1 PASS 'taosdata'
sql select * from information_schema.ins_users
if $rows != 4 then 
  return -1
endi

sql GRANT read ON d1.* to u1;
sql GRANT write ON d2.* to u1;

print =============== re connect
print user u1 login
sql close
sql connect u1

sql_error drop database d1;
sql_error drop database d2;

sql_error create stable d1.st (ts timestamp, i int) tags (j int)
sql use d2
sql create table d2_stb (ts timestamp, i int) tags (j int)

# Insufficient privilege for operation
sql_error create topic d2_topic_1 as select ts, i from d2_stb

sql use d1
# Insufficient privilege for operation
sql_error drop topic d1_topic_1
sql create topic d1_topic_2 as select ts, i from d1_stb

sql drop topic d1_topic_2

system sh/exec.sh -n dnode1 -s stop -x SIGINT
