Throw 400 for invalid input

This commit is contained in:
swagg boi 2022-08-15 17:11:14 -04:00
parent e702fd9139
commit 25d8085529
3 changed files with 29 additions and 15 deletions

View file

@ -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');
}

View file

@ -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

View file

@ -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/);