Skip to content

Commit

Permalink
actions: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Damm committed Sep 27, 2018
1 parent 913eb32 commit d53b24f
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions spec/actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require 'busted.runner'()
local function init_mock(options)
-- mock global variable 'KSR'
local ksr_mock = {
err = function(msg) end,
maxfwd = {
process_maxfwd = function(counter)
if options.maxfwdReached then
Expand All @@ -12,6 +13,18 @@ local function init_mock(options)
end
end,
},
pv = {
get = function(key)
if key == "$si" then return options.si or "1.2.3.4"
elseif key == "$sp" then return options.sp or "55555" end
end,
},
registrar = {
save = function(table, flags) if options.location_saved then return 1 else return -1 end end,
},
sanity = {
sanity_check = function(flags, uri_checks) if options.is_sane then return 1 else return -1 end end,
},
}
_G["KSR"] = mock(ksr_mock)

Expand All @@ -20,8 +33,16 @@ local function init_mock(options)
}
_G["core"] = mock(core_mock)

local message_mock = {
is_options = function() return options.is_options end,
is_request_to_local_request_domain = function() return options.is_request_to_local_request_domain end,
has_empty_request_user = function() return options.has_empty_request_user end,
}
_G["message"] = mock(message_mock)

local reply_mock = {
stateless = function(cause, reason) end,
with_stateless_error_and_exit = function() end,
}
_G["reply"] = mock(reply_mock)
end
Expand All @@ -44,3 +65,56 @@ describe("Check max forwards -> ", function()
end)
end)

describe("Save Registration -> ", function()
it("Saving Registration was successful", function()
init_mock{ location_saved = true }
actions.save_register()
assert.spy(reply.with_stateless_error_and_exit).was.called(0)
end)
it("Saving Registration was unsuccessful", function()
init_mock{ location_saved = false }
actions.save_register()
assert.spy(reply.with_stateless_error_and_exit).was.called()
end)
end)

describe("Handle OPTIONS -> ", function()
it("Message is an OPTIONS request, request user is empty, directed to local domain", function()
init_mock{ is_options = true, has_empty_request_user = true, is_request_to_local_request_domain = true }
actions.handle_options()
assert.spy(reply.stateless).was.called_with(200, "Keepalive")
assert.spy(core.exit).was.called()
end)
it("Message is an OPTIONS request but request user is not empty", function()
init_mock{ is_options = true, has_empty_request_user = false, is_request_to_local_request_domain = true }
actions.handle_options()
assert.spy(reply.stateless).was.called(0)
end)
it("Message is an OPTIONS request but not directed to the local domain", function()
init_mock{ is_options = true, has_empty_request_user = true, is_request_to_local_request_domain = false }
actions.handle_options()
assert.spy(reply.stateless).was.called(0)
end)
it("Message is NOT an OPTIONS request", function()
init_mock{ is_options = false, has_empty_request_user = true, is_request_to_local_request_domain = true }
actions.handle_options()
assert.spy(reply.stateless).was.called(0)
end)
end)

describe("Sanity Check -> ", function()
it("Sanity Check passed", function()
init_mock{ is_sane = true }
actions.check_sanity()
assert.spy(KSR.sanity.sanity_check).was.called()
assert.spy(KSR.err).was.called(0)
assert.spy(core.exit).was.called(0)
end)
it("Sanity Check passed", function()
init_mock{ is_sane = false }
actions.check_sanity()
assert.spy(KSR.sanity.sanity_check).was.called()
assert.spy(KSR.err).was.called()
assert.spy(core.exit).was.called()
end)
end)

0 comments on commit d53b24f

Please sign in to comment.