Entering MooseX, Part the Eleventh
Well still milking this one for all its worth and today I will do another .t file.
This time round I am going to test the API validation. You might remember that I did add a little validation code and this is one that really should be tested. Many is the time where a lovely API has been created but no validation so one was able to enter all sorts of nu-sanitized data and commands against the API.
So in my little API I want to check for at least one of my API keys and that if a key is present it points to an Array-Ref.
So here is what I came up with again very simple which I always find best
#!perl -T
use Test::More tests => 5;
use Test::Moose::More;
use_ok('MooseX::AuthorizedMethodRoles');
{
package test_role1;
use Moose::Role;
has 'nothing_r1 '=> (is => 'ro',default=>'role 1');
}
{
package test_role2;
use Moose::Role;
has 'nothing_r2' => (is => 'ro',default=>'role 2');
}
{
package test_role3;
use Moose::Role;
has 'nothing_r3' => (is => 'ro',default=>'role 3');
}
eval {
{
package validation_test_one_of_array_ref;
use MooseX::AuthorizedMethodRoles;
with 'test_role1';
authorized_roles ping => {one_of=>{'test_role1'=>1,'test_role2'=>'2'}}, sub {
return "ping test_one_of_pass";
};
}
};
ok(scalar($@),"validation_test_one_of_array_ref" );
eval {
{
package validation_test_required_array_ref;
use MooseX::AuthorizedMethodRoles;
with 'test_role1';
authorized_roles ping => {required=>{'test_role1'=>1,'test_role2'=>'2'}}, sub {
return "ping test_one_of_pass";
};
}
};
ok(scalar($@),"validation_test_required_array_ref" );
eval {
{
package validation_test_not_in_API;
use MooseX::AuthorizedMethodRoles;
with 'test_role1';
authorized_roles ping => {not_API=>['test_role1','test_role2']}, sub {
return "ping test_one_of_pass";
};
}
};
ok(scalar($@),"validation_test_not_in_API" );
eval{
{
package validation_test_API_extra_array_ref;
use MooseX::AuthorizedMethodRoles;
with 'test_role1';
authorized_roles ping => {not_API=>['test_role1','test_role2'],
required=>['test_role1','test_role2']}, sub {
return "ping test_one_of_pass";
};
}
};
ok(!scalar($@),"validation_test_API_extra_array_ref" );
Not a great deal of rocket science here but it is neat that I can use the eval{} here to capture what is normally a compile time error. I have five simple tests the first a simple use test the next two checking to ensure there is an array-ref for each key, the fourth checking that I have to have at least one of my API keys and not something else and the last checking to make sure an extra key has not effect on the API.
Not a bad little test if I say so myself
N.B.
Seems I have a hidden bug in the original version of this post. See this post on how I goofed.
Leave a comment