Validating LDAP through QTP – 2

Moving from checking the user Existence from the previous post, we shall now try to understand how we can validate the user details present in the LDAP for a particular user.

So let us define a function getUserDetail(USerID,Field).

This will take the user-name and field as an input, and return Value of the field specified.

public Function getUserDetail(USerID,Field)

Set con = CreateObject("adodb.connection")
con.Open "Provider=ADsDSOObject;", "cn=root","password"

Select Case lcase(Field)
Case "firstname"
Field = "givenname"
Case "lastname"
Field = "sn"
Case "userdisabled"
Field = "ou"
Case "mobile"
Field = "mobile"
Case "email"
Field = "mail"
Case "phone"
Field = "homePhone"
Case "title"
Field = "businessCategory"
Case "password"
Field = "userPassword"
Case else
Reporter.ReportEvent micFail, "Invalid field; not found in ldap","Invalid field; not found in ldap"
Exit Function
End Select '
str= "select "& Field & " from 'LDAP://ldapserver:389/cn=users,dc=compName,dc=com' where ObjectClass='inetorgperson'and uid='"& UserName & "'"

Set rs = con.Execute(str)

val=""

Do While Not rs.EOF Or rs.BOF
ReturnValue = rs.Fields(Field)
val=""
If IsArray(ReturnValue) Then
val = ReturnValue(0)
For I = LBound(ReturnValue) + 1  To UBound(ReturnValue)
If ReturnValue(I) <> "" Then
val = val & "," & ReturnValue(I)
End If
Next
Else
Val = ReturnValue
End If
rs.MoveNext
Loop

If TypeName(val) = "Byte()" then
val=OctetToHexStr(val)

end if
getUserDetail= Val & ""
End Function

Function OctetToHexStr(arrbytOctet)
Dim k
OctetToHexStr = ""
For k = 1 To Lenb(arrbytOctet)
OctetToHexStr = OctetToHexStr &  Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
Next
End Function

This function can fetch most of the basic fields such as FirstName, LastName, Phone.. etc. It can also fetch the password, but will be encycpted.

Most of the field updates can be validated with this function, And even Password reset can validated with simple logic as below.

oldpwd=getUserDetail("userid","password")
/*      code to click on reset password button        */
newpwd=getUserDetail("userid","password")

if oldpwd = newpwd then
   Reporter.ReportEvent micFail, "Reset Password failed in LDAP","Reset Password failed in LDAP"
else
   Reporter.ReportEvent micPass, "Reset Password passed in LDAP","Reset Password passed in LDAP"
endif

PS: Fields that we can query are limited the basic fields, And not any user defined fields.