Skip to content

Commit 05137aa

Browse files
etewiahclaude
andcommitted
Re-enable authentication on editor controllers
Activate authenticate_admin_user! on EditorController, PagePartsController, and ThemeSettingsController. Update specs to use real Devise sign_in and add authentication tests for unauthenticated and non-admin users. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2c3634b commit 05137aa

File tree

5 files changed

+42
-26
lines changed

5 files changed

+42
-26
lines changed

app/controllers/pwb/editor/page_parts_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class Editor::PagePartsController < ApplicationController
77
skip_before_action :footer_content
88
# Skip CSRF for API calls (forms include their own token)
99
skip_before_action :verify_authenticity_token, only: [:update]
10-
# TODO: Re-enable authentication before production
11-
# before_action :authenticate_admin_user!
10+
before_action :authenticate_admin_user!
1211
before_action :set_page_part, only: [:show, :update]
1312

1413
# Handle record not found gracefully

app/controllers/pwb/editor/theme_settings_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class Editor::ThemeSettingsController < ApplicationController
77
skip_before_action :footer_content
88
# Skip CSRF for API calls
99
skip_before_action :verify_authenticity_token, only: [:update]
10-
# TODO: Re-enable authentication before production
11-
# before_action :authenticate_admin_user!
10+
before_action :authenticate_admin_user!
1211

1312
def show
1413
render json: {

app/controllers/pwb/editor_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ class EditorController < ApplicationController
66
# Skip theme path setup since editor has its own layout
77
skip_before_action :set_theme_path
88

9-
# TODO: Re-enable authentication before production
109
# Ensure only admins can access the editor
11-
# before_action :authenticate_admin_user!
10+
before_action :authenticate_admin_user!
1211

1312
def show
1413
# The path to load in the iframe (defaults to root)

spec/controllers/pwb/editor/theme_settings_controller_spec.rb

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
module Pwb
66
RSpec.describe Editor::ThemeSettingsController, type: :controller do
77
let!(:website) { FactoryBot.create(:pwb_website) }
8-
let(:admin_user) { FactoryBot.create(:pwb_user, :admin) }
8+
let(:admin_user) { FactoryBot.create(:pwb_user, :admin, website: website) }
9+
let(:regular_user) { FactoryBot.create(:pwb_user, website: website) }
910

1011
before do
1112
@request.env["devise.mapping"] = ::Devise.mappings[:user]
13+
sign_in admin_user, scope: :user
1214
end
1315

1416
describe "GET #show" do
@@ -47,29 +49,22 @@ module Pwb
4749

4850
json = response.parsed_body
4951
expect(json["status"]).to eq("success")
50-
# style_variables includes custom and palette colors; check custom was stored
5152
expect(json["style_variables"]).to include("primary_color")
52-
# Verify the raw stored value
5353
website.reload
5454
expect(website.style_variables_for_theme['default']['primary_color']).to eq("#ff0000")
5555
end
5656

5757
it "merges with existing style variables" do
58-
# Set initial values via the controller
59-
# Using body_style and theme which are NOT in the palette colors
6058
patch :update, params: { style_variables: { primary_color: "#111111", body_style: "siteLayout.boxed", theme: "dark" } }, format: :json
6159
expect(response).to have_http_status(:success)
6260

63-
# Update only primary_color
6461
patch :update, params: { style_variables: { primary_color: "#333333" } }, format: :json
6562

6663
response.parsed_body
6764
expect(response).to have_http_status(:success)
6865

69-
# Verify raw stored values
7066
website.reload
7167
expect(website.style_variables_for_theme['default']['primary_color']).to eq("#333333")
72-
# body_style and theme should be preserved (not in palette colors)
7368
expect(website.style_variables_for_theme['default']['body_style']).to eq("siteLayout.boxed")
7469
expect(website.style_variables_for_theme['default']['theme']).to eq("dark")
7570
end
@@ -81,5 +76,33 @@ module Pwb
8176
expect(json["message"]).to eq("Theme settings saved successfully")
8277
end
8378
end
79+
80+
describe "authentication" do
81+
context "when not signed in" do
82+
before { sign_out :user }
83+
84+
it "returns unauthorized for show" do
85+
get :show, format: :json
86+
expect(response).to have_http_status(:unauthorized)
87+
end
88+
89+
it "returns unauthorized for update" do
90+
patch :update, params: { style_variables: { primary_color: "#ff0000" } }, format: :json
91+
expect(response).to have_http_status(:unauthorized)
92+
end
93+
end
94+
95+
context "when signed in as non-admin" do
96+
before do
97+
sign_out :user
98+
sign_in regular_user, scope: :user
99+
end
100+
101+
it "returns unauthorized for show" do
102+
get :show, format: :json
103+
expect(response).to have_http_status(:unauthorized)
104+
end
105+
end
106+
end
84107
end
85108
end

spec/controllers/pwb/editor_controller_spec.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,36 @@
44

55
module Pwb
66
RSpec.describe EditorController, type: :controller do
7-
# routes { Pwb::Engine.routes }
8-
9-
let(:admin_user) { FactoryBot.create(:pwb_user, :admin) }
10-
let(:regular_user) { FactoryBot.create(:pwb_user) }
117
let!(:website) { FactoryBot.create(:pwb_website) }
8+
let(:admin_user) { FactoryBot.create(:pwb_user, :admin, website: website) }
9+
let(:regular_user) { FactoryBot.create(:pwb_user, website: website) }
1210

1311
before do
1412
@request.env["devise.mapping"] = ::Devise.mappings[:user]
1513
end
1614

1715
describe "GET #show" do
18-
# NOTE: Authentication is currently disabled for easier testing
19-
# These tests are skipped until authentication is re-enabled
2016
context "when user is not logged in" do
21-
it "allows access (auth temporarily disabled)" do
17+
it "redirects to root" do
2218
get :show
23-
expect(response).to have_http_status(:success)
19+
expect(response).to redirect_to(root_path)
2420
end
2521
end
2622

2723
context "when user is logged in but not admin" do
2824
before do
29-
allow(controller).to receive(:current_user).and_return(regular_user)
25+
sign_in regular_user, scope: :user
3026
end
3127

32-
it "allows access (auth temporarily disabled)" do
28+
it "redirects to root" do
3329
get :show
34-
expect(response).to have_http_status(:success)
30+
expect(response).to redirect_to(root_path)
3531
end
3632
end
3733

3834
context "when user is admin" do
3935
before do
40-
allow(controller).to receive(:current_user).and_return(admin_user)
36+
sign_in admin_user, scope: :user
4137
end
4238

4339
it "renders the show template" do

0 commit comments

Comments
 (0)