Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Janell-Huyck committed Feb 21, 2024
1 parent 31abb71 commit bb85d37
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .ruby-gemset
Original file line number Diff line number Diff line change
@@ -1 +1 @@
aaec
aaec
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@

context 'and the book does not belong to the submitter' do
it 'does not destroy the requested book' do
skip 'waiting for related logic to be merged from PR # 323'
expect { Book.find(already_created_book_by_another_submitter.id) }.not_to raise_error
expect do
delete :destroy, params: { id: already_created_book_by_another_submitter.id }
Expand All @@ -75,13 +74,11 @@
end

it 'redirects to the publications_path' do
skip 'waiting for related logic to be merged from PR # 323'
delete :destroy, params: { id: already_created_book_by_another_submitter.id }
expect(response).to redirect_to(publications_path)
end

it 'displays a flash alert' do
skip 'waiting for related logic to be merged from PR # 323'
delete :destroy, params: { id: already_created_book_by_another_submitter.id }
expect(flash[:danger]).to eql 'You are not authorized to delete this publication.'
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,88 +11,55 @@
# 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(:submitter_id) { submitter.id.to_s }
let(:another_submitter_id) { another_submitter.id.to_s }

let(:valid_attributes) do
{ 'author_first_name' => %w[Test Person],
'author_last_name' => %w[Case 2],
'college_ids' => ['', '1', '2'],
'uc_department' => 'Dept',
'work_title' => 'WT',
'other_title' => 'OT',
'publisher' => 'Pub',
'city' => 'City',
'publication_date' => 'Today',
'url' => 'www.fake.com',
'doi' => 'doi:' }
before do
book
end

let(:already_created_book_by_submitter) { FactoryBot.create(:book, valid_attributes.merge(submitter_id:)) }
let(:already_created_book_by_another_submitter) { FactoryBot.create(:book, valid_attributes.merge(submitter_id: another_submitter_id)) }

# Check to make sure that the correct @submitter is set when show is called
# and that the correct template is rendered when appropriate.
describe 'GET #show' do
before do
already_created_book_by_submitter
already_created_book_by_another_submitter
end

context 'when attempting to view books as a submitter' do
context 'as an admin user' do
before do
login_as_submitter_of(already_created_book_by_submitter)
session[:admin] = true
get :show, params: { id: book.id }
end

context 'and the book belongs to the submitter' do
it 'leaves @submitter set to the current submitter' do
expect(assigns(:submitter)).to eql already_created_book_by_submitter.submitter
get :show, params: { id: already_created_book_by_submitter.id }
expect(assigns(:submitter)).to eql already_created_book_by_submitter.submitter
end
it 'sets @submitter correctly' do
expect(assigns(:submitter).id.to_s).to eq(book.submitter_id)
end

it 'renders the show template' do
get :show, params: { id: already_created_book_by_submitter.id }
expect(response).to render_template('show')
end
it 'renders the show template' do
expect(response).to render_template(:show)
end

context 'and the book does not belong to the submitter' do
it 'leaves @submitter set to the current submitter' do
expect(assigns(:submitter)).to eql already_created_book_by_submitter.submitter
get :show, params: { id: already_created_book_by_another_submitter.id }
expect(assigns(:submitter)).to eql already_created_book_by_submitter.submitter
context 'when the book does not exist' do
before do
session[:admin] = true
end

it 'throws a 404 error' do
expect { get :show, params: { id: already_created_book_by_another_submitter.id } }
.to raise_error(ActiveRecord::RecordNotFound)
it 'responds with a 404 not found' do
expect { get :show, params: { id: 'nonexistent' } }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end

context 'when attempting to delete books as an admin' do
before do
login_as_admin
end

it 'sets @submitter to the submitter of the requested book' do
expect(assigns(:submitter)).to be_nil
get :show, params: { id: already_created_book_by_submitter.id }
expect(assigns(:submitter)).to eql already_created_book_by_submitter.submitter

get :show, params: { id: already_created_book_by_another_submitter.id }
expect(assigns(:submitter)).to eql already_created_book_by_another_submitter.submitter
end
context 'as a submitter' do
context 'who owns the book' do
before do
session[:admin] = nil
book
login_as_submitter_of(book)
end

it 'renders the show template' do
get :show, params: { id: already_created_book_by_submitter.id }
expect(response).to render_template('show')
it 'does not set @submitter' do
get :show, params: { id: book.id }
expect(assigns(:submitter)).to be_nil
end

get :show, params: { id: already_created_book_by_another_submitter.id }
expect(response).to render_template('show')
it 'renders the show template' do
get :show, params: { id: book.id }
expect(response).to render_template(:show)
end
end
end
end
Expand Down

0 comments on commit bb85d37

Please sign in to comment.