diff --git a/PostText.pl b/PostText.pl index d9887bc..ea2c361 100755 --- a/PostText.pl +++ b/PostText.pl @@ -28,6 +28,8 @@ helper thread => sub { under sub ($c) { $c->session(expires => time() + 31536000); + $c->stash(status => 400) if $c->flash('invalid_input'); + 1; }; @@ -51,10 +53,6 @@ group { base_path => $base_path ); - unless (my $thread = @$threads[0]) { - $c->stash(status => 404) - } - $c->render(); }; }; @@ -74,8 +72,16 @@ any [qw{GET POST}], '/post', sub ($c) { $v->required('title')->size(1, 127 ); $v->required('post' )->size(2, 4000); - $c->thread->create_thread($thread_author, $thread_title, $thread_body) - unless $v->has_error(); + if ($v->has_error) { + $c->flash(invalid_input => 'Invalid thread title/text.') + } + else { + $c->thread->create_thread( + $thread_author, + $thread_title, + $thread_body + ) + } return $c->redirect_to('view'); } diff --git a/README.md b/README.md index cdbe94c..aba5800 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,6 @@ Run the tests locally (against development environment) ## TODOs -1. Handle POST with no params (error) -1. Moar tests... +1. Display error for invalid input (call `flash()` in template) 1. Pick a date format -1. **Moar tests!!** +1. Reply model diff --git a/t/post.t b/t/post.t index 33dd6cb..8ab4305 100644 --- a/t/post.t +++ b/t/post.t @@ -6,20 +6,29 @@ use Test::More; use Mojo::File qw{curfile}; use Test::Mojo; -my $script = curfile->dirname->sibling('PostText.pl'); -my $t = Test::Mojo->new($script); -my %valid_params = ( +my $script = curfile->dirname->sibling('PostText.pl'); +my $t = Test::Mojo->new($script); +my %valid_params = ( name => 'Anonymous', title => 'hi', post => 'ayy... lmao' ); +my %invalid_params = ( + name => 'Anonymous', + title => '', + post => 'a' + ); $t->ua->max_redirects(1); -$t->get_ok('/post')->status_is(200)->text_like(h2 => qr/New Thread/);; +# GET +$t->get_ok('/post')->status_is(200)->text_like(h2 => qr/New Thread/); -# This should fail!! 08142022 -$t->post_ok('/post')->status_is(200); +# POST +$t->post_ok('/post')->status_is(200)->text_like(h2 => qr/New Thread/); + +$t->post_ok('/post', form => \%invalid_params)->status_is(400) + ->text_like(h2 => qr/View Threads/); $t->post_ok('/post', form => \%valid_params)->status_is(200) ->text_like(h2 => qr/View Threads/);