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
   my $user2_index = $this->add_user(
34
   my $user2_index = $this->add_user(
35
     $this->{config}{User}{Email},
35
     $this->{config}{User}{Email},
36
     $this->{config}{User}{Username},
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
 sub test_user_authentification {
46
 sub test_user_authentification {
42
   my ($this, $index) = @_;
47
   my ($this, $index) = @_;
43
   my $auth = $this->{users}[ $index ]->authentification;
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
 sub test_user_create {
70
 sub test_user_create {
48
   my ($this, $index) = @_;
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
 sub test_user_already_created {
91
 sub test_user_already_created {
60
   return $this->{users}[ $index ]->create;  
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
 sub add_user {
107
 sub add_user {
64
   my ($this, $email, $username, $password) = @_ ;
108
   my ($this, $email, $username, $password) = @_ ;
65
   push(
109
   push(
71
 sub run {
115
 sub run {
72
   my $this = shift;
116
   my $this = shift;
73
   $this->test_rq_empty();
117
   $this->test_rq_empty();
74
-  return $this->test_user();
118
+  $this->test_user();
75
   $this->brief();
119
   $this->brief();
76
 }
120
 }
77
 
121
 

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

22
   my $path = (exists $_[0])? $_[0] : "";
22
   my $path = (exists $_[0])? $_[0] : "";
23
   my $method = (exists $_[1])? $_[1] : "";
23
   my $method = (exists $_[1])? $_[1] : "";
24
   my $content = (exists $_[2])? $_[2] : "";
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
   $req->header( 'Content-Type' => 'application/json' );
28
   $req->header( 'Content-Type' => 'application/json' );
29
+
30
+  if( $authorization ){
31
+    $req->header(Authorization => $authorization);
32
+  }
33
+
28
   $req->content( $content );
34
   $req->content( $content );
29
   my $res = $this->{ua}->request($req) ;
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
       $this->json_parse( $res->decoded_content )
38
       $this->json_parse( $res->decoded_content )
33
       : $res->status_line();
39
       : $res->status_line();
34
 }
40
 }
93
 sub json_parse {
99
 sub json_parse {
94
   my ($this, $json_text) = @_;
100
   my ($this, $json_text) = @_;
95
   my $json = JSON->new;
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
 sub get_countSuccess {
115
 sub get_countSuccess {

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

18
 our $batch = Batch->new( %config );
18
 our $batch = Batch->new( %config );
19
 print "\nMaking all tests\n";
19
 print "\nMaking all tests\n";
20
 
20
 
21
-print $batch->run();
21
+print Dumper $batch->run();

Loading…
Cancel
Save