Skip to content

Commit

Permalink
Fix failing tests, and display of error flash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Dananji committed Dec 7, 2022
1 parent 7d91155 commit c82ef98
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 16 deletions.
6 changes: 3 additions & 3 deletions app/controllers/admin/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ def create
redirect_to @collection, notice: 'Collection was successfully created.'
end
format.json do
render json: @collection
render json: {id: @collection.id}, status: 200
end
end
else
logger.warn "Failed to create collection #{@collection.name rescue '<unknown>'}: #{@collection.errors.full_messages}"
respond_to do |format|
format.html do
flash.now[:error] = @collection.errors.full_messages.to_sentence
render action: 'new'
end
format.json do
flash[:error] = "Failed to create collection #{@collection.name rescue '<unknown>'}: #{@collection.errors.full_messages}"
render json: {errors: @collection.errors, flash: flash}
render json: { errors: ['Failed to create collection:']+@collection.errors.full_messages}, status: 422
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions app/views/admin/collections/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Unless required by applicable law or agreed to in writing, software distributed
specific language governing permissions and limitations under the License.
--- END LICENSE_HEADER BLOCK ---
%>
<% unless Avalon::ControlledVocabulary.vocabulary[:units] %>
<% raise Avalon::VocabularyNotFound.new "Units vocabulary not found." %>
<% end %>
<% @page_title = t('collections.title', :application_name => application_name) %>
<% unless @collections.empty? %>
Expand All @@ -21,7 +26,7 @@ Unless required by applicable law or agreed to in writing, software distributed
<h1 class="page-title">My Collections</h1>
</div>
<div class="col-xs-6 collection-btn">
<%= link_to('<span class="btn btn-primary headline-button">Create Collection</span>'.html_safe, new_admin_collection_path) unless cannot? :create, Admin::Collection %>
<%= link_to('<span class="btn btn-primary btn-large">Create Collection</span>'.html_safe, new_admin_collection_path) unless cannot? :create, Admin::Collection %>
</div>
</div>

Expand Down Expand Up @@ -69,7 +74,7 @@ Unless required by applicable law or agreed to in writing, software distributed
<p>Would you like to create one?</p>
<div class="whitespace"></div>
<p>
<%= button_tag("Create Collection", class: 'btn btn-primary btn-large', data: {toggle:"modal", target:"#new_collection"}) %>
<%= link_to('<span class="btn btn-primary btn-large">Create Collection</span>'.html_safe, new_admin_collection_path) %>
</p>
<% else %>
<p>You'll need to be assigned to one</p>
Expand Down
8 changes: 3 additions & 5 deletions app/views/admin/collections/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ Unless required by applicable law or agreed to in writing, software distributed
specific language governing permissions and limitations under the License.
--- END LICENSE_HEADER BLOCK ---
%>
<% unless Avalon::ControlledVocabulary.vocabulary[:units] %>
<% raise Avalon::VocabularyNotFound.new "Units vocabulary not found." %>
<% end %>
<div class="page-title-wrapper">
<h1 class="page-title">New collection</h1>
</div>
<%= bootstrap_form_for @collection, action: 'create' do |f| %>
<%= f.text_field :name %>
<% if @collection.new_record? || can?(:update_unit, @collection)%>
Expand Down
80 changes: 80 additions & 0 deletions spec/features/capybara_admin_collection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2011-2022, The Trustees of Indiana University and Northwestern
# University. Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# --- END LICENSE_HEADER BLOCK ---

require 'rails_helper'

describe 'Admin Collection' do
after { Warden.test_reset! }
it 'checks navigation when create new collection is accessed' do
user = FactoryBot.create(:administrator)
login_as user, scope: :user
visit '/'
click_link('Manage Content')
expect(page).to have_current_path('/admin/collections')
expect(page).to have_link('Create Collection')
click_link('Create Collection')
expect(page).to have_current_path('/admin/collections/new')
expect(page).to have_content('Name')
expect(page).to have_content('Description')
expect(page).to have_content('Unit')
expect(page).to have_content('Default Unit')
expect(page).to have_link('Cancel')
end
it 'is able to create a new collection' do
user = FactoryBot.create(:administrator)
login_as user, scope: :user
visit '/admin/collections/'
click_link('Create Collection')
fill_in('admin_collection_name', with: 'Test Collection')
select('Default Unit', from: 'admin_collection_unit')
click_on('Create Collection')
visit '/admin/collections'
expect(page).to have_content('Test Collection')
expect(page).to have_content('Title')
expect(page).to have_content('Items')
expect(page).to have_content('Managers')
expect(page).to have_content('Description')
expect(page).to have_link('Delete')
end
it 'is able to view collection by clicking on collection name' do
user = FactoryBot.create(:administrator)
login_as user, scope: :user
visit '/admin/collections'
click_link('Create Collection')
fill_in('admin_collection_name', with: 'Test Collection')
select('Default Unit', from: 'admin_collection_unit')
click_on('Create Collection')
visit '/admin/collections'
click_on('Test Collection')
expect(page).to have_content('Test Collection')
expect(page).to have_link('Create An Item')
expect(page).to have_link('List All Items')
expect(page).to have_button('Edit Collection Info')
expect(page).to have_content('Default Unit')
end
it 'is able to delete a collection' do
user = FactoryBot.create(:administrator)
login_as user, scope: :user
visit '/admin/collections'
click_link('Create Collection')
fill_in('admin_collection_name', with: 'Test Collection')
select('Default Unit', from: 'admin_collection_unit')
click_on('Create Collection')
visit '/admin/collections'
click_link('Delete')
expect(page).to have_content('Are you certain you want to remove the collection Test Collection?')
expect(page).to have_button('Yes, I am sure')
expect(page).to have_link('No, go back')
end
end
7 changes: 1 addition & 6 deletions spec/features/capybara_navigation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@
expect(page).to have_current_path('/admin/collections')
expect(page).to have_content('Skip to main content')
expect(page).to have_link('Selected Items (0)')
expect(page).to have_button('Create Collection')
expect(page).to have_content('Name')
expect(page).to have_content('Description')
expect(page).to have_content('Unit')
expect(page).to have_content('Default Unit')
expect(page).to have_link('Cancel')
expect(page).to have_link('Create Collection')
end
it 'checks naviagtion to Manage Groups' do
user = FactoryBot.create(:administrator)
Expand Down

0 comments on commit c82ef98

Please sign in to comment.