Compare commits

...
Sign in to create a new pull request.

27 commits

Author SHA1 Message Date
nas
a3e14141c0 add create and remove task 2022-02-21 18:32:46 +01:00
nas
0b7b76d315 showing test types 2022-02-21 17:39:01 +01:00
nas
2e6c90b8b0 enable every test 2022-02-20 14:20:10 +01:00
nas
f557a5fb72 test activity assertion 2022-02-20 14:15:15 +01:00
nas
fc68457ace move all test in the test class 2022-02-19 16:17:55 +01:00
nas
1a51906892 add install script for the remote test 2022-02-19 15:59:08 +01:00
nas
178527cb30 test user creation and deletion 2022-02-19 12:52:23 +01:00
nas
ce69862008 add missing class 2022-02-19 12:51:50 +01:00
nas
765a1ccf5b test user creation refusal (already created) 2022-02-18 15:27:26 +01:00
nas
1798f05e47 fix, now really adding new user 2022-02-18 14:54:36 +01:00
nas
c144c7bfa8 test user authentification 2022-02-16 13:51:39 +01:00
nas
ce99956d11 Merge branch 'remote_test' of https://git.yannweb.net/cli/Chronobriq-API into remote_test 2022-02-15 12:56:15 +01:00
nas
f8d1f45b4a remove demo from api 2022-02-15 12:56:05 +01:00
nas
83fa294881 remove relicate 2022-02-15 11:40:34 +01:00
nas
2e41abbdd6 remove wrong declaration 2022-02-14 17:47:11 +01:00
nas
97f791aa4b re structure test, in object oriented way 2022-02-14 17:41:55 +01:00
nas
65ebd89464 add test user 2022-02-03 15:28:34 +01:00
nas
89101280ee basic testing funciton and skelton 2022-02-03 15:11:36 +01:00
nas
e62e8d57b2 get token 2022-02-01 10:23:46 +01:00
nas
33105b2aa7 need email 2022-01-31 19:55:05 +01:00
nas
19e1e894ae move to correct path 2022-01-31 19:34:01 +01:00
nas
0a8564226d move to correct path 2022-01-31 19:33:09 +01:00
nas
a68bc7f8c0 functions to call api 2022-01-31 18:29:29 +01:00
nas
900e39f3a9 ignore config file, write it from sample 2022-01-31 18:16:31 +01:00
nas
a90ca02942 forge url, and get a response (404) 2022-01-31 18:15:43 +01:00
nas
4aa73db77b split host name and path 2022-01-31 18:05:53 +01:00
nas
714a4abfcd sample config for remote test 2022-01-31 17:59:39 +01:00
8 changed files with 482 additions and 1 deletions

9
.gitignore vendored
View file

@ -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
View 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
View 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
View 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
View 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
View 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

View 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
View 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();