|
| 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_each(function(cg) |
| 45 | + cg.server:exec(function() |
| 46 | + if box.schema.user.exists('testuser') then |
| 47 | + box.schema.user.drop('testuser') |
| 48 | + end |
| 49 | + end) |
| 50 | +end) |
| 51 | + |
| 52 | +g.after_all(function(cg) |
| 53 | + cg.server:drop() |
| 54 | + fio.rmtree(cg.server.workdir) |
| 55 | +end) |
| 56 | + |
| 57 | +g.test_user_without_password_created = function(cg) |
| 58 | + cg.server:exec(function() |
| 59 | + -- Create a user without a password -- |
| 60 | + box.schema.user.create('testuser') |
| 61 | + -- End: Create a user without a password -- |
| 62 | + t.assert_equals(box.space._user.index.name:select { 'testuser' }[1][5]['chap-sha1'], nil) |
| 63 | + end) |
| 64 | +end |
| 65 | + |
| 66 | +g.test_user_with_password_created = function(cg) |
| 67 | + cg.server:exec(function() |
| 68 | + -- Create a user with a password -- |
| 69 | + box.schema.user.create('testuser', { password = 'foobar' }) |
| 70 | + -- End: Create a user with a password -- |
| 71 | + t.assert_equals(box.space._user.index.name:select { 'testuser' }[1][5]['chap-sha1'], 'm1ADQ7xS4pERcutSrlz0hHYExuU=') |
| 72 | + end) |
| 73 | +end |
| 74 | + |
| 75 | +g.test_current_user_password_set = function(cg) |
| 76 | + cg.server:exec(function() |
| 77 | + box.session.su('admin') |
| 78 | + -- Set a password for the current user -- |
| 79 | + box.schema.user.passwd('foobar') |
| 80 | + -- End: Set a password for the current user -- |
| 81 | + t.assert_equals(box.space._user.index.name:select { 'admin' }[1][5]['chap-sha1'], 'm1ADQ7xS4pERcutSrlz0hHYExuU=') |
| 82 | + end) |
| 83 | +end |
| 84 | + |
| 85 | +g.test_specified_user_password_set = function(cg) |
| 86 | + cg.server:exec(function() |
| 87 | + box.schema.user.create('testuser') |
| 88 | + -- Set a password for the specified user -- |
| 89 | + box.schema.user.passwd('testuser', 'foobar') |
| 90 | + -- End: Set a password for the specified user -- |
| 91 | + t.assert_equals(box.space._user.index.name:select { 'testuser' }[1][5]['chap-sha1'], 'm1ADQ7xS4pERcutSrlz0hHYExuU=') |
| 92 | + end) |
| 93 | +end |
| 94 | + |
| 95 | +g.test_grant_revoke_privileges_user = function(cg) |
| 96 | + cg.server:exec(function() |
| 97 | + box.schema.user.create('testuser', { password = 'foobar' }) |
| 98 | + box.schema.user.grant('testuser', 'execute', 'universe') |
| 99 | + -- Grant privileges to the specified user -- |
| 100 | + box.schema.user.grant('testuser', 'read', 'space', 'writers') |
| 101 | + box.schema.user.grant('testuser', 'read,write', 'space', 'books') |
| 102 | + -- End: Grant privileges to the specified user -- |
| 103 | + box.session.su('testuser') |
| 104 | + local _, delete_writer_error = pcall(function() |
| 105 | + box.space.writers:delete(3) |
| 106 | + end) |
| 107 | + t.assert_equals(delete_writer_error:unpack().message, "Write access to space 'writers' is denied for user 'testuser'") |
| 108 | + |
| 109 | + box.session.su('admin') |
| 110 | + -- Revoke space reading -- |
| 111 | + box.schema.user.revoke('testuser', 'write', 'space', 'books') |
| 112 | + -- End: Revoke space reading -- |
| 113 | + box.session.su('testuser') |
| 114 | + local _, delete_book_error = pcall(function() |
| 115 | + box.space.books:delete(10) |
| 116 | + end) |
| 117 | + t.assert_equals(delete_book_error:unpack().message, "Write access to space 'books' is denied for user 'testuser'") |
| 118 | + |
| 119 | + box.session.su('admin') |
| 120 | + -- Revoke session -- |
| 121 | + box.schema.user.revoke('testuser', 'session', 'universe') |
| 122 | + -- End: Revoke session -- |
| 123 | + local _, change_user_error = pcall(function() |
| 124 | + box.session.su('testuser') |
| 125 | + end) |
| 126 | + t.assert_equals(change_user_error:unpack().message, "Session access to universe '' is denied for user 'testuser'") |
| 127 | + end) |
| 128 | +end |
| 129 | + |
| 130 | +g.test_user_dropped = function(cg) |
| 131 | + cg.server:exec(function() |
| 132 | + box.schema.user.create('testuser') |
| 133 | + -- Drop a user -- |
| 134 | + box.schema.user.drop('testuser') |
| 135 | + -- End: Drop a user -- |
| 136 | + t.assert_equals(box.schema.user.exists('testuser'), false) |
| 137 | + end) |
| 138 | +end |
0 commit comments