From bcfb1c572673f54ccd97864b48549c5fea1b7d80 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 2 Sep 2010 12:28:48 -0500 Subject: add tests --- t/01-basic.t | 81 +++++++++++++++++++++ t/02-file-root.t | 156 ++++++++++++++++++++++++++++++++++++++++ t/data/01/htpasswd | 1 + t/data/02/bar/.keep | 0 t/data/02/foo/.htpasswd | 1 + t/data/02/foo/bar/baz/.htpasswd | 1 + 6 files changed, 240 insertions(+) create mode 100644 t/01-basic.t create mode 100644 t/02-file-root.t create mode 100644 t/data/01/htpasswd create mode 100644 t/data/02/bar/.keep create mode 100644 t/data/02/foo/.htpasswd create mode 100644 t/data/02/foo/bar/baz/.htpasswd (limited to 't') diff --git a/t/01-basic.t b/t/01-basic.t new file mode 100644 index 0000000..9646510 --- /dev/null +++ b/t/01-basic.t @@ -0,0 +1,81 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use Plack::Test; + +use HTTP::Request::Common; +use Path::Class; +use Plack::Builder; + +{ + my $app = builder { + enable 'Auth::Htpasswd', + file => file(__FILE__)->dir->subdir('data', '01')->file('htpasswd'); + sub { + [ + 200, + [ 'Content-Type' => 'text/plain' ], + [ "Hello $_[0]->{REMOTE_USER}" ], + ] + }; + }; + + test_psgi app => $app, client => sub { + my $cb = shift; + + { + my $res = $cb->(GET "/"); + is($res->code, 401, "plain request gets 401"); + } + + { + my $res = $cb->( + GET "/", "Authorization" => "Basic dGVzdDplZGNiYQ", + ); + is($res->code, 401, "request with wrong password gets 401"); + } + + { + my $res = $cb->( + GET "/", "Authorization" => "Basic dHNldDphYmNkZQ", + ); + is($res->code, 401, "request with unknown username gets 401"); + } + + { + my $res = $cb->( + GET "/", "Authorization" => "Basic dGVzdDphYmNkZQ", + ); + is($res->code, 200, "valid authentication succeeds"); + is($res->content, "Hello test", "and gets the right content"); + } + }; +} + +{ + my $app = builder { + enable 'Auth::Htpasswd', + file => file(__FILE__)->dir->subdir('data', '01', 'htpasswd'), + realm => 'my realm'; + sub { + [ + 200, + [ 'Content-Type' => 'text/plain' ], + [ "Hello $_[0]->{REMOTE_USER}" ], + ] + }; + }; + + test_psgi app => $app, client => sub { + my $cb = shift; + + { + my $res = $cb->(GET "/"); + is($res->header('WWW-Authenticate'), 'Basic realm="my realm"', + "can set realm"); + } + }; +} + +done_testing; diff --git a/t/02-file-root.t b/t/02-file-root.t new file mode 100644 index 0000000..f053efb --- /dev/null +++ b/t/02-file-root.t @@ -0,0 +1,156 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use Plack::Test; + +use HTTP::Request::Common; +use Path::Class; +use Plack::Builder; + +my $data_root = file(__FILE__)->dir->subdir('data', '02'); + +{ + my $app = builder { + enable 'Auth::Htpasswd', + file_root => $data_root->subdir('foo'); + sub { + [ + 200, + [ 'Content-Type' => 'text/plain' ], + [ "Hello $_[0]->{REMOTE_USER}: $_[0]->{PATH_INFO}" ], + ] + }; + }; + + test_psgi app => $app, client => sub { + my $cb = shift; + + { + my $res = $cb->(GET "/"); + is($res->code, 401, "plain request gets 401"); + } + + { + my $res = $cb->(GET "/", 'Authorization' => 'Basic Zm9vOjEyMzQ'); + is($res->code, 200, "authorized request gets 200"); + is($res->content, 'Hello foo: /', "and gets the right content"); + } + + { + my $res = $cb->(GET "/bar.txt"); + is($res->code, 401, "plain request gets 401"); + } + + { + my $res = $cb->(GET "/bar.txt", + 'Authorization' => 'Basic Zm9vOjEyMzQ'); + is($res->code, 200, "authorized request gets 200"); + is($res->content, 'Hello foo: /bar.txt', + "and gets the right content"); + } + + { + my $res = $cb->(GET "/bar", 'Authorization' => 'Basic Zm9vOjEyMzQ'); + is($res->code, 200, "authorized request gets 200"); + is($res->content, 'Hello foo: /bar', "and gets the right content"); + } + + { + my $res = $cb->(GET "/bar/baz.txt", + 'Authorization' => 'Basic Zm9vOjEyMzQ'); + is($res->code, 200, "authorized request gets 200"); + is($res->content, 'Hello foo: /bar/baz.txt', + "and gets the right content"); + } + + { + my $res = $cb->(GET "/bar/baz", + 'Authorization' => 'Basic Zm9vOjEyMzQ'); + is($res->code, 401, "user foo isn't authorized for this path"); + } + + { + my $res = $cb->(GET "/bar/baz", + 'Authorization' => 'Basic YmF6OjQzMjE'); + is($res->code, 200, "but user baz is"); + is($res->content, 'Hello baz: /bar/baz', + "and gets the right content"); + } + + { + my $res = $cb->(GET "/bar/baz/quux.txt", + 'Authorization' => 'Basic Zm9vOjEyMzQ'); + is($res->code, 401, "user foo isn't authorized for this path"); + } + + { + my $res = $cb->(GET "/bar/baz/quux.txt", + 'Authorization' => 'Basic YmF6OjQzMjE'); + is($res->code, 200, "but user baz is"); + is($res->content, 'Hello baz: /bar/baz/quux.txt', + "and gets the right content"); + } + + { + my $res = $cb->(GET "/rab/zab"); + is($res->code, 401, "plain request gets 401"); + } + + { + my $res = $cb->(GET "/rab/zab", + 'Authorization' => 'Basic Zm9vOjEyMzQ'); + is($res->code, 200, "authorized request gets 200"); + is($res->content, 'Hello foo: /rab/zab', + "and gets the right content"); + } + }; +} + +{ + my $app = builder { + enable 'Auth::Htpasswd', + file_root => $data_root->subdir('bar'); + sub { + [ + 200, + [ 'Content-Type' => 'text/plain' ], + [ "Hello $_[0]->{REMOTE_USER}: $_[0]->{PATH_INFO}" ], + ] + }; + }; + + test_psgi app => $app, client => sub { + my $cb = shift; + + { + my $res = $cb->(GET "/"); + is($res->code, 401, "no .htpasswd found means deny"); + } + }; +} + +{ + my $app = builder { + enable 'Auth::Htpasswd', + file_root => $data_root->subdir('foo', 'bar'); + sub { + [ + 200, + [ 'Content-Type' => 'text/plain' ], + [ "Hello $_[0]->{REMOTE_USER}: $_[0]->{PATH_INFO}" ], + ] + }; + }; + + test_psgi app => $app, client => sub { + my $cb = shift; + + { + my $res = $cb->(GET "/", 'Authorization' => 'Basic Zm9vOjEyMzQ'); + is($res->code, 401, "don't look up above file_root for .htpasswd"); + } + }; +} + +done_testing; diff --git a/t/data/01/htpasswd b/t/data/01/htpasswd new file mode 100644 index 0000000..137f418 --- /dev/null +++ b/t/data/01/htpasswd @@ -0,0 +1 @@ +test:oKeo66hkZoe4w diff --git a/t/data/02/bar/.keep b/t/data/02/bar/.keep new file mode 100644 index 0000000..e69de29 diff --git a/t/data/02/foo/.htpasswd b/t/data/02/foo/.htpasswd new file mode 100644 index 0000000..7a60e94 --- /dev/null +++ b/t/data/02/foo/.htpasswd @@ -0,0 +1 @@ +foo:O3iy.RDxWd4kY diff --git a/t/data/02/foo/bar/baz/.htpasswd b/t/data/02/foo/bar/baz/.htpasswd new file mode 100644 index 0000000..2406f3c --- /dev/null +++ b/t/data/02/foo/bar/baz/.htpasswd @@ -0,0 +1 @@ +baz:PjRVVGhf7kx0w -- cgit v1.2.3-54-g00ecf