Move omniauth form partial to a component
This way we simplify the view a little bit and replace some slow system tests with faster component tests.
This commit is contained in:
20
app/components/devise/omniauth_form_component.html.erb
Normal file
20
app/components/devise/omniauth_form_component.html.erb
Normal file
@@ -0,0 +1,20 @@
|
||||
<div class="row">
|
||||
<div class="small-12 column">
|
||||
<div class="margin-bottom">
|
||||
<strong><%= t("omniauth.info.#{action}") %></strong>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<% oauth_logins.each do |login| %>
|
||||
<div class="small-12 medium-6 large-4 column end">
|
||||
<%= 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 %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="small-12 column auth-divider">
|
||||
<span><%= t("omniauth.or_fill") %></span>
|
||||
</div>
|
||||
</div>
|
||||
22
app/components/devise/omniauth_form_component.rb
Normal file
22
app/components/devise/omniauth_form_component.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
<% if oauth_logins.any? %>
|
||||
<div class="row">
|
||||
<div class="small-12 column">
|
||||
<div class="margin-bottom">
|
||||
<strong><%= t("omniauth.info.#{action}") %></strong>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<% oauth_logins.each do |login| %>
|
||||
<div class="small-12 medium-6 large-4 column end">
|
||||
<%= 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 %>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="small-12 column auth-divider">
|
||||
<span><%= t("omniauth.or_fill") %></span>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
@@ -1,7 +1,7 @@
|
||||
<% provide :title, t("devise_views.sessions.new.title") %>
|
||||
<h2><%= t("devise_views.sessions.new.title") %></h2>
|
||||
|
||||
<%= render "devise/omniauth_form", action: "sign_in" %>
|
||||
<%= render Devise::OmniauthFormComponent.new("sign_in") %>
|
||||
|
||||
<p>
|
||||
<%= sanitize(t("devise_views.shared.links.signup",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<% provide :title, t("devise_views.users.registrations.new.title") %>
|
||||
<h2><%= t("devise_views.users.registrations.new.title") %></h2>
|
||||
|
||||
<%= 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 %>
|
||||
|
||||
56
spec/components/devise/omniauth_form_component_spec.rb
Normal file
56
spec/components/devise/omniauth_form_component_spec.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user