Skip to content

Commit

Permalink
Expand publications controller tests, clarify #show
Browse files Browse the repository at this point in the history
  • Loading branch information
Janell-Huyck committed Feb 21, 2024
1 parent 5f00e12 commit 31a8a7a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .ruby-gemset
Original file line number Diff line number Diff line change
@@ -1 +1 @@
aaec
aaec
4 changes: 3 additions & 1 deletion app/controllers/publications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ def index
end

def show
return unless session[:admin]

resource = controller_name.singularize
resource_instance = instance_variable_get("@#{resource}")

raise ActiveRecord::RecordNotFound unless resource_instance

@submitter = helpers.find_submitter(resource_instance.id) if session[:admin]
@submitter = helpers.find_submitter(resource_instance.id)
end

def new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
# need to define it here.
RSpec.describe BooksController, type: :controller do
let(:submitter) { FactoryBot.create(:submitter) }
let(:another_submitter) { FactoryBot.create(:submitter) }
let(:book) { FactoryBot.create(:book, submitter_id: submitter.id.to_s) }
let(:another_book) { FactoryBot.create(:book, submitter_id: another_submitter.id.to_s) }

before do
book
Expand Down Expand Up @@ -44,13 +46,13 @@
end

context 'as a submitter' do
context 'who owns the book' do
before do
session[:admin] = nil
book
login_as_submitter_of(book)
end
before do
session[:admin] = nil
book
login_as_submitter_of(book)
end

context 'who owns the book' do
it 'does not set @submitter' do
get :show, params: { id: book.id }
expect(assigns(:submitter)).to be_nil
Expand All @@ -61,6 +63,13 @@
expect(response).to render_template(:show)
end
end

context 'who does not own the book' do
it 'raises a 404 not found error and does not set submitter' do
expect { get :show, params: { id: another_book.id } }.to raise_error(ActiveRecord::RecordNotFound)
expect(assigns(:submitter)).to be_nil
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@

context 'and the book does not belong to the submitter' do
it 'does not update the requested book and raises a 404 error' do
expect {
put :update, params: { id: already_created_book_by_another_submitter.id, book: new_attributes }}
expect do
put :update, params: { id: already_created_book_by_another_submitter.id, book: new_attributes }
end
.to raise_error(ActiveRecord::RecordNotFound)

already_created_book_by_another_submitter.reload
Expand Down

0 comments on commit 31a8a7a

Please sign in to comment.