Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 935b670e2c14fd9d84a8d68789e4ebc684f9e7d9 1 parent 30de4cb
Dave Cross authored February 09, 2014
11  lib/Lystyng.pm
@@ -88,6 +88,17 @@ get '/register' => sub {
88 88
 post '/register' => sub {
89 89
   my $user_rs = resultset('User');
90 90
   my @errors;
  91
+
  92
+  foreach (qw[username name email password password2]) {
  93
+    push @errors, qq[Field "$_" is missing] unless defined param($_);
  94
+  }
  95
+
  96
+  if (@errors) {
  97
+    return template 'register', {
  98
+      errors => \@errors,
  99
+    };
  100
+  }
  101
+
91 102
   if (param('password') ne param('password2')) {
92 103
     push @errors, 'Your passwords do not match.';
93 104
   }
40  t/003_user_admin.t
@@ -8,6 +8,12 @@ use lib 'lib';
8 8
 use Lystyng;
9 9
 use Dancer::Test;
10 10
 
  11
+use Lystyng::Schema;
  12
+my $sch = Lystyng::Schema->connect(
  13
+  "dbi:mysql:database=$ENV{LYSTYNG_DB_NAME}",
  14
+  $ENV{LYSTYNG_DB_USER}, $ENV{LYSTYNG_DB_PASS},
  15
+);
  16
+
11 17
 my %route = (
12 18
   register => 200,
13 19
   login    => 200,
@@ -20,4 +26,38 @@ for (keys %route) {
20 26
     "response status is $route{$_} for /$_";
21 27
 }
22 28
 
  29
+my $user_hash = {
  30
+  username  => 'test',
  31
+  name      => 'Test User',
  32
+  email     => 'test@example.com',
  33
+  password  => 'TEST',
  34
+};
  35
+
  36
+my $response = dancer_response POST => '/register', {
  37
+  params => $user_hash,
  38
+};
  39
+
  40
+ok $response, 'Got a response from /register';
  41
+is $response->status, 200, 'Response is 200';
  42
+like $response->content, qr[is missing], 'Password2 is missing';
  43
+
  44
+$user_hash->{password2} = 'Something else';
  45
+$response = dancer_response POST => '/register', {
  46
+  params => $user_hash,
  47
+};
  48
+
  49
+ok $response, 'Got a response from /register';
  50
+is $response->status, 200, 'Response is 200';
  51
+like $response->content, qr[do not match], 'Passwords do not match';
  52
+
  53
+$user_hash->{password2} = 'TEST';
  54
+$response = dancer_response POST => '/register', {
  55
+  params => $user_hash,
  56
+};
  57
+
  58
+ok $response, 'Got a response from /register';
  59
+is $response->status, 302, 'Response is 302';
  60
+
  61
+$sch->resultset('User')->delete;
  62
+
23 63
 done_testing;

0 notes on commit 935b670

Please sign in to comment.
Something went wrong with that request. Please try again.