Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improved tests for user registration. Fixed a bug they exposed.

  • Loading branch information...
commit 935b670e2c14fd9d84a8d68789e4ebc684f9e7d9 1 parent 30de4cb
Dave Cross authored
Showing with 51 additions and 0 deletions.
  1. +11 −0 lib/Lystyng.pm
  2. +40 −0 t/003_user_admin.t
11 lib/Lystyng.pm
View
@@ -88,6 +88,17 @@ get '/register' => sub {
post '/register' => sub {
my $user_rs = resultset('User');
my @errors;
+
+ foreach (qw[username name email password password2]) {
+ push @errors, qq[Field "$_" is missing] unless defined param($_);
+ }
+
+ if (@errors) {
+ return template 'register', {
+ errors => \@errors,
+ };
+ }
+
if (param('password') ne param('password2')) {
push @errors, 'Your passwords do not match.';
}
40 t/003_user_admin.t
View
@@ -8,6 +8,12 @@ use lib 'lib';
use Lystyng;
use Dancer::Test;
+use Lystyng::Schema;
+my $sch = Lystyng::Schema->connect(
+ "dbi:mysql:database=$ENV{LYSTYNG_DB_NAME}",
+ $ENV{LYSTYNG_DB_USER}, $ENV{LYSTYNG_DB_PASS},
+);
+
my %route = (
register => 200,
login => 200,
@@ -20,4 +26,38 @@ for (keys %route) {
"response status is $route{$_} for /$_";
}
+my $user_hash = {
+ username => 'test',
+ name => 'Test User',
+ email => 'test@example.com',
+ password => 'TEST',
+};
+
+my $response = dancer_response POST => '/register', {
+ params => $user_hash,
+};
+
+ok $response, 'Got a response from /register';
+is $response->status, 200, 'Response is 200';
+like $response->content, qr[is missing], 'Password2 is missing';
+
+$user_hash->{password2} = 'Something else';
+$response = dancer_response POST => '/register', {
+ params => $user_hash,
+};
+
+ok $response, 'Got a response from /register';
+is $response->status, 200, 'Response is 200';
+like $response->content, qr[do not match], 'Passwords do not match';
+
+$user_hash->{password2} = 'TEST';
+$response = dancer_response POST => '/register', {
+ params => $user_hash,
+};
+
+ok $response, 'Got a response from /register';
+is $response->status, 302, 'Response is 302';
+
+$sch->resultset('User')->delete;
+
done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.