summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2010-09-02 12:28:48 -0500
committerJesse Luehrs <doy@tozt.net>2010-09-02 12:28:48 -0500
commitbcfb1c572673f54ccd97864b48549c5fea1b7d80 (patch)
treebf763d274943052cdeb8acd6c8d21290564edc23 /t
parentfcb937cc7d38451b532c195a4f5a197fa6846cbb (diff)
downloadplack-middleware-auth-htpasswd-bcfb1c572673f54ccd97864b48549c5fea1b7d80.tar.gz
plack-middleware-auth-htpasswd-bcfb1c572673f54ccd97864b48549c5fea1b7d80.zip
add tests
Diffstat (limited to 't')
-rw-r--r--t/01-basic.t81
-rw-r--r--t/02-file-root.t156
-rw-r--r--t/data/01/htpasswd1
-rw-r--r--t/data/02/bar/.keep0
-rw-r--r--t/data/02/foo/.htpasswd1
-rw-r--r--t/data/02/foo/bar/baz/.htpasswd1
6 files changed, 240 insertions, 0 deletions
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
--- /dev/null
+++ b/t/data/02/bar/.keep
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