Compare commits

...

2 Commits

Author SHA1 Message Date
Zane C. B-H 15ca300a67 tests fine now 2020-11-30 05:19:26 -06:00
Zane C. B-H d89fbc97e8 more misc updates and start work on the tests 2020-11-30 04:44:56 -06:00
2 changed files with 86 additions and 24 deletions

View File

@ -117,11 +117,8 @@ Example...
=cut
sub new {
my %args;
if ( defined( $_[1] ) ) {
%args = { $_[1] };
}
else {
my $args=$_[1];
if ( !defined( $_[1] ) ) {
die('No argument hash ref passed');
}
@ -133,16 +130,16 @@ sub new {
};
#make sure all the keys required are present
foreach my $args_key ( keys(%args) ) {
if ( !defiend( $args{$args_key} ) ) {
foreach my $args_key ( keys( %{$args_valid_keys} ) ) {
if ( !defined( $args->{$args_key} ) ) {
die( 'The key "' . $args_key . '" is not present in the args hash ref' );
}
}
my $self = {
company => $args{company},
accessID => $args{accessID},
accessKey => $args{accessKey},
company => $args->{company},
accessID => $args->{accessID},
accessKey => $args->{accessKey},
};
bless $self;
@ -197,11 +194,8 @@ Example with timestamp...
sub signature {
my $self = $_[0];
my %args;
if ( defined( $_[1] ) ) {
%args = { $_[1] };
}
else {
my $args=$_[1];
if ( !defined( $_[1] ) ) {
die('No argument hash ref passed');
}
@ -212,32 +206,32 @@ sub signature {
};
# make sure are the required variables are present
foreach my $args_key ( keys(%args) ) {
if ( !defined( $args{$args_key} ) ) {
foreach my $args_key ( keys( %{$args_valid_keys} ) ) {
if ( !defined( $args->{$args_key} ) ) {
die( 'The key "' . $args_key . '" is not present in the args hash ref' );
}
}
# If not specified, assume it is a request it is not needed for and set it to blank.
if ( !defined( $args{data} ) ) {
$args{data} = '';
if ( !defined( $args->{data} ) ) {
$args->{data} = '';
}
# generate the timestamp if needed
# gettimeofday returns microseconds... convert to milliseconds
if ( !defined( $args{timestmp} ) ) {
if ( !defined( $args->{timestamp} ) ) {
# gettimeofday returns microseconds... convert to milliseconds
$args{timestamp} = gettimeofday * 1000;
$args->{timestamp} = gettimeofday * 1000;
# appears to only want the integer portion based on their examples
# https://www.logicmonitor.com/support/rest-api-developers-guide/v1/rest-api-v1-examples
$args{timestamp} = int( $args{timestamp} );
$args->{timestamp} = int( $args->{timestamp} );
}
# put together the string that will be used for the signature
# https://www.logicmonitor.com/support/rest-api-developers-guide/overview/using-logicmonitors-rest-api#ss-header-24
my $string = $args{HTTPverb} . $args{timestamp} . $args{data} . $args{path};
my $string = $args->{HTTPverb} . $args->{timestamp} . $args->{data} . $args->{path};
# create the signature and return it
my $sig;
@ -251,7 +245,7 @@ sub signature {
die( 'Failed to generate the signature... ' . $@ );
}
return $self;
return $sig;
}
=head1 AUTHOR

68
t/sig.t Normal file
View File

@ -0,0 +1,68 @@
#!perl
use 5.006;
use strict;
use warnings;
use Test::More;
BEGIN {
use_ok( 'LogicMonitor::REST::Signature' );
}
my $company='foo';
my $accessKey='some key';
my $accessID='some ID';
# make sure it errors when undef or missing values
my $worked=0;
my $lmsig_helper;
eval{
$lmsig_helper=LogicMonitor::REST::Signature->new({
});
$worked=1
};
ok( $worked eq '0', 'init') or diag("Iinitated with missing values");
# make sure we init it
$worked=0;
eval{
$lmsig_helper=LogicMonitor::REST::Signature->new({
company=>$company,
accessKey=>$accessKey,
accessID=>$accessID,
});
$worked=1
};
ok( $worked eq '1', 'init') or diag("Failed to init the object... ".$@);
# make sure it can generate a known one, which requires a time stamp
$worked=0;
eval{
my $sig=$lmsig_helper->signature({
HTTPverb=>'GET',
path=>'/foo',
data=>'',
timestamp=>'1',
});
if ( $sig ne 'e0bb5OESDeQdMvtJy1Nr6Nju7Nd9axVXHUhMQjjA3f4=' ){
die 'Got "'.$sig.'" but was expecting "e0bb5OESDeQdMvtJy1Nr6Nju7Nd9axVXHUhMQjjA3f4="';
}
$worked=1
};
ok( $worked eq '1', 'signature 0') or diag("Failed to create the expected signature... ".$@);
# make sure it can generate a known one, which requires a time stamp
$worked=0;
eval{
my $sig=$lmsig_helper->signature({
HTTPverb=>'GET',
path=>'/foo',
data=>'',
});
if (! defined ( $sig ) ){
die( 'Got a return of undef' );
}
$worked=1
};
ok( $worked eq '1', 'signature 1') or diag("Failed to create the a signature when auto generating a timestamp... ".$@);
done_testing(5);