File: authorize.t

package info (click to toggle)
libmastodon-client-perl 0.017-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 336 kB
  • sloc: perl: 1,525; makefile: 9
file content (196 lines) | stat: -rw-r--r-- 4,821 bytes parent folder | download
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
use Test2::V0;
use Test2::Tools::Spec;

use Log::Any::Test;
use Log::Any qw( $log );

use Mastodon::Client;

describe 'authorize' => sub {
  before_each 'Clear log' => sub {
    $log->clear;
  };

  describe 'Needs a client_id and client_secret' => sub {
    my %params;

    case 'No client_id' => sub {
      %params = ( client_secret => 'secret' );
    };

    case 'No client_secret' => sub {
      %params = ( client_id => 'id' );
    };

    case 'Neither' => sub {
      %params = ();
    };

    it 'Dies' => sub {
      my $client = Mastodon::Client->new(
        %params,
        instance => 'botsin.space',
      );

      ok dies { $client->authorize }, 'It dies';

      is $log->msgs, [
        {
          level => 'critical',
          message => match(qr/without client_id and client_secret/),
          category => 'Mastodon',
        },
      ], 'Logged dying message';
    };
  };

  describe 'Warnings' => sub {
    my ( %client_params, $post_response, $warning );

    after_each 'Reset' => sub {
      %client_params = ();
      undef $post_response;
    };

    case 'Already authorized' => sub {
      %client_params = ( access_token => 'foo' );
      $warning = match qr/already authorized/;
    };

    case 'It receives an error' => sub {
      $post_response = {
        error             => 1,
        error_description => 'this is a test'
      };
      $warning = 'this is a test';
    };

    it 'Warns' => sub {
      my $mock = mock 'Mastodon::Client', override => [
        post => sub { return $post_response },
      ];

      my $client = Mastodon::Client->new(
        instance      => 'botsin.space',
        client_id     => 'id',
        client_secret => 'secret',
        %client_params,
      );

      is $client->authorize, $client, 'Method returns $self';
      is $log->msgs, [
        {
          message  => $warning,
          level    => 'warning',
          category => 'Mastodon',
        },
      ], 'Raises expected warning';
    };
  };

  it q{Dies if granted scopes don't match} => sub {
    my $client = Mastodon::Client->new(
      instance      => 'botsin.space',
      client_id     => 'id',
      client_secret => 'secret',
    );

    my $mock = mock 'Mastodon::Client', override => [
      post => sub {
        return { scope => $_[2]->{scope} . ' extra' };
      },
    ];

    ok dies { $client->authorize }, 'It dies';

    is $log->msgs, [
      {
        message  => match(qr/scopes do not match/),
        level    => 'critical',
        category => 'Mastodon',
      },
    ];
  };

  it 'Sets access token and authorization time' => sub {
    my $client = Mastodon::Client->new(
      instance      => 'botsin.space',
      client_id     => 'id',
      client_secret => 'secret',
    );

    my $mock = mock 'Mastodon::Client', override => [
      post => sub {
        return {
          scope        => $_[2]->{scope},
          created_at   => '2018-12-16T12:20:40.123Z',
          access_token => 'a token',
        };
      },
    ];

    is $client->authorize, object {
      call access_token => 'a token';
      call authorized   => '2018-12-16T12:20:40';
      etc;
    }, 'Method sets attributes';

    is $log->msgs, [], 'Nothing logged';
  };

  describe 'Accepts different credentials' => sub {
    my ( %check, %params, $data );

    before_each 'Clear data' => sub { undef $data };

    case 'No params' => sub {
      note 'This is legacy behaviour, we should probably die instead';
      %params = ();
      %check = ( username => '', password => '', grant_type => 'password' );
    };

    case 'Access code' => sub {
      %params = ( access_code => 'access_code' );
      %check = ( grant_type => 'authorization_code', code => 'access_code' );
    };

    case 'Username and password' => sub {
      %check = %params = ( username => 'username', password => 'password' );
      $check{grant_type} = 'password';
    };

    it 'Works' => sub {
      my $mock = mock 'Mastodon::Client', override => [
        post => sub {
          ( undef, undef, $data ) = @_;
          return {
            created_at   => 1,
            scope        => $data->{scope} // '',
            access_token => 'mocked_token',
          };
        },
      ];

      my $client = Mastodon::Client->new(
        instance      => 'botsin.space',
        client_id     => 'id',
        client_secret => 'secret',
      );

      $client->authorize( %params );

      is $data, hash {
        field $_             => $check{$_} for keys %check;
        field client_id      => $client->client_id;
        field client_secret  => $client->client_secret;
        field redirect_uri   => T;
        field scope          => T;
        end;
      }, 'Posted correct data payload';

      is $log->msgs, [], 'Nothing logged';
    };
  };
};

done_testing();