summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2017-12-16 18:36:46 +0000
committerdakkar <dakkar@thenautilus.net>2017-12-16 18:44:04 +0000
commite81b1cb52e386f03b4a2912fbffb88bac8e428d0 (patch)
treef093382da01052e2e2cc4f6538f150395c0df8fa /t
parentusers model (diff)
downloadUltramarine-e81b1cb52e386f03b4a2912fbffb88bac8e428d0.tar.gz
Ultramarine-e81b1cb52e386f03b4a2912fbffb88bac8e428d0.tar.bz2
Ultramarine-e81b1cb52e386f03b4a2912fbffb88bac8e428d0.zip
authentication middleware
Diffstat (limited to 't')
-rw-r--r--t/tests/middleware/authentication.t65
1 files changed, 65 insertions, 0 deletions
diff --git a/t/tests/middleware/authentication.t b/t/tests/middleware/authentication.t
new file mode 100644
index 0000000..b696aaa
--- /dev/null
+++ b/t/tests/middleware/authentication.t
@@ -0,0 +1,65 @@
+use v6.d.PREVIEW;
+use Test;
+use Ultramarine::Middleware::Authentication;
+
+class TestUsers {
+ has @.responses;
+ has @!calls;
+
+ method authenticate(|c) {
+ @!calls.push(c);
+ return @.responses.shift;
+ }
+
+ method calls() { return @!calls }
+}
+
+class TestRequest {
+ has %!query;
+ submethod BUILD(:%!query) {}
+ method query-value($key) {
+ return [ %!query{$key} ];
+ }
+}
+
+my $users = TestUsers.new(responses=>[0,1,0,1]);
+
+my $auth = Ultramarine::Middleware::Authentication.new(:$users);
+
+sub test_auth(%query,$expected,$message?) {
+ my $req = TestRequest.new(:%query);
+ my $supply = supply { emit $req };
+ react {
+ whenever $auth.process($supply) -> $maybe-authed {
+ my $is-authed = $maybe-authed ~~ Ultramarine::Request::Authed;
+ if ($expected) {
+ subtest {
+ ok($is-authed,'should be authed');
+ is($maybe-authed.user,%query<u>,'with the correct user');
+ }, $message;
+ }
+ else {
+ nok($is-authed,$message);
+ }
+ done;
+ };
+ };
+}
+
+my %user-password = ( :u<user>,:p<password> );
+my %user-token = ( :u<user>,:t<token>,:s<salt> );
+
+# the fail-pass-fail-pass comes from $users.responses
+test_auth(%user-password,0,'auth fail should pass through');
+test_auth(%user-password,1,'auth pass should apply trait');
+test_auth(%user-token,0,'auth fail should pass through');
+test_auth(%user-token,1,'auth pass should apply trait');
+
+is-deeply(
+ $users.calls,
+ [ |(\(:user<user>,:password<password>) xx 2),
+ |(\(:user<user>,:token<token>,:salt<salt>) xx 2) ],
+ 'the model should have been called with the correct parameters'
+);
+
+done-testing;