X-Git-Url: https://git.defcon.no/?a=blobdiff_plain;f=api%2Ft%2Fuser.t;h=6373c6cdf696b1dcb5fd77aeeadf899bfd0faab9;hb=32b42c418a3bc21529c8992916c6ead97c15ff1e;hp=6548f26f7f8a4159fd497c4aad4dd1751ec322a4;hpb=be41f0805a67c31020a0910511d15f4c70603f59;p=hermes diff --git a/api/t/user.t b/api/t/user.t index 6548f26..6373c6c 100644 --- a/api/t/user.t +++ b/api/t/user.t @@ -1,142 +1,155 @@ #!/usr/bin/perl - -use Test::More 'no_plan'; +# Copyright (c) 2012, Gjøvik University College +# All rights reserved. + +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the Gjøvik University College nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY Gjøvik University College ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL Gjøvik University College BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 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"; +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); +login_apikey(); + +# First: fetch a supported domain from the API... +$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'); + +# NOW: Set the $test_domain to something useful (i.e. the first reported domain) +$test_domain = $data->{'list'}[0]; +$test_remote_domain = "external." . $test_domain; - #$data = decode_json( $response->content); - is( $data->{'response'}, 'ok', 'auth/login'); - undef $response; undef $data; -} +ok($test_domain, 'test_domain set.'); +ok($test_remote_domain, 'test_remote_domain set.'); +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(); +