|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | +use lib 't/lib'; |
| 4 | + |
| 5 | +use MetaCPAN::Server::Test qw( app GET test_psgi ); |
| 6 | +use Test::More; |
| 7 | + |
| 8 | +# The `oauth_tmp` cookie carries the OAuth params from the leg that starts the |
| 9 | +# flow to the leg that finishes it (the provider callback). When those legs land |
| 10 | +# on different *.metacpan.org hosts a host-only cookie is lost and login |
| 11 | +# silently fails, so the cookie domain is configurable via `oauth_cookie_domain` |
| 12 | +# (set to the shared parent domain in production). We ship a sane default and |
| 13 | +# allow it to be overridden -- or disabled (host-only) with an empty value. |
| 14 | + |
| 15 | +test_psgi app, sub { |
| 16 | + my $cb = shift; |
| 17 | + |
| 18 | + subtest 'applies the cookie domain from config' => sub { |
| 19 | + my $domain = MetaCPAN::Server->config->{oauth_cookie_domain}; |
| 20 | + ok defined $domain && length $domain, |
| 21 | + 'a default oauth_cookie_domain is configured'; |
| 22 | + |
| 23 | + my $res = $cb->( GET '/login/github?client_id=metacpan.dev' ); |
| 24 | + like $res->header('Set-Cookie'), qr/oauth_tmp=/, |
| 25 | + 'oauth_tmp cookie set'; |
| 26 | + like $res->header('Set-Cookie'), qr/domain=\Q$domain\E/i, |
| 27 | + "Set-Cookie carries the configured domain ($domain)"; |
| 28 | + }; |
| 29 | + |
| 30 | + subtest 'config override is honored' => sub { |
| 31 | + my $config = MetaCPAN::Server->config; |
| 32 | + local $config->{oauth_cookie_domain} = '.example.test'; |
| 33 | + |
| 34 | + my $res = $cb->( GET '/login/github?client_id=metacpan.dev' ); |
| 35 | + like $res->header('Set-Cookie'), qr/domain=\.example\.test/i, |
| 36 | + 'overridden domain applied'; |
| 37 | + }; |
| 38 | + |
| 39 | + subtest 'empty oauth_cookie_domain disables the domain (host-only)' => |
| 40 | + sub { |
| 41 | + my $config = MetaCPAN::Server->config; |
| 42 | + local $config->{oauth_cookie_domain} = q{}; |
| 43 | + |
| 44 | + my $res = $cb->( GET '/login/github?client_id=metacpan.dev' ); |
| 45 | + unlike $res->header('Set-Cookie'), qr/domain=/i, |
| 46 | + 'no Domain attribute when explicitly disabled'; |
| 47 | + }; |
| 48 | +}; |
| 49 | + |
| 50 | +done_testing; |
0 commit comments