File: overdrive-patron.t

package info (click to toggle)
libwebservice-ils-perl 0.18-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 348 kB
  • sloc: perl: 2,645; makefile: 2; sh: 2
file content (451 lines) | stat: -rw-r--r-- 15,766 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/perl

use Modern::Perl;

use Test::More tests => 11;
use Data::Dumper;

use FindBin;
use lib "$FindBin::Bin/lib";
use T::Discovery;
use T::OverDrive;

my $EMAIL = 'nobody@nowhere.com';
my %NON_LOCKABLE_FORMAT = map { $_ => 1 } (
    'ebook-overdrive', 'audiobook-overdrive', 'ebook-mediado'
);
#use WebService::ILS::OverDrive;
#$WebService::ILS::OverDrive::DEBUG = 1;

use_ok('WebService::ILS::OverDrive::Patron');

SKIP: {
    skip "Not testing OverDrive::Patron API, WEBSERVICE_ILS_TEST_OVERDRIVE_PATRON not set", 10
      unless $ENV{WEBSERVICE_ILS_TEST_OVERDRIVE_PATRON};

    my $od_id     = $ENV{OVERDRIVE_TEST_CLIENT_ID}
        or BAIL_OUT("Env OVERDRIVE_TEST_CLIENT_ID not set");
    my $od_secret = $ENV{OVERDRIVE_TEST_CLIENT_SECRET}
        or BAIL_OUT("Env OVERDRIVE_TEST_CLIENT_SECRET not set");
    my $od_website_id = $ENV{OVERDRIVE_TEST_WEBSITE_ID}
        or BAIL_OUT("Env OVERDRIVE_TEST_WEBSITE_ID not set");
    my $od_authorization_name = $ENV{OVERDRIVE_TEST_AUTHORIZATION_NAME} || 'odapilibrary';
    my $od_user_id = $ENV{OVERDRIVE_TEST_USER_ID}
        or BAIL_OUT("Env OVERDRIVE_TEST_USER_ID not set");
    my $od_password = $ENV{OVERDRIVE_TEST_USER_PASSWORD};

    my $od = WebService::ILS::OverDrive::Patron->new({
        test => 1,
        client_id => $od_id,
        client_secret => $od_secret,
    });

    ok($od->auth_by_user_id($od_user_id, $od_password, $od_website_id, $od_authorization_name), "auth_by_user_id()");

    my $patron = T::OverDrive::patron($od);
    my ($hold_limit, $checkout_limit, $active);
    if ($patron) {
        $hold_limit = $patron->{hold_limit};
        $checkout_limit = $patron->{checkout_limit};
        $active = $patron->{active};
    }
    #BAIL_OUT("Patron not active") unless $active;

    clear($od) if $ENV{OVERDRIVE_TEST_CLEAR};

    my $init_checkouts = $od->checkouts;
    my $init_holds = $od->holds;

    my ($total_init_holds);
    if ($init_holds) {
        $total_init_holds = $init_holds->{total};
    }
    ok(defined $total_init_holds, "Holds") or diag(Dumper($init_holds));

    my ($total_init_checkouts);
    if ($init_checkouts) {
        $total_init_checkouts = $init_checkouts->{total};
    }
    ok(defined $total_init_checkouts, "Checkouts") or diag(Dumper($init_checkouts));

    my ($items, $random_page) = T::Discovery::search_all_random_page( $od );
    BAIL_OUT("No items in search results, cannot test circulation") unless $items && @$items;

    subtest "Place hold" => sub {
        if ($total_init_holds >= $hold_limit) {
            my $item = $init_holds->{items}[0];
            test_remove_hold($od, $item->{id});
            test_place_hold($od, $init_holds, $item);
        }
        else {
            my $item;
            while ($items) {
                $item = pick_unused_item($od, [@{ $init_checkouts->{items} }, @{ $init_holds->{items} }], $items);
                last if $item;
                diag( Dumper($init_checkouts, $init_holds, $items) );
                ($items, $random_page) = T::Discovery::search_all_random_page( $od, $random_page + 1 );
            }
            BAIL_OUT("Cannot find appropriate item to place hold") unless $item;

            my $item_id = test_place_hold($od, $init_holds, $item);
            SKIP: {
                skip "Could not place hold", 1 unless $item_id;

                test_remove_hold($od, $item_id);
            }
        }
    };

    if ($total_init_checkouts >= $checkout_limit - 1) {
        foreach(@{ $init_checkouts->{items} }) {
            next if $_->{format};

            $od->return($_->{id}) or BAIL_OUT("Checkouts at full capacity and cannot return");
            $total_init_checkouts--;

            last if $total_init_checkouts < $checkout_limit - 1;
        }
        $init_checkouts = $od->checkouts;
    }
    BAIL_OUT("Checkouts at full capacity and no item can be returned")
      if $total_init_checkouts >= $checkout_limit;

    subtest "Checkout no format" => sub {
        my $item;
        while ($items) {
            $item = pick_unused_item($od, $init_checkouts->{items}, $items, 'AVAILABLE_ONLY');
            last if $item;
            diag( Dumper($init_checkouts, $items) );
            ($items, $random_page) = T::Discovery::search_all_random_page( $od, $random_page + 1 );
        }
        BAIL_OUT("Cannot find appropriate item to checkout") unless $item;

        test_checkout($od, $init_checkouts, $item);
        $total_init_checkouts++;
    };

    subtest "Checkout with format" => sub {
        SKIP: {
            skip "Not testing checkout with format locking, OVERDRIVE_TEST_LOCK_FORMAT not set", 1
              unless $ENV{OVERDRIVE_TEST_LOCK_FORMAT};

            SKIP: {
                skip "Checkouts at full capacity, cannot test checkout with locked format", 1
                  if $total_init_checkouts >= $checkout_limit;

                $init_checkouts = $od->checkouts;
                my $item;
                while ($items) {
                    $item = pick_unused_item($od, $init_checkouts->{items}, $items, 'AVAILABLE_ONLY');
                    last if $item;
                    ($items, $random_page) = T::Discovery::search_all_random_page( $od, $random_page + 1 );
                }

                SKIP: {
                    skip "Cannot find appropriate item to checkout with locked format", 1
                      unless $item;

                    test_checkout_with_format($od, $init_checkouts, $item);
                }
            }
        }
    };

    subtest "Standard search" => sub { T::OverDrive::search( $od ) };

    $patron = $od->native_patron;
    ok($patron && $patron->{patronId}, "Native patron")
      or diag(Dumper($patron));

    subtest "Native search"   => sub { T::OverDrive::native_search( $od ) };
}

sub test_place_hold {
    my ($od, $init_holds, $item) = @_;

    my $item_id = $item->{id};
    my ($hold, $cannot_place_hold);
    {
        local $@;
        $hold = eval { $od->place_hold($item_id, $EMAIL, "AUTO_CHECKOUT") };
        if ($@) {
            diag("$@\n".Dumper($item));
            $cannot_place_hold = ($@ =~ m/not allowed to place a hold on this title/io);
        }
    }
    my ($hold_item_id, $hold_item_id_uc, $total_holds, $ok);
    SKIP: {
        skip "Cannot place hold", 1 if $cannot_place_hold;

        if ($hold) {
            $hold_item_id = uc $hold->{id};
            $hold_item_id_uc = uc $hold_item_id;
            $total_holds = $hold->{total};
        }
#       $ok = ok($hold_item_id_uc eq uc($item_id) && $total_holds == scalar(@$hold_items) + 1, "Place hold")
        $ok = ok($hold_item_id_uc && $hold_item_id_uc eq uc($item_id), "Place hold")
          or diag(Dumper($init_holds, $item, $hold));
    }

    SKIP: {
        skip "Cannot place hold", 2 unless $ok;

        my $holds = $od->holds;
        my $found;
        foreach (@{ $holds->{items} }) {
            if (uc($_->{id}) eq $hold_item_id_uc) {
                $found = 1;
                last;
            }
        }
        ok ($found && $holds->{total} == $init_holds->{total} + 1, "Hold in the list")
          or diag(Dumper($hold, $holds, $init_holds));

        my $same_hold = $od->place_hold($item_id, $EMAIL, "AUTO_CHECKOUT");
        ok(
            $same_hold->{id} eq $hold->{id} &&
            $same_hold->{placed_datetime} eq $hold->{placed_datetime},
            "Place same hold")
          or diag(Dumper($same_hold, $hold));
    }

    return $hold_item_id;
}

sub test_remove_hold {
    my ($od, $item_id) = @_;

    ok( $od->remove_hold($item_id), "Remove hold" );
    ok( $od->remove_hold($item_id), "Remove hold again");
}

sub test_checkout {
    my ($od, $init_checkouts, $item) = @_;

    my $item_id = $item->{id};
    my $checkout;
    {
        local $@;
        $checkout = eval { $od->checkout($item_id) };
        diag("$@\n".Dumper($item)) if $@;
    }
    my ($checkout_item_id, $checkout_item_id_uc);
    if ($checkout) {
        $checkout_item_id = $checkout->{id};
        $checkout_item_id_uc = uc $checkout_item_id;
    }
    my $ok = ok($checkout_item_id_uc && $checkout_item_id_uc eq uc($item_id), "Checkout")
      or diag(Dumper($init_checkouts, $item, $checkout));

    SKIP: {
        skip "Cannot checkout", 6 unless $ok;

        my $checkouts = $od->checkouts;
        my $found;
        foreach (@{ $checkouts->{items} }) {
            if (uc($_->{id}) eq $checkout_item_id_uc) {
                $found = $_;
                last;
            }
        }
#       ok ($found && $checkouts->{total} == $init_checkouts->{total} + 1, "Checkout in the list")
#       Sometimes API loses marbles when it comes to counting
        ok ($found, "Checkout in the list")
          or diag(Dumper($checkout, $checkouts, $init_checkouts));

        SKIP: {
            skip "Checkout not found", 5 unless $found;

            my $formats = $od->checkout_formats($item_id);
            $ok = $formats && scalar(keys %$formats);
            ok ($ok, "Checkout formats")
              or diag(Dumper($formats, $item));
            my $available_format;
            if ($ok) {
                while ( my($format, $available) = each %$formats ) {
                    if ($available) {
                        $available_format = $format;
                        last;
                    }
                }
                diag(Dumper($formats)) unless $available_format;
            }
            SKIP: {
                skip "Available format not found", 1 unless $available_format;
                test_download_url($od, $item_id, $available_format);
            }

            my $same_checkout = $od->checkout($item_id);
            ok(
                $same_checkout->{id} eq $checkout->{id} &&
                $same_checkout->{checkout_datetime} eq $checkout->{checkout_datetime},
                "Same checkout not locked")
              or diag(Dumper($same_checkout, $checkout));

            ok( $od->return($item_id), "Return" );
            # This is a bug in OverDrive API
            $ok = eval { $od->return($item_id) };
            if ($@) {
                diag("Return again: $@\nPassing nevertheless");
                $ok = 1;
            }
            ok( $ok, "Return again");

            my $lockable_format;
            SKIP: {
                skip "Not testing format locking, OVERDRIVE_TEST_LOCK_FORMAT not set", 3
                  unless $ENV{OVERDRIVE_TEST_LOCK_FORMAT};

                while ( my($format, $available) = each %$formats ) {
                    unless ($available) {
                        $lockable_format = $format;
                        last;
                    }
                }
                diag(Dumper($formats)) unless $lockable_format;

                SKIP: {
                    skip "Checkout formats cannot be locked in", 3
                      unless $lockable_format;

                    $checkout = $od->checkout($item_id);

                    my $res = $od->lock_format($item_id, $lockable_format);
                    ok($res eq $lockable_format, "Lock format $lockable_format") or diag("Format: $res");

                    my $same_checkout = $od->checkout($item_id);
                    ok(
                        $same_checkout->{id} eq $checkout->{id} &&
                        $same_checkout->{checkout_datetime} eq $checkout->{checkout_datetime} &&
                        $same_checkout->{format} eq $lockable_format,
                        "Same checkout format locked")
                      or diag(Dumper($same_checkout, $checkout, $lockable_format));

                    test_download_url($od, $item_id, $lockable_format);
                }
            }
        }
    }

    return $checkout_item_id;
}

sub test_checkout_with_format {
    my ($od, $init_checkouts, $item) = @_;

    my $item_id = $item->{id};
    my ($checkout, $lockable_format);
    foreach my $format ( @{$item->{formats} || []} ) {
        local $@;
        $checkout = eval { $od->checkout($item_id, $format) };
        diag("$@\n".Dumper($item)) if $@;
        if ($checkout) {
            $lockable_format = $format;
            last;
        }
    }
    diag(Dumper($item)) unless $checkout;
    SKIP: {
        skip "Checkout formats cannot be locked in", 4
          unless $checkout;

        my $checkout_item_id = $checkout->{id};
        my $checkout_item_id_uc = uc $checkout_item_id;
        my $checkout_item_format = $checkout->{format};
        my $checkout_item_format_lc = $checkout_item_format ? lc ($checkout_item_format) : "";
        my $ok = ok(
            $checkout_item_id_uc && $checkout_item_id_uc eq uc($item_id) &&
            $checkout_item_format_lc eq lc($lockable_format),
            "Checkout with locked format $lockable_format"
        ) or diag(Dumper($init_checkouts, $item, $checkout));

        SKIP: {
            skip "Mismatched checkout with locked format $lockable_format", 3 unless $ok;

            my $checkouts = $od->checkouts;
            my $found;
            foreach (@{ $checkouts->{items} }) {
                if (uc($_->{id}) eq $checkout_item_id_uc) {
                    $found = $_;
                    last;
                }
            }
            ok (
                $found && $found->{format} && lc($found->{format}) eq $checkout_item_format_lc,
                "Checkout in the list"
            ) or diag(Dumper($checkout, $checkouts, $init_checkouts));

            SKIP: {
                skip "Checkout not found", 2 unless $found;

                my $same_checkout = $od->checkout($item_id);
                ok(
                    uc($same_checkout->{id}) eq $checkout_item_id_uc &&
                    $same_checkout->{checkout_datetime} eq $checkout->{checkout_datetime} &&
                    $same_checkout->{format} && lc($same_checkout->{format}) eq $checkout_item_format_lc,
                    "Same checkout without specified format")
                  or diag(Dumper($same_checkout, $checkout));

                $same_checkout = $od->checkout($item_id, $lockable_format);
                ok(
                    uc($same_checkout->{id}) eq $checkout_item_id_uc &&
                    $same_checkout->{checkout_datetime} eq $checkout->{checkout_datetime} &&
                    $same_checkout->{format} && lc($same_checkout->{format}) eq $checkout_item_format_lc,
                    "Same checkout with specified format")
                  or diag(Dumper($same_checkout, $checkout));
            }
        }

        return $checkout_item_id;
    }

    return;
}

sub test_download_url  {
    my ($od, $item_id, $format) = @_;

    my $download_url = $od->checkout_download_url(
        $item_id,
        $format,
        "http://wherever.com/failure",
        "http://wherever.com/success"
    );
    ok($download_url, "Download url");
}

sub pick_unused_item {
    my ($od, $used_items, $pool_items, $available_only) = @_;

    POOL_ITEMS_LOOP:
    foreach my $pi (@$pool_items) {
        if ($used_items) {
            my $id_uc = uc $pi->{id};
            foreach (@$used_items) {
                next POOL_ITEMS_LOOP if uc($_->{id}) eq $id_uc;
            }
        }

        my $formats = $pi->{formats} or next;
        next if $available_only && !$od->is_item_available($pi->{id});

        foreach my $format (@$formats) {
            next if $format eq 'periodicals-nook';
            next if $format =~ m/-streaming$/o;
            next if $available_only && $NON_LOCKABLE_FORMAT{$format};

            return $pi;
        }
    }
    return;
}

sub clear {
    my ($od) = @_;

    my $checkouts = $od->checkouts;
    eval { $od->return($_->{id}) } foreach @{$checkouts->{items}};

    my $holds = $od->holds;
    $od->remove_hold($_->{id}) foreach @{$holds->{items}};
}