summaryrefslogtreecommitdiff
path: root/t/mocked.t
blob: 57a44397b8753c34e328a06c6b8f522c8229cab9 (plain)
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
use Mojo::Base -strict;
use Mojolicious;
use Test::Mojo;
use Test::More;

{
  use Mojolicious::Lite;
  plugin OAuth2 => {mocked => {key => '42'}};

  get '/no-redirect' => sub {
    my $c = shift;

    $c->oauth2->get_token_p('mocked', {redirect => 0})->then(
      sub {
        return $c->render(text => 'No token') unless my $provider_res = shift;    # Redirect
        return $c->render(text => "Token $provider_res->{access_token}");
      }
    )->catch(sub { $c->reply->exception(shift) });
  };

  get '/profile' => sub {
    my $c = shift;

    $c->oauth2->get_token_p('mocked')->then(
      sub {
        return unless my $provider_res = shift;                                   # Redirect
        return $c->render(text => "Token $provider_res->{access_token}");
      }
    )->catch(sub { $c->reply->exception(shift) });
  };
}

my $t = Test::Mojo->new;

$t->get_ok('/profile')->status_is(302);
my $location     = Mojo::URL->new($t->tx->res->headers->location);
my $callback_url = Mojo::URL->new($location->query->param('redirect_uri'));
is($location->query->param('client_id'), '42', 'got client_id');

$t->get_ok($location)->status_is(200)->element_exists('a');
my $res = Mojo::URL->new($t->tx->res->dom->at('a')->{href});
is($res->path,                 $callback_url->path, 'Returns to the right place');
is($res->query->param('code'), 'fake_code',         'Includes fake code');

$t->get_ok($res)->status_is(200)->content_is('Token fake_token');
$t->get_ok('/profile?error=access_denied')->status_is(500)->content_like(qr{>access_denied<});

$t->get_ok('/no-redirect')->status_is(200)->content_like(qr{No token});

done_testing;