Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add capability to pass an icon setting into auth provider registration (#34584)

Chris Alberti1f227a5eb4ef
diff --git a/app/assets/javascripts/discourse/app/models/login-method.js b/app/assets/javascripts/discourse/app/models/login-method.js
index 0e2c6c0769c..75da429d503 100644
--- a/app/assets/javascripts/discourse/app/models/login-method.js
+++ b/app/assets/javascripts/discourse/app/models/login-method.js
@@ -41,6 +41,11 @@ export default class LoginMethod extends EmberObject {
return this.title_override || i18n(`login.${this.name}.title`);
}
+ @discourseComputed
+ icon() {
+ return this.icon_override || "user";
+ }
+
@discourseComputed
screenReaderTitle() {
return (
diff --git a/app/serializers/auth_provider_serializer.rb b/app/serializers/auth_provider_serializer.rb
index 11e2d4819db..6026408d8f0 100644
--- a/app/serializers/auth_provider_serializer.rb
+++ b/app/serializers/auth_provider_serializer.rb
@@ -6,7 +6,7 @@ class AuthProviderSerializer < ApplicationSerializer
:custom_url,
:frame_height,
:frame_width,
- :icon,
+ :icon_override,
:name,
:pretty_name_override,
:provider_url,
@@ -24,4 +24,8 @@ class AuthProviderSerializer < ApplicationSerializer
def title_override
object.title_setting ? SiteSetting.get(object.title_setting) : object.title
end
+
+ def icon_override
+ object.icon_setting ? SiteSetting.get(object.icon_setting) : object.icon
+ end
end
diff --git a/lib/auth/auth_provider.rb b/lib/auth/auth_provider.rb
index e44a3e6f8c5..a9573cd5fc9 100644
--- a/lib/auth/auth_provider.rb
+++ b/lib/auth/auth_provider.rb
@@ -14,6 +14,7 @@ class Auth::AuthProvider
frame_height
frame_width
icon
+ icon_setting
pretty_name
pretty_name_setting
title
diff --git a/spec/serializers/auth_provider_serializer_spec.rb b/spec/serializers/auth_provider_serializer_spec.rb
new file mode 100644
index 00000000000..a3ca417e002
--- /dev/null
+++ b/spec/serializers/auth_provider_serializer_spec.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+RSpec.describe AuthProviderSerializer do
+ fab!(:user)
+
+ before do
+ allow(SiteSetting).to receive(:get).and_call_original
+ allow(SiteSetting).to receive(:get).with(:test_icon).and_return("bullseye")
+ allow(SiteSetting).to receive(:get).with(:test_pretty_name).and_return("new pretty name")
+ allow(SiteSetting).to receive(:get).with(:test_title).and_return("new_title")
+ end
+
+ let(:authenticator) do
+ Class
+ .new(Auth::ManagedAuthenticator) do
+ def name
+ "test_auth"
+ end
+
+ def enabled?
+ true
+ end
+ end
+ .new
+ end
+
+ let(:serializer) do
+ AuthProviderSerializer.new(auth_provider, scope: Guardian.new(user), root: false)
+ end
+
+ context "without overridden attributes" do
+ let(:auth_provider) do
+ Auth::AuthProvider.new(
+ authenticator:,
+ icon: "flash",
+ pretty_name: "old pretty name",
+ title: "old_title",
+ )
+ end
+
+ it "returns the original values" do
+ json = serializer.as_json
+ expect(json[:pretty_name_override]).to eq("old pretty name")
+ expect(json[:title_override]).to eq("old_title")
+ expect(json[:icon_override]).to eq("flash")
+ end
+ end
+
+ context "with overridden attributes" do
+ let(:auth_provider) do
+ Auth::AuthProvider.new(
+ authenticator:,
+ icon: "flash",
+ icon_setting: :test_icon,
+ pretty_name: "old pretty name",
+ pretty_name_setting: :test_pretty_name,
+ title: "old_title",
+ title_setting: :test_title,
+ )
+ end
+
+ it "returns the overridden values" do
+ json = serializer.as_json
+ expect(json[:pretty_name_override]).to eq("new pretty name")
+ expect(json[:title_override]).to eq("new_title")
+ expect(json[:icon_override]).to eq("bullseye")
+ end
+ end
+end
diff --git a/spec/system/user_page/user_preferences_account_spec.rb b/spec/system/user_page/user_preferences_account_spec.rb
index 1130354146b..5485028c693 100644
--- a/spec/system/user_page/user_preferences_account_spec.rb
+++ b/spec/system/user_page/user_preferences_account_spec.rb
@@ -44,6 +44,57 @@ describe "User preferences | Account", type: :system do
end
end
+ describe "overridden provider attributes" do
+ before do
+ authenticator =
+ Class
+ .new(Auth::ManagedAuthenticator) do
+ def name
+ "test_auth"
+ end
+
+ def enabled?
+ true
+ end
+ end
+ .new
+
+ provider =
+ Auth::AuthProvider.new(
+ authenticator:,
+ icon: "flash",
+ icon_setting: :test_icon,
+ pretty_name: "old pretty name",
+ pretty_name_setting: :test_pretty_name,
+ title: "old_title",
+ title_setting: :test_title,
+ )
+ DiscoursePluginRegistry.register_auth_provider(provider)
+
+ allow(SiteSetting).to receive(:get).and_call_original
+ allow(SiteSetting).to receive(:get).with(:test_icon).and_return("bullseye")
+ allow(SiteSetting).to receive(:get).with(:test_pretty_name).and_return("new pretty name")
+ allow(SiteSetting).to receive(:get).with(:test_title).and_return("new_title")
+ end
+
+ after { DiscoursePluginRegistry.reset! }
+
+ it "displays the correct name when overridden" do
+ user_account_preferences_page.visit(user)
+ name = find(".pref-associated-accounts table tr.test-auth .associated-account__name")
+ expect(name).not_to have_text("old pretty name")
+ expect(name).to have_text("new pretty name")
+ end
+
+ it "displays the correct icon when overridden" do
+ user_account_preferences_page.visit(user)
+ icon_classes =
+ find(".pref-associated-accounts table tr.test-auth .associated-account__icon svg")[:class]
+ expect(icon_classes).not_to have_content("d-icon-flash")
+ expect(icon_classes).to have_content("d-icon-bullseye")
+ end
+ end
+
describe "external login provider URLs" do
it "shows provider URLs as links when available" do
SiteSetting.enable_discord_logins = true