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