diff options
author | Matthew Somerville <matthew@mysociety.org> | 2020-02-18 09:13:43 +0000 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2020-02-26 11:38:18 +0000 |
commit | 80d5e66ccc5b0d99db1391e035b584e9ae015339 (patch) | |
tree | 854df9c2e1223021f45260d05b3f3d4924024868 | |
parent | 6954823390e7332bd8d1ab0afa28b3c0fb02db39 (diff) |
Fix password setting in createsuperuser script.
-rwxr-xr-x | bin/createsuperuser | 2 | ||||
-rw-r--r-- | perllib/DBIx/Class/FixMyStreet/EncodedColumn.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/CreateSuperuser.pm | 20 | ||||
-rw-r--r-- | t/script/createsuperuser.t | 23 |
4 files changed, 39 insertions, 8 deletions
diff --git a/bin/createsuperuser b/bin/createsuperuser index 98b36aa36..7a4946228 100755 --- a/bin/createsuperuser +++ b/bin/createsuperuser @@ -30,4 +30,4 @@ BEGIN { use FixMyStreet; use FixMyStreet::Script::CreateSuperuser; -FixMyStreet::Script::CreateSuperuser::createsuperuser(); +exit FixMyStreet::Script::CreateSuperuser::createsuperuser(@ARGV); diff --git a/perllib/DBIx/Class/FixMyStreet/EncodedColumn.pm b/perllib/DBIx/Class/FixMyStreet/EncodedColumn.pm index 3be6e4594..82e6e591e 100644 --- a/perllib/DBIx/Class/FixMyStreet/EncodedColumn.pm +++ b/perllib/DBIx/Class/FixMyStreet/EncodedColumn.pm @@ -10,7 +10,7 @@ sub set_column { my $self = shift; if ($_[0] eq 'password') { my $cobrand = $self->result_source->schema->cobrand; - if ($cobrand->moniker eq 'tfl') { + if ($cobrand && $cobrand->moniker eq 'tfl') { if (defined $_[1]) { if (defined $_[2]) { $self->set_extra_metadata(tfl_password => $_[1]); diff --git a/perllib/FixMyStreet/Script/CreateSuperuser.pm b/perllib/FixMyStreet/Script/CreateSuperuser.pm index 69d165abb..cbbea577a 100644 --- a/perllib/FixMyStreet/Script/CreateSuperuser.pm +++ b/perllib/FixMyStreet/Script/CreateSuperuser.pm @@ -7,19 +7,27 @@ use FixMyStreet; use FixMyStreet::DB; sub createsuperuser { - die "Specify a single email address and optionally password to create a superuser or grant superuser status to." if (@ARGV < 1 || @ARGV > 2); + my ($email, $password) = @_; - my $user = FixMyStreet::DB->resultset('User')->find_or_new({ email => $ARGV[0] }); + unless ($email) { + warn "Specify a single email address and optionally password to create a superuser or grant superuser status to.\n"; + return 1; + } + + my $user = FixMyStreet::DB->resultset('User')->find_or_new({ email => $email }); if ( !$user->in_storage ) { - die "Specify a password for this new user." if (@ARGV < 2); - $user->password($ARGV[1]); + unless ($password) { + warn "Specify a password for this new user.\n"; + return 1; + } + $user->password($password); $user->is_superuser(1); $user->insert; } else { $user->update({ is_superuser => 1 }); } print $user->email . " is now a superuser.\n"; + return 0; } - -1;
\ No newline at end of file +1; diff --git a/t/script/createsuperuser.t b/t/script/createsuperuser.t new file mode 100644 index 000000000..1d4826111 --- /dev/null +++ b/t/script/createsuperuser.t @@ -0,0 +1,23 @@ +use Test::More; +use Test::Output; + +use_ok 'FixMyStreet::Script::CreateSuperuser'; + +stderr_like { FixMyStreet::Script::CreateSuperuser::createsuperuser(); } + qr/Specify a single email address/, 'Email error shown'; +stderr_is { FixMyStreet::Script::CreateSuperuser::createsuperuser('test@example.org'); } + "Specify a password for this new user.\n", 'Password error shown'; +stdout_is { FixMyStreet::Script::CreateSuperuser::createsuperuser('test@example.org', 'password'); } + "test\@example.org is now a superuser.\n", 'Correct message shown'; + +my $user = FixMyStreet::DB->resultset("User")->find({ email => 'test@example.org' }); +ok $user, 'user created'; +is $user->is_superuser, 1, 'is a superuser'; + +$user->update({ is_superuser => 0 }); +stdout_is { FixMyStreet::Script::CreateSuperuser::createsuperuser('test@example.org'); } + "test\@example.org is now a superuser.\n", 'Correct message shown'; +$user->discard_changes; +is $user->is_superuser, 1, 'is a superuser again'; + +done_testing; |