liquid_feedback_frontend
view env/ldap/test.lua @ 1071:58f48a8a202a
Imported and merged LDAP patch
| author | bsw | 
|---|---|
| date | Fri Jul 18 21:42:59 2014 +0200 (2014-07-18) | 
| parents | |
| children | aefef1556d55 | 
 line source
     1 -- Part of test case for ldap4lqfb
     3 function ldap.test()
     5   local global_count = 0
     6   local global_failed = 0
     8   local function checkMember(uid, login, name, units_with_voting_priv, units_with_polling_priv)
    10     local failed = false
    12     local function testError(err, expected, got)
    13       failed = true
    14       global_failed = global_failed + 1
    15       if expected then
    16         print ("[" .. uid .. "] FAILED: " .. err .. " (expected: '" .. expected .. "' but got '" .. tostring(got) .. "')") 
    17       else
    18         print ("[" .. uid .. "] FAILED: " .. err)
    19       end
    20     end
    22     local function testOk(test, expected)
    23       if test then
    24         print ("[" .. uid .. "] " .. test .. " ok ('" .. expected .. "')")
    25       else
    26         print ("[" .. uid .. "] success")
    27       end
    28     end
    30     local function test(field, expected, value)
    31       global_count = global_count + 1
    32       if expected ~= value then
    33         testError(field, expected, value)
    34         return
    35       end
    36       testOk(field, expected)
    37     end
    39     local members = Member:new_selector()
    40       :add_field{ "authority_data->'login' as authority_data_login" }
    41       :add_where{ "authority = ? AND authority_data->'uid' = ?", "ldap", uid }
    42       :exec()
    44     if #members < 1 then
    45       testError("Member not found in DB")
    46       return
    47     end
    49     if #members > 1 then
    50       testError("Found more than one DB entry")
    51       return
    52     end
    54     local member = members[1]
    56     if login == nil then
    57       if not member.locked then
    58         testError("Member not locked")
    59       else
    60         testOk("Member is locked", "true")
    61       end
    62       return
    63     end
    65     test("login", login, member.authority_data_login)
    66     test("name", name, member.name)
    68     for i, unit_id in ipairs(units_with_voting_priv) do
    69       global_count = global_count + 1
    70       local privilege = Privilege:by_pk(unit_id, member.id)
    71       if privilege and privilege.voting_right then
    72         testOk("voting_right", unit_id)
    73       else
    74         testError("voting_right", unit_id, "")
    75       end
    76     end
    78     local privileges_selector = Privilege:new_selector()
    79       :add_where{ "member_id = ?", member.id }
    80       :add_where("voting_right = true")
    81     if #units_with_voting_priv > 0 then
    82       local privileges = privileges_selector:add_where{ "unit_id NOT IN ($)", units_with_voting_priv }
    83     end
    84     local privileges = privileges_selector:exec()
    86     if #privileges > 0 then
    87       testError("voting_right", "count: 0", "count: " .. #privileges)
    88     else
    89       testOk("voting_right", "count: 0")
    90     end
    92     for i, unit_id in ipairs(units_with_polling_priv) do
    93       global_count = global_count + 1
    94       local privilege = Privilege:by_pk(unit_id, member.id)
    95       if privilege and privilege.polling_right then
    96         testOk("polling_right", unit_id)
    97       else
    98         testError("polling_right", unit_id, "")
    99       end
   100     end
   102     local privileges_selector = Privilege:new_selector()
   103       :add_where{ "member_id = ?", member.id }
   104       :add_where("polling_right = true")
   105     if #units_with_polling_priv > 0 then
   106       privileges_selector:add_where{ "unit_id NOT IN ($)", units_with_polling_priv }
   107     end
   108     local privileges = privileges_selector:exec()
   110     if #privileges > 0 then
   111       testError("polling_right", "count: " .. #units_with_polling_priv, "count: " .. #privileges)
   112     else
   113       testOk("polling_right", "count: " .. #units_with_polling_priv)
   114     end
   116     if not failed then
   117       return true
   118     end
   120     return false
   122   end
   124   checkMember("1000", "alice", "Alice Amberg", { 1 }, { })
   125   checkMember("1001", "bob", "Bob Bobbersen", { 1, 2 }, { 1, 3 })
   126   checkMember("1002", "chris", "Chris Carv", { 1, 2 }, { 1, 3 })
   127   checkMember("1003", "daisy", "Daisy Duck", { 3 }, { 1, 2 })
   128   checkMember("1004", "ernst", "Ernst Ernst", { 3 }, { 1, 2 })
   129   checkMember("1005", "fredi", "Frederike Frei", { 1 }, { })
   130   checkMember("1006")
   131   checkMember("1007", "helen", "Helen Hofstatter", { 1, 2 }, { 1, 3 })
   132   checkMember("1008", "iwan", "Iwan Iwanowski", { 1 }, { })
   133   checkMember("1009", "jasmina", "Jasmina Jasik", { 1 }, { })
   135   print()
   137   local success_count = global_count - global_failed
   139   print (success_count .. " of " .. global_count .. " tests succeeded.")
   141 end
