Merge 027fff4b57 into c0cdf3e6e5
This commit is contained in:
commit
4417536816
3 changed files with 69 additions and 1 deletions
|
|
@ -6,6 +6,19 @@ module Admin
|
||||||
@user = User.new
|
@user = User.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@user = User.new(user_params)
|
||||||
|
@user.password = Devise.friendly_token[0, 20]
|
||||||
|
@user.username = @user.email.split('@')[0]
|
||||||
|
@user.skip_confirmation!
|
||||||
|
if @user.save
|
||||||
|
redirect_to admin_users_path, notice: "User created. Name: #{@user.name}, email: #{@user.email}"
|
||||||
|
else
|
||||||
|
flash[:error] = "An error prohibited this user from being saved: #{@user.errors.full_messages.join('. ')}."
|
||||||
|
render :new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@users = User.all
|
@users = User.all
|
||||||
end
|
end
|
||||||
|
|
@ -33,5 +46,13 @@ module Admin
|
||||||
@user.destroy
|
@user.destroy
|
||||||
redirect_to admin_users_path, notice: 'User got deleted'
|
redirect_to admin_users_path, notice: 'User got deleted'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
# Only allow a trusted parameter "white list" through.
|
||||||
|
def user_params
|
||||||
|
# params.require(:user).permit(:email, :name, :affiliation, :biography)
|
||||||
|
params[:user]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
- if @users
|
- if @users
|
||||||
= "(#{@users.length})"
|
= "(#{@users.length})"
|
||||||
|
|
||||||
= link_to 'New User', new_admin_user_path, class: 'btn btn-success pull-right'
|
= link_to 'New User', new_admin_user_path, class: 'btn btn-success pull-right' unless CONFIG['authentication']['ichain']['enabled']
|
||||||
%table.table.table-striped.table-bordered.table-hover#users-datatable
|
%table.table.table-striped.table-bordered.table-hover#users-datatable
|
||||||
%thead
|
%thead
|
||||||
%th ID
|
%th ID
|
||||||
|
|
|
||||||
|
|
@ -50,4 +50,51 @@ describe Admin::UsersController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'GET #new' do
|
||||||
|
it 'assigns a new user to @user variable' do
|
||||||
|
get :new
|
||||||
|
expect(assigns(:user)).to be_a_new(User)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'renders the :new template' do
|
||||||
|
get :new
|
||||||
|
expect(response).to render_template :new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'POST #create' do
|
||||||
|
context 'with valid attributes' do
|
||||||
|
it 'saves the user to the database' do
|
||||||
|
expected = expect do
|
||||||
|
post :create, user: { name: 'New User', email: 'newuser@osem.localhost' }
|
||||||
|
end
|
||||||
|
expected.to change { User.count }.by 1
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'redirects to users#index' do
|
||||||
|
post :create, user: { name: 'New User', email: 'newuser@osem.localhost' }
|
||||||
|
expect(response).to redirect_to admin_users_path
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'shows success message' do
|
||||||
|
post :create, user: { name: 'New User', email: 'newuser@osem.localhost' }
|
||||||
|
expect(flash[:notice]).to match('User created. Name: New User, email: newuser@osem.localhost')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with invalid attributes' do
|
||||||
|
it 'does not save the user to the database' do
|
||||||
|
expected = expect do
|
||||||
|
post :create
|
||||||
|
end
|
||||||
|
expected.to_not change { User.count }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 're-renders the new template' do
|
||||||
|
post :create
|
||||||
|
|
||||||
|
expect(response).to be_success
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue