-
Notifications
You must be signed in to change notification settings - Fork 5
Add feature flag support via Flipper #663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dc12885
Add feature flag support via Flipper
mwtrew cdc75fb
Enable Flipper UI web interface
mwtrew 09bf0ac
Enable school-level flags for logged-in users
mwtrew 8ea7e68
Require admin authentication for Flipper UI access
mwtrew 8a6cf3d
Return only enabled flags
mwtrew a666dba
Fix Rubocop violations
mwtrew f36edfb
Fix Rubocop violations
mwtrew 9f0f082
Fix Rubocop violations
mwtrew 026e52d
Return JSON object from features endpoint
mwtrew 1d363eb
Move Flipper auth tests to new file
mwtrew 9d35472
Merge branch 'main' of github.com:RaspberryPiFoundation/editor-api in…
mwtrew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Api | ||
| class FeaturesController < ApiController | ||
| def index | ||
| school = current_user&.schools&.first | ||
|
|
||
| enabled_feature_keys = Flipper.features | ||
| .select { |feature| Flipper.enabled?(feature.key, school) } | ||
| .map(&:key) | ||
|
|
||
| render json: { enabled: enabled_feature_keys } | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| Rails.application.configure do | ||
| ## Memoization ensures that only one adapter call is made per feature per request. | ||
| ## For more info, see https://www.flippercloud.io/docs/optimization#memoization | ||
| # config.flipper.memoize = true | ||
|
|
||
| ## Flipper preloads all features before each request, which is recommended if: | ||
| ## * you have a limited number of features (< 100?) | ||
| ## * most of your requests depend on most of your features | ||
| ## * you have limited gate data combined across all features (< 1k enabled gates, like individual actors, across all features) | ||
| ## | ||
| ## For more info, see https://www.flippercloud.io/docs/optimization#preloading | ||
| # config.flipper.preload = true | ||
|
|
||
| ## Warn or raise an error if an unknown feature is checked | ||
| ## Can be set to `:warn`, `:raise`, or `false` | ||
| # config.flipper.strict = Rails.env.development? && :warn | ||
|
|
||
| ## Show Flipper checks in logs | ||
| # config.flipper.log = true | ||
|
|
||
| ## Reconfigure Flipper to use the Memory adapter and disable Cloud in tests | ||
| # config.flipper.test_help = true | ||
|
|
||
| ## The path that Flipper Cloud will use to sync features | ||
| # config.flipper.cloud_path = "_flipper" | ||
|
|
||
| ## The instrumenter that Flipper will use. Defaults to ActiveSupport::Notifications. | ||
| # config.flipper.instrumenter = ActiveSupport::Notifications | ||
| end | ||
|
|
||
| Flipper.configure do |config| | ||
| ## Configure other adapters that you want to use here: | ||
| ## See http://flippercloud.io/docs/adapters | ||
| # config.use Flipper::Adapters::ActiveSupportCacheStore, Rails.cache, expires_in: 5.minutes | ||
| end | ||
|
|
||
| ## Register a group that can be used for enabling features. | ||
| ## | ||
| ## Flipper.enable_group :my_feature, :admins | ||
| ## | ||
| ## See https://www.flippercloud.io/docs/features#enablement-group | ||
| # | ||
| # Flipper.register(:admins) do |actor| | ||
| # actor.respond_to?(:admin?) && actor.admin? | ||
| # end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| class CreateFlipperTables < ActiveRecord::Migration[7.2] | ||
| def up | ||
| create_table :flipper_features do |t| | ||
| t.string :key, null: false | ||
| t.timestamps null: false | ||
| end | ||
| add_index :flipper_features, :key, unique: true | ||
|
|
||
| create_table :flipper_gates do |t| | ||
| t.string :feature_key, null: false | ||
| t.string :key, null: false | ||
| t.text :value | ||
| t.timestamps null: false | ||
| end | ||
| add_index :flipper_gates, [:feature_key, :key, :value], unique: true, length: { value: 255 } | ||
| end | ||
|
|
||
| def down | ||
| drop_table :flipper_gates | ||
| drop_table :flipper_features | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class AdminSessionConstraint | ||
| def matches?(request) | ||
| current_user = request.session[:current_user] | ||
| return false unless current_user | ||
|
|
||
| User.new(current_user).admin? | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Flipper' do | ||
| let(:school) { create(:school) } | ||
| let(:student) { create(:student, school:) } | ||
| let(:teacher) { create(:teacher, school:) } | ||
| let(:admin_user) { create(:admin_user) } | ||
|
|
||
| describe 'Feature flag web interface' do | ||
| it 'is hidden from unauthenticated users' do | ||
| # Act | ||
| get '/admin/flipper/features' | ||
|
|
||
| # Assert | ||
| expect(response).to have_http_status(:not_found) | ||
| end | ||
|
|
||
| it 'is hidden from student users' do | ||
| # Arrange | ||
| sign_in student | ||
|
|
||
| # Act | ||
| get '/admin/flipper/features' | ||
|
|
||
| # Assert | ||
| expect(response).to have_http_status(:not_found) | ||
| end | ||
|
|
||
| it 'is hidden from teacher users' do | ||
| # Arrange | ||
| sign_in teacher | ||
|
|
||
| # Act | ||
| get '/admin/flipper/features' | ||
|
|
||
| # Assert | ||
| expect(response).to have_http_status(:not_found) | ||
| end | ||
|
|
||
| it 'is visible to admins' do | ||
| # Arrange | ||
| sign_in admin_user | ||
|
|
||
| # Act | ||
| get '/admin/flipper/features' | ||
|
|
||
| # Assert | ||
| expect(response).to have_http_status(:success) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe 'Features' do | ||
| let(:headers) { { Authorization: UserProfileMock::TOKEN } } | ||
| let(:school_class) { create(:school_class, teacher_ids: [teacher.id], school:) } | ||
| let(:school) { create(:school) } | ||
| let(:student) { create(:student, school:) } | ||
| let(:teacher) { create(:teacher, school:) } | ||
| let(:admin_user) { create(:admin_user) } | ||
|
|
||
| describe 'Feature flag API' do | ||
| it 'returns a globally-disabled feature as disabled' do | ||
| # Arrange | ||
| Flipper.disable :some_global_feature | ||
|
|
||
| # Act | ||
| get '/api/features' | ||
|
|
||
| # Assert | ||
| expect(response.body).not_to include('some_global_feature') | ||
| end | ||
|
|
||
| it 'returns a globally-enabled feature as enabled' do | ||
| # Arrange | ||
| Flipper.enable :some_global_feature | ||
|
|
||
| # Act | ||
| get '/api/features' | ||
|
|
||
| # Assert | ||
| expect(response.body).to include('some_global_feature') | ||
| end | ||
|
|
||
| it 'returns a school-level feature as disabled for logged-out user' do | ||
| # Arrange | ||
| Flipper.enable_actor :some_school_level_feature, school | ||
|
|
||
| # Act | ||
| get '/api/features' | ||
|
|
||
| # Assert | ||
| expect(response.body).not_to include('some_school_level_feature') | ||
| end | ||
|
|
||
| it 'returns a school-level feature as enabled for a student in that school' do | ||
| # Arrange | ||
| authenticated_in_hydra_as(student) | ||
|
|
||
| Flipper.enable_actor :some_school_level_feature, school | ||
|
|
||
| # Act | ||
| get '/api/features', headers: headers | ||
|
|
||
| # Assert | ||
| expect(response.body).to include('some_school_level_feature') | ||
| end | ||
|
|
||
| it 'returns both school-level and global features as enabled for a student in a school' do | ||
| # Arrange | ||
| authenticated_in_hydra_as(student) | ||
|
|
||
| Flipper.enable_actor :some_school_level_feature, school | ||
| Flipper.enable :some_global_feature | ||
|
|
||
| # Act | ||
| get '/api/features', headers: headers | ||
|
|
||
| # Assert | ||
| expect(response.body).to include('some_school_level_feature') | ||
| expect(response.body).to include('some_global_feature') | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.