From 5564b3420c5316743e1cbc63be96735880d2153e Mon Sep 17 00:00:00 2001 From: Jon Langseth Date: Wed, 18 Jan 2012 15:25:59 +0100 Subject: [PATCH] Changed sql_dbquery to return result on zero rows. Resulted in some patchings. Added test for new domain node. --- api/lib/alias_functions.php | 8 ++-- api/lib/db_functions.php | 1 - api/lib/number_functions.php | 10 ++--- api/lib/phone_functions.php | 6 +-- api/lib/user_functions.php | 2 +- api/t/domain.t | 86 ++++++++++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 api/t/domain.t diff --git a/api/lib/alias_functions.php b/api/lib/alias_functions.php index 51c3b58..aa66821 100644 --- a/api/lib/alias_functions.php +++ b/api/lib/alias_functions.php @@ -25,7 +25,7 @@ function get_destination ( $alias_username, $alias_domain ) // At this point, no error checking is performed, instead the empty array is returned. if ( ! $result ) return $aliases; - while ( $row = mysql_fetch_assoc( $result ) ) + while ( $row = @mysql_fetch_assoc( $result ) ) { array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) ); } @@ -57,7 +57,7 @@ function get_aliases ( $dest_username, $dest_domain ) // At this point, no error checking is performed, instead the empty array is returned. if ( ! $result ) return $aliases; - while ( $row = mysql_fetch_assoc( $result ) ) + while ( $row = @mysql_fetch_assoc( $result ) ) { array_push( $aliases, array( 'destination' => $row['destination'], 'alias' => $row['alias'] ) ); } @@ -85,7 +85,7 @@ function alias_exists( $alias_username, $alias_domain ) $result = sql_dbquery($config['kamailio_db'], $query); if ( !$result ) return true; // This is an error. Better to fail claiming alias exists... - $row = mysql_fetch_row($result); + $row = @mysql_fetch_row($result); if ( !$row ) return true; // This is an error. Better to fail claiming alias exists... $num_r = $row[0]; if ( $num_r == 1 ) @@ -94,7 +94,7 @@ function alias_exists( $alias_username, $alias_domain ) $config['kamailio_alias_table'], sql_clean($alias_username), sql_clean($alias_domain)); $result = sql_dbquery($config['kamailio_db'], $query); - $row = mysql_fetch_row($result); + $row = @mysql_fetch_row($result); if ( is_string( $row[0] ) ) return $row[0]; return true; // Failure mode.. } diff --git a/api/lib/db_functions.php b/api/lib/db_functions.php index 6ec0f53..2aad11d 100644 --- a/api/lib/db_functions.php +++ b/api/lib/db_functions.php @@ -7,7 +7,6 @@ function sql_dbquery( $db, $query ) if ( ! mysql_select_db( $db ) ) return false; $result = mysql_query( $query ); if ( !$result ) return false; - if (mysql_num_rows($result) == 0) return false; return $result; } function sql_dbexec ( $db, $query ) diff --git a/api/lib/number_functions.php b/api/lib/number_functions.php index d814a99..3ddf159 100644 --- a/api/lib/number_functions.php +++ b/api/lib/number_functions.php @@ -39,7 +39,7 @@ function number_inpool( $number ) $test = "SELECT COUNT(*) FROM " . $config['numbers_table'] . " WHERE number = '" . $number . "'"; $result = sql_dbquery($config['provision_db'], $test); if ( !$result ) return false; - $row = mysql_fetch_row($result); + $row = @mysql_fetch_row($result); if ( !$row ) return false; $num_r = $row[0]; if ( $num_r == 1 ) return true; @@ -54,9 +54,9 @@ function get_random_numbers ( $limit = 0 ) if ( $limit && is_numeric( $limit ) ) $query .= " LIMIT " . $limit; $result = sql_dbquery( $config['provision_db'], $query ); - print mysql_error(); + if ( !$result ) return null; - if (mysql_num_rows($result) < 1 ) return null; + if ( @mysql_num_rows($result) < 1 ) return null; $rows = array(); while ( $row = mysql_fetch_assoc( $result ) ) { @@ -81,9 +81,9 @@ function get_numbers ( $search=null, $limit = 0 ) $result = sql_dbquery( $config['provision_db'], $query ); - print mysql_error(); + if ( !$result ) return null; - if (mysql_num_rows($result) < 1 ) return null; + if ( @mysql_num_rows($result) < 1 ) return null; $rows = array(); while ( $row = mysql_fetch_assoc( $result ) ) { diff --git a/api/lib/phone_functions.php b/api/lib/phone_functions.php index 17fbe10..e527aea 100644 --- a/api/lib/phone_functions.php +++ b/api/lib/phone_functions.php @@ -23,7 +23,7 @@ function get_user_phones ( $username, $domain ) $result = sql_dbquery( $config['provision_db'], $query ); if ( !$result ) return null; - if (mysql_num_rows($result) < 1 ) return null; + if ( @mysql_num_rows($result) < 1 ) return null; $rows = array(); while ( $row = mysql_fetch_assoc( $result ) ) array_push( $rows, $row['mac'] ); @@ -42,7 +42,7 @@ function get_phone_users ( $macaddress ) $result = sql_dbquery( $config['provision_db'], $query ); if ( !$result ) return null; - if (mysql_num_rows($result) < 1 ) return null; + if ( @mysql_num_rows($result) < 1 ) return null; $rows = array(); while ( $row = mysql_fetch_assoc( $result ) ) { @@ -108,7 +108,7 @@ function list_phones ( $search = null ) $result = sql_dbquery( $config['provision_db'], $query ); if ( !$result ) return null; - if (mysql_num_rows($result) < 1 ) return null; + if ( @mysql_num_rows($result) < 1 ) return null; $rows = array(); while ( $row = mysql_fetch_assoc( $result ) ) { diff --git a/api/lib/user_functions.php b/api/lib/user_functions.php index e618d4c..3a53c88 100644 --- a/api/lib/user_functions.php +++ b/api/lib/user_functions.php @@ -244,7 +244,7 @@ function list_users ( $search = null ) $result = sql_dbquery( $config['provision_db'], $query ); if ( !$result ) return null; $list = array(); - while ( $row = mysql_fetch_row( $result ) ) + while ( $row = @mysql_fetch_row( $result ) ) { array_push( $list, array( "user" => $row[0], "displayname" => $row[1] ) ); } diff --git a/api/t/domain.t b/api/t/domain.t new file mode 100644 index 0000000..38467df --- /dev/null +++ b/api/t/domain.t @@ -0,0 +1,86 @@ +#!/usr/bin/perl + +use Test::More 'no_plan'; + +use strict; +use LWP; +use Data::Dumper; +use JSON; + +my $api_base = "http://10.0.2.5/hermes/api/"; +my $api_key = "6327c08b70f9"; +my $api_user = "test"; +my $api_pass = "Very5ecr3t"; + +my $test_domain = "example.com"; #TODO: Must fetch this from API + +# plan tests => 2; +my ($g_ua, $response, $data, $temp); + + +$g_ua = LWP::UserAgent->new; +isa_ok( $g_ua, 'LWP::UserAgent', '$g_ua'); + +$g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant +isa_ok( $g_ua->cookie_jar, 'HTTP::Cookies', '$g_ua->cookies'); + +TODO: { + local $TODO = 'auth/login not implemented yet'; + $response = $g_ua->get( $api_base . "auth/login?username=" . $api_user . + "password=" . $api_pass . "&key=" . $api_key); + + #$data = decode_json( $response->content); + is( $data->{'response'}, 'ok', 'auth/login'); + undef $response; undef $data; +} + +$response = $g_ua->get( $api_base . "domain/list"); +isa_ok( $response, 'HTTP::Response', 'domain/list $response'); +ok ($response->is_success, 'domain/list is_success'); +$data = decode_json( $response->content); +ok($data, 'domain/list JSON decode'); +is( $data->{'response'}, 'ok', 'domain/list result'); +ok($data->{'list'}, 'domain/list array'); +undef $response; undef $data; + +TODO: { + local $TODO = 'not implemented yet'; + $response = $g_ua->get( $api_base . "domain/get_servers"); + isa_ok( $response, 'HTTP::Response', 'domain/list $response'); + ok ($response->is_success, 'domain/list is_success'); + $data = decode_json( $response->content); + ok($data, 'domain/get_servers JSON decode'); + is( $data->{'response'}, 'ok', 'domain/get_servers result'); + ok($data->{'server'}->{'domain'}, 'domain/get_servers - domain'); + ok($data->{'server'}->{'registrar'}, 'domain/get_servers - registrar'); + ok($data->{'server'}->{'r_port'}, 'domain/get_servers - r_port'); + ok($data->{'server'}->{'proxy'}, 'domain/get_servers - proxy'); + ok($data->{'server'}->{'p_port'}, 'domain/get_servers - p_port'); + ok($data->{'server'}->{'prov_url'}, 'domain/get_servers - prov_url'); + undef $response; undef $data; + +} + +TODO: { + local $TODO = 'not implemented yet'; + $response = $g_ua->get( $api_base . "domain/set_servers" . + "?domain=" . $test_domain . # TODO This should be fetched from domain/list !! + "®istrar=registrar." . $test_domain . + "&r_port=5060" . + "&proxy=proxy." . $test_domain . + "&p_port=5060" . + "&prov_url=http://phone." . $test_domain ); + + isa_ok( $response, 'HTTP::Response', 'domain/list $response'); + ok ($response->is_success, 'domain/list is_success'); + $data = decode_json( $response->content); + ok($data, 'domain/get_servers JSON decode'); + is( $data->{'response'}, 'ok', 'domain/get_servers result'); + ok($data->{'server'}->{'domain'}, 'domain/get_servers - domain'); + ok($data->{'server'}->{'registrar'}, 'domain/get_servers - registrar'); + ok($data->{'server'}->{'r_port'}, 'domain/get_servers - r_port'); + ok($data->{'server'}->{'proxy'}, 'domain/get_servers - proxy'); + ok($data->{'server'}->{'p_port'}, 'domain/get_servers - p_port'); + ok($data->{'server'}->{'prov_url'}, 'domain/get_servers - prov_url'); + undef $response; undef $data; +} -- 2.39.2