Compare commits
27 commits
master
...
remote_tes
| Author | SHA1 | Date | |
|---|---|---|---|
| a3e14141c0 | |||
| 0b7b76d315 | |||
| 2e6c90b8b0 | |||
| f557a5fb72 | |||
| fc68457ace | |||
| 1a51906892 | |||
| 178527cb30 | |||
| ce69862008 | |||
| 765a1ccf5b | |||
| 1798f05e47 | |||
| c144c7bfa8 | |||
| ce99956d11 | |||
| f8d1f45b4a | |||
| 83fa294881 | |||
| 2e41abbdd6 | |||
| 97f791aa4b | |||
| 65ebd89464 | |||
| 89101280ee | |||
| e62e8d57b2 | |||
| 33105b2aa7 | |||
| 19e1e894ae | |||
| 0a8564226d | |||
| a68bc7f8c0 | |||
| 900e39f3a9 | |||
| a90ca02942 | |||
| 4aa73db77b | |||
| 714a4abfcd |
8 changed files with 482 additions and 1 deletions
9
.gitignore
vendored
9
.gitignore
vendored
|
|
@ -32,4 +32,11 @@
|
|||
# Ignore master key for decrypting credentials and more.
|
||||
/config/master.key
|
||||
.vscode
|
||||
.env
|
||||
.env
|
||||
|
||||
# Ignore config file
|
||||
*.cfg
|
||||
|
||||
# Do not git perl dependencies
|
||||
test/remote/bin
|
||||
test/remote/lib
|
||||
107
test/remote/NRT/Batch.pm
Normal file
107
test/remote/NRT/Batch.pm
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
use NRT::Test;
|
||||
use NRT::Helper;
|
||||
use NRT::User;
|
||||
use NRT::Activity;
|
||||
use NRT::Task;
|
||||
|
||||
package Batch;
|
||||
|
||||
sub new {
|
||||
my ($classe, %config) = @_;
|
||||
my $this = {};
|
||||
$classe = ref($classe) || $classe;
|
||||
bless ( $this, $classe );
|
||||
$this->{config} = {%config} ;
|
||||
$this->{tester} = Test->new( %config );
|
||||
$this->{users} = [];
|
||||
$this->{users_index} = 0;
|
||||
$this->{default_user_index} = "";
|
||||
return $this;
|
||||
}
|
||||
|
||||
sub test_rq_empty {
|
||||
my $this = shift;
|
||||
print "\nTesting requests\n";
|
||||
my $rq = $this->{tester}->get_code();
|
||||
return $this->{tester}->assertEqual( $rq, "404 Not Found" );
|
||||
}
|
||||
|
||||
sub test_user {
|
||||
my $this = shift;
|
||||
print "\nTesting users\n";
|
||||
$this->{tester}->test_user_authentification( $this->get_default_user );
|
||||
$this->{tester}->test_user_already_created( $this->get_default_user );
|
||||
my $user2_index = $this->add_user(
|
||||
$this->{config}{User}{Email},
|
||||
$this->{config}{User}{Username},
|
||||
$this->{config}{User}{Password} );
|
||||
$this->{tester}->test_user_not_exists(
|
||||
$this->{users}[$user2_index]);
|
||||
$this->{tester}->test_user_create(
|
||||
$this->{users}[$user2_index]);
|
||||
$this->{tester}->test_user_exists(
|
||||
$this->{users}[$user2_index]);
|
||||
$this->{tester}->test_user_remove(
|
||||
$this->{users}[$user2_index]);
|
||||
$this->{tester}->test_user_removed(
|
||||
$this->{users}[$user2_index]);
|
||||
$this->{tester}->test_user_already_removed(
|
||||
$this->{users}[$user2_index]);
|
||||
}
|
||||
|
||||
sub add_user {
|
||||
my ($this, $email, $username, $password) = @_ ;
|
||||
push(
|
||||
@{$this->{users}},
|
||||
User->new( $email, $username, $password, $this->{tester})) ;
|
||||
return $this->{users_index}++;
|
||||
}
|
||||
|
||||
sub add_default_user {
|
||||
my ($this) = @_ ;
|
||||
return $this->{default_user_index} = $this->add_user(
|
||||
$this->{config}{Authentification}{Email},
|
||||
$this->{config}{Authentification}{Username},
|
||||
$this->{config}{Authentification}{Password} )
|
||||
}
|
||||
|
||||
sub get_default_user {
|
||||
my ($this) = @_ ;
|
||||
if( $this->{default_user_index} eq "" ){
|
||||
$this->add_default_user ;
|
||||
}
|
||||
return $this->{users}[ $this->{default_user_index} ];
|
||||
}
|
||||
|
||||
sub test_activity {
|
||||
my $this = shift;
|
||||
print "\nTesting activities\n";
|
||||
my $activity = Activity->new( "test_1", $this->get_default_user );
|
||||
$this->{tester}->test_create_activity( $activity );
|
||||
$this->{tester}->test_activity_exists( $activity );
|
||||
my $task = Task->new( "test_1", $activity );
|
||||
$this->{tester}->test_task_not_exists( $task );
|
||||
$this->{tester}->test_create_task( $task );
|
||||
$this->{tester}->test_task_exists( $task );
|
||||
$this->{tester}->test_task_remove( $task );
|
||||
$this->{tester}->test_task_not_exists( $task );
|
||||
$this->{tester}->test_activity_remove( $activity );
|
||||
$this->{tester}->test_activity_not_exists( $activity );
|
||||
}
|
||||
|
||||
sub run {
|
||||
my $this = shift;
|
||||
$this->test_rq_empty();
|
||||
$this->test_user();
|
||||
$this->test_activity();
|
||||
$this->brief();
|
||||
}
|
||||
|
||||
sub brief {
|
||||
my $this = shift;;
|
||||
print "\nResults :\n";
|
||||
Helper::printColor( "bright_green", "Tests succeed : ".$this->{tester}->{countSuccess}."\n" );
|
||||
Helper::printColor( "bright_red", "Tests failed : ".$this->{tester}->{countFailure}."\n" );
|
||||
}
|
||||
|
||||
return 1;
|
||||
13
test/remote/NRT/Helper.pm
Normal file
13
test/remote/NRT/Helper.pm
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
use Term::ANSIColor;
|
||||
|
||||
package Helper;
|
||||
|
||||
sub printColor {
|
||||
my $color = (exists $_[0])? $_[0] : "";
|
||||
my $text = (exists $_[0])? $_[1] : "";
|
||||
print Term::ANSIColor::color( "$color" );
|
||||
print "$text";
|
||||
print Term::ANSIColor::color('reset')
|
||||
}
|
||||
|
||||
return 1;
|
||||
231
test/remote/NRT/Test.pm
Normal file
231
test/remote/NRT/Test.pm
Normal file
|
|
@ -0,0 +1,231 @@
|
|||
use NRT::Helper;
|
||||
use LWP::UserAgent;
|
||||
|
||||
package Test;
|
||||
require Exporter;
|
||||
|
||||
sub new {
|
||||
my ($classe, %config) = @_;
|
||||
my $this = {};
|
||||
$classe = ref($classe) || $classe;
|
||||
bless ( $this, $classe );
|
||||
$this->{url} = "$config{Authentification}{Protocol}$config{Authentification}{Host}$config{Authentification}{Path}";
|
||||
$this->{ua} = LWP::UserAgent->new(timeout => 10);
|
||||
$this->{ua}->env_proxy;
|
||||
$this->{countSuccess} = 0;
|
||||
$this->{countFailure} = 0;
|
||||
return $this;
|
||||
}
|
||||
|
||||
sub rq {
|
||||
my $this = shift ;
|
||||
my $path = (exists $_[0])? $_[0] : "";
|
||||
my $method = (exists $_[1])? $_[1] : "";
|
||||
my $content = (exists $_[2])? $_[2] : "";
|
||||
my $authorization = (exists $_[3])? $_[3] : "";
|
||||
|
||||
my $req = HTTP::Request->new( $method, "$this->{url}$path" );
|
||||
$req->header( 'Content-Type' => 'application/json' );
|
||||
|
||||
if( $authorization ){
|
||||
$req->header(Authorization => $authorization);
|
||||
}
|
||||
|
||||
$req->content( $content );
|
||||
my $res = $this->{ua}->request($req) ;
|
||||
|
||||
return ( $res->is_success() && $this->is_json( $res->decoded_content ) )?
|
||||
$this->json_parse( $res->decoded_content )
|
||||
: $res->status_line();
|
||||
}
|
||||
|
||||
sub assertEqual {
|
||||
my ($this, $value1, $value2, $equal ) = @_;
|
||||
$equal //= 1;
|
||||
my $success = 0;
|
||||
|
||||
my $test = ( $value1 eq $value2 );
|
||||
$test = ( $equal )? $test : !$test;
|
||||
$assertedComparator = ($equal)? "equal" : "NOT equal" ;
|
||||
$unassertedComparator = ($equal)? "NOT equal" : "equal" ;
|
||||
|
||||
if( $test )
|
||||
{
|
||||
$this->record_result( 1 );
|
||||
print " $value1 ";
|
||||
Helper::printColor( "bright_blue", $assertedComparator);
|
||||
print " $value2\n";
|
||||
return 1;
|
||||
}else{
|
||||
$this->record_result( 0 );
|
||||
print " $value1 ";
|
||||
Helper::printColor( "bright_blue", $assertedComparator);
|
||||
print " $value2\n";
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
|
||||
sub assertNotEqual {
|
||||
my $this = shift;
|
||||
return $this->assertEqual( $_[0], $_[1], 0 );
|
||||
}
|
||||
|
||||
sub get_code {
|
||||
my ($this, $path) = @_;
|
||||
if(!defined $path){
|
||||
$path = "";
|
||||
#exit 1;
|
||||
}
|
||||
my $response = $this->{ua}->get( "$this->{url}$path" );
|
||||
|
||||
if ($response->is_success) {
|
||||
return $response->decoded_content;
|
||||
}else{
|
||||
return $response->status_line;
|
||||
}
|
||||
}
|
||||
|
||||
sub record_result {
|
||||
my $this = shift ;
|
||||
my $status = (exists $_[0])? $_[0] : false;
|
||||
|
||||
if( $status ){
|
||||
Helper::printColor( "green", "Success :");
|
||||
$this->{countSuccess}++;
|
||||
}else{
|
||||
Helper::printColor( "red", "Failure :");
|
||||
$this->{countFailure}++;
|
||||
}
|
||||
}
|
||||
|
||||
sub json_parse {
|
||||
my ($this, $json_text) = @_;
|
||||
my $json = JSON->new;
|
||||
$res = eval{ $json->decode("$json_text") };
|
||||
return ($@)?
|
||||
0 :
|
||||
$res ;
|
||||
}
|
||||
|
||||
sub is_json {
|
||||
my ($this, $json_text) = @_;
|
||||
my $json = JSON->new;
|
||||
$res = eval{ $json->decode("$json_text") };
|
||||
return ($@)? 0 : 1 ;
|
||||
}
|
||||
|
||||
sub get_countSuccess {
|
||||
my $this = shift;
|
||||
return $this->{countSuccess} ;
|
||||
}
|
||||
|
||||
sub get_countFailure{
|
||||
return $countFailure ;
|
||||
}
|
||||
|
||||
sub get_url{
|
||||
my $this = shift;
|
||||
return $this->{url} ;
|
||||
}
|
||||
|
||||
sub test_rq_empty {
|
||||
my $this = shift;
|
||||
my $rq = $this->get_code();
|
||||
return $this->assertEqual( $rq, "404 Not Found" );
|
||||
}
|
||||
|
||||
sub test_user_authentification {
|
||||
my ($this, $user) = @_;
|
||||
my $auth = $user->authentification;
|
||||
return $this->assertEqual( $auth->{email}, $user->{email} );
|
||||
}
|
||||
|
||||
sub test_user_exists {
|
||||
my ($this, $user) = @_;
|
||||
my $ruser = $user->fetch;
|
||||
return $this->assertEqual( $ruser->{data}{attributes}{email}, $user->{email} );
|
||||
}
|
||||
|
||||
sub test_user_not_exists {
|
||||
my ($this, $user) = @_;
|
||||
return $this->assertNotEqual( $ruser->{data}{attributes}{email}, $user->{email} );
|
||||
}
|
||||
|
||||
sub test_user_create {
|
||||
my ($this, $user) = @_;
|
||||
my $creation = $user->create();
|
||||
return $this->assertEqual( $creation->{data}{attributes}{email}, $user->{email} );
|
||||
}
|
||||
|
||||
sub test_user_already_removed {
|
||||
my ($this, $user) = @_;
|
||||
return $this->assertEqual($user->remove, '404 Not Found');
|
||||
}
|
||||
|
||||
sub test_user_removed {
|
||||
my ($this, $user) = @_;
|
||||
my $user = $user->fetch();
|
||||
return $this->assertEqual( $user, '404 Not Found' );
|
||||
}
|
||||
|
||||
sub test_user_remove {
|
||||
my ($this, $user) = @_;
|
||||
return $this->assertEqual( $user->remove, '204 No Content');
|
||||
}
|
||||
|
||||
sub test_user_already_created {
|
||||
my ($this, $user) = @_;
|
||||
my $creation = $user->fetch;
|
||||
return $this->assertEqual( $creation->{data}{id}, $user->get_id);
|
||||
}
|
||||
|
||||
sub test_create_activity {
|
||||
my ($this, $activity) = @_;
|
||||
my $res = $activity->create;
|
||||
return $this->assertEqual( $res->{data}{attributes}{name}, $activity->{name} );
|
||||
}
|
||||
|
||||
sub test_activity_remove {
|
||||
my ($this, $activity) = @_;
|
||||
$activity->remove;
|
||||
return $this->assertNotEqual( $ractivity->{data}{id}, $activity->{id} );
|
||||
}
|
||||
|
||||
sub test_activity_exists {
|
||||
my ($this, $activity) = @_;
|
||||
my $ractivity = $activity->fetch;
|
||||
return $this->assertEqual( $ractivity->{data}{id}, $activity->{id} );
|
||||
}
|
||||
|
||||
sub test_activity_not_exists {
|
||||
my ($this, $activity) = @_;
|
||||
my $ractivity = $activity->fetch;
|
||||
return $this->assertNotEqual( $ractivity->{data}{id}, $activity->{id} );
|
||||
}
|
||||
|
||||
sub test_create_task {
|
||||
my ($this, $task) = @_;
|
||||
my $res = $task->create;
|
||||
return $this->assertEqual( $res->{data}{attributes}{name}, $task->{name} );
|
||||
}
|
||||
|
||||
sub test_task_exists {
|
||||
my ($this, $task) = @_;
|
||||
my $rtask = $task->fetch;
|
||||
return $this->assertEqual( $rtask->{data}{id}, $task->{id} );
|
||||
}
|
||||
|
||||
sub test_task_not_exists {
|
||||
my ($this, $task) = @_;
|
||||
my $rtask = $task->fetch;
|
||||
eval {exists $rtask->{data}{id}};
|
||||
return $this->assertNotEqual( $@, 0 );
|
||||
}
|
||||
|
||||
sub test_task_remove {
|
||||
my ($this, $task) = @_;
|
||||
my $rtask = $task->remove;
|
||||
return $this->assertNotEqual( $rtask->{data}{id}, $task->{id} );
|
||||
}
|
||||
|
||||
return 1;
|
||||
85
test/remote/NRT/User.pm
Normal file
85
test/remote/NRT/User.pm
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
use LWP::Simple;
|
||||
use NRT::Test;
|
||||
use NRT::Activity;
|
||||
use Crypt::JWT(decode_jwt);
|
||||
|
||||
package User;
|
||||
|
||||
sub new {
|
||||
my ($classe, $email, $username, $password, $manager) = @_;
|
||||
my $this = {};
|
||||
$classe = ref($classe) || $classe;
|
||||
bless ( $this, $classe );
|
||||
$this->{email} = $email;
|
||||
$this->{username} = $username;
|
||||
$this->{password} = $password;
|
||||
$this->{manager} = $manager;
|
||||
$this->{token} = "";
|
||||
$this->{id} = "";
|
||||
$this->{activities} = [];
|
||||
return $this;
|
||||
}
|
||||
|
||||
sub authentification {
|
||||
my $this = shift ;
|
||||
my $data = "{\"user\" : {\"email\" : \"".$this->{email}."\", \"password\" : \"".$this->{password}."\"}}";
|
||||
$rq = $this->{manager}->rq( "tokens", "POST", $data ); ;
|
||||
$this->{token} = $rq->{token} ;
|
||||
return $rq;
|
||||
}
|
||||
|
||||
sub create {
|
||||
my ($this) = shift ;
|
||||
my $data = "{\"user\" : {\"username\" : \"". $this->{username} ."\", \"email\" : \"".$this->{email}."\", \"password\" : \"".$this->{password}."\"}}";
|
||||
$rq = $this->{manager}->rq( "users", "POST", "$data" );
|
||||
$this->{id} = $rq->{data}{id};
|
||||
return $rq;
|
||||
}
|
||||
|
||||
sub remove {
|
||||
my ($this) = @_ ;
|
||||
my $data = "{\"user\" : {\"username\" : \"". $this->{username} ."\", \"email\" : \"".$this->{email}."\", \"password\" : \"".$this->{password}."\"}}";
|
||||
my $user_id = Crypt::JWT::decode_jwt(token => $this->get_token, ignore_signature => 1)->{user_id};
|
||||
return $this->{manager}->rq( "users/".$user_id, "DELETE", $data, $this->get_token );
|
||||
}
|
||||
|
||||
sub fetch {
|
||||
my ($this) = @_ ;
|
||||
return $this->{manager}->rq(
|
||||
"users/".$this->get_id,
|
||||
"GET",
|
||||
"",
|
||||
$this->get_token );
|
||||
}
|
||||
|
||||
sub get_token {
|
||||
my $this = shift;
|
||||
if ($this->{token} eq ""){
|
||||
$this->authentification;
|
||||
}
|
||||
return (exists $this->{token})?
|
||||
$this->{token} :
|
||||
0 ;
|
||||
}
|
||||
|
||||
sub get_id {
|
||||
my $this = shift;
|
||||
if( !($this->{id} eq "") ){
|
||||
return $this->{id};
|
||||
}
|
||||
$token = $this->get_token;
|
||||
if( !$token ){
|
||||
return 0;
|
||||
}
|
||||
return Crypt::JWT::decode_jwt(token => $token, ignore_signature => 1)->{user_id};
|
||||
}
|
||||
|
||||
sub create_activity {
|
||||
my ($this, $name) = @_;
|
||||
my $activity = Activity->new(
|
||||
$name,
|
||||
$this);;
|
||||
return $activity;
|
||||
}
|
||||
|
||||
return 1;
|
||||
6
test/remote/install.sh
Normal file
6
test/remote/install.sh
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
echo "\nInstalling PERL dependancies\n"
|
||||
echo "Will use cpan. If you would not install cpan, you may need to manually download and install these.\n"
|
||||
cpan install LWP::UserAgent
|
||||
cpan install HTTP::Request
|
||||
cpan install JSON::WebToken
|
||||
cpan install Crypt::JWT
|
||||
12
test/remote/test.cfg.sample
Normal file
12
test/remote/test.cfg.sample
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
[Authentification]
|
||||
Protocol : https://
|
||||
Host : localhost/
|
||||
Path : api/v1/
|
||||
Email : test@cli.su
|
||||
Username : test_remote
|
||||
Password : NJfccjabWSXqmpTRhfXRs6Re2CAIsJyhz3TAo4rpWz2nlMgWqTScmbsXoIlwdHB1
|
||||
|
||||
[Users]
|
||||
Userame : perl_test
|
||||
Password : letestdesesgrandsmorts
|
||||
Email : test@perl.su
|
||||
20
test/remote/test.pl
Normal file
20
test/remote/test.pl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/perl
|
||||
# @file journal.pl
|
||||
# @author nas
|
||||
# @date 31/01/2022
|
||||
# @brief Remote call to the API in order to test
|
||||
BEGIN { push @INC,'.'; };
|
||||
use Config::Std;
|
||||
use LWP::UserAgent;
|
||||
use lib qw(..);
|
||||
use JSON qw( );
|
||||
use Data::Dumper;
|
||||
|
||||
use NRT::Batch;
|
||||
|
||||
print User::v ;
|
||||
read_config 'test.cfg' => our %config;
|
||||
our $batch = Batch->new( %config );
|
||||
print "\nMaking all tests\n";
|
||||
|
||||
print Dumper $batch->run();
|
||||
Loading…
Add table
Add a link
Reference in a new issue