Skip to content

Commit

Permalink
Merge pull request #2475 from alphagov/only-show-applications-on-dash…
Browse files Browse the repository at this point in the history
…board-if-they-have-user-facing-home-page

Only show apps on dashboard if they have a home page
  • Loading branch information
floehopper authored Oct 30, 2023
2 parents 11dc9f8 + 475e6ca commit acae02f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/root_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class RootController < ApplicationController
skip_after_action :verify_authorized

def index
applications = Doorkeeper::Application.not_api_only.can_signin(current_user)
applications = Doorkeeper::Application.not_api_only.with_home_uri.can_signin(current_user)

@applications_and_permissions = zip_permissions(applications, current_user)
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/doorkeeper/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Doorkeeper::Application < ActiveRecord::Base # rubocop:disable Rails/Appli
scope :not_retired, -> { where(retired: false) }
scope :api_only, -> { where(api_only: true) }
scope :not_api_only, -> { where(api_only: false) }
scope :with_home_uri, -> { where.not(home_uri: nil).where.not(home_uri: "") }
scope :can_signin, ->(user) { with_signin_permission_for(user) }
scope :with_signin_delegatable,
lambda {
Expand Down Expand Up @@ -77,6 +78,8 @@ def home_uri
private

def substituted_uri(uri)
return if uri.blank?

uri_pattern = Rails.configuration.oauth_apps_uri_sub_pattern
uri_sub = Rails.configuration.oauth_apps_uri_sub_replacement

Expand Down
2 changes: 1 addition & 1 deletion app/views/doorkeeper_applications/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
name: "doorkeeper_application[home_uri]",
type: "text",
hint: "Used to link to the app on the dashboard",
hint: "Used to link to the app on the dashboard. Leave blank if app has no user-facing home page.",
value: @application.home_uri,
autocomplete: "off"
} %>
Expand Down
10 changes: 10 additions & 0 deletions test/controllers/root_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def setup
assert_select "h3", count: 1
end

test "do not display application with no home URI on dashboard even if you have signin permission" do
application = create(:application, name: "App without home page", home_uri: nil)
user = create(:user, with_signin_permissions_for: [application])
sign_in user

get :index

assert_select "h3", text: "App without home page", count: 0
end

test "do not display retired application on dashboard even if you have signin permission" do
application = create(:application, name: "Retired app", retired: true)
user = create(:user, with_signin_permissions_for: [application])
Expand Down
50 changes: 39 additions & 11 deletions test/models/doorkeeper/application_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,29 @@ class Doorkeeper::ApplicationTest < ActiveSupport::TestCase
assert_equal "https://app.com/", application.home_uri
end

should "return application substituted home uri if match" do
Rails.application.config.stubs(oauth_apps_uri_sub_pattern: "replace.me")
Rails.application.config.stubs(oauth_apps_uri_sub_replacement: "new.domain")
context "when URI substitution is enabled" do
setup do
Rails.application.config.stubs(oauth_apps_uri_sub_pattern: "replace.me")
Rails.application.config.stubs(oauth_apps_uri_sub_replacement: "new.domain")
end

application = create(:application, home_uri: "https://app.replace.me/")
should "return nil if application home uri is nil" do
application = create(:application, home_uri: nil)

assert_equal "https://app.new.domain/", application.home_uri
end
assert_nil application.home_uri
end

should "return application original home uri if not matched" do
Rails.application.config.stubs(oauth_apps_uri_sub_pattern: "replace.me")
Rails.application.config.stubs(oauth_apps_uri_sub_replacement: "new.domain")
should "return application substituted home uri if match" do
application = create(:application, home_uri: "https://app.replace.me/")

application = create(:application, home_uri: "https://app.keep.me/")
assert_equal "https://app.new.domain/", application.home_uri
end

assert_equal "https://app.keep.me/", application.home_uri
should "return application original home uri if not matched" do
application = create(:application, home_uri: "https://app.keep.me/")

assert_equal "https://app.keep.me/", application.home_uri
end
end
end

Expand Down Expand Up @@ -224,6 +231,27 @@ class Doorkeeper::ApplicationTest < ActiveSupport::TestCase
end
end

context ".with_home_uri" do
setup do
@app = create(:application)
end

should "include apps that have a home URI" do
@app.update!(home_uri: "http://gov.uk")
assert_equal [@app], Doorkeeper::Application.with_home_uri
end

should "exclude apps that has a nil home URI" do
@app.update!(home_uri: nil)
assert_equal [], Doorkeeper::Application.with_home_uri
end

should "exclude apps that has a blank home URI" do
@app.update!(home_uri: "")
assert_equal [], Doorkeeper::Application.with_home_uri
end
end

context ".can_signin" do
should "return applications that the user can signin into" do
user = create(:user)
Expand Down

0 comments on commit acae02f

Please sign in to comment.