Browse Source

test user creation and deletion

nas 2 years ago
parent
commit
178527cb30
3 changed files with 69 additions and 9 deletions
  1. 49
    5
      test/remote/NRT/Batch.pm
  2. 19
    3
      test/remote/NRT/Test.pm
  3. 1
    1
      test/remote/test.pl

+ 49
- 5
test/remote/NRT/Batch.pm View File

@@ -34,19 +34,58 @@ sub test_user {
34 34
   my $user2_index = $this->add_user(
35 35
     $this->{config}{User}{Email},
36 36
     $this->{config}{User}{Username},
37
-    $this->{config}{User}{Password} );
38
-  return $this->test_user_create( $user2_index );
37
+      $this->{config}{User}{Password} );
38
+  $this->test_user_not_exists( $user2_index );
39
+  $this->test_user_create( $user2_index );
40
+  $this->test_user_exists( $user2_index );
41
+  $this->test_user_remove( $user2_index );
42
+  $this->test_user_removed( $user2_index );
43
+  $this->test_user_already_removed( $user2_index );
39 44
 }
40 45
 
41 46
 sub test_user_authentification {
42 47
   my ($this, $index) = @_;
43 48
   my $auth = $this->{users}[ $index ]->authentification;
44
-  return $this->{tester}->assertEqual( $auth->{email}, $this->{config}{Authentification}{Email} );
49
+  return $this->{tester}->assertEqual( $auth->{email}, $this->{users}[ $index ]->{email} );
50
+}
51
+
52
+sub test_user_exists {
53
+  my ($this, $index) = @_;
54
+  my $user = $this->{users}[ $index ]->fetch;
55
+  return $this->{tester}->assertEqual( $user->{data}{attributes}{email}, $this->{users}[ $index ]->{email} );
56
+}
57
+
58
+sub test_user_not_exists {
59
+  my ($this, $index) = @_;
60
+  my $user = $this->{users}[ $index ]->fetch();
61
+  return $this->{tester}->assertEqual( $user, 0 );
62
+}
63
+
64
+sub test_user_removed {
65
+  my ($this, $index) = @_;
66
+  my $user = $this->{users}[ $index ]->fetch();
67
+  return $this->{tester}->assertEqual( $user, '404 Not Found' );
45 68
 }
46 69
 
47 70
 sub test_user_create {
48 71
   my ($this, $index) = @_;
49
-  return $this->create_user( $index );
72
+  my $creation = $this->{users}[ $index ]->create();
73
+  return $this->{tester}->assertEqual( $creation->{data}{attributes}{email}, $this->{users}[ $index ]->{email} );
74
+}
75
+
76
+sub test_user_already_removed {
77
+  my ($this, $index) = @_;
78
+  return $this->{tester}->assertEqual($this->remove_user( $index ), '404 Not Found');
79
+}
80
+
81
+sub test_user_remove {
82
+  my ($this, $index) = @_;
83
+  return $this->{tester}->assertEqual($this->remove_user( $index ), '204 No Content');
84
+}
85
+
86
+sub remove_user {
87
+  my ($this, $index) = @_;
88
+  return $this->{users}[ $index ]->remove;
50 89
 }
51 90
 
52 91
 sub test_user_already_created {
@@ -60,6 +99,11 @@ sub create_user {
60 99
   return $this->{users}[ $index ]->create;  
61 100
 }
62 101
 
102
+sub remove_user {
103
+  my ($this, $index) = @_;
104
+  return $this->{users}[ $index ]->remove;  
105
+}
106
+
63 107
 sub add_user {
64 108
   my ($this, $email, $username, $password) = @_ ;
65 109
   push(
@@ -71,7 +115,7 @@ sub add_user {
71 115
 sub run {
72 116
   my $this = shift;
73 117
   $this->test_rq_empty();
74
-  return $this->test_user();
118
+  $this->test_user();
75 119
   $this->brief();
76 120
 }
77 121
 

+ 19
- 3
test/remote/NRT/Test.pm View File

@@ -22,13 +22,19 @@ sub rq {
22 22
   my $path = (exists $_[0])? $_[0] : "";
23 23
   my $method = (exists $_[1])? $_[1] : "";
24 24
   my $content = (exists $_[2])? $_[2] : "";
25
+  my $authorization = (exists $_[3])? $_[3] : "";
25 26
 
26
-  my $req = HTTP::Request->new( $method, $this->{url}.$path );
27
+  my $req = HTTP::Request->new( $method, "$this->{url}$path" );
27 28
   $req->header( 'Content-Type' => 'application/json' );
29
+
30
+  if( $authorization ){
31
+    $req->header(Authorization => $authorization);
32
+  }
33
+
28 34
   $req->content( $content );
29 35
   my $res = $this->{ua}->request($req) ;
30 36
 
31
-  return ( $res->is_success() )?
37
+  return ( $res->is_success() && $this->is_json( $res->decoded_content ) )?
32 38
       $this->json_parse( $res->decoded_content )
33 39
       : $res->status_line();
34 40
 }
@@ -93,7 +99,17 @@ sub record_result {
93 99
 sub json_parse {
94 100
   my ($this, $json_text) = @_;
95 101
   my $json = JSON->new;
96
-  return $json->decode("$json_text");
102
+  $res = eval{ $json->decode("$json_text") };
103
+  return ($@)?
104
+    0 :
105
+    $res ;
106
+}
107
+
108
+sub is_json {
109
+  my ($this, $json_text) = @_;
110
+  my $json = JSON->new;
111
+  $res = eval{ $json->decode("$json_text") };
112
+  return ($@)? 0 : 1 ;
97 113
 }
98 114
 
99 115
 sub get_countSuccess {

+ 1
- 1
test/remote/test.pl View File

@@ -18,4 +18,4 @@ read_config 'test.cfg' => our %config;
18 18
 our $batch = Batch->new( %config );
19 19
 print "\nMaking all tests\n";
20 20
 
21
-print $batch->run();
21
+print Dumper $batch->run();

Loading…
Cancel
Save