Skip to content

Commit 4530623

Browse files
committed
Credentials: roles sample
1 parent 3a01294 commit 4530623

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
local fio = require('fio')
2+
local server = require('luatest.server')
3+
local t = require('luatest')
4+
local g = t.group()
5+
g.before_all(function(cg)
6+
cg.server = server:new {
7+
box_cfg = {},
8+
workdir = fio.cwd() .. '/tmp'
9+
}
10+
cg.server:start()
11+
cg.server:exec(function()
12+
box.schema.space.create('writers')
13+
box.space.writers:format({
14+
{ name = 'id', type = 'unsigned' },
15+
{ name = 'name', type = 'string' }
16+
})
17+
box.space.writers:create_index('primary', { parts = { 'id' } })
18+
19+
box.schema.space.create('books')
20+
box.space.books:format({
21+
{ name = 'id', type = 'unsigned' },
22+
{ name = 'title', type = 'string' },
23+
{ name = 'author_id', foreign_key = { space = 'writers', field = 'id' } },
24+
})
25+
box.space.books:create_index('primary', { parts = { 'id' } })
26+
27+
box.space.writers:insert { 1, 'Leo Tolstoy' }
28+
box.space.writers:insert { 2, 'Fyodor Dostoevsky' }
29+
box.space.writers:insert { 3, 'Alexander Pushkin' }
30+
31+
box.space.books:insert { 1, 'War and Peace', 1 }
32+
box.space.books:insert { 2, 'Anna Karenina', 1 }
33+
box.space.books:insert { 3, 'Resurrection', 1 }
34+
box.space.books:insert { 4, 'Crime and Punishment', 2 }
35+
box.space.books:insert { 5, 'The Idiot', 2 }
36+
box.space.books:insert { 6, 'The Brothers Karamazov', 2 }
37+
box.space.books:insert { 7, 'Eugene Onegin', 3 }
38+
box.space.books:insert { 8, 'The Captain\'s Daughter', 3 }
39+
box.space.books:insert { 9, 'Boris Godunov', 3 }
40+
box.space.books:insert { 10, 'Ruslan and Ludmila', 3 }
41+
end)
42+
end)
43+
44+
g.after_all(function(cg)
45+
cg.server:drop()
46+
fio.rmtree(cg.server.workdir)
47+
end)
48+
49+
g.test_role_granted_revoked = function(cg)
50+
cg.server:exec(function()
51+
box.schema.user.create('testuser', { password = 'foobar' })
52+
53+
-- Create roles --
54+
box.schema.role.create('books_space_manager')
55+
box.schema.role.create('writers_space_reader')
56+
-- End: Create roles --
57+
58+
-- Grant read/write privileges to a role --
59+
box.schema.role.grant('books_space_manager', 'read,write', 'space', 'books')
60+
-- Grant write privileges to a role --
61+
box.schema.role.grant('writers_space_reader', 'read', 'space', 'writers')
62+
-- End: Grant privileges to roles --
63+
64+
-- Grant a role to a role --
65+
box.schema.role.create('all_spaces_manager')
66+
box.schema.role.grant('all_spaces_manager', 'books_space_manager')
67+
box.schema.role.grant('all_spaces_manager', 'writers_space_reader')
68+
-- End: Grant a role to a role --
69+
70+
-- Grant a role to a user --
71+
box.schema.user.grant('testuser', 'books_space_manager')
72+
box.schema.user.grant('testuser', 'writers_space_reader')
73+
-- End: Grant a role to a user --
74+
75+
-- Test removing a tuple from 'writers' --
76+
box.session.su('testuser')
77+
local _, delete_writer_error = pcall(function()
78+
box.space.writers:delete(3)
79+
end)
80+
t.assert_equals(delete_writer_error:unpack().message, "Write access to space 'writers' is denied for user 'testuser'")
81+
box.session.su('admin')
82+
83+
-- Revoking a role from a user --
84+
box.schema.user.revoke('testuser', 'execute', 'role', 'writers_space_reader')
85+
-- End: Revoking a role from a user --
86+
87+
-- Test selecting data from 'writers' --
88+
box.session.su('testuser')
89+
local _, select_writer_error = pcall(function()
90+
box.space.writers:select(3)
91+
end)
92+
t.assert_equals(select_writer_error:unpack().message, "Read access to space 'writers' is denied for user 'testuser'")
93+
box.session.su('admin')
94+
95+
-- Dropping a role --
96+
box.schema.role.drop('writers_space_reader')
97+
-- End: Dropping a role --
98+
99+
-- Test roles exist --
100+
t.assert_equals(box.schema.role.exists('books_space_manager'), true)
101+
t.assert_equals(box.schema.role.exists('all_spaces_manager'), true)
102+
t.assert_equals(box.schema.role.exists('writers_space_reader'), false)
103+
end)
104+
end

0 commit comments

Comments
 (0)