Throw 400 for invalid input

This commit is contained in:
swaggboi 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) { under sub ($c) {
$c->session(expires => time() + 31536000); $c->session(expires => time() + 31536000);
$c->stash(status => 400) if $c->flash('invalid_input');
1; 1;
}; };
@ -51,10 +53,6 @@ group {
base_path => $base_path base_path => $base_path
); );
unless (my $thread = @$threads[0]) {
$c->stash(status => 404)
}
$c->render(); $c->render();
}; };
}; };
@ -74,8 +72,16 @@ any [qw{GET POST}], '/post', sub ($c) {
$v->required('title')->size(1, 127 ); $v->required('title')->size(1, 127 );
$v->required('post' )->size(2, 4000); $v->required('post' )->size(2, 4000);
$c->thread->create_thread($thread_author, $thread_title, $thread_body) if ($v->has_error) {
unless $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'); return $c->redirect_to('view');
} }

View File

@ -24,7 +24,6 @@ Run the tests locally (against development environment)
## TODOs ## TODOs
1. Handle POST with no params (error) 1. Display error for invalid input (call `flash()` in template)
1. Moar tests...
1. Pick a date format 1. Pick a date format
1. **Moar tests!!** 1. Reply model

View File

@ -13,13 +13,22 @@ my %valid_params = (
title => 'hi', title => 'hi',
post => 'ayy... lmao' post => 'ayy... lmao'
); );
my %invalid_params = (
name => 'Anonymous',
title => '',
post => 'a'
);
$t->ua->max_redirects(1); $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 # POST
$t->post_ok('/post')->status_is(200); $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) $t->post_ok('/post', form => \%valid_params)->status_is(200)
->text_like(h2 => qr/View Threads/); ->text_like(h2 => qr/View Threads/);