From 7801db7ba463a71b8a11a2867486b012a2ec3972 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 12 Jan 2024 04:19:25 -0500 Subject: [PATCH] Spec coverage for custom css endpoint (#28706) --- app/controllers/custom_css_controller.rb | 13 +++++ app/views/custom_css/show.css.erb | 6 +-- spec/requests/custom_css_spec.rb | 60 ++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 spec/requests/custom_css_spec.rb diff --git a/app/controllers/custom_css_controller.rb b/app/controllers/custom_css_controller.rb index e7a02ea89..62f8e0d77 100644 --- a/app/controllers/custom_css_controller.rb +++ b/app/controllers/custom_css_controller.rb @@ -1,8 +1,21 @@ # frozen_string_literal: true class CustomCssController < ActionController::Base # rubocop:disable Rails/ApplicationController + before_action :set_user_roles + def show expires_in 3.minutes, public: true render content_type: 'text/css' end + + private + + def custom_css_styles + Setting.custom_css + end + helper_method :custom_css_styles + + def set_user_roles + @user_roles = UserRole.where(highlighted: true).where.not(color: [nil, '']) + end end diff --git a/app/views/custom_css/show.css.erb b/app/views/custom_css/show.css.erb index 9cd38fb37..78da809ed 100644 --- a/app/views/custom_css/show.css.erb +++ b/app/views/custom_css/show.css.erb @@ -1,8 +1,8 @@ -<%- if Setting.custom_css.present? %> -<%= raw Setting.custom_css %> +<%- if custom_css_styles.present? %> +<%= raw custom_css_styles %> <%- end %> -<%- UserRole.where(highlighted: true).select { |role| role.color.present? }.each do |role| %> +<%- @user_roles.each do |role| %> .user-role-<%= role.id %> { --user-role-accent: <%= role.color %>; } diff --git a/spec/requests/custom_css_spec.rb b/spec/requests/custom_css_spec.rb new file mode 100644 index 000000000..5271ed4a5 --- /dev/null +++ b/spec/requests/custom_css_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'Custom CSS' do + include RoutingHelper + + describe 'GET /custom.css' do + context 'without any CSS or User Roles' do + it 'returns empty stylesheet' do + get '/custom.css' + + expect(response.content_type).to include('text/css') + expect(response.body.presence).to be_nil + end + end + + context 'with CSS settings' do + before do + Setting.custom_css = expected_css + end + + it 'returns stylesheet from settings' do + get '/custom.css' + + expect(response.content_type).to include('text/css') + expect(response.body.strip).to eq(expected_css) + end + + def expected_css + <<~CSS.strip + body { background-color: red; } + CSS + end + end + + context 'with highlighted colored UserRole records' do + before do + _highlighted_colored = Fabricate :user_role, highlighted: true, color: '#336699', id: '123_123_123' + _highlighted_no_color = Fabricate :user_role, highlighted: true, color: '' + _no_highlight_with_color = Fabricate :user_role, highlighted: false, color: '' + end + + it 'returns stylesheet from settings' do + get '/custom.css' + + expect(response.content_type).to include('text/css') + expect(response.body.strip).to eq(expected_css) + end + + def expected_css + <<~CSS.strip + .user-role-123123123 { + --user-role-accent: #336699; + } + CSS + end + end + end +end