test activity assertion

This commit is contained in:
nas 2022-02-20 14:15:15 +01:00
commit f557a5fb72
4 changed files with 93 additions and 25 deletions

View file

@ -1,6 +1,7 @@
use NRT::Test; use NRT::Test;
use NRT::Helper; use NRT::Helper;
use NRT::User; use NRT::User;
use NRT::Activity;
package Batch; package Batch;
@ -13,6 +14,7 @@ sub new {
$this->{tester} = Test->new( %config ); $this->{tester} = Test->new( %config );
$this->{users} = []; $this->{users} = [];
$this->{users_index} = 0; $this->{users_index} = 0;
$this->{default_user_index} = "";
return $this; return $this;
} }
@ -24,18 +26,12 @@ sub test_rq_empty {
sub test_user { sub test_user {
my $this = shift; my $this = shift;
my $user1_index = $this->add_user( $this->{tester}->test_user_authentification( $this->get_default_user );
$this->{config}{Authentification}{Email}, $this->{tester}->test_user_already_created( $this->get_default_user );
$this->{config}{Authentification}{Username},
$this->{config}{Authentification}{Password} );
$this->{tester}->test_user_authentification(
$this->{users}[$user1_index]);
$this->{tester}->test_user_already_created(
$this->{users}[$user1_index]);
my $user2_index = $this->add_user( my $user2_index = $this->add_user(
$this->{config}{User}{Email}, $this->{config}{User}{Email},
$this->{config}{User}{Username}, $this->{config}{User}{Username},
$this->{config}{User}{Password} ); $this->{config}{User}{Password} );
$this->{tester}->test_user_not_exists( $this->{tester}->test_user_not_exists(
$this->{users}[$user2_index]); $this->{users}[$user2_index]);
$this->{tester}->test_user_create( $this->{tester}->test_user_create(
@ -58,10 +54,37 @@ sub add_user {
return $this->{users_index}++; 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;
my $activity = Activity->new( "test_1", $this->get_default_user );
$this->{tester}->test_create_activity( $activity );
$this->{tester}->test_activity_exists( $activity );
$this->{tester}->test_activity_remove( $activity );
$this->{tester}->test_activity_not_exists( $activity );
}
sub run { sub run {
my $this = shift; my $this = shift;
$this->test_rq_empty(); #$this->test_rq_empty();
$this->test_user(); $this->test_user();
return $this->test_activity();
$this->brief(); $this->brief();
} }

View file

@ -41,23 +41,25 @@ sub rq {
sub assertEqual { sub assertEqual {
my ($this, $value1, $value2, $equal ) = @_; my ($this, $value1, $value2, $equal ) = @_;
$equal ||= 1; $equal //= 1;
my $success = 0; my $success = 0;
my $test = ( $value1 eq $value2 ); my $test = ( $value1 eq $value2 );
$test = ( $equal )? $test : !$test; $test = ( $equal )? $test : !$test;
$assertedComparator = ($equal)? "equal" : "NOT equal" ;
$unassertedComparator = ($equal)? "NOT equal" : "equal" ;
if( $test ) if( $test )
{ {
$this->record_result( 1 ); $this->record_result( 1 );
print " $value1 "; print " $value1 ";
Helper::printColor( "bright_blue", "equal"); Helper::printColor( "bright_blue", $assertedComparator);
print " $value2\n"; print " $value2\n";
return 1; return 1;
}else{ }else{
$this->record_result( 0 ); $this->record_result( 0 );
print " $value1 "; print " $value1 ";
Helper::printColor( "bright_blue", "NOT equal"); Helper::printColor( "bright_blue", $assertedComparator);
print " $value2\n"; print " $value2\n";
return 0; return 0;
}; };
@ -65,7 +67,7 @@ sub assertEqual {
sub assertNotEqual { sub assertNotEqual {
my $this = shift; my $this = shift;
$this->assertEqual( $_[0], $_[1], 0 ); return $this->assertEqual( $_[0], $_[1], 0 );
} }
sub get_code { sub get_code {
@ -146,8 +148,7 @@ sub test_user_exists {
sub test_user_not_exists { sub test_user_not_exists {
my ($this, $user) = @_; my ($this, $user) = @_;
my $user = $user->fetch(); return $this->assertNotEqual( $ruser->{data}{attributes}{email}, $user->{email} );
return $this->assertEqual( $user, 0 );
} }
sub test_user_create { sub test_user_create {
@ -174,8 +175,32 @@ sub test_user_remove {
sub test_user_already_created { sub test_user_already_created {
my ($this, $user) = @_; my ($this, $user) = @_;
my $creation = $user->create; my $creation = $user->fetch;
return $this->assertNotEqual( $creation, "422 Unprocessable Entity"); 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) = @_;
return $activity->remove;
return $this->assertEqual( $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} );
} }
return 1; return 1;

View file

@ -1,5 +1,6 @@
use LWP::Simple; use LWP::Simple;
use NRT::Test; use NRT::Test;
use NRT::Activity;
use Crypt::JWT(decode_jwt); use Crypt::JWT(decode_jwt);
package User; package User;
@ -15,6 +16,7 @@ sub new {
$this->{manager} = $manager; $this->{manager} = $manager;
$this->{token} = ""; $this->{token} = "";
$this->{id} = ""; $this->{id} = "";
$this->{activities} = [];
return $this; return $this;
} }
@ -43,12 +45,11 @@ sub remove {
sub fetch { sub fetch {
my ($this) = @_ ; my ($this) = @_ ;
$token = $this->get_token; return $this->{manager}->rq(
if( !$token ){ "users/".$this->get_id,
return 0; "GET",
} "",
my $user_id = Crypt::JWT::decode_jwt(token => $token, ignore_signature => 1)->{user_id}; $this->get_token );
return $this->{manager}->rq( "users/".$user_id, "GET", "", $this->get_token );
} }
sub get_token { sub get_token {
@ -61,4 +62,24 @@ sub get_token {
0 ; 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; return 1;

View file

@ -10,7 +10,6 @@ use lib qw(..);
use JSON qw( ); use JSON qw( );
use Data::Dumper; use Data::Dumper;
use NRT::User;
use NRT::Batch; use NRT::Batch;
print User::v ; print User::v ;