-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '285-user-name-edits' into develop
- Loading branch information
Showing
11 changed files
with
325 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
## | ||
# Controller to allow current user to change their name | ||
# | ||
class Users::NamesController < ApplicationController | ||
before_action :check_user_logged_in, :check_user_suspension, :load_user | ||
|
||
def update | ||
if @edit_user.update(user_params) | ||
flash[:notice] = _('Name successfully updated.') | ||
redirect_to user_url(@edit_user) | ||
else | ||
render action: 'edit' | ||
end | ||
end | ||
|
||
private | ||
|
||
def user_params | ||
params.require(:user).permit(:name) | ||
end | ||
|
||
def check_user_logged_in | ||
return if authenticated? | ||
|
||
flash[:error] = _('You need to be logged in to change your name') | ||
redirect_to frontpage_url | ||
end | ||
|
||
def check_user_suspension | ||
return unless current_user.suspended? | ||
|
||
flash[:error] = _('Suspended users cannot edit their profile') | ||
redirect_to edit_profile_about_me_path | ||
end | ||
|
||
def load_user | ||
# Don't make changes to the current_user, this could brake the layout as we | ||
# use name/url_name in the login bar URLs | ||
@edit_user = User.find_by(url_name: current_user.url_name) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<% @title = _('Change your name used on {{site_name}}', site_name: site_name) %> | ||
|
||
<h1><%= @title %></h1> | ||
|
||
<div id="notice"> | ||
<p> | ||
<%= _('When you send a request, we use the name you gave us. This makes ' \ | ||
'it hard to stop your old and new names from being connected.') %> | ||
</p> | ||
<p> | ||
<%= _('If you change your name, your old requests won\'t change. Your ' \ | ||
'old name will still show up on requests that you\'ve already sent ' \ | ||
'and on your public profile page.') %> | ||
</p> | ||
<p> | ||
<%= _('If you don\'t want your new name to be linked to your old one, ' \ | ||
'it\'s a good idea to make a new account instead.') %> | ||
</p> | ||
<p> | ||
<%= _('If you are trying to remove your name, please | ||
<a href="{{contact_us_url}}">contact us</a>.', | ||
contact_us_url: help_contact_path) %> | ||
</p> | ||
</div> | ||
|
||
<div id="change_name" class="change_name"> | ||
<%= form_for @edit_user, url: users_name_path do |f| %> | ||
<%= foi_error_messages_for :edit_user %> | ||
|
||
<p> | ||
<label class="form_label" for="name"><%= _('Name:') %></label> | ||
<%= f.text_field 'name', { size: 20 } %> | ||
</p> | ||
|
||
<div class="form_button"> | ||
<%= submit_tag _('Change your name') %> | ||
</div> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Users::NamesController do | ||
describe 'GET edit' do | ||
context 'without a logged in user' do | ||
it 'redirects to the home page' do | ||
sign_in nil | ||
get :edit | ||
expect(response).to redirect_to(frontpage_path) | ||
end | ||
end | ||
|
||
context 'with a logged in user' do | ||
let(:user) { FactoryBot.create(:user) } | ||
|
||
it 'assigns the currently logged in user' do | ||
sign_in user | ||
get :edit | ||
expect(assigns[:user]).to eq(user) | ||
end | ||
|
||
it 'is successful' do | ||
sign_in user | ||
get :edit | ||
expect(response).to be_successful | ||
end | ||
|
||
it 'renders the edit form' do | ||
sign_in user | ||
get :edit | ||
expect(response).to render_template(:edit) | ||
end | ||
end | ||
end | ||
|
||
describe 'PUT update' do | ||
context 'without a logged in user' do | ||
it 'redirects to the sign in page' do | ||
sign_in nil | ||
put :update, params: { user: { about_me: 'Bobby' } } | ||
expect(response).to redirect_to(frontpage_path) | ||
end | ||
end | ||
|
||
context 'with a banned user' do | ||
before { sign_in FactoryBot.create(:user, :banned) } | ||
|
||
it 'displays an error' do | ||
put :update, params: { user: { name: 'Bobby' } } | ||
expect(flash[:error]).to eq('Suspended users cannot edit their profile') | ||
end | ||
|
||
it 'redirects to edit' do | ||
put :update, params: { user: { name: 'Bobby' } } | ||
expect(response).to redirect_to(edit_profile_about_me_path) | ||
end | ||
end | ||
|
||
context 'with valid attributes' do | ||
let(:user) { FactoryBot.create(:user) } | ||
before { sign_in user } | ||
|
||
it 'assigns the currently logged in user' do | ||
put :update, params: { user: { name: 'Bobby' } } | ||
expect(assigns[:user]).to eq(user) | ||
end | ||
|
||
it 'updates the user name' do | ||
put :update, params: { user: { name: 'Bobby' } } | ||
expect(user.reload.name).to eq('Bobby') | ||
end | ||
end | ||
|
||
context 'with bad parameters' do | ||
before { sign_in FactoryBot.create(:user) } | ||
|
||
it 'can raise missing parameter exeception' do | ||
expect { | ||
put :update, params: { name: 'Bobby' } | ||
}.to raise_error(ActionController::ParameterMissing) | ||
end | ||
|
||
it 'can raise unpermitted parameter exeception' do | ||
expect { | ||
put :update, params: { user: { name: 'Updated text', role_ids: [1] } } | ||
}.to raise_error(ActionController::UnpermittedParameters) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.