From d7c0f715d2fdd763560a8c74244973358225a38b Mon Sep 17 00:00:00 2001 From: swag Date: Sat, 8 Jan 2022 22:01:51 -0500 Subject: [PATCH] More tests --- README.md | 1 - guestbook-ng.pl | 3 +-- t/basic.t | 4 +++- t/validation.t | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 t/validation.t diff --git a/README.md b/README.md index 919af7f..6469f01 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,5 @@ Add the `-v` option for more verbose output ## TODOs -1. More tests 1. /spam route would be interesting 1. Include the total number of visitors or messages diff --git a/guestbook-ng.pl b/guestbook-ng.pl index 40ab7ea..352b42a 100755 --- a/guestbook-ng.pl +++ b/guestbook-ng.pl @@ -46,8 +46,7 @@ under sub ($c) { $c->session(expiration => 604800); - $c->stash(status => 403) - if $c->flash('error') eq 'This message was flagged as spam'; + $c->stash(status => 403) if $c->flash('error'); 1; }; diff --git a/t/basic.t b/t/basic.t index f0256cb..0a6d78b 100644 --- a/t/basic.t +++ b/t/basic.t @@ -15,9 +15,11 @@ my %form = ( answer => 'false' ); +$t->ua->max_redirects(1); + $t->get_ok('/')->status_is(200) ->text_is(h2 => 'Messages from the World Wide Web'); $t->get_ok('/sign')->status_is(200)->text_is(h2 => 'Sign the Guestbook'); -$t->post_ok('/sign', form => \%form)->status_is(302); +$t->post_ok('/sign', form => \%form)->status_is(200); done_testing(); diff --git a/t/validation.t b/t/validation.t new file mode 100644 index 0000000..8bed2f4 --- /dev/null +++ b/t/validation.t @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; +use Mojo::File qw{curfile}; +use Test::Mojo; + +my $script = curfile->dirname->sibling('guestbook-ng.pl'); +my $t = Test::Mojo->new($script); +my %invalid_form = ( + name => 'swagg boi', + url => 'INVALID://', + message => '', + answer => 'false' + ); +my %spam_form = ( + name => 'swagg boi', + url => 'http://localhost/', + message => 'hi', + answer => 0 + ); + +$t->ua->max_redirects(1); + +# Invalid input +$t->post_ok('/sign', form => \%invalid_form)->status_is(200) + ->content_like(qr/cannot be blank/); +$t->post_ok('/sign', form => \%invalid_form)->status_is(200) + ->content_like(qr/URL does not appear to be/); + +# Spam test +$t->post_ok('/sign', form => \%spam_form)->status_is(403); + +done_testing();