From: Jon Langseth Date: Fri, 20 Jan 2012 23:06:12 +0000 (+0100) Subject: Updated previous tests to new framework X-Git-Url: https://git.defcon.no/?a=commitdiff_plain;h=28b34f2086ab9df805d9ee4c6b0db6624a811582;p=hermes Updated previous tests to new framework --- diff --git a/api/t/domain.t b/api/t/domain.t index 917ab16..6988203 100644 --- a/api/t/domain.t +++ b/api/t/domain.t @@ -1,57 +1,37 @@ #!/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 = undef; +use Test::More 'no_plan'; +use tests_common; -# plan tests => 2; -my ($g_ua, $response, $data, $temp, @t); +# 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 ($data, $temp, $test_domain, $test_remote_domain ); -$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; -} +login_apikey(); -$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'); +$data = exec_apinode("domain/list", undef); is( $data->{'response'}, 'ok', 'domain/list result'); ok($data->{'list'}, 'domain/list array'); # NOW: Set the $test_domain to something useful (i.e. the first reported domain) $test_domain = $data->{'list'}[0]; ok($test_domain, 'test_domain set.'); -undef $response; undef $data; +undef $data; -$response = $g_ua->get( $api_base . "domain/get_servers?domain=" . $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'); +$data = exec_apinode("domain/get_servers", { "domain" => $test_domain }); is( $data->{'response'}, 'ok', 'domain/get_servers result'); ok($data->{'servers'}->{'domain'}, 'domain/get_servers - domain'); ok($data->{'servers'}->{'registrar'}, 'domain/get_servers - registrar'); @@ -59,20 +39,17 @@ ok($data->{'servers'}->{'r_port'}, 'domain/get_servers - r_port'); ok($data->{'servers'}->{'proxy'}, 'domain/get_servers - proxy'); ok($data->{'servers'}->{'p_port'}, 'domain/get_servers - p_port'); ok($data->{'servers'}->{'prov_url'}, 'domain/get_servers - prov_url'); -undef $response; undef $data; - -$response = $g_ua->get( $api_base . "domain/set_servers" . - "?domain=" . $test_domain . - "®istrar=registrar." . $test_domain . - "&r_port=5060" . - "&proxy=proxy." . $test_domain . - "&p_port=5060" . - "&prov_url=http://phone." . $test_domain . "/hermes/prov/" ); +undef $data; + +$data = exec_apinode("domain/set_servers", { + "domain" => $test_domain, + "registrar" => "registrar." . $test_domain, + "r_port" => 5060, + "proxy" => "proxy." . $test_domain, + "p_port" => 5060, + "prov_url" => "http://phone." . $test_domain . "/hermes/prov/", +} ); -isa_ok( $response, 'HTTP::Response', 'domain/list $response'); -ok ($response->is_success, 'domain/list is_success'); -$data = decode_json( $response->content); -ok($data, 'domain/set_servers JSON decode'); is( $data->{'response'}, 'ok', 'domain/set_servers result'); ok($data->{'servers'}->{'domain'}, 'domain/set_servers - domain'); ok($data->{'servers'}->{'registrar'}, 'domain/set_servers - registrar'); @@ -80,12 +57,7 @@ ok($data->{'servers'}->{'r_port'}, 'domain/set_servers - r_port'); ok($data->{'servers'}->{'proxy'}, 'domain/set_servers - proxy'); ok($data->{'servers'}->{'p_port'}, 'domain/set_servers - p_port'); ok($data->{'servers'}->{'prov_url'}, 'domain/set_servers - prov_url'); -undef $response; undef $data; +undef $data; + +logout(); -TODO: { - local $TODO = 'auth/logout not implemented yet'; - $response = $g_ua->get( $api_base . "auth/logout"); - #$data = decode_json( $response->content); - is( $data->{'response'}, 'ok', 'auth/logout'); - undef $response; undef $data; -} diff --git a/api/t/user.t b/api/t/user.t index 81d908d..fca67eb 100644 --- a/api/t/user.t +++ b/api/t/user.t @@ -1,50 +1,29 @@ #!/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_username = "testuser-" . int(rand(255)); -my $test_aliasname = "testalias-" . int(rand(255)); - -my $test_domain = undef; - -my $test_remote_domain = undef; - +use Test::More 'no_plan'; +use tests_common; -# plan tests => 2; -my ($g_ua, $response, $data, $temp); +# 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 ($data, $temp, $test_domain, $test_remote_domain ); -$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; -} +login_apikey(); # First: fetch a supported domain from the API... -$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); +$data = exec_apinode("domain/list", undef); ok($data, 'domain/list JSON decode'); is( $data->{'response'}, 'ok', 'domain/list result'); ok($data->{'list'}, 'domain/list array'); @@ -55,106 +34,98 @@ $test_remote_domain = "external." . $test_domain; ok($test_domain, 'test_domain set.'); ok($test_remote_domain, 'test_remote_domain set.'); -undef $response; undef $data; +undef $data; -$response = $g_ua->get( $api_base . "user/list"); -isa_ok( $response, 'HTTP::Response', 'user/list $response'); -ok ($response->is_success, 'user/list is_success'); -$data = decode_json( $response->content); +$data = exec_apinode("user/list", undef); ok($data, 'user/list JSON decode'); -is( $data->{'response'}, 'ok', 'user/list result'); +is($data->{'response'}, 'ok', 'user/list result'); ok($data->{'list'}, 'user/list array'); -undef $response; undef $data; +undef $data; -$response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_domain); -$data = decode_json( $response->content); +$data = exec_apinode("user/available", { "user" => $test_username . "\@" . $test_domain }); is( $data->{'response'}, 'ok', 'user/available is available'); -undef $response; undef $data; +undef $data; -$response = $g_ua->get( $api_base . "user/add_local?user=" . $test_username . "\@" . $test_domain . "&displayname=Automatic testing&email=noreply\@" . $test_domain); -$data = decode_json( $response->content); +$data = exec_apinode("user/add_local", { + "user" => $test_username . "\@" . $test_domain, + "displayname" => "Automatic testing", + "email" => "noreply\@" . $test_domain, +} ); is( $data->{'response'}, 'ok', 'user/add_local created new account'); #TODO: Add tests to verify the data from add_local... -undef $response; undef $data; +undef $data; -$response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_domain); -$data = decode_json( $response->content); -is( $data->{'response'}, 'failed', 'user/available has been created'); -undef $response; undef $data; +$data = exec_apinode("user/available", { "user" => $test_username . "\@" . $test_domain }); +is( $data->{'response'}, 'failed', 'user/available has been created'); +undef $data; -$response = $g_ua->get( $api_base . "user/get?user=" . $test_username . "\@" . $test_domain); -$data = decode_json( $response->content); +$data = exec_apinode("user/get", { "user" => $test_username . "\@" . $test_domain }); is( $data->{'response'}, 'ok', 'user/get returns'); is( $data->{'user'}->{'username'}, $test_username, 'user/get username correct'); is( $data->{'user'}->{'domain'}, $test_domain, 'user/get domain correct'); -ok( $data->{'user'}->{'ha1'} =~ m/[a-z0-9]{16,}/, 'user/get ha1 hash generated'); -undef $response; undef $data; - -$response = $g_ua->get( $api_base . "user/gen_pw"); -ok( $response->content =~ m/[a-z0-9]{24}/, 'user/gen_pw format OK'); -$temp = $response->content; -undef $response; undef $data; - -$response = $g_ua->get( $api_base . "user/change_pw?user=" . $test_username . "\@" . $test_domain . "&password=" . $temp ); -$data = decode_json( $response->content); +ok( $data->{'user'}->{'ha1'} =~ m/[a-z0-9]{16,}/, 'user/get ha1 hash generated'); +undef $data; + +$data = exec_apinode("user/gen_pw", undef); +ok( $data =~ m/[a-zA-Z0-9]{24}/, 'user/gen_pw format OK'); +$temp = $data; +undef $data; + +$data = exec_apinode("user/change_pw", { + "user" => $test_username . "\@" . $test_domain, + "password" => $temp, +} ); is( $data->{'response'}, 'ok', 'user/change_pw'); -undef $response; undef $data; +undef $data; # TODO: This tests _almost_ all parameters. Update API docs, then update this test. -$response = $g_ua->get( $api_base . "user/update?user=" . $test_username . "\@" . $test_domain . - "&displayname=Changed" . - "&linetext=" . $test_username . - "®istrar=registrar." . $test_domain . - "&r_port=5061" . - "&proxy=proxy." . $test_domain . - "&p_port=5060"); - -$data = decode_json( $response->content); +$data = exec_apinode("user/update", { + "user" => $test_username . "\@" . $test_domain, + "displayname" => "Changed", + "linetext" => $test_username, + "registrar" => "registrar." . $test_domain, + "r_port" => 5061, + "proxy" => "proxy." . $test_domain, + "p_port" => 5060, +}); is( $data->{'response'}, 'ok', 'user/update'); ok( $data->{'updated'}, 'user/update updated list'); ok( $data->{'skipped'}, 'user/update skipped list'); -undef $response; undef $data; +undef $data; -$response = $g_ua->get( $api_base . "user/remove?user=" . $test_username . "\@" . $test_domain); -$data = decode_json( $response->content); +$data = exec_apinode("user/remove", { "user" => $test_username . "\@" . $test_domain }); is( $data->{'response'}, 'ok', 'user/remove deleted user'); -undef $response; undef $data; - -$response = $g_ua->get( $api_base . "user/add_remote?user=" . $test_username . "\@" . $test_domain . - "&displayname=Automatic testing" . - "&password=verysecretpasswd" . - "®istrar=registrar" . $test_domain); - -$data = decode_json( $response->content); +undef $data; + +$data = exec_apinode("user/add_remote", { + "user" => $test_username . "\@" . $test_domain, + "displayname" => "Automatic testing", + "password" => "verysecretpasswd", + "registrar" => "registrar" . $test_domain, +}); is( $data->{'response'}, 'failed', 'user/add_remote fails for local domain'); #TODO: Add tests to verify the data from add_local... -undef $response; undef $data; +undef $data; -$response = $g_ua->get( $api_base . "user/add_remote?user=" . $test_username . "\@" . $test_remote_domain . - "&displayname=Automatic testing" . - "&password=verysecretpasswd" . - "®istrar=registrar" . $test_domain); +$data = exec_apinode("user/add_remote", { + "user" => $test_username . "\@" . $test_remote_domain, + "displayname" => "Automatic testing", + "password" => "verysecretpasswd", + "registrar" => "registrar" . $test_domain, -$data = decode_json( $response->content); +}); is( $data->{'response'}, 'ok', 'user/add_remote ok for remote domain'); -#TODO: Add tests to verify the data from add_local... -undef $response; undef $data; - +#TODO: Add tests to verify the data from add_remote... +undef $data; -$response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_remote_domain); -$data = decode_json( $response->content); +$data = exec_apinode("user/available", { "user" => $test_username . "\@" . $test_remote_domain }); is( $data->{'response'}, 'failed', 'user/available has been created'); -undef $response; undef $data; +undef $data; + -$response = $g_ua->get( $api_base . "user/remove?user=" . $test_username . "\@" . $test_remote_domain); -$data = decode_json( $response->content); +$data = exec_apinode("user/remove", { "user" => $test_username . "\@" . $test_remote_domain }); is( $data->{'response'}, 'ok', 'user/remove deleted user'); -undef $response; undef $data; - -TODO: { - local $TODO = 'auth/logout not implemented yet'; - $response = $g_ua->get( $api_base . "auth/logout"); - #$data = decode_json( $response->content); - is( $data->{'response'}, 'ok', 'auth/logout'); - undef $response; undef $data; -} +undef $data; + +logout(); +