Skip to content

Commit

Permalink
Add ACL tests and update tests
Browse files Browse the repository at this point in the history
Test password query

ACL/acls basic tests

Tests for /mc

Test ACL removal

Remove print from channel ban test
  • Loading branch information
S-S-X committed Jan 3, 2024
1 parent d52798c commit bbe8e49
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 16 deletions.
31 changes: 21 additions & 10 deletions spec/fixtures/minetest.conf
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
# Basic configuration
beerchat.colorize_channels = *
beerchat.moderator_channel_name = mod

# Plugin configuration

beerchat.enable_alias = true
beerchat.moderator_channel_name = mod

beerchat.enable_announce = true
# Enable plugins

beerchat.enable_mute = true
beerchat.enable_me = true
beerchat.enable_hash = true
beerchat.enable_acl = true
beerchat.enable_pm = true
beerchat.enable_whisper = true
beerchat.enable_jail = true
beerchat.enable_ban = true

beerchat.enable_remote_mute = true
beerchat.enable_cleaner = true
beerchat.enable_override = true
beerchat.enable_colorize = true
beerchat.enable_announce = true
beerchat.enable_force2channel = true
beerchat.enable_password = true
beerchat.enable_event-logging = true
beerchat.enable_alias = true

beerchat.enable_jail = true
beerchat.jail.channel_name = jailchannel
# Plugin configuration

beerchat.enable_remote_mute = true
beerchat.jail.channel_name = jailchannel
beerchat.colorize_channels = *

# Web relay

secure.http_mods = beerchat
beerchat.matterbridge_url = http://matterbridge:4242
beerchat.matterbridge_token = mytoken
27 changes: 22 additions & 5 deletions spec/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ describe("Mod initialization", function()

end)

describe("Chatting", function()
describe("Core functionality", function()

local M = function(s) return require("luassert.match").matches(s) end
local SX = Player("SX", { shout = 1 })

setup(function()
beerchat.channels["testchannel"] = { owner = "beerholder", color = beerchat.default_channel_color }
beerchat.channels["testchannel2"] = { owner = "SX", color = beerchat.default_channel_color }
mineunit:execute_on_joinplayer(SX)
end)

Expand All @@ -36,15 +38,30 @@ describe("Chatting", function()
assert.not_nil(beerchat.channels["foo"])
end)

it("switches channels", function()
SX:send_chat_message("#foo")
assert.equals("foo", SX:get_meta():get_string("beerchat:current_channel"))
SX:send_chat_message("Everyone ignore me, this is just a test")
it("joins channel", function()
assert.is_nil(beerchat.playersChannels["SX"]["testchannel"])
SX:send_chat_message("/jc testchannel")
assert.not_nil(beerchat.playersChannels["SX"]["testchannel"])
end)

it("deletes channel", function()
SX:send_chat_message("/dc foo")
assert.is_nil(beerchat.channels["foo"])
end)

it("lists channels", function()
SX:send_chat_message("/jc testchannel")
SX:send_chat_message("/jc testchannel2")
spy.on(minetest, "chat_send_player")
SX:send_chat_message("/mc")
assert.spy(minetest.chat_send_player).called_with("SX", M("testchannel.+testchannel"))
end)

it("lists channel information", function()
SX:send_chat_message("/jc testchannel")
spy.on(minetest, "chat_send_player")
SX:send_chat_message("/mc testchannel")
assert.spy(minetest.chat_send_player).called_with("SX", M("beerholder"))
end)

end)
90 changes: 90 additions & 0 deletions spec/plugin_acl_acls_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require("mineunit")

mineunit("core")
mineunit("player")
mineunit("server")

describe("ACL/acls", function()

-- Load bare acls.lua, assuming no dependencies other than channels table.
_G.beerchat = {
channels = {
TEST = {}
}
}

local acls
local SX = Player("SX", { shout = 1, fast = 1 })
setup(function() mineunit:execute_on_joinplayer(SX) end)
teardown(function() mineunit:execute_on_leaveplayer(SX) end)
before_each(function() acls = sourcefile("plugin/acl/acls")(nil, function() end) end)

it("acls:set_role", function()
acls:set_role("TEST", "SX", "read")
assert.same(acls.data.TEST, { SX = "read" })
end)

it("acls:set_role privilege", function()
acls:set_role("TEST", "$fast", "write")
assert.same(acls.data.TEST, { ["$fast"] = "write" })
end)

it("acls:get_role", function()
assert.is_nil(acls:get_role("TEST", "SX"))
end)

it("acls:get_role non player", function()
assert.is_nil(acls:get_role("TEST", "?"))
end)

it("acls:get_privilege_role", function()
assert.is_nil(acls:get_privilege_role("TEST", "SX"))
end)

it("acls:get_privilege_role non player", function()
assert.is_nil(acls:get_privilege_role("TEST", "?"))
end)

it("acls:write_storage", function()
acls:write_storage()
end)

it("acls:check_access", function()
assert.is_nil(acls:check_access("TEST", "SX"))
end)

it("returns correct player role", function()
acls:set_role("TEST", "SX", "deny")
acls:set_role("TEST", "SX", "manager")
assert.equals(acls:get_role("TEST", "SX"), "manager")
acls:set_role("TEST", "SX", "deny")
assert.equals(acls:get_role("TEST", "SX"), "deny")
end)

it("returns correct privilege role", function()
acls:set_role("TEST", "$fast", "deny")
acls:set_role("TEST", "$fast", "manager")
assert.equals(acls:get_role("TEST", "SX"), "manager")
acls:set_role("TEST", "$fast", "deny")
assert.equals(acls:get_role("TEST", "SX"), "deny")
end)

it("acls:check_access deny", function()
acls:set_role("TEST", "$fast", "deny")
assert.is_false(acls:check_access("TEST", "SX"))
assert.is_false(acls:check_access("TEST", "SX", "read"))
assert.is_false(acls:check_access("TEST", "SX", "write"))
assert.is_false(acls:check_access("TEST", "SX", "manager"))
assert.is_false(acls:check_access("TEST", "SX", "owner"))
end)

it("acls:check_access write", function()
acls:set_role("TEST", "$fast", "write")
assert.is_nil(acls:check_access("TEST", "SX"))
assert.is_nil(acls:check_access("TEST", "SX", "read"))
assert.is_nil(acls:check_access("TEST", "SX", "write"))
assert.is_false(acls:check_access("TEST", "SX", "manager"))
assert.is_false(acls:check_access("TEST", "SX", "owner"))
end)

end)
139 changes: 139 additions & 0 deletions spec/plugin_acl_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
require("mineunit")

mineunit("core")
mineunit("player")
mineunit("server")

sourcefile("init")

describe("ACL", function()

local M = function(s) return require("luassert.match").matches(s) end
local ANY = require("luassert.match")._
assert:register("matcher", "has_channel", function(_, args)
return function(msg)
return type(msg) == "table" and msg.channel == args[1]
end
end)
local CHANNEL = require("luassert.match").has_channel

local SX = Player("SX", { shout = 1 })
local Sam = Player("Sam", { shout = 1 })

setup(function()
mineunit:execute_on_joinplayer(SX)
mineunit:execute_on_joinplayer(Sam)
-- Test channels
beerchat.channels["acl-password"] = { owner = "SX", color = beerchat.default_channel_color }
beerchat.channels["acl-default-role"] = { owner = "SX", color = beerchat.default_channel_color }
beerchat.channels["acl-owner-role"] = { owner = "SX", color = beerchat.default_channel_color }
beerchat.channels["acl-manager-role"] = { owner = "SX", color = beerchat.default_channel_color }
beerchat.channels["acl-write-role"] = { owner = "SX", color = beerchat.default_channel_color }
beerchat.channels["acl-read-role"] = { owner = "SX", color = beerchat.default_channel_color }
beerchat.channels["acl-update-role"] = { owner = "SX", color = beerchat.default_channel_color }
beerchat.channels["acl-delete-role"] = { owner = "SX", color = beerchat.default_channel_color }
beerchat.channels["acl-chat"] = { owner = "SX", color = beerchat.default_channel_color }
end)

before_each(function()
beerchat.set_player_channel("SX", "main")
beerchat.set_player_channel("Sam", "main")
end)

teardown(function()
mineunit:execute_on_leaveplayer(Sam)
mineunit:execute_on_leaveplayer(SX)
end)

it("checks password", function()
SX:send_chat_message("/cc #acl-password,qwerty")
spy.on(minetest, "chat_send_player")
-- Initiate password protected join, it should ask for password and should not join channel
Sam:send_chat_message("/jc #acl-password")
assert.spy(minetest.chat_send_player).called_with("Sam", M(".+assword.+lease.+assword"))
assert.spy(minetest.chat_send_player).not_called_with("SX", ANY)
assert.is_nil(beerchat.playersChannels["Sam"]["acl-password"])
-- Password is not visible to other players
Sam:send_chat_message("qwerty")
assert.spy(minetest.chat_send_player).not_called_with("SX", ANY)
assert.not_nil(beerchat.playersChannels["Sam"]["acl-password"])
-- Next messages will be visible to other players
Sam:send_chat_message("qwerty")
assert.spy(minetest.chat_send_player).called_with("SX", ANY)
end)

it("/invite_channel sets default role", function()
spy.on(minetest, "chat_send_player")
SX:send_chat_message("/invite_channel #acl-default-role Sam")
assert.spy(minetest.chat_send_player).called_with("SX", M("Invite sent.+Sam"))
assert.spy(minetest.chat_send_player).called_with("Sam", M("SX.+invite.+join.+channel"))
end)

it("/invite_channel sets owner role", function()
spy.on(minetest, "chat_send_player")
SX:send_chat_message("/invite_channel #acl-owner-role Sam owner")
assert.spy(minetest.chat_send_player).called_with("SX", M("Invite sent.+Sam"))
assert.spy(minetest.chat_send_player).called_with("Sam", M("SX.+invite.+join.+channel"))
end)

it("/invite_channel sets manager role", function()
spy.on(minetest, "chat_send_player")
SX:send_chat_message("/invite_channel #acl-manager-role Sam manager")
assert.spy(minetest.chat_send_player).called_with("SX", M("Invite sent.+Sam"))
assert.spy(minetest.chat_send_player).called_with("Sam", M("SX.+invite.+join.+channel"))
end)

it("/invite_channel sets write role", function()
spy.on(minetest, "chat_send_player")
SX:send_chat_message("/invite_channel #acl-write-role Sam write")
assert.spy(minetest.chat_send_player).called_with("SX", M("Invite sent.+Sam"))
assert.spy(minetest.chat_send_player).called_with("Sam", M("SX.+invite.+join.+channel"))
end)

it("/invite_channel sets read role", function()
spy.on(minetest, "chat_send_player")
SX:send_chat_message("/invite_channel #acl-read-role Sam read")
assert.spy(minetest.chat_send_player).called_with("SX", M("Invite sent.+Sam"))
assert.spy(minetest.chat_send_player).called_with("Sam", M("SX.+invite.+join.+channel"))
end)

it("/invite_channel updates role", function()
SX:send_chat_message("/invite_channel #acl-update-role Sam read")
spy.on(minetest, "chat_send_player")
SX:send_chat_message("/invite_channel #acl-update-role Sam manager")
assert.spy(minetest.chat_send_player).not_called_with("Sam", ANY)
assert.spy(minetest.chat_send_player).called_with("SX", M("update.+manager"))
end)

it("/invite_channel removes role", function()
SX:send_chat_message("/invite_channel #acl-delete-role Sam manager")
spy.on(minetest, "chat_send_player")
SX:send_chat_message("/invite_channel #acl-delete-role -d Sam")
assert.spy(minetest.chat_send_player).not_called_with("Sam", ANY)
assert.spy(minetest.chat_send_player).called_with("SX", M("emoved.+elete"))
end)

it("read role allows reading messages", function()
beerchat.set_player_channel("SX", "acl-chat")
beerchat.set_player_channel("Sam", "acl-chat")
SX:send_chat_message("/channel_acl #acl-chat Sam read")
spy.on(minetest, "chat_send_player")
SX:send_chat_message("Test message")
-- Channel message allowed and delivered
assert.spy(minetest.chat_send_player).called_with("Sam", M("Test message"))
assert.spy(minetest.chat_send_player).called_with("SX", M("Test message"))
end)

it("read role disallows sending messages", function()
beerchat.set_player_channel("SX", "acl-chat")
beerchat.set_player_channel("Sam", "acl-chat")
SX:send_chat_message("/channel_acl #acl-chat Sam read")
spy.on(minetest, "chat_send_player")
Sam:send_chat_message("Test message")
-- Channel message disallowed and player informed
assert.spy(minetest.chat_send_player).not_called_with("SX", ANY)
assert.spy(minetest.chat_send_player).not_called_with("Sam", M("Test message"))
assert.spy(minetest.chat_send_player).called_with("Sam", ANY)
end)

end)
2 changes: 1 addition & 1 deletion spec/plugin_ban_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("channel ban", function()
SX:send_chat_message("/channel_ban "..XX_name)
spy.on(beerchat, "send_on_channel")
spy.on(beerchat, "execute_callbacks")
spy.on(minetest, "chat_send_player") beerchat.register_callback("on_send_on_channel", print)
spy.on(minetest, "chat_send_player")
SX:send_chat_message("test")
assert.spy(beerchat.send_on_channel).was.called()
assert.spy(beerchat.execute_callbacks).was.called_with("on_send_on_channel", "SX", ANY, ANY)
Expand Down

0 comments on commit bbe8e49

Please sign in to comment.