#!/usr/bin/perl -X
require '/var/www/EventStaffAPI/UnitTest/UnitTest_Globals.pl';

$HTTP = $ARGV[0];
$DEBUG = $ARGV[1];


##Test 1
$xml = "<\?xml version=\"1.0\"\?>";
$xml .="<fwxg_api>
<api_action>retrieve_api_descriptor</api_action>
<action_content>
</action_content>
</fwxg_api>
";

$receivedHash = DoHTTPCall($xml);

##Test 1
$xml = "<\?xml version=\"1.0\"\?>";
$xml .="<fwxg_api>
<api_action>user_functions_get_agency_from_domain</api_action>
<action_content>
 <domain>predetorlinux.dedicated.co.za</domain>
</action_content>
</fwxg_api>
";

$receivedHash = DoHTTPCall($xml);

####RETURN_CHECKS####
if($receivedHash->{status} ne "true") { print "Call Returned Failed \@ Line(".__LINE__.")\n"; exit(-1); }
$agency_id = $receivedHash->{agency_id};


##Test 1
$xml = "<\?xml version=\"1.0\"\?>";
$xml .="<fwxg_api>
<api_action>user_functions_user_type_list</api_action>
<action_content>
</action_content>
</fwxg_api>
";

$receivedHash = DoHTTPCall($xml);

####RETURN_CHECKS####
if($receivedHash->{status} ne "true") { print "Call Returned Failed \@ Line(".__LINE__.")\n"; exit(-1); }

##Test 1
$xml = "<\?xml version=\"1.0\"\?>";
$xml .="<fwxg_api>
<api_action>user_functions_login</api_action>
<action_content>
 <agency_id>$agency_id</agency_id>
 <username>agency1_admin</username>
 <password>predetor</password> 
</action_content>
</fwxg_api>
";

$receivedHash = DoHTTPCall($xml);

####RETURN_CHECKS####
if($receivedHash->{status} ne "true") { print "Call Returned Failed \@ Line(".__LINE__.")\n"; exit(-1); }
$session_id = $receivedHash->{session_id};

##Test 1
$xml = "<\?xml version=\"1.0\"\?>";
$xml .="<fwxg_api>
<api_action>user_profile_functions_user_profile_types_list</api_action>
<action_content>
</action_content>
</fwxg_api>
";

$receivedHash = DoHTTPCall($xml);

####RETURN_CHECKS####
if($receivedHash->{status} ne "true") { print "Call Returned Failed \@ Line(".__LINE__.")\n"; exit(-1); }
if(ref($receivedHash->{user_profile_types}->{user_profile_type}) eq "HASH")
{
 $user_profile_type_id= $receivedHash->{user_profile_types}->{user_profile_type}->{user_profile_type_id};
}
else
{
 $user_profile_type_id= $receivedHash->{user_profile_types}->{user_profile_type}->[0]->{user_profile_type_id};
}


##Test 1
$xml = "<\?xml version=\"1.0\"\?>";
$xml .="<fwxg_api>
<api_action>user_profile_functions_user_profile_list</api_action>
<action_content>
 <session_id>$session_id</session_id>
</action_content>
</fwxg_api>
";

$receivedHash = DoHTTPCall($xml);

####RETURN_CHECKS####
if($receivedHash->{status} ne "true") { print "Call Returned Failed \@ Line(".__LINE__.")\n"; exit(-1); }
if(ref($receivedHash->{user_profiles}->{user_profile}) eq "HASH")
{
 $user_profile_id= $receivedHash->{user_profiles}->{user_profile}->{user_profile_id};
}
else
{
 $user_profile_id= $receivedHash->{user_profiles}->{user_profile}->[0]->{user_profile_id};
}

##Test 1
$xml = "<\?xml version=\"1.0\"\?>";
$xml .="<fwxg_api>
<api_action>user_profile_functions_user_profile_edit</api_action>
<action_content>
 <session_id>$session_id</session_id>
   <user_profile_id>$user_profile_id</user_profile_id>
   <user_profile_value>Unit Test</user_profile_value>
</action_content>
</fwxg_api>
";

$receivedHash = DoHTTPCall($xml);

####RETURN_CHECKS####
if($receivedHash->{status} ne "true") { print "Call Returned Failed \@ Line(".__LINE__.")\n"; exit(-1); }

##Test 1
$xml = "<\?xml version=\"1.0\"\?>";
$xml .="<fwxg_api>
<api_action>user_profile_functions_user_profile_add</api_action>
<action_content>
 <session_id>$session_id</session_id>
   <user_profile_type_id>$user_profile_type_id</user_profile_type_id>
   <user_profile_value>Unit Test</user_profile_value>
</action_content>
</fwxg_api>
";

$receivedHash = DoHTTPCall($xml);

####RETURN_CHECKS####
if($receivedHash->{status} eq "true") { print "Call Returned Failed \@ Line(".__LINE__.")\n"; exit(-1); }


exit(0);


