From: Jon Langseth Date: Fri, 20 Jan 2012 16:03:25 +0000 (+0100) Subject: Making the test-harness more stream-lined. Only updated auth.t so far... X-Git-Url: https://git.defcon.no/?p=hermes;a=commitdiff_plain;h=1455ccca33de385a5d18db4d2aae30ab46472f9d Making the test-harness more stream-lined. Only updated auth.t so far... --- diff --git a/api/t/auth.t b/api/t/auth.t index 5ff2e74..d01c52b 100644 --- a/api/t/auth.t +++ b/api/t/auth.t @@ -1,237 +1,112 @@ #!/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 = "4hErgw3QFJLeuXGW"; -my $invalid_key = "invalidkey" . int(rand(255)); +use Test::More 'no_plan'; +use tests_common; -my $test_username = "testauth-" . int(rand(255)); -my $test_password = "foobarbaz"; -my $invalid_username = "failauth-" . int(rand(255)); +# Configuration variables defined in tests_common +# Make sure those are updated/set correctly.. +# $api_base +# $api_key +# +# Further variables defined in tests_common: +# $invalid_key +# $test_username +# $test_password +# $invalid_username -my ($g_ua, $session, $auth_key, $response, $data, $temp); +my ($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'); -##################################################################################### login_apikey(); -$response = $g_ua->get( $api_base . "auth/new_apikey" . - "?session=" . $session . - "&auth_key=" . $auth_key . - "&host_ip=10.0.3.86" . - "&access=full_read"); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode('auth/new_apikey', { 'host_ip' => '10.0.3.87', 'access' => 'full_read' }); is( $data->{'response'}, 'ok', 'auth/new_apikey ok'); ok( $data->{'key'}, 'auth/new_apikey new key'); $temp = $data->{'key'} if ( $data->{'response'} eq 'ok' ); -undef $data; undef $response; +undef $data; -$response = $g_ua->get( $api_base . "auth/list_apikeys" . - "?session=" . $session . - "&auth_key=" . $auth_key); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode('auth/list_apikeys', undef); is( $data->{'response'}, 'ok', 'auth/list_apikeys ok'); ok( $data->{'list'}, 'auth/list_apikeys list'); -undef $data; undef $response; +undef $data; -$response = $g_ua->get( $api_base . "auth/remove_apikey" . - "?session=" . $session . - "&auth_key=" . $auth_key . - "&api_key=" . $temp ); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode('auth/remove_apikey', { "api_key" => $temp }); is( $data->{'response'}, 'ok', 'auth/remove_apikey ok'); ok( $data->{'key'}, 'auth/remove_apikey key'); -undef $data; undef $response; - -$response = $g_ua->get( $api_base . "auth/authorize_user" . - "?session=" . $session . - "&auth_key=" . $auth_key . - "&username=" . $test_username . - "&access=read_write"); -$data = decode_json( $response->content) if $response->is_success; +undef $data; + +$data = exec_apinode("auth/authorize_user", { "username" => $test_username, "access" => "read_write" }); is( $data->{'response'}, 'ok', 'auth/authorize_user ok'); ok( $data->{'user'}, 'auth/authorize_user user set'); is( $data->{'user'}, $test_username, 'auth/authorize_user user is ' . $test_username); ok( $data->{'access'}, 'auth/authorize_user access set'); isnt( $data->{'access'}, 'no_access', 'auth/authorize_user level'); -undef $data; undef $response; +undef $data; -$response = $g_ua->get( $api_base . "auth/list_users" . - "?session=" . $session . - "&auth_key=" . $auth_key); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode("auth/list_users", undef ); is( $data->{'response'}, 'ok', 'auth/list_users ok'); ok( $data->{'list'}, 'auth/list_users list'); -undef $data; undef $response; +undef $data; -$response = $g_ua->get( $api_base . "auth/ping" . - "?session=" . $session . - "&auth_key=" . $auth_key ); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode("auth/ping", undef ); is( $data->{'response'}, 'pong', 'auth/ping PONG!'); ok( $data->{'auth_key'}, 'auth/ping key set'); ok( not ($data->{'auth_key'} eq $auth_key), 'auth/ping key changed'); $auth_key = $data->{'auth_key'}; -undef $data; undef $response; +undef $data; -$response = $g_ua->get( $api_base . "auth/list_apikeys" . - "?session=" . $session . - "&auth_key=" . $auth_key); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode("auth/list_apikeys", undef ); is( $data->{'response'}, 'ok', 'auth/ping new key accepted.'); -undef $data; undef $response; +undef $data; logout(); ok( !$session, 'logged out, session cleared'); login_user(); -$response = $g_ua->get( $api_base . "auth/authorize_user" . - "?session=" . $session . - "&auth_key=" . $auth_key . - "&username=" . $test_username . - "&access=full_read"); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode("auth/authorize_user", { "username" => $test_username, "access" => "full_read" }); is( $data->{'response'}, 'ok', 'auth/authorize_user as user ok'); is( $data->{'user'}, $test_username, 'auth/authorize_user user is ' . $test_username); ok( $data->{'access'}, 'auth/authorize_user access set'); isnt( $data->{'access'}, 'no_access', 'auth/authorize_user level'); -undef $data; undef $response; +undef $data; -$response = $g_ua->get( $api_base . "auth/remove_user" . - "?session=" . $session . - "&auth_key=" . $auth_key . - "&username=" . $test_username ); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode("auth/remove_user", { "username" => $test_username }); is( $data->{'response'}, 'failed', 'auth/remove_user as nonpriv user fails'); is( $data->{'cause'}, 'unauthorized', 'auth/remove_user cause correct'); -undef $data; undef $response; +undef $data; -$response = $g_ua->get( $api_base . "auth/add_user" . - "?session=" . $session . - "&auth_key=" . $auth_key . - "&username=" . $test_username ); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode("auth/add_user", { "username" => $test_username }); is( $data->{'response'}, 'notimplemented', 'auth/add_user TODO: notimplemented.'); -undef $data; undef $response; +undef $data; -$response = $g_ua->get( $api_base . "auth/update_user" . - "?session=" . $session . - "&auth_key=" . $auth_key . - "&username=" . $test_username ); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode("auth/update_user", { "username" => $test_username }); is( $data->{'response'}, 'notimplemented', 'auth/update_user TODO: notimplemented.'); -undef $data; undef $response; +undef $data; logout(); ok( !$session, 'logged out, session cleared'); login_apikey(); -$response = $g_ua->get( $api_base . "auth/remove_user" . - "?session=" . $session . - "&auth_key=" . $auth_key . - "&username=" . $test_username ); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode("auth/remove_user", { "&username" => $test_username }); is( $data->{'response'}, 'ok', 'auth/remove_user as user ok'); is( $data->{'user'}, $test_username, 'auth/remove_user removed ' . $test_username); -undef $data; undef $response; +undef $data; logout(); ok( !$session, 'logged out, session cleared'); -$response = $g_ua->get( $api_base . "auth/list_apikeys" . - "?session=" . $session . - "&auth_key=" . $auth_key); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode("auth/list_apikeys", undef ); is( $data->{'response'}, 'failed', 'auth/list_apikeys denied after logout'); is( $data->{'cause'}, 'unauthorized', 'auth/list_apikeys cause correct'); -undef $data; undef $response; +undef $data; -$response = $g_ua->get( $api_base . "auth/list_users" . - "?session=" . $session . - "&auth_key=" . $auth_key); -$data = decode_json( $response->content) if $response->is_success; +$data = exec_apinode("auth/list_users", undef ); is( $data->{'response'}, 'failed', 'auth/list_users denied after logout'); is( $data->{'cause'}, 'unauthorized', 'auth/list_users cause correct'); -undef $data; undef $response; - - - - -##################################################################################### -##################################################################################### -##################################################################################### -##################################################################################### -sub login_apikey -{ - $response = $g_ua->get( $api_base . "auth/login" . - "?api_key=" . $api_key ); - ok ($response->is_success, 'login_apikey request is_success'); - $data = decode_json( $response->content) if $response->is_success; - die("HTTP error") unless $response->is_success; - - is( $data->{'response'}, 'ok', 'login_apikey logged in'); - ok( $data->{'session'}, 'login_apikey session set'); - ok( $data->{'auth_key'}, 'login_apikey auth_key set'); - if ( $data->{'response'} eq "ok" ) - { - $session = $data->{'session'}; - $auth_key = $data->{'auth_key'}; - } - else - { - exit; - } - undef $data; undef $response; -} -sub login_user -{ - $response = $g_ua->get( $api_base . "auth/login" . - "?username=" . $test_username . - "&password=" . $test_password ); - - ok ($response->is_success, 'login_user request is_success'); - $data = decode_json( $response->content) if $response->is_success; - die("HTTP error") unless $response->is_success; - - is( $data->{'response'}, 'ok', 'login_user logged in'); - ok( $data->{'session'}, 'login_user session set'); - ok( $data->{'auth_key'}, 'login_user auth_key set'); - if ( $data->{'response'} eq "ok" ) - { - $session = $data->{'session'}; - $auth_key = $data->{'auth_key'}; - } - else - { - exit; - } - undef $data; undef $response; -} -sub logout -{ - undef $data; undef $response; - $response = $g_ua->get( $api_base . "auth/logout" . - "?session=" . $session ); - - ok ($response->is_success, 'logout request is_success'); - die("HTTP error") unless $response->is_success; - - $data = decode_json( $response->content) if $response->is_success; - is( $data->{'response'}, 'ok', 'logout ok'); - - undef $session; - undef $auth_key; -} +undef $data; diff --git a/api/t/tests_common.pm b/api/t/tests_common.pm new file mode 100644 index 0000000..9c2ec83 --- /dev/null +++ b/api/t/tests_common.pm @@ -0,0 +1,130 @@ +package tests_common; + +use Test::More; + +use strict; +use warnings; + +use LWP; +use Data::Dumper; +use JSON; + +BEGIN { + use Exporter(); + our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); + @ISA = qw(Exporter); + @EXPORT = qw($api_base $api_key $invalid_key $test_username $test_password $invalid_username $g_ua $session $auth_key &exec_apinode &login_apikey &login_user &logout); + %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); + @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); +} +our $api_base = "http://10.0.2.5/hermes/api/"; +our $api_key = "4hErgw3QFJLeuXGW"; +our $invalid_key = "invalidkey" . int(rand(255)); + +our $test_username = "testauth-" . int(rand(255)); +our $test_password = "foobarbaz"; +our $invalid_username = "failauth-" . int(rand(255)); + +our ($g_ua, $session, $auth_key); + +$g_ua = LWP::UserAgent->new; +$g_ua->cookie_jar({}); # In-memory jar, look at HTTP::Cookies for persistant + + +sub exec_apinode($$) +{ + my $node = shift; + my $param = shift; + + my ( $response, $data ); + + $session = "" if not defined $session; + $auth_key = "" if not defined $auth_key; + my $url = $api_base . $node . + "?session=" . $session . + "&auth_key=" . $auth_key; + + foreach my $key ( keys %$param ) + { + $url .= "&" . $key . "=" . $param->{$key}; + } + $response = $g_ua->get( $url ); + if ( $response->is_success ) + { + if ( $response->content =~ m/\s*{/ ) + { + $data = decode_json( $response->content); + } + else + { + $data = $response->content; + } + + } + return $data; +} + +sub login_apikey +{ + my $response = $g_ua->get( $api_base . "auth/login" . + "?api_key=" . $api_key ); + ok ($response->is_success, 'login_apikey request is_success'); + my $data = decode_json( $response->content) if $response->is_success; + die("HTTP error") unless $response->is_success; + + is( $data->{'response'}, 'ok', 'login_apikey logged in'); + ok( $data->{'session'}, 'login_apikey session set'); + ok( $data->{'auth_key'}, 'login_apikey auth_key set'); + if ( $data->{'response'} eq "ok" ) + { + $session = $data->{'session'}; + $auth_key = $data->{'auth_key'}; + } + else + { + exit; + } + undef $data; undef $response; +} + +sub login_user +{ + my $response = $g_ua->get( $api_base . "auth/login" . + "?username=" . $test_username . + "&password=" . $test_password ); + + ok ($response->is_success, 'login_user request is_success'); + my $data = decode_json( $response->content) if $response->is_success; + die("HTTP error") unless $response->is_success; + + is( $data->{'response'}, 'ok', 'login_user logged in'); + ok( $data->{'session'}, 'login_user session set'); + ok( $data->{'auth_key'}, 'login_user auth_key set'); + if ( $data->{'response'} eq "ok" ) + { + $session = $data->{'session'}; + $auth_key = $data->{'auth_key'}; + } + else + { + exit; + } + undef $data; undef $response; +} + +sub logout +{ + my $response = $g_ua->get( $api_base . "auth/logout" . + "?session=" . $session ); + + ok ($response->is_success, 'logout request is_success'); + die("HTTP error") unless $response->is_success; + + my $data = decode_json( $response->content) if $response->is_success; + is( $data->{'response'}, 'ok', 'logout ok'); + + undef $session; undef $auth_key; +} + + +1;