From fad90980f62121baefd57420aeb2a5c0734e0aba Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Sun, 23 Apr 2017 17:02:01 +0100 Subject: [PATCH] Removed unnecessary code and test --- lib/Pear/LocalLoop/Controller/Api/User.pm | 341 --------- t/user-history.t | 806 ---------------------- 2 files changed, 1147 deletions(-) delete mode 100644 t/user-history.t diff --git a/lib/Pear/LocalLoop/Controller/Api/User.pm b/lib/Pear/LocalLoop/Controller/Api/User.pm index abbeddd..f1532a6 100644 --- a/lib/Pear/LocalLoop/Controller/Api/User.pm +++ b/lib/Pear/LocalLoop/Controller/Api/User.pm @@ -27,344 +27,3 @@ sub post_day { } 1; - -__END__ -use Data::Dumper; -use Mojo::JSON; -use DateTime; -use DateTime::Duration; -use TryCatch; - -sub post_user_history { - my $self = shift; - - my $userId = $self->stash->{api_user}->id; - my $json = $self->req->json; - if ( ! defined $json ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'JSON is missing.', - }, - status => 400,); #Malformed request - } - - my $retrieveType = $json->{retrieveType}; - if ( ! defined $retrieveType ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'retrieveType is missing.', - }, - status => 400,); #Malformed request - } - elsif (! Scalar::Util::looks_like_number($retrieveType)){ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'retrieveType does not look like a number.', - }, - status => 400,); #Malformed request - } - - #Date time. - my $startDateTime; - my $endDateTime; - - #One day - if ($retrieveType == 1){ - my $startDayNumber = $json->{dayNumber}; - if ( ! defined $startDayNumber ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'dayNumber is missing.', - }, - status => 400,); #Malformed request - } - elsif (! Scalar::Util::looks_like_number($startDayNumber)){ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'dayNumber does not look like a number.', - }, - status => 400,); #Malformed request - } - - my $startMonthNumber = $json->{monthNumber}; - if ( ! defined $startMonthNumber ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'monthNumber is missing.', - }, - status => 400,); #Malformed request - } - elsif (! Scalar::Util::looks_like_number($startMonthNumber)){ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'monthNumber does not look like a number.', - }, - status => 400,); #Malformed request - } - - my $startYear = $json->{year}; - if ( ! defined $startYear ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'year is missing.', - }, - status => 400,); #Malformed request - } - elsif (! Scalar::Util::looks_like_number($startYear)){ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'year does not look like a number.', - }, - status => 400,); #Malformed request - } - - try - { - $startDateTime = DateTime->new( - year => $startYear, - month => $startMonthNumber, - day => $startDayNumber, - ); - } - catch - { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'date is invalid.', - }, - status => 400,); #Malformed request - }; - - $endDateTime = $startDateTime->clone(); - - } - elsif ($retrieveType == 2){ - my $startDayNumber = $json->{startDayNumber}; - if ( ! defined $startDayNumber ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'startDayNumber is missing.', - }, - status => 400,); #Malformed request - } - elsif (! Scalar::Util::looks_like_number($startDayNumber)){ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'startDayNumber does not look like a number.', - }, - status => 400,); #Malformed request - } - - my $startMonthNumber = $json->{startMonthNumber}; - if ( ! defined $startMonthNumber ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'startMonthNumber is missing.', - }, - status => 400,); #Malformed request - } - elsif (! Scalar::Util::looks_like_number($startMonthNumber)){ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'startMonthNumber does not look like a number.', - }, - status => 400,); #Malformed request - } - - my $startYear = $json->{startYear}; - if ( ! defined $startYear ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'startYear is missing.', - }, - status => 400,); #Malformed request - } - elsif (! Scalar::Util::looks_like_number($startYear)){ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'startYear does not look like a number.', - }, - status => 400,); #Malformed request - } - - try - { - $startDateTime = DateTime->new( - year => $startYear, - month => $startMonthNumber, - day => $startDayNumber, - ); - } - catch - { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'start date is invalid.', - }, - status => 400,); #Malformed request - }; - - - my $endDayNumber = $json->{endDayNumber}; - if ( ! defined $endDayNumber ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'endDayNumber is missing.', - }, - status => 400,); #Malformed request - } - elsif (! Scalar::Util::looks_like_number($endDayNumber)){ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'endDayNumber does not look like a number.', - }, - status => 400,); #Malformed request - } - - my $endMonthNumber = $json->{endMonthNumber}; - if ( ! defined $endMonthNumber ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'endMonthNumber is missing.', - }, - status => 400,); #Malformed request - } - elsif (! Scalar::Util::looks_like_number($endMonthNumber)){ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'endMonthNumber does not look like a number.', - }, - status => 400,); #Malformed request - } - - my $endYear = $json->{endYear}; - if ( ! defined $endYear ) { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'endYear is missing.', - }, - status => 400,); #Malformed request - } - elsif (! Scalar::Util::looks_like_number($endYear)){ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'endYear does not look like a number.', - }, - status => 400,); #Malformed request - } - - try - { - $endDateTime = DateTime->new( - year => $endYear, - month => $endMonthNumber, - day => $endDayNumber, - ); - } - catch - { - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'end date is invalid.', - }, - status => 400,); #Malformed request - }; - - } - else{ - $self->app->log->debug('Path Error: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - success => Mojo::JSON->false, - message => 'retrieveType can only be 1 or 2.', - }, - status => 400,); #Malformed request - } - - $endDateTime->add(days => 1); - - my $startEpoch = $startDateTime->epoch(); - my $endEpoch = $endDateTime->epoch(); - - $self->app->log->debug( "startEpoch: " . Dumper($startEpoch)); - $self->app->log->debug( "endEpoch: " . Dumper($endEpoch)); - - my $dataSpend = {}; - - my $statementSelectPendingTrans = $self->db->prepare("SELECT TimeDateSubmitted, ValueMicroCurrency FROM Transactions WHERE BuyerUserId_FK = ? AND ? <= TimeDateSubmitted AND TimeDateSubmitted < ? ORDER BY TimeDateSubmitted ASC"); - $statementSelectPendingTrans->execute($userId, $startEpoch, $endEpoch); - - #We assume "microCurrencySum" is always more than 0 due to database and input constraints in "/upload". - sub add_value_to_hash { - my ($self, $microCurrencySum, $dateTimePreviousState, $dataSpend) = @_; - - #if ($microCurrencySum != 0) { - my $year = $dateTimePreviousState->year(); - my $month = $dateTimePreviousState->month(); - my $day = $dateTimePreviousState->day(); - - $dataSpend->{$year}{$month}{$day} = $microCurrencySum; - #} - } - - if (my ($timeDateSubmitted, $valueMicroCurrency) = $statementSelectPendingTrans->fetchrow_array()) { - my $dateTimeTruncator = DateTime->from_epoch(epoch => $timeDateSubmitted); - $dateTimeTruncator->truncate( to => 'day'); - - #Set to 0 then add the current value like the else block. - my $microCurrencySum = $valueMicroCurrency; - #Set to the first row time - my $dateTimePreviousState = $dateTimeTruncator; - - while (my ($timeDateSubmitted, $valueMicroCurrency) = $statementSelectPendingTrans->fetchrow_array()) { - $dateTimeTruncator = DateTime->from_epoch(epoch => $timeDateSubmitted); - $dateTimeTruncator->truncate( to => 'day'); - - if (DateTime->compare($dateTimePreviousState, $dateTimeTruncator) != 0 ){ - add_value_to_hash($self, $microCurrencySum, $dateTimePreviousState, $dataSpend); - $microCurrencySum = $valueMicroCurrency; #Reset to 0 then add the current value like the else block. - $dateTimePreviousState = $dateTimeTruncator; - } - else{ - $microCurrencySum += $valueMicroCurrency; #Same day to keep adding the values. - } - } - - add_value_to_hash($self, $microCurrencySum, $dateTimePreviousState, $dataSpend); - } - - - $self->app->log->debug('Path Success: file:' . __FILE__ . ', line: ' . __LINE__); - return $self->render( json => { - microCurencySpent => $dataSpend, - success => Mojo::JSON->true, - }, - status => 200,); - -} - -1; - diff --git a/t/user-history.t b/t/user-history.t deleted file mode 100644 index 3ff1ea2..0000000 --- a/t/user-history.t +++ /dev/null @@ -1,806 +0,0 @@ -use Mojo::Base -strict; -use Test::More skip_all => 'User History needs reworking'; -use Test::Mojo; -use Mojo::JSON qw(encode_json);; -use Time::Fake; -use Data::Dumper; -use DateTime; - -use FindBin; - -BEGIN { - $ENV{MOJO_MODE} = 'testing'; - $ENV{MOJO_LOG_LEVEL} = 'debug'; -} - -my $t = Test::Mojo->new("Pear::LocalLoop"); - -my $dbh = $t->app->db; - -#Dump all pf the test tables and start again. -my $sqlDeployment = Mojo::File->new("$FindBin::Bin/../dropschema.sql")->slurp; -for (split ';', $sqlDeployment){ - $dbh->do($_) or die $dbh->errstr; -} - -my $sqlDeployment = Mojo::File->new("$FindBin::Bin/../schema.sql")->slurp; -for (split ';', $sqlDeployment){ - $dbh->do($_) or die $dbh->errstr; -} - -my @accountTokens = ('a', 'b', 'c'); -my $tokenStatement = $dbh->prepare('INSERT INTO AccountTokens (AccountTokenName) VALUES (?)'); -foreach (@accountTokens){ - my $rowsAdded = $tokenStatement->execute($_); -} - -my $dateTimeNow = DateTime->now(); - -#Plus 2 days so you cannot have a bug where it goes past midnight when you run the test, plus one hour to remove odd error -my $dateTimeInitial = $dateTimeNow->clone()->truncate(to => day)->add(days => 2, hours => 1); -my $dateTimePlusTwoDays = $dateTimeInitial->clone()->add(days => 2); -my $dateTimePlusOneMonth = $dateTimeInitial->clone()->add(months => 1); -my $dateTimePlusOneYear = $dateTimeInitial->clone()->add(years => 1, days => 1); - -my $dateTimePlusThreeDays = $dateTimeInitial->clone()->add(days => 3); -my $dateTimePlusOneMonthMinusOneDay = $dateTimePlusOneMonth->clone()->subtract(days => 1); - -#Clock skew second diffs -my $dateTimeInitialDiff = $dateTimeInitial->delta_ms($dateTimeNow)->delta_minutes() * 60; -my $dateTimePlusTwoDaysSecondsDiff = $dateTimePlusTwoDays->delta_ms($dateTimeNow)->delta_minutes() * 60; -my $dateTimePlusOneMonthSecondsDiff = $dateTimePlusOneMonth->delta_ms($dateTimeNow)->delta_minutes() * 60; -my $dateTimePlusOneYearSecondsDiff = $dateTimePlusOneYear->delta_ms($dateTimeNow)->delta_minutes() * 60; - -#Change to the initial time. -Time::Fake->offset("+" . $dateTimeInitialDiff . "s"); - -#This depends on "register.t", "login.t", "upload.t" and "admin-approve.t" working. - -#Valid customer, this also tests that redirects are disabled for register. -print "test 1 - Create customer user account (Reno)\n"; -my $emailReno = 'reno@shinra.energy'; -my $passwordReno = 'turks'; -my $testJson = { - 'usertype' => 'customer', - 'token' => shift(@accountTokens), - 'username' => 'Reno', - 'email' => $emailReno, - 'postcode' => 'E1 MP01', - 'password' => $passwordReno, - 'age' => '20-35' -}; -$t->post_ok('/api/register' => json => $testJson) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - -print "test 2 - Create organisation user account (Choco Billy)\n"; -my $emailBilly = 'choco.billy@chocofarm.org'; -my $passwordBilly = 'Choco'; -my $testJson = { - 'usertype' => 'organisation', - 'token' => shift(@accountTokens), - 'username' => 'ChocoBillysGreens', - 'email' => $emailBilly, - 'postcode' => 'E4 C12', - 'password' => $passwordBilly, - 'fulladdress' => 'Chocobo Farm, Eastern Continent, Gaia' -}; -$t->post_ok('/api/register' => json => $testJson) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - - -print "test 3 - Create admin account\n"; -my $emailAdmin = 'admin@foodloop.net'; -my $passwordAdmin = 'ethics'; -my $testJson = { - 'usertype' => 'customer', - 'token' => shift(@accountTokens), - 'username' => 'admin', - 'email' => $emailAdmin, - 'postcode' => 'NW1 W01', - 'password' => $passwordAdmin, - 'age' => '35-50' -}; -$t->post_ok('/api/register' => json => $testJson) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - -print "test 4 - Making 'admin' an Admin\n"; -my $adminUserId = $t->app->db->selectrow_array("SELECT UserId FROM Users WHERE Email = ?", undef, ($emailAdmin)); -is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Administrators")}[0],0,"No admins"; -$t->app->db->prepare("INSERT INTO Administrators (UserId) VALUES (?)")->execute($adminUserId); -is @{$t->app->db->selectrow_arrayref("SELECT COUNT(*) FROM Administrators")}[0],1,"1 admin"; - -sub logout { - $t->post_ok('/api/logout', json => { session_key => $session_key }) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); -} - -my $session_key; - -sub login_reno { - $testJson = { - 'email' => $emailReno, - 'password' => $passwordReno, - }; - $t->post_ok('/api/login' => json => $testJson) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - $session_key = $t->tx->res->json('/session_key'); -} - -sub login_chocobilly { - $testJson = { - 'email' => $emailBilly, - 'password' => $passwordBilly, - }; - $t->post_ok('/api/login' => json => $testJson) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - $session_key = $t->tx->res->json('/session_key'); -} - -sub login_admin { - $testJson = { - 'email' => $emailAdmin, - 'password' => $passwordAdmin, - }; - $t->post_ok('/api/login' => json => $testJson) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - $session_key = $t->tx->res->json('/session_key'); -} - -print "test 5 - Login non-admin Reno\n"; -login_reno(); - - -print "test 6 - Reno spends at Turtle\'s Paradise\n"; -my $nameToTestTurtle = 'Turtle\'s Paradise'; -$json = { - transaction_value => 10, - transaction_type => 3, - organisation_name => $nameToTestTurtle, - street_name => "Town centre", - town => " Wutai", - postcode => "NW11 8AD", - session_key => $session_key, -}; -my $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}}; -$t->post_ok('/api/upload' => form => $upload ) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); -my $unvalidatedOrganisationId = $t->app->schema->resultset('PendingOrganisation')->find({ name => $nameToTestTurtle })->pendingorganisationid; - -#Change to 2 days later -Time::Fake->offset("+" . $dateTimePlusTwoDaysSecondsDiff . "s"); - -print "test 7 - Reno spends at Turtle\'s Paradise, 2 days later, transaction 1/2\n"; -$json = { - transaction_value => 20, - transaction_type => 2, - organisation_id => $unvalidatedOrganisationId, - session_key => $session_key, -}; -my $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}}; -$t->post_ok('/api/upload' => form => $upload ) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - -print "test 8 - Reno spends at Turtle\'s Paradise, 2 days later, transaction 2/2\n"; -$json = { - transaction_value => 40, - transaction_type => 2, - organisation_id => $unvalidatedOrganisationId, - session_key => $session_key, -}; -my $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}}; -$t->post_ok('/api/upload' => form => $upload ) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - -print "test 9 - Logout non-admin Reno (time offset causes session to expire)\n"; -logout(); - -#Change to 1 month later -Time::Fake->offset("+" . $dateTimePlusOneMonthSecondsDiff . "s"); - -print "test 10 - Login non-admin Reno\n"; -login_reno(); - -print "test 11 - Reno spends at Turtle\'s Paradise, 1 month later\n"; -$json = { - transaction_value => 80, - transaction_type => 2, - organisation_id => $unvalidatedOrganisationId, - session_key => $session_key, -}; -my $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}}; -$t->post_ok('/api/upload' => form => $upload ) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - -print "test 12 - Logout non-admin Reno\n"; -logout(); - -#Change to 1 year (and a bit) later -Time::Fake->offset("+" . $dateTimePlusOneYearSecondsDiff . "s"); - -print "test 13 - Login non-admin Reno\n"; -login_reno(); - -print "test 14 - Reno spends at Turtle\'s Paradise, 1 year later\n"; -$json = { - transaction_value => 160, - transaction_type => 2, - organisation_id => $unvalidatedOrganisationId, - session_key => $session_key, -}; -my $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}}; -$t->post_ok('/api/upload' => form => $upload ) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - -print "test 15 - Logout non-admin Reno\n"; -logout(); - -#Change to 2 days later -Time::Fake->offset("+" . $dateTimePlusTwoDaysSecondsDiff . "s"); - -print "test 16 - Login Admin\n"; -login_admin(); - -print "test 17 - Admin approves Turtle\'s Paradise.\n"; -$json = { - unvalidatedOrganisationId => $unvalidatedOrganisationId, - session_key => $session_key, -}; -$t->post_ok('/api/admin-approve' => json => $json) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); -my $validatedOrganisationId = $t->tx->res->json->{validatedOrganisationId}; - - -print "test 18 - Logout Admin\n"; -logout(); - -print "test 19 - Login non-admin Chocobilly\n"; -login_chocobilly(); - -print "test 20 - Chocobilly spends at Turtle\'s Paradise, 2 days later\n"; -#Added to test and see if the later values from different users merge together. They shouldn't -$json = { - transaction_value => 320, - transaction_type => 1, - organisation_id => $validatedOrganisationId, - session_key => $session_key, -}; -my $upload = {json => Mojo::JSON::encode_json($json), file => {file => './t/test.jpg'}}; -$t->post_ok('/api/upload' => form => $upload ) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true); - -print "test 21 - Logout non-admin Chocobilly\n"; -logout(); - -#Change back to 1 year (and a bit) later -Time::Fake->offset("+" . $dateTimePlusOneYearSecondsDiff . "s"); - -##Actual testing from here onwards. - -print "test 22 - Login non-admin Reno\n"; -login_reno(); - - -print "test 23 - No JSON\n"; -$t->post_ok('/api/user-history') - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->json_like('/message', qr/JSON is missing/); - -print "test 24 - retrieveType is missing\n"; -$json = { - dayNumber => $dateTimePlusThreeDays->day(), - monthNumber => $dateTimePlusThreeDays->month(), - year => $dateTimePlusThreeDays->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/retrieveType is missing/i); - -print "test 25 - retrieveType is not a number\n"; -$json = { - retrieveType => "A", - dayNumber => $dateTimePlusThreeDays->day(), - monthNumber => $dateTimePlusThreeDays->month(), - year => $dateTimePlusThreeDays->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/retrieveType does not look like a number/i); - -print "test 26 - retrieveType is not 1 or 2\n"; -$json = { - retrieveType => 0, - dayNumber => $dateTimePlusThreeDays->day(), - monthNumber => $dateTimePlusThreeDays->month(), - year => $dateTimePlusThreeDays->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/retrieveType can only be 1 or 2./i); - -#Single tests - -print "test 27 - single date - dayNumber is missing\n"; -$json = { - retrieveType => 1, - monthNumber => $dateTimePlusThreeDays->month(), - year => $dateTimePlusThreeDays->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/dayNumber is missing./i); - -print "test 28 - single date - dayNumber is not a number\n"; -$json = { - retrieveType => 1, - dayNumber => "A", - monthNumber => $dateTimePlusThreeDays->month(), - year => $dateTimePlusThreeDays->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/dayNumber does not look like a number./i); - -print "test 29 - single date - monthNumber is missing\n"; -$json = { - retrieveType => 1, - dayNumber => $dateTimePlusThreeDays->day(), - year => $dateTimePlusThreeDays->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/monthNumber is missing./i); - -print "test 30 - single date - monthNumber is not a number\n"; -$json = { - retrieveType => 1, - dayNumber => $dateTimePlusThreeDays->day(), - monthNumber => "ABC", - year => $dateTimePlusThreeDays->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/monthNumber does not look like a number./i); - -print "test 31 - single date - year is missing\n"; -$json = { - retrieveType => 1, - dayNumber => $dateTimePlusThreeDays->day(), - monthNumber => $dateTimePlusThreeDays->month(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/year is missing./i); - -print "test 32 - single date - year is not a number\n"; -$json = { - retrieveType => 1, - dayNumber => $dateTimePlusThreeDays->day(), - monthNumber => $dateTimePlusThreeDays->month(), - year => "I1", - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/year does not look like a number./i); - -print "test 33 - Invalid date\n"; -$json = { - retrieveType => 1, - dayNumber => $dateTimePlusThreeDays->day(), - monthNumber => ($dateTimePlusThreeDays->month() + 13), - year => $dateTimePlusThreeDays->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/date is invalid./i); - -#Range tests. - -print "test 34 - range date - startDayNumber is missing\n"; -$json = { - retrieveType => 2, - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/startDayNumber is missing./i); - -print "test 35 - range date - startDayNumber is not a number\n"; -$json = { - retrieveType => 2, - startDayNumber => "2ER", - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/startDayNumber does not look like a number./i); - - -print "test 36 - range date - startMonthNumber is missing\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/startMonthNumber is missing./i); - -print "test 37 - range date - startMonthNumber is not a number\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => "Text", - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/startMonthNumber does not look like a number./i); - -print "test 38 - range date - startYear is missing\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/startYear is missing./i); - -print "test 39 - range date - startYear is not a number\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => "Years2", - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/startYear does not look like a number./i); - -print "test 40 - Invalid start date\n"; -$json = { - retrieveType => 2, - startDayNumber => ($dateTimeInitial->day() + 60), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/start date is invalid./i); - -## Valid data testing. - -print "test 41 - range date - endDayNumber is missing\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/endDayNumber is missing./i); - -print "test 42 - range date - endDayNumber is not a number\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => "2EF", - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/endDayNumber does not look like a number./i); - -print "test 43 - range date - endMonthNumber is missing\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/endMonthNumber is missing./i); - -print "test 44 - range date - endMonthNumber is not a number\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => "A5G", - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/endMonthNumber does not look like a number./i); - -print "test 43 - range date - endYear is missing\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/endYear is missing./i); - -print "test 44 - range date - endYear is not a number\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => "ABC", - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/endYear does not look like a number./i); - -print "test 40 - Invalid end date\n"; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => ($dateTimePlusOneYear->day() - 60), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(400) - ->json_is('/success', Mojo::JSON->false) - ->content_like(qr/end date is invalid./i); - - -print "test 41 - Test single day with no transactions\n"; -my $expectedReturnedStats = {}; -$json = { - retrieveType => 1, - dayNumber => $dateTimePlusThreeDays->day(), - monthNumber => $dateTimePlusThreeDays->month(), - year => $dateTimePlusThreeDays->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true) - ->json_has('/microCurencySpent') - ->json_is('/microCurencySpent',$expectedReturnedStats); -my $spend = $t->tx->res->json->{microCurencySpent}; -print Dumper($spend) . "\n"; - -print "test 42 - Test single day with one transaction\n"; -$expectedReturnedStats = {}; -$expectedReturnedStats->{$dateTimeInitial->year}{$dateTimeInitial->month}{$dateTimeInitial->day} = 10; -$json = { - retrieveType => 1, - dayNumber => $dateTimeInitial->day(), - monthNumber => $dateTimeInitial->month(), - year => $dateTimeInitial->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true) - ->json_has('/microCurencySpent') - ->json_is('/microCurencySpent',$expectedReturnedStats)->or(sub { use Data::Dumper; diag Dumper $t->tx->res }); -my $spend = $t->tx->res->json->{microCurencySpent}; -print Dumper($spend) . "\n"; - -print "test 43 - Test single day with multiple transactions and user separateness\n"; -$expectedReturnedStats = {}; -$expectedReturnedStats->{$dateTimePlusTwoDays->year}{$dateTimePlusTwoDays->month}{$dateTimePlusTwoDays->day} = 60; -$json = { - retrieveType => 1, - dayNumber => $dateTimePlusTwoDays->day(), - monthNumber => $dateTimePlusTwoDays->month(), - year => $dateTimePlusTwoDays->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true) - ->json_has('/microCurencySpent') - ->json_is('/microCurencySpent',$expectedReturnedStats); -my $spend = $t->tx->res->json->{microCurencySpent}; -print Dumper($spend) . "\n"; - -print "test 44 - Test range with no transactions\n"; -#Empty range -$expectedReturnedStats = {}; -$json = { - retrieveType => 2, - startDayNumber => $dateTimePlusThreeDays->day(), - startMonthNumber => $dateTimePlusThreeDays->month(), - startYear => $dateTimePlusThreeDays->year(), - endDayNumber => $dateTimePlusOneMonthMinusOneDay->day(), - endMonthNumber => $dateTimePlusOneMonthMinusOneDay->month(), - endYear => $dateTimePlusOneMonthMinusOneDay->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true) - ->json_has('/microCurencySpent') - ->json_is('/microCurencySpent',$expectedReturnedStats); -my $spend = $t->tx->res->json->{microCurencySpent}; -print Dumper($spend) . "\n"; - -print "test 45 - Test range with multiple very similar dated transactions (2 day window), test multiple transactions on a day and user separateness\n"; -#Testing boundary condition one day before the next value. -$expectedReturnedStats = {}; -$expectedReturnedStats->{$dateTimeInitial->year}{$dateTimeInitial->month}{$dateTimeInitial->day} = 10; -$expectedReturnedStats->{$dateTimePlusTwoDays->year}{$dateTimePlusTwoDays->month}{$dateTimePlusTwoDays->day} = 60; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneMonthMinusOneDay->day(), - endMonthNumber => $dateTimePlusOneMonthMinusOneDay->month(), - endYear => $dateTimePlusOneMonthMinusOneDay->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true) - ->json_has('/microCurencySpent') - ->json_is('/microCurencySpent',$expectedReturnedStats); -my $spend = $t->tx->res->json->{microCurencySpent}; -print Dumper($spend) . "\n"; - -print "test 46 - Test range with multiple transactions spread over a few days, a month and a year, as well as user separateness\n"; -$expectedReturnedStats = {}; -$expectedReturnedStats->{$dateTimeInitial->year}{$dateTimeInitial->month}{$dateTimeInitial->day} = 10; -$expectedReturnedStats->{$dateTimePlusTwoDays->year}{$dateTimePlusTwoDays->month}{$dateTimePlusTwoDays->day} = 60; -$expectedReturnedStats->{$dateTimePlusOneMonth->year}{$dateTimePlusOneMonth->month}{$dateTimePlusOneMonth->day} = 80; -$expectedReturnedStats->{$dateTimePlusOneYear->year}{$dateTimePlusOneYear->month}{$dateTimePlusOneYear->day} = 160; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true) - ->json_has('/microCurencySpent') - ->json_is('/microCurencySpent',$expectedReturnedStats); -my $spend = $t->tx->res->json->{microCurencySpent}; -print Dumper($spend) . "\n"; - -print "test 47 - Logout non-admin Reno\n"; -logout(); - -print "test 48 - Login non-admin Chocobilly\n"; -login_chocobilly(); - -print "test 49- Test user separateness under different user.\n"; -$expectedReturnedStats = {}; -$expectedReturnedStats->{$dateTimePlusTwoDays->year}{$dateTimePlusTwoDays->month}{$dateTimePlusTwoDays->day} = 320; -$json = { - retrieveType => 2, - startDayNumber => $dateTimeInitial->day(), - startMonthNumber => $dateTimeInitial->month(), - startYear => $dateTimeInitial->year(), - endDayNumber => $dateTimePlusOneYear->day(), - endMonthNumber => $dateTimePlusOneYear->month(), - endYear => $dateTimePlusOneYear->year(), - session_key => $session_key, -}; -$t->post_ok('/api/user-history' => json => $json) - ->status_is(200) - ->json_is('/success', Mojo::JSON->true) - ->json_has('/microCurencySpent') - ->json_is('/microCurencySpent',$expectedReturnedStats); -my $spend = $t->tx->res->json->{microCurencySpent}; -print Dumper($spend) . "\n"; - -print "test 50 - Logout non-admin Chocobilly\n"; -logout(); - -done_testing();