bsw@1071: -- Part of test case for ldap4lqfb bsw@1071: bsw@1071: function ldap.test() bsw@1071: bsw@1071: local global_count = 0 bsw@1071: local global_failed = 0 bsw@1071: bsw@1071: local function checkMember(uid, login, name, units_with_voting_priv, units_with_polling_priv) bsw@1071: bsw@1071: local failed = false bsw@1071: bsw@1071: local function testError(err, expected, got) bsw@1071: failed = true bsw@1071: global_failed = global_failed + 1 bsw@1071: if expected then bsw@1071: print ("[" .. uid .. "] FAILED: " .. err .. " (expected: '" .. expected .. "' but got '" .. tostring(got) .. "')") bsw@1071: else bsw@1071: print ("[" .. uid .. "] FAILED: " .. err) bsw@1071: end bsw@1071: end bsw@1071: bsw@1071: local function testOk(test, expected) bsw@1071: if test then bsw@1071: print ("[" .. uid .. "] " .. test .. " ok ('" .. expected .. "')") bsw@1071: else bsw@1071: print ("[" .. uid .. "] success") bsw@1071: end bsw@1071: end bsw@1071: bsw@1071: local function test(field, expected, value) bsw@1071: global_count = global_count + 1 bsw@1071: if expected ~= value then bsw@1071: testError(field, expected, value) bsw@1071: return bsw@1071: end bsw@1071: testOk(field, expected) bsw@1071: end bsw@1071: bsw@1071: local members = Member:new_selector() bsw@1074: :add_field{ "authority_login" } bsw@1074: :add_where{ "authority = ? AND authority_uid = ?", "ldap", uid } bsw@1071: :exec() bsw@1071: bsw@1071: if #members < 1 then bsw@1071: testError("Member not found in DB") bsw@1071: return bsw@1071: end bsw@1071: bsw@1071: if #members > 1 then bsw@1071: testError("Found more than one DB entry") bsw@1071: return bsw@1071: end bsw@1071: bsw@1071: local member = members[1] bsw@1071: bsw@1071: if login == nil then bsw@1071: if not member.locked then bsw@1071: testError("Member not locked") bsw@1071: else bsw@1071: testOk("Member is locked", "true") bsw@1071: end bsw@1071: return bsw@1071: end bsw@1071: bsw@1074: test("login", login, member.authority_login) bsw@1071: test("name", name, member.name) bsw@1071: bsw@1071: for i, unit_id in ipairs(units_with_voting_priv) do bsw@1071: global_count = global_count + 1 bsw@1071: local privilege = Privilege:by_pk(unit_id, member.id) bsw@1071: if privilege and privilege.voting_right then bsw@1071: testOk("voting_right", unit_id) bsw@1071: else bsw@1071: testError("voting_right", unit_id, "") bsw@1071: end bsw@1071: end bsw@1071: bsw@1071: local privileges_selector = Privilege:new_selector() bsw@1071: :add_where{ "member_id = ?", member.id } bsw@1071: :add_where("voting_right = true") bsw@1071: if #units_with_voting_priv > 0 then bsw@1071: local privileges = privileges_selector:add_where{ "unit_id NOT IN ($)", units_with_voting_priv } bsw@1071: end bsw@1071: local privileges = privileges_selector:exec() bsw@1071: bsw@1071: if #privileges > 0 then bsw@1071: testError("voting_right", "count: 0", "count: " .. #privileges) bsw@1071: else bsw@1071: testOk("voting_right", "count: 0") bsw@1071: end bsw@1071: bsw@1071: for i, unit_id in ipairs(units_with_polling_priv) do bsw@1071: global_count = global_count + 1 bsw@1071: local privilege = Privilege:by_pk(unit_id, member.id) bsw@1071: if privilege and privilege.polling_right then bsw@1071: testOk("polling_right", unit_id) bsw@1071: else bsw@1071: testError("polling_right", unit_id, "") bsw@1071: end bsw@1071: end bsw@1071: bsw@1071: local privileges_selector = Privilege:new_selector() bsw@1071: :add_where{ "member_id = ?", member.id } bsw@1071: :add_where("polling_right = true") bsw@1071: if #units_with_polling_priv > 0 then bsw@1071: privileges_selector:add_where{ "unit_id NOT IN ($)", units_with_polling_priv } bsw@1071: end bsw@1071: local privileges = privileges_selector:exec() bsw@1071: bsw@1071: if #privileges > 0 then bsw@1071: testError("polling_right", "count: " .. #units_with_polling_priv, "count: " .. #privileges) bsw@1071: else bsw@1071: testOk("polling_right", "count: " .. #units_with_polling_priv) bsw@1071: end bsw@1071: bsw@1071: if not failed then bsw@1071: return true bsw@1071: end bsw@1071: bsw@1071: return false bsw@1071: bsw@1071: end bsw@1071: bsw@1071: checkMember("1000", "alice", "Alice Amberg", { 1 }, { }) bsw@1071: checkMember("1001", "bob", "Bob Bobbersen", { 1, 2 }, { 1, 3 }) bsw@1071: checkMember("1002", "chris", "Chris Carv", { 1, 2 }, { 1, 3 }) bsw@1071: checkMember("1003", "daisy", "Daisy Duck", { 3 }, { 1, 2 }) bsw@1071: checkMember("1004", "ernst", "Ernst Ernst", { 3 }, { 1, 2 }) bsw@1071: checkMember("1005", "fredi", "Frederike Frei", { 1 }, { }) bsw@1071: checkMember("1006") bsw@1071: checkMember("1007", "helen", "Helen Hofstatter", { 1, 2 }, { 1, 3 }) bsw@1071: checkMember("1008", "iwan", "Iwan Iwanowski", { 1 }, { }) bsw@1071: checkMember("1009", "jasmina", "Jasmina Jasik", { 1 }, { }) bsw@1071: bsw@1071: print() bsw@1071: bsw@1071: local success_count = global_count - global_failed bsw@1071: bsw@1071: print (success_count .. " of " .. global_count .. " tests succeeded.") bsw@1071: bsw@1071: end