diff --git a/spec/controllers/staff/makerstore_links_controller_spec.rb b/spec/controllers/staff/makerstore_links_controller_spec.rb new file mode 100644 index 000000000..d727b7b7e --- /dev/null +++ b/spec/controllers/staff/makerstore_links_controller_spec.rb @@ -0,0 +1,58 @@ +require "rails_helper" + +RSpec.describe Staff::MakerstoreLinksController, type: :controller do + before(:each) { session[:user_id] = (create :user, :staff).id } + + describe "GET /index" do + context "logged in as staff" do + it "should return http success" do + get :index + expect(response).to have_http_status(:success) + end + end + + context "not logged in" do + it "should return http redirect" do + session[:user_id] = nil + get :index + expect(response).to_not have_http_status(:success) + end + end + end + + describe "POST /create" do + context "create a new link" do + it "should create link" do + makerstore_link_params = + FactoryBot.attributes_for(:makerstore_link, :with_image) + expect { + post :create, params: { makerstore_link: makerstore_link_params } + }.to change(MakerstoreLink, :count).by 1 + end + + it "should reject link without image" do + makerstore_link_params = FactoryBot.attributes_for(:makerstore_link) + expect { + post :create, params: { makerstore_link: makerstore_link_params } + }.to change(MakerstoreLink, :count).by 0 + expect(flash[:alert]&.empty?).to be_falsy + end + end + end + + describe "PATCH #update" do + context "hiding and showing a link" do + it "should hide a link" do + link = create(:makerstore_link, :with_image) + put :update, params: { id: link.id, makerstore_link: { shown: false } } + expect(MakerstoreLink.find(link.id).shown).to be false + end + + it "should show a link" do + link = create(:makerstore_link, :with_image, :hidden) + put :update, params: { id: link.id, makerstore_link: { shown: true } } + expect(MakerstoreLink.find(link.id).shown).to be true + end + end + end +end diff --git a/spec/factories/makerstore_links.rb b/spec/factories/makerstore_links.rb new file mode 100644 index 000000000..86104c2cf --- /dev/null +++ b/spec/factories/makerstore_links.rb @@ -0,0 +1,15 @@ +FactoryBot.define do + factory :makerstore_link do + title { Faker::Alphanumeric.alphanumeric(number: 10) } + url { Faker::Internet.url } + shown { true } + order { 0 } + trait :with_image do + image { FilesTestHelper.png } + end + + trait :hidden do + shown { false } + end + end +end