Skip to content

Commit

Permalink
tests passing with UserAuthentication checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Janell-Huyck committed Nov 28, 2023
1 parent e6e31be commit fd0cb2f
Show file tree
Hide file tree
Showing 23 changed files with 320 additions and 213 deletions.
5 changes: 4 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ class ApplicationController < ActionController::Base
include Pagy::Backend
include UserAuthentication

before_action :check_date
prepend_before_action :check_date

skip_before_action :require_authenticated_user, only: :check_date
skip_before_action :check_date, only: :closed

private

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/publications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def update
format.html { redirect_to instance_variable }
format.json { render :show, status: :created, location: instance_variable }
else
format.html { render :new }
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: instance_variable.errors, status: :unprocessable_entity }
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

context 'when admin is not logged in' do
before do
allow(controller).to receive(:session).and_return(admin: false)
session[:admin] = false
session[:submitter_id] = FactoryBot.create(:submitter).id
end

it 'redirects to publications_path' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
RSpec.describe AdminController, type: :controller do
let(:common_params) { { 'controller_name' => 'other_publications' } }
let(:admin_session) { { 'admin' => true } }
let(:submitter_session) { { 'submitter_id' => FactoryBot.create(:submitter).id } }

describe 'GET #csv' do
context 'when the user is an admin' do
Expand Down Expand Up @@ -33,7 +34,8 @@

context 'when the user is not an admin' do
it 'redirects even if a valid format is provided' do
get(:csv, params: common_params.merge({ format: 'csv' }))
get(:csv, params: common_params.merge({ format: 'csv' }), session: submitter_session)
expect(response).to have_http_status(302)
expect(response).to redirect_to('/publications')
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

RSpec.describe AdminController, type: :controller do
describe 'POST #toggle_links' do
before do
session[:admin] = true
end

context 'when session[:links] is true' do
before do
session[:links] = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
RSpec.describe ApplicationController, type: :controller do
include ApplicationHelper

let(:submitter) { FactoryBot.create(:submitter) }
let(:submitter_session) { { submitter_id: submitter.id } }

controller(ApplicationController) do
def index
render plain: 'Hello, world!'
Expand All @@ -20,16 +23,17 @@ def index
context 'when EXPIRATION_DATE is in the past and user is not admin' do
it 'redirects to the closed page' do
allow(ENV).to receive(:fetch).with('EXPIRATION_DATE').and_return('2000-01-01')
get :index
get :index, session: submitter_session
expect(response).to redirect_to(page_route('closed'))
end
end

context 'when EXPIRATION_DATE is in the future' do
it 'does not redirect' do
allow(ENV).to receive(:fetch).with('EXPIRATION_DATE').and_return('3000-01-01')
get :index
expect(response.body).to eq('Hello, world!')
get :index, session: submitter_session
expect(response).to have_http_status(:ok)
expect(response.body).to include('Hello, world!')
end
end

Expand Down
23 changes: 15 additions & 8 deletions spec/controllers/artworks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

let(:submitter) { FactoryBot.create(:submitter) }
let(:valid_session) { { submitter_id: submitter.id } }
let(:artwork) { Artwork.create! valid_attributes }

it_behaves_like 'restricts non-logged-in users', {
'index' => :get,
Expand All @@ -26,7 +27,7 @@

describe 'POST #create' do
context 'with valid params' do
it 'creates a new Other Publication' do
it 'creates a new Artwork' do
expect do
post :create, params: { artwork: valid_attributes }, session: valid_session
end.to change(Artwork, :count).by(1)
Expand All @@ -39,6 +40,12 @@
end

context 'with invalid params' do
it 'does not create a new Artwork' do
expect do
post :create, params: { artwork: invalid_attributes }, session: valid_session
end.not_to change(Artwork, :count)
end

it "redirects to the 'new' template with status 'unprocessable_entity'" do
post :create, params: { artwork: invalid_attributes }, session: valid_session
expect(response).to render_template(:new)
Expand All @@ -54,39 +61,39 @@
end

it 'updates the requested other publication' do
artwork = Artwork.create! valid_attributes
put :update, params: { id: artwork.to_param, artwork: new_attributes }, session: valid_session
artwork.reload
expect(artwork.date).to eql 'new date'
expect(artwork.college_ids).to eql [6, 7]
end

it 'redirects to the artwork' do
artwork = Artwork.create! valid_attributes
put :update, params: { id: artwork.to_param, artwork: valid_attributes }, session: valid_session
expect(response).to redirect_to(artwork)
end
end

context 'with invalid params' do
it "returns a success response (i.e. to display the 'edit' template)" do
artwork = Artwork.create! valid_attributes
it "redirects to the 'edit' template with status 'unprocessable_entity'" do
put :update, params: { id: artwork.to_param, artwork: invalid_attributes }, session: valid_session
expect(response).to be_successful
expect(response).to render_template(:edit)
expect(response.status).to eql 422
end
end
end

describe 'DELETE #destroy' do
before do
artwork
end

it 'destroys the requested artwork' do
artwork = Artwork.create! valid_attributes
expect do
delete :destroy, params: { id: artwork.to_param }, session: valid_session
end.to change(Artwork, :count).by(-1)
end

it 'redirects to the artworks list' do
artwork = Artwork.create! valid_attributes
delete :destroy, params: { id: artwork.to_param }, session: valid_session
expect(response).to redirect_to(artworks_url)
end
Expand Down
31 changes: 20 additions & 11 deletions spec/controllers/book_chapters_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
{ 'author_first_name' => ['Bad'], 'author_last_name' => [''], 'college_ids' => [''], 'uc_department' => '', 'work_title' => '', 'other_title' => '', 'publisher' => '', 'page_numbers' => '', 'city' => '', 'publication_date' => '', 'url' => '', 'doi' => '' }
end

let(:valid_session) { { submitter_id: 1 } }
let(:submitter) { FactoryBot.create(:submitter) }
let(:valid_session) { { submitter_id: submitter.id } }
let(:book_chapter) { BookChapter.create! valid_attributes }

it_behaves_like 'restricts non-logged-in users', {
'index' => :get,
Expand All @@ -29,7 +31,7 @@
FactoryBot.create(:submitter)
end

it 'creates a new Other Publication' do
it 'creates a new BookChapter' do
expect do
post :create, params: { book_chapter: valid_attributes }, session: valid_session
end.to change(BookChapter, :count).by(1)
Expand All @@ -42,9 +44,16 @@
end

context 'with invalid params' do
it "returns a success response (i.e. to display the 'new' template)" do
it 'does not create a new Book Chapter' do
expect do
post :create, params: { book_chapter: invalid_attributes }, session: valid_session
end.not_to change(Artwork, :count)
end

it "redirects to the 'new' template with status 'unprocessable_entity'" do
post :create, params: { book_chapter: invalid_attributes }, session: valid_session
expect(response).to be_successful
expect(response).to render_template(:new)
expect(response.status).to eql 422
end
end
end
Expand All @@ -56,39 +65,39 @@
end

it 'updates the requested other publication' do
book_chapter = BookChapter.create! valid_attributes
put :update, params: { id: book_chapter.to_param, book_chapter: new_attributes }, session: valid_session
book_chapter.reload
expect(book_chapter.url).to eql 'www.cool.com'
expect(book_chapter.college_ids).to eql [6, 7]
end

it 'redirects to the book_chapter' do
book_chapter = BookChapter.create! valid_attributes
put :update, params: { id: book_chapter.to_param, book_chapter: valid_attributes }, session: valid_session
expect(response).to redirect_to(book_chapter)
end
end

context 'with invalid params' do
it "returns a success response (i.e. to display the 'edit' template)" do
book_chapter = BookChapter.create! valid_attributes
it "redirects to the 'edit' template with status 'unprocessable_entity'" do
put :update, params: { id: book_chapter.to_param, book_chapter: invalid_attributes }, session: valid_session
expect(response).to be_successful
expect(response).to render_template(:edit)
expect(response.status).to eql 422
end
end
end

describe 'DELETE #destroy' do
before do
book_chapter
end

it 'destroys the requested book_chapter' do
book_chapter = BookChapter.create! valid_attributes
expect do
delete :destroy, params: { id: book_chapter.to_param }, session: valid_session
end.to change(BookChapter, :count).by(-1)
end

it 'redirects to the book_chapters list' do
book_chapter = BookChapter.create! valid_attributes
delete :destroy, params: { id: book_chapter.to_param }, session: valid_session
expect(response).to redirect_to(book_chapters_url)
end
Expand Down
28 changes: 18 additions & 10 deletions spec/controllers/books_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
{ 'author_first_name' => ['Bad'], 'author_last_name' => [''], 'college_ids' => [''], 'uc_department' => '', 'work_title' => '', 'other_title' => '', 'publisher' => '', 'city' => '', 'publication_date' => '', 'url' => '', 'doi' => '' }
end

let(:valid_session) { { submitter_id: 1 } }
let(:submitter) { FactoryBot.create(:submitter) }
let(:valid_session) { { submitter_id: submitter.id } }
let(:book) { Book.create! valid_attributes }

it_behaves_like 'restricts non-logged-in users', {
'index' => :get,
Expand Down Expand Up @@ -42,9 +44,16 @@
end

context 'with invalid params' do
it "returns a success response (i.e. to display the 'new' template)" do
it 'does not create a new Book' do
expect do
post :create, params: { book: invalid_attributes }, session: valid_session
end.not_to change(Book, :count)
end

it "redirects to the 'new' template with status 'unprocessable_entity'" do
post :create, params: { book: invalid_attributes }, session: valid_session
expect(response).to be_successful
expect(response).to render_template(:new)
expect(response.status).to eql 422
end
end
end
Expand All @@ -56,39 +65,38 @@
end

it 'updates the requested book' do
book = Book.create! valid_attributes
put :update, params: { id: book.to_param, book: new_attributes }, session: valid_session
book.reload
expect(book.url).to eql 'www.cool.com'
expect(book.college_ids).to eql [3, 4]
end

it 'redirects to the book' do
book = Book.create! valid_attributes
put :update, params: { id: book.to_param, book: valid_attributes }, session: valid_session
expect(response).to redirect_to(book)
end
end

context 'with invalid params' do
it "returns a success response (i.e. to display the 'edit' template)" do
book = Book.create! valid_attributes
it "redirects to the 'edit' template with status 'unprocessable_entity'" do
put :update, params: { id: book.to_param, book: invalid_attributes }, session: valid_session
expect(response).to be_successful
expect(response).to render_template(:edit)
expect(response.status).to eql 422
end
end
end

describe 'DELETE #destroy' do
before do
book
end
it 'destroys the requested book' do
book = Book.create! valid_attributes
expect do
delete :destroy, params: { id: book.to_param }, session: valid_session
end.to change(Book, :count).by(-1)
end

it 'redirects to the books list' do
book = Book.create! valid_attributes
delete :destroy, params: { id: book.to_param }, session: valid_session
expect(response).to redirect_to(books_url)
end
Expand Down
Loading

0 comments on commit fd0cb2f

Please sign in to comment.