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