From 52655d3a2822ab2400a13fc54ffb6973eb111aaa Mon Sep 17 00:00:00 2001 From: James Smith Date: Fri, 15 Mar 2024 23:46:23 +0000 Subject: [PATCH] authorization for library controller --- app/controllers/libraries_controller.rb | 2 +- app/policies/library_policy.rb | 4 ++ spec/requests/libraries_spec.rb | 53 ++++++++++++++++++------- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/app/controllers/libraries_controller.rb b/app/controllers/libraries_controller.rb index 60f8901fc..e4c43e937 100644 --- a/app/controllers/libraries_controller.rb +++ b/app/controllers/libraries_controller.rb @@ -38,7 +38,7 @@ class LibrariesController < ApplicationController def update @library.update(library_params) - uptags = library_params[:tag_regex].reject(&:empty?) + uptags = library_params[:tag_regex]&.reject(&:empty?) @library.tag_regex = uptags if @library.save redirect_to models_path, notice: t(".success") diff --git a/app/policies/library_policy.rb b/app/policies/library_policy.rb index f4a07c405..265384c07 100644 --- a/app/policies/library_policy.rb +++ b/app/policies/library_policy.rb @@ -19,6 +19,10 @@ class LibraryPolicy < ApplicationPolicy !Flipper.enabled?(:demo_mode) && user.admin? end + def scan? + true + end + class Scope attr_reader :user, :scope diff --git a/spec/requests/libraries_spec.rb b/spec/requests/libraries_spec.rb index e2a5faf49..85e68cf93 100644 --- a/spec/requests/libraries_spec.rb +++ b/spec/requests/libraries_spec.rb @@ -12,24 +12,32 @@ require "rails_helper" # DELETE /libraries/:id(.:format) libraries#destroy RSpec.describe "Libraries" do + let(:admin) { create(:user, admin: true) } + context "when signed out" do it "needs testing when multiuser is enabled" end context "when signed in" do before do - sign_in create(:user) + sign_in admin @library = create(:library) do |library| create_list(:model, 2, library: library) end end - describe "POST /libraries/:id/scan" do # rubocop:todo RSpec/RepeatedExampleGroupBody - it "needs testing" + describe "POST /libraries/:id/scan" do + it "scans a single library" do + expect { post "/libraries/#{@library.id}/scan" }.to have_enqueued_job(Scan::DetectFilesystemChangesJob).exactly(:once) + expect(response).to redirect_to("/libraries/#{@library.id}") + end end - describe "POST /libraries/scan" do # rubocop:todo RSpec/RepeatedExampleGroupBody - it "needs testing" + describe "POST /libraries/scan" do + it "scans all libraries" do + expect { post "/libraries/scan" }.to have_enqueued_job(Scan::DetectFilesystemChangesJob).exactly(:once) + expect(response).to redirect_to("/models") + end end describe "GET /libraries" do @@ -39,16 +47,25 @@ RSpec.describe "Libraries" do end end - describe "POST /libraries/" do # rubocop:todo RSpec/RepeatedExampleGroupBody - it "needs testing" + describe "POST /libraries/" do + it "creates a new library" do + post "/libraries", params: {library: {name: "new"}} + expect(response).to have_http_status(:success) + end end - describe "GET /libraries/new" do # rubocop:todo RSpec/RepeatedExampleGroupBody - it "needs testing" + describe "GET /libraries/new" do + it "shows the new library form" do + get "/libraries/new" + expect(response).to have_http_status(:success) + end end - describe "GET /libraries/:id/edit" do # rubocop:todo RSpec/RepeatedExampleGroupBody - it "needs testing" + describe "GET /libraries/:id/edit" do + it "shows the edit library form" do + get "/libraries/#{@library.id}/edit" + expect(response).to have_http_status(:success) + end end describe "GET /libraries/:id" do @@ -58,12 +75,18 @@ RSpec.describe "Libraries" do end end - describe "PATCH /libraries/:id" do # rubocop:todo RSpec/RepeatedExampleGroupBody - it "needs testing" + describe "PATCH /libraries/:id" do + it "updates the library" do + patch "/libraries/#{@library.id}", params: {library: {name: "new"}} + expect(response).to redirect_to("/models") + end end - describe "DELETE /libraries/:id" do # rubocop:todo RSpec/RepeatedExampleGroupBody - it "needs testing" + describe "DELETE /libraries/:id" do + it "removes the library" do + delete "/libraries/#{@library.id}" + expect(response).to redirect_to("/libraries") + end end end end