Skip to content

Commit 208bebf

Browse files
committed
Add feature gate for example posts
System tests need additional css scoping due to ambiguous match of "form" on posts page; when user is logged in, a sign out button is also rendered within a form element.
1 parent 5652d31 commit 208bebf

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

app/controllers/examples/posts_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Examples::PostsController < ApplicationController
2+
before_action :feature_enabled!
3+
24
def index
35
render Examples::Posts::IndexView.new(posts: Examples::Post.limit(25).order(created_at: :desc))
46
end
@@ -26,4 +28,11 @@ def post_params = params.fetch(:examples_post, {})
2628
def post_params_permitted
2729
post_params.permit(:title, :postable_type, link_attributes: [:url], image_attributes: [:url], markdown_attributes: [:body])
2830
end
31+
32+
def feature_enabled!
33+
return if user_signed_in? &&
34+
Flipper.enabled?(:example_posts, current_user)
35+
36+
raise ActionController::RoutingError.new("Not Found")
37+
end
2938
end

spec/requests/examples/posts_spec.rb

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,39 @@
22

33
RSpec.describe "Examples::Posts", type: :request do
44
describe "GET /index" do
5-
it "is successful" do
6-
get examples_posts_path
7-
expect(response).to have_http_status(:success)
5+
context "feature enabled" do
6+
before do
7+
user = login_user
8+
Flipper.enable(:example_posts, user)
9+
end
10+
11+
it "is successful" do
12+
get examples_posts_path
13+
expect(response).to have_http_status(:success)
14+
end
15+
end
16+
17+
context "user not authenticated" do
18+
before do
19+
Flipper.enable(:example_posts)
20+
end
21+
22+
it "is not found" do
23+
get examples_posts_path
24+
expect(response).to have_http_status(:not_found)
25+
end
26+
end
27+
28+
context "feature disabled" do
29+
before do
30+
login_user
31+
Flipper.disable(:example_posts)
32+
end
33+
34+
it "is not found" do
35+
get examples_posts_path
36+
expect(response).to have_http_status(:not_found)
37+
end
838
end
939
end
1040
end

spec/support/warden_helpers.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module WardenHelpers
22
def login_admin_user(admin_user = FactoryBot.create(:admin_user))
33
login_as(admin_user, scope: :admin_user)
4+
admin_user
45
end
56

67
def login_user(user = FactoryBot.create(:user))
78
login_as(user, scope: :user)
9+
user
810
end
911
end
1012

spec/system/examples/posts_spec.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
require "rails_helper"
22

33
RSpec.describe "Examples: Posts", type: :system do
4+
before do
5+
user = login_user
6+
Flipper.enable(:example_posts, user)
7+
end
8+
49
it "renders the form inputs dynamically and retains previously entered values" do
510
visit examples_posts_path
611

712
click_link "New Post"
813

9-
within "form" do
14+
within "main form" do
1015
expect(page).to have_content("Title")
1116
expect(page).to have_content("Text (Markdown)")
1217

@@ -18,7 +23,7 @@
1823

1924
choose "Link"
2025

21-
within "form" do
26+
within "main form" do
2227
expect(page).to have_content("Title")
2328
expect(page).to have_content("Link URL")
2429

@@ -29,7 +34,7 @@
2934

3035
choose "Image"
3136

32-
within "form" do
37+
within "main form" do
3338
expect(page).to have_content("Title")
3439
expect(page).to have_content("Image URL")
3540

@@ -40,7 +45,7 @@
4045

4146
choose "Text"
4247

43-
within "form" do
48+
within "main form" do
4449
expect(page).to have_content("Title")
4550
expect(page).to have_content("Markdown")
4651

@@ -52,14 +57,14 @@
5257

5358
choose "Link"
5459

55-
within "form" do
60+
within "main form" do
5661
expect(page).to have_field("Title", with: "My Post 🎸")
5762
expect(page).to have_field("Link URL", with: "https://example.com/link")
5863
end
5964

6065
choose "Image"
6166

62-
within "form" do
67+
within "main form" do
6368
expect(page).to have_field("Title", with: "My Post 🎸")
6469
expect(page).to have_field("Image URL", with: "https://example.com/image.jpg")
6570
end
@@ -70,7 +75,7 @@
7075

7176
click_link "New Post"
7277

73-
within "form" do
78+
within "main form" do
7479
fill_in "Title", with: "My Post 🎸"
7580
fill_in "Text (Markdown)", with: "# This is a post about guitars 🎸"
7681

@@ -88,7 +93,7 @@
8893

8994
choose "Link"
9095

91-
within "form" do
96+
within "main form" do
9297
fill_in "Title", with: "My Link 🎸"
9398
fill_in "Link URL", with: "https://example.com/link"
9499

@@ -106,7 +111,7 @@
106111

107112
choose "Image"
108113

109-
within "form" do
114+
within "main form" do
110115
fill_in "Title", with: "My Image 🎸"
111116
fill_in "Image URL", with: "https://example.com/image.jpg"
112117

0 commit comments

Comments
 (0)