From: Jon Langseth Date: Tue, 17 Jan 2012 21:39:59 +0000 (+0100) Subject: Adding the first of many API tests. Run with perl... X-Git-Url: https://git.defcon.no/?a=commitdiff_plain;h=be41f0805a67c31020a0910511d15f4c70603f59;p=hermes Adding the first of many API tests. Run with perl... --- diff --git a/api/t/debian-perl-packages b/api/t/debian-perl-packages new file mode 100644 index 0000000..a9fc4c7 --- /dev/null +++ b/api/t/debian-perl-packages @@ -0,0 +1 @@ +libjson-perl libjson-xs-perl diff --git a/api/t/user.t b/api/t/user.t new file mode 100644 index 0000000..6548f26 --- /dev/null +++ b/api/t/user.t @@ -0,0 +1,142 @@ +#!/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 = "example.com"; #TODO: Must fetch this from API + +my $test_remote_domain = "external.example.com"; + +# 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 . "user/list"); +isa_ok( $response, 'HTTP::Response', 'user/list $response'); +ok ($response->is_success, 'user/list is_success'); +$data = decode_json( $response->content); +ok($data, 'user/list JSON decode'); +is( $data->{'response'}, 'ok', 'user/list result'); +ok($data->{'list'}, 'user/list array'); +undef $response; undef $data; + +$response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_domain); +$data = decode_json( $response->content); +is( $data->{'response'}, 'ok', 'user/available is available'); +undef $response; 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); +is( $data->{'response'}, 'ok', 'user/add_local created new account'); +#TODO: Add tests to verify the data from add_local... +undef $response; 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; + +$response = $g_ua->get( $api_base . "user/get?user=" . $test_username . "\@" . $test_domain); +$data = decode_json( $response->content); +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); +is( $data->{'response'}, 'ok', 'user/change_pw'); +undef $response; 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); +is( $data->{'response'}, 'ok', 'user/update'); +ok( $data->{'updated'}, 'user/update updated list'); +ok( $data->{'skipped'}, 'user/update skipped list'); +undef $response; undef $data; + +$response = $g_ua->get( $api_base . "user/remove?user=" . $test_username . "\@" . $test_domain); +$data = decode_json( $response->content); +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); +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; + +$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 = 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; + + +$response = $g_ua->get( $api_base . "user/available?user=" . $test_username . "\@" . $test_remote_domain); +$data = decode_json( $response->content); +is( $data->{'response'}, 'failed', 'user/available has been created'); +undef $response; undef $data; + +$response = $g_ua->get( $api_base . "user/remove?user=" . $test_username . "\@" . $test_remote_domain); +$data = decode_json( $response->content); +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; +}