package tests_common;
#  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 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;

	$param->{'session'} = $session;
	$param->{'auth_key'} = $auth_key;

	$response = $g_ua->post( $url, $param );
	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->post( $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->post( $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->post( $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;
