Line 0
Link Here
|
|
|
1 |
package t::db_dependent::Api::V1::Messages; |
2 |
|
3 |
use Modern::Perl; |
4 |
use Test::More; |
5 |
|
6 |
use t::lib::TestObjects::ObjectFactory; |
7 |
use t::lib::TestObjects::MessageQueueFactory; |
8 |
|
9 |
use C4::Letters; |
10 |
|
11 |
sub get200 { |
12 |
my ($class, $restTest, $driver) = @_; |
13 |
my $testContext = $restTest->get_testContext(); |
14 |
my $activeUser = $restTest->get_activeBorrower(); |
15 |
my $path = $restTest->get_routePath(); |
16 |
|
17 |
#Create the test context. |
18 |
my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup( |
19 |
{ |
20 |
subject => "The quick brown fox", |
21 |
content => "Jumps over the lazy dog.", |
22 |
cardnumber => $activeUser->{'_result'}->{'_column_data'}->{cardnumber}, |
23 |
message_transport_type => 'sms', |
24 |
from_address => '11A001@example.com', |
25 |
}, undef, $testContext, undef, undef); |
26 |
|
27 |
my $messagenumber = $messages->{'11A001@example.com'}->{message_id}; |
28 |
|
29 |
$driver->get_ok($path => {Accept => 'text/json'}); |
30 |
$driver->status_is(200); |
31 |
|
32 |
$driver->json_has("message_id", "Message id in JSON."); |
33 |
|
34 |
return 1; |
35 |
} |
36 |
|
37 |
sub get404 { |
38 |
my ($class, $restTest, $driver) = @_; |
39 |
my $path = $restTest->get_routePath(); |
40 |
my $testContext = $restTest->get_testContext(); |
41 |
|
42 |
$driver->get_ok($path => {Accept => 'text/json'}); |
43 |
$driver->status_is(404); |
44 |
|
45 |
return 1; |
46 |
} |
47 |
|
48 |
sub get_n_200 { |
49 |
my ($class, $restTest, $driver) = @_; |
50 |
my $testContext = $restTest->get_testContext(); |
51 |
my $activeUser = $restTest->get_activeBorrower(); |
52 |
my $path = $restTest->get_routePath(); |
53 |
|
54 |
#Create the test context. |
55 |
my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup( |
56 |
{ |
57 |
subject => "The quick brown fox", |
58 |
content => "Jumps over the lazy dog.", |
59 |
cardnumber => $activeUser->{'_result'}->{'_column_data'}->{cardnumber}, |
60 |
message_transport_type => 'sms', |
61 |
from_address => '11A002@example.com', |
62 |
}, undef, $testContext, undef, undef); |
63 |
|
64 |
my $messagenumber = $messages->{'11A002@example.com'}->{message_id}; |
65 |
$path =~ s/\{messagenumber\}/$messagenumber/; |
66 |
|
67 |
$driver->get_ok($path => {Accept => 'text/json'}); |
68 |
$driver->status_is(200); |
69 |
|
70 |
$driver->json_is('/message_id' => $messagenumber, "Message $messagenumber found!"); |
71 |
|
72 |
return 1; |
73 |
} |
74 |
|
75 |
sub get_n_404 { |
76 |
my ($class, $restTest, $driver) = @_; |
77 |
my $path = $restTest->get_routePath(); |
78 |
|
79 |
$path =~ s/\{messagenumber\}/12349329003007/; |
80 |
$driver->get_ok($path => {Accept => 'text/json'}); |
81 |
$driver->status_is(404); |
82 |
|
83 |
return 1; |
84 |
} |
85 |
|
86 |
sub post201 { |
87 |
my ($class, $restTest, $driver) = @_; |
88 |
my $path = $restTest->get_routePath(); |
89 |
my $activeUser = $restTest->get_activeBorrower(); |
90 |
|
91 |
$driver->post_ok($path => {Accept => 'text/json'} => json => { |
92 |
subject => "hello", |
93 |
content => "world", |
94 |
message_transport_type => "email", |
95 |
borrowernumber => $activeUser->{'_result'}->{'_column_data'}->{borrowernumber}, |
96 |
}); |
97 |
$driver->status_is(201); |
98 |
|
99 |
# dequeue the message right after so that get 404 tests will work |
100 |
my $json = $driver->tx->res->json(); |
101 |
C4::Letters::DequeueLetter({ message_id => $json->{message_id} }); |
102 |
|
103 |
return 1; |
104 |
} |
105 |
|
106 |
sub post400 { |
107 |
my ($class, $restTest, $driver) = @_; |
108 |
my $path = $restTest->get_routePath(); |
109 |
my $activeUser = $restTest->get_activeBorrower(); |
110 |
|
111 |
#post with missing mandatory parameters |
112 |
$driver->post_ok($path => {Accept => 'text/json'} => json => { |
113 |
subject => "hello" |
114 |
}); |
115 |
$driver->status_is(400); |
116 |
|
117 |
return 1; |
118 |
} |
119 |
|
120 |
sub post_n_resend204 { |
121 |
my ($class, $restTest, $driver) = @_; |
122 |
my $testContext = $restTest->get_testContext(); |
123 |
my $activeUser = $restTest->get_activeBorrower(); |
124 |
my $path = $restTest->get_routePath(); |
125 |
|
126 |
#Create the test context. |
127 |
my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup( |
128 |
{ |
129 |
subject => "The quick brown fox", |
130 |
content => "Jumps over the lazy dog.", |
131 |
cardnumber => $activeUser->{'_result'}->{'_column_data'}->{cardnumber}, |
132 |
message_transport_type => 'sms', |
133 |
from_address => '11A003@example.com', |
134 |
}, undef, $testContext, undef, undef); |
135 |
|
136 |
# send and get the message |
137 |
C4::Letters::SendQueuedMessages(); |
138 |
my $message = C4::Letters::GetMessage($messages->{'11A003@example.com'}->{message_id}); |
139 |
my $messagenumber = $messages->{'11A003@example.com'}->{message_id}; |
140 |
$path =~ s/\{messagenumber\}/$messagenumber/; |
141 |
|
142 |
# sms status should be failed |
143 |
is($message->{status}, "failed", "Send failed correctly"); |
144 |
|
145 |
$driver->post_ok($path => {Accept => 'text/json'}); |
146 |
$driver->status_is(204); |
147 |
|
148 |
$message = C4::Letters::GetMessage($messages->{'11A003@example.com'}->{message_id}); |
149 |
is($message->{status}, "pending", "Resend set message status to pending correctly"); |
150 |
|
151 |
return 1; |
152 |
} |
153 |
|
154 |
sub post_n_resend404 { |
155 |
my ($class, $restTest, $driver) = @_; |
156 |
my $path = $restTest->get_routePath(); |
157 |
|
158 |
$path =~ s/\{messagenumber\}/12349329003007/; |
159 |
$driver->post_ok($path => {Accept => 'text/json'}); |
160 |
$driver->status_is(404); |
161 |
|
162 |
return 1; |
163 |
} |
164 |
|
165 |
sub put_n_200 { |
166 |
my ($class, $restTest, $driver) = @_; |
167 |
my $testContext = $restTest->get_testContext(); |
168 |
my $activeUser = $restTest->get_activeBorrower(); |
169 |
my $path = $restTest->get_routePath(); |
170 |
|
171 |
#Create the test context. |
172 |
my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup( |
173 |
{ |
174 |
subject => "The quick brown fox", |
175 |
content => "Jumps over the lazy dog.", |
176 |
cardnumber => $activeUser->{'_result'}->{'_column_data'}->{cardnumber}, |
177 |
message_transport_type => 'sms', |
178 |
from_address => '11A003@example.com', |
179 |
}, undef, $testContext, undef, undef); |
180 |
|
181 |
my $messagenumber = $messages->{'11A003@example.com'}->{message_id}; |
182 |
$path =~ s/\{messagenumber\}/$messagenumber/; |
183 |
|
184 |
$driver->put_ok($path => {Accept => 'text/json'} => json => { |
185 |
subject => "hello", |
186 |
content => "world", |
187 |
}); |
188 |
$driver->status_is(200); |
189 |
|
190 |
my $json = $driver->tx->res->json(); |
191 |
is($json->{subject}, "hello", "Put updated message subject correctly"); |
192 |
|
193 |
return 1; |
194 |
} |
195 |
|
196 |
sub put_n_400 { |
197 |
my ($class, $restTest, $driver) = @_; |
198 |
my $testContext = $restTest->get_testContext(); |
199 |
my $activeUser = $restTest->get_activeBorrower(); |
200 |
my $path = $restTest->get_routePath(); |
201 |
|
202 |
$path =~ s/\{messagenumber\}/12349329003007/; |
203 |
$driver->put_ok($path => {Accept => 'text/json'} => json => { |
204 |
message_transport_type => "hello", #invalid transport type |
205 |
}); |
206 |
$driver->status_is(400); |
207 |
|
208 |
return 1; |
209 |
} |
210 |
|
211 |
sub put_n_404 { |
212 |
my ($class, $restTest, $driver) = @_; |
213 |
my $path = $restTest->get_routePath(); |
214 |
|
215 |
$path =~ s/\{messagenumber\}/12349329003007/; |
216 |
$driver->put_ok($path => {Accept => 'text/json'}); |
217 |
$driver->status_is(404); |
218 |
|
219 |
return 1; |
220 |
} |
221 |
|
222 |
sub delete_n_204 { |
223 |
my ($class, $restTest, $driver) = @_; |
224 |
my $testContext = $restTest->get_testContext(); |
225 |
my $activeUser = $restTest->get_activeBorrower(); |
226 |
my $path = $restTest->get_routePath(); |
227 |
|
228 |
#Create the test context. |
229 |
my $messages = t::lib::TestObjects::MessageQueueFactory->createTestGroup( |
230 |
{ |
231 |
subject => "The quick brown fox", |
232 |
content => "Jumps over the lazy dog.", |
233 |
cardnumber => $activeUser->{'_result'}->{'_column_data'}->{cardnumber}, |
234 |
message_transport_type => 'sms', |
235 |
from_address => '11A004@example.com', |
236 |
}, undef, $testContext, undef, undef); |
237 |
|
238 |
my $messagenumber = $messages->{'11A004@example.com'}->{message_id}; |
239 |
$path =~ s/\{messagenumber\}/$messagenumber/; |
240 |
|
241 |
$driver->delete_ok($path => {Accept => 'text/json'}); |
242 |
$driver->status_is(204); |
243 |
|
244 |
return 1; |
245 |
} |
246 |
|
247 |
sub delete_n_404 { |
248 |
my ($class, $restTest, $driver) = @_; |
249 |
my $path = $restTest->get_routePath(); |
250 |
|
251 |
$path =~ s/\{messagenumber\}/12349329003007/; |
252 |
$driver->delete_ok($path => {Accept => 'text/json'}); |
253 |
$driver->status_is(404); |
254 |
|
255 |
return 1; |
256 |
} |
257 |
|
258 |
1; |