diff options
author | Dave Arter <davea@mysociety.org> | 2019-08-06 14:38:43 +0100 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2019-08-07 10:28:58 +0100 |
commit | 09645c78dead379ce0acd27462d0fc3c6779948a (patch) | |
tree | d9bf9009f32df88eac995c9a512600d71deb12b9 | |
parent | 64d141404b5b6714b76c7606ad63cd05a78bf9c5 (diff) |
Redirect correctly after editing user to remove them from cobrand
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin/Users.pm | 17 | ||||
-rw-r--r-- | t/app/controller/admin/users.t | 43 |
2 files changed, 58 insertions, 2 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin/Users.pm b/perllib/FixMyStreet/App/Controller/Admin/Users.pm index d29d2b8b9..fd18caf21 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/Users.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/Users.pm @@ -174,7 +174,7 @@ sub add : Local : Args(0) { $c->forward( '/admin/log_edit', [ $user->id, 'user', 'edit' ] ); $c->flash->{status_message} = _("Updated!"); - $c->res->redirect( $c->uri_for_action( 'admin/users/edit', $user->id ) ); + $c->detach('post_edit_redirect', [ $user ]); } sub fetch_body_roles : Private { @@ -415,7 +415,8 @@ sub edit : Path : Args(1) { $c->forward( '/admin/log_edit', [ $id, 'user', 'edit' ] ); } $c->flash->{status_message} = _("Updated!"); - return $c->res->redirect( $c->uri_for_action( 'admin/users/edit', $user->id ) ); + + $c->detach('post_edit_redirect', [ $user ]); } if ( $user->from_body ) { @@ -442,6 +443,18 @@ sub edit : Path : Args(1) { return 1; } +sub post_edit_redirect : Private { + my ( $self, $c, $user ) = @_; + + # User may not be visible on this cobrand, e.g. if their from_body + # wasn't set. + if ( $c->cobrand->users->find( { id => $user->id } ) ) { + return $c->res->redirect( $c->uri_for_action( 'admin/users/edit', $user->id ) ); + } else { + return $c->res->redirect( $c->uri_for_action( 'admin/users/index' ) ); + } +} + sub import :Local { my ( $self, $c, $id ) = @_; diff --git a/t/app/controller/admin/users.t b/t/app/controller/admin/users.t index ce29a5f7c..b6da170ba 100644 --- a/t/app/controller/admin/users.t +++ b/t/app/controller/admin/users.t @@ -3,6 +3,8 @@ use FixMyStreet::TestMech; my $mech = FixMyStreet::TestMech->new; my $user = $mech->create_user_ok('test@example.com', name => 'Test User'); +my $user2 = $mech->create_user_ok('test2@example.com', name => 'Test User 2'); +my $user3 = $mech->create_user_ok('test3@example.com', name => 'Test User 3'); my $superuser = $mech->create_user_ok('superuser@example.com', name => 'Super User', is_superuser => 1); @@ -120,6 +122,47 @@ subtest 'user_edit does not show user from another council' => sub { }; }; +$mech->log_out_ok; + +subtest 'user_edit redirects appropriately' => sub { + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'oxfordshire' ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $user2->update({ from_body => $oxfordshire->id }); + $user3->update({ from_body => $oxfordshire->id }); + $user3->user_body_permissions->create( { + body => $oxfordshire, + permission_type => 'user_edit', + } ); + $user3->user_body_permissions->create( { + body => $oxfordshire, + permission_type => 'user_assign_body', + } ); + $mech->log_in_ok( $user3->email ); + + $mech->get_ok('/admin/users/' . $user2->id); + $mech->submit_form_ok( { with_fields => { + name => "Updated Name" + } } ); + $user2->discard_changes; + is $user2->name, "Updated Name", "Name set correctly"; + is $mech->uri->path, '/admin/users/' . $user2->id, 'redirected back to user form'; + + $mech->get_ok('/admin/users/' . $user2->id); + $mech->submit_form_ok( { with_fields => { + body => undef + } } ); + $user2->discard_changes; + is $user2->from_body, undef, "from_body unset"; + is $mech->uri->path, '/admin/users', 'redirected back to users list'; + + $mech->log_out_ok; + }; +}; + +$mech->log_in_ok( $superuser->email ); + for my $test ( { desc => 'add user - blank form', |