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.

Validating LDAP through QTP – 1

For the complete and Good Test Coverage, we usually cover the UI check, DB check as part of the automation. One more area which we more over tried to have a check is the LDAP. All user related information such as UserName, FirstName, mail-id, password, contact information are stored in LDAP Securely. I will we explain how we can Automate The LDAP tests with this series of Post.

Firstly, we shall try to understand how we can communicate with LDAP through QTP/VB scripts. LDAP internally have its own Database, which we can query the information. The process is simple as in the case of Database query. we first establish a connection, query the information. process the output.

As an example, we will define a function getDNofUser(username).

This will take the user-name as an input, and return empty string if the user doesn’t exist, or a complete Distinguished name of that user.

public Function getDNofUser(UserName)
 Set con = CreateObject("adodb.connection")
 con.Open "Provider=ADsDSOObject;", "cn=root", "password"
 sql = "select cn from 'LDAP://ldapserver:389/cn=users,dc=compName,dc=com' 
where ObjectClass='inetorgperson'and uid='"& UserName & "'"

 Set rs = con.Execute(sql)
 val=""      

 Do until rs.EOF
 ReturnValue = rs.Fields(0)
 val=""
 If IsArray(ReturnValue) Then
 val = "cn=" & ReturnValue(0) & ",cn=users,dc=compName,dc=com"
 Else
 Val = "cn=" & ReturnValue & ",cn=users,dc=compName,dc=com"
 End If
 rs.MoveNext
 Loop
 getDNofUser = val
End Function

The line

con.Open "Provider=ADsDSOObject;", "cn=root", "password" 

creates a connection object, we have to provide the LDAP credentials here. It need not be cn=root alway, It can be any user with read permission to the directory structure.

The line

sql = "select cn from 'LDAP://ldapserver:389/cn=users,dc=compName,dc=com' 
where ObjectClass='inetorgperson'and uid='"& UserName & "'"

is the actually query from which we would be fetching the detail from.

Note that in from clause, we are providing the ldap details: servername, portnumber, user Container and baseDN. In the where clause we are specifying the objectclass the user belong and the uid we are interested.

Once we execute this sql we will get the recordset. Little processing it we will be able to validate the user is present or not in the LDAP. So the Create User Sanity Test Case will Pass!! 🙂