File: 30refresh.t

package info (click to toggle)
libnet-oauth2-perl 0.67-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 224 kB
  • sloc: perl: 760; makefile: 7
file content (78 lines) | stat: -rw-r--r-- 2,159 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl
use strict;
use warnings FATAL => "all";

use Test::More;
use HTTP::Response;
use JSON::MaybeXS;

use Net::OAuth2;
use Net::OAuth2::AccessToken;
use Net::OAuth2::Profile::WebServer;

my $json = JSON::MaybeXS->new;

BEGIN {
   eval "require Test::Mock::LWP::Dispatch";
   plan skip_all => "Test::Mock::LWP::Dispatch not installed" if $@;

   Test::Mock::LWP::Dispatch->import;
   plan tests => 7;
}

my $at_response = {
    token_type => 'Bearer',
    expires_in => time(),
    access_token => 'my-new-access-token',
    id_token => 'my-new-id-token',
};

my %p = (
    access_token_path  => "/o/oauth2/token",
    authorize_path     => "/o/oauth2/auth",
    client_id          => "my-id",
    client_secret      => "my-secret",
    refresh_token_path => "/o/oauth2/token",
    scope =>
      "http://www.google.com/reader/api http://www.google.com/reader/atom",
    site => "https://accounts.google.com"
);

$mock_ua->map(
    "$p{site}$p{refresh_token_path}" => sub {
        my $req = shift;
#        diag Dumper $req->content;
        HTTP::Response->new(
            200, "OK",
            [ "content-type" => "application/json", ],
            $json->encode($at_response),
        );
    }
);

my $id     = 'my-id';
my $secret = 'my-secret';
my $access_token_str = 'my-access_token';
my $refresh_token_str = 'my-refresh_token';

ok my $profile = Net::OAuth2::Profile::WebServer->new(%p)
   ,'instantiate Net::OAuth2::Profile::WebServer';

ok my $access_token = Net::OAuth2::AccessToken->new( 
    refresh_token => $refresh_token_str,
    access_token  => $access_token_str,
    profile       => $profile)
   ,'instantiate Net::OAuth2::AccessToken with Webserver Profile';

ok $access_token->refresh, 'access_token->refresh';
is $access_token->access_token, $at_response->{access_token},
  'response access token has been set';

is $access_token->refresh_token, $refresh_token_str,
  'refresh token remains unchanged';

$at_response->{refresh_token} = 'new-refresh-token';
ok $access_token->refresh, 'access_token->refresh';
is $access_token->refresh_token, $at_response->{refresh_token},
  'new response refresh token has been set';