diff --git a/app/components/devise/omniauth_form_component.html.erb b/app/components/devise/omniauth_form_component.html.erb new file mode 100644 index 000000000..dfc62cc8b --- /dev/null +++ b/app/components/devise/omniauth_form_component.html.erb @@ -0,0 +1,20 @@ +
+
+
+ <%= t("omniauth.info.#{action}") %> +
+
+ + <% oauth_logins.each do |login| %> +
+ <%= link_to t("omniauth.#{login}.name"), send("user_#{login}_omniauth_authorize_path"), + title: t("omniauth.#{login}.#{action}"), + class: "button-#{login.to_s.delete_suffix("_oauth2")} button expanded", + method: :post %> +
+ <% end %> + +
+ <%= t("omniauth.or_fill") %> +
+
diff --git a/app/components/devise/omniauth_form_component.rb b/app/components/devise/omniauth_form_component.rb new file mode 100644 index 000000000..fbde17268 --- /dev/null +++ b/app/components/devise/omniauth_form_component.rb @@ -0,0 +1,22 @@ +class Devise::OmniauthFormComponent < ApplicationComponent + attr_reader :action + + def initialize(action) + @action = action + end + + def render? + oauth_logins.any? + end + + private + + def oauth_logins + [ + (:twitter if feature?(:twitter_login)), + (:facebook if feature?(:facebook_login)), + (:google_oauth2 if feature?(:google_login)), + (:wordpress_oauth2 if feature?(:wordpress_login)) + ].compact + end +end diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb index b5ac46af9..dc293a72d 100644 --- a/app/helpers/settings_helper.rb +++ b/app/helpers/settings_helper.rb @@ -1,13 +1,4 @@ module SettingsHelper - def oauth_logins - [ - (:twitter if feature?(:twitter_login)), - (:facebook if feature?(:facebook_login)), - (:google_oauth2 if feature?(:google_login)), - (:wordpress_oauth2 if feature?(:wordpress_login)) - ].compact - end - def feature?(name) setting["feature.#{name}"].presence || setting["process.#{name}"].presence || setting[name].presence end diff --git a/app/views/devise/_omniauth_form.html.erb b/app/views/devise/_omniauth_form.html.erb deleted file mode 100644 index 8cbe25844..000000000 --- a/app/views/devise/_omniauth_form.html.erb +++ /dev/null @@ -1,22 +0,0 @@ -<% if oauth_logins.any? %> -
-
-
- <%= t("omniauth.info.#{action}") %> -
-
- - <% oauth_logins.each do |login| %> -
- <%= link_to t("omniauth.#{login}.name"), send("user_#{login}_omniauth_authorize_path"), - title: t("omniauth.#{login}.#{action}"), - class: "button-#{login.to_s.delete_suffix("_oauth2")} button expanded", - method: :post %> -
- <% end %> - -
- <%= t("omniauth.or_fill") %> -
-
-<% end %> diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index bad3ce637..544d6c299 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -1,7 +1,7 @@ <% provide :title, t("devise_views.sessions.new.title") %>

<%= t("devise_views.sessions.new.title") %>

-<%= render "devise/omniauth_form", action: "sign_in" %> +<%= render Devise::OmniauthFormComponent.new("sign_in") %>

<%= sanitize(t("devise_views.shared.links.signup", diff --git a/app/views/users/registrations/new.html.erb b/app/views/users/registrations/new.html.erb index 1382b385d..145f33e3e 100644 --- a/app/views/users/registrations/new.html.erb +++ b/app/views/users/registrations/new.html.erb @@ -1,7 +1,7 @@ <% provide :title, t("devise_views.users.registrations.new.title") %>

<%= t("devise_views.users.registrations.new.title") %>

-<%= render "devise/omniauth_form", action: "sign_up" %> +<%= render Devise::OmniauthFormComponent.new("sign_up") %> <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= render "shared/errors", resource: resource %> diff --git a/spec/components/devise/omniauth_form_component_spec.rb b/spec/components/devise/omniauth_form_component_spec.rb new file mode 100644 index 000000000..9c9a6f814 --- /dev/null +++ b/spec/components/devise/omniauth_form_component_spec.rb @@ -0,0 +1,56 @@ +require "rails_helper" + +describe Devise::OmniauthFormComponent do + describe "#oauth_logins" do + let(:component) { Devise::OmniauthFormComponent.new("sign_up") } + + before do + Setting["feature.facebook_login"] = false + Setting["feature.twitter_login"] = false + Setting["feature.google_login"] = false + Setting["feature.wordpress_login"] = false + end + + it "is not rendered when all authentications are disabled" do + render_inline component + + expect(page).not_to be_rendered + end + + it "renders the twitter link when the feature is enabled" do + Setting["feature.twitter_login"] = true + + render_inline component + + expect(page).to have_link "Twitter" + expect(page).to have_link count: 1 + end + + it "renders the facebook link when the feature is enabled" do + Setting["feature.facebook_login"] = true + + render_inline component + + expect(page).to have_link "Facebook" + expect(page).to have_link count: 1 + end + + it "renders the google link when the feature is enabled" do + Setting["feature.google_login"] = true + + render_inline component + + expect(page).to have_link "Google" + expect(page).to have_link count: 1 + end + + it "renders the wordpress link when the feature is enabled" do + Setting["feature.wordpress_login"] = true + + render_inline component + + expect(page).to have_link "Wordpress" + expect(page).to have_link count: 1 + end + end +end diff --git a/spec/system/users_auth_spec.rb b/spec/system/users_auth_spec.rb index 60f3c8877..936b035cd 100644 --- a/spec/system/users_auth_spec.rb +++ b/spec/system/users_auth_spec.rb @@ -127,103 +127,6 @@ describe "Users" do end context "OAuth authentication" do - context "Form buttons" do - before do - Setting["feature.facebook_login"] = false - Setting["feature.twitter_login"] = false - Setting["feature.google_login"] = false - Setting["feature.wordpress_login"] = false - end - - scenario "No button will appear if all features are disabled" do - visit new_user_registration_path - - expect(page).not_to have_link "Twitter" - expect(page).not_to have_link "Facebook" - expect(page).not_to have_link "Google" - expect(page).not_to have_link "Wordpress" - - visit new_user_session_path - - expect(page).not_to have_link "Twitter" - expect(page).not_to have_link "Facebook" - expect(page).not_to have_link "Google" - expect(page).not_to have_link "Wordpress" - end - - scenario "Twitter login button will appear if feature is enabled" do - Setting["feature.twitter_login"] = true - - visit new_user_registration_path - - expect(page).to have_link "Twitter" - expect(page).not_to have_link "Facebook" - expect(page).not_to have_link "Google" - expect(page).not_to have_link "Wordpress" - - visit new_user_session_path - - expect(page).to have_link "Twitter" - expect(page).not_to have_link "Facebook" - expect(page).not_to have_link "Google" - expect(page).not_to have_link "Wordpress" - end - - scenario "Facebook login button will appear if feature is enabled" do - Setting["feature.facebook_login"] = true - - visit new_user_registration_path - - expect(page).not_to have_link "Twitter" - expect(page).to have_link "Facebook" - expect(page).not_to have_link "Google" - expect(page).not_to have_link "Wordpress" - - visit new_user_session_path - - expect(page).not_to have_link "Twitter" - expect(page).to have_link "Facebook" - expect(page).not_to have_link "Google" - expect(page).not_to have_link "Wordpress" - end - - scenario "Google login button will appear if feature is enabled" do - Setting["feature.google_login"] = true - - visit new_user_registration_path - - expect(page).not_to have_link "Twitter" - expect(page).not_to have_link "Facebook" - expect(page).to have_link "Google" - expect(page).not_to have_link "Wordpress" - - visit new_user_session_path - - expect(page).not_to have_link "Twitter" - expect(page).not_to have_link "Facebook" - expect(page).to have_link "Google" - expect(page).not_to have_link "Wordpress" - end - - scenario "Wordpress login button will appear if feature is enabled" do - Setting["feature.wordpress_login"] = true - - visit new_user_registration_path - - expect(page).not_to have_link "Twitter" - expect(page).not_to have_link "Facebook" - expect(page).not_to have_link "Google" - expect(page).to have_link "Wordpress" - - visit new_user_session_path - - expect(page).not_to have_link "Twitter" - expect(page).not_to have_link "Facebook" - expect(page).not_to have_link "Google" - expect(page).to have_link "Wordpress" - end - end - context "Twitter" do let(:twitter_hash) { { uid: "12345", info: { name: "manuela" }} } let(:twitter_hash_with_email) do