Skip to content

Commit

Permalink
feat: Add Friendly Signup (#78)
Browse files Browse the repository at this point in the history
* feat: Add friendly signup

* feat: Add configurable options to the config

* fix: Fix some tests and add new ones

* fix: Missing text modification

* chore: Merge Extra user fields

---------

Co-authored-by: quentinchampenois <[email protected]>
  • Loading branch information
2 people authored and moustachu committed Sep 5, 2024
1 parent 79be495 commit 951c22c
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 114 deletions.
4 changes: 4 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ BACKUP_S3SYNC_ACCESS_KEY=
BACKUP_S3SYNC_SECRET_KEY=
BACKUP_S3SYNC_BUCKET=
BACKUP_S3RETENTION_ENABLED=
FRIENDLY_SIGNUP_OVERRIDE_PASSWORDS=1
FRIENDLY_SIGNUP_INSTANT_VALIDATION=1
FRIENDLY_SIGNUP_HIDE_NICKNAME=1
FRIENDLY_SIGNUP_USE_CONFIRMATION_CODES=1

# timeout session (in minutes)
DECIDIM_SESSION_TIMEOUT=
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gem "decidim-cache_cleaner"
gem "decidim-custom_proposal_states", git: "https://github.com/alecslupu-pfa/decidim-module-custom_proposal_states", branch: DECIDIM_BRANCH
gem "decidim-decidim_awesome", "~> 0.9.1"
gem "decidim-extra_user_fields", git: "https://github.com/OpenSourcePolitics/decidim-module-extra_user_fields.git", branch: DECIDIM_BRANCH
gem "decidim-friendly_signup", git: "https://github.com/OpenSourcePolitics/decidim-module-friendly_signup.git"
gem "decidim-homepage_interactive_map", git: "https://github.com/OpenSourcePolitics/decidim-module-homepage_interactive_map.git", branch: DECIDIM_BRANCH
gem "decidim-slider", git: "https://github.com/OpenSourcePolitics/decidim-module-slider", branch: "rc/0.27"
gem "decidim-spam_detection", "4.0.0"
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ GIT
decidim-core (>= 0.27.0, < 0.28)
deface (~> 1.5)

GIT
remote: https://github.com/OpenSourcePolitics/decidim-module-friendly_signup.git
revision: 9bc2c4928185b9e775b3bad3921f9ae00b49acca
specs:
decidim-friendly_signup (0.4.5)
decidim-core (~> 0.27)

GIT
remote: https://github.com/OpenSourcePolitics/decidim-module-homepage_interactive_map.git
revision: dd685166fdf953a11bd6a9e0dac56feca3bd0708
Expand Down Expand Up @@ -981,6 +988,7 @@ DEPENDENCIES
decidim-decidim_awesome (~> 0.9.1)
decidim-dev (~> 0.27.0)
decidim-extra_user_fields!
decidim-friendly_signup!
decidim-homepage_interactive_map!
decidim-slider!
decidim-spam_detection (= 4.0.0)
Expand Down
26 changes: 11 additions & 15 deletions app/views/decidim/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,31 @@
<div class="row">
<div class="columns large-6 medium-10 medium-centered">

<%= decidim_form_for(@form, namespace: "registration", as: resource_name, url: registration_path(resource_name), html: { class: "register-form new_user", id: "register-form" }) do |f| %>
<%= decidim_form_for(@form, namespace: "registration", as: resource_name, url: registration_path(resource_name), html: { class: "register-form new_user#{friendly_override_activated?(:use_instant_validation) ? ' instant-validation' : ''}", id: "register-form" }, data: { "validation-url" => decidim_friendly_signup.validate_path }) do |f| %>
<%= invisible_captcha %>
<div class="card">
<div class="card__content">
<%= form_required_explanation %>

<div class="user-person">
<div class="field">
<%= f.text_field :name, help_text: t(".username_help") %>
<%= f.text_field :name, help_text: t(".username_help"), autocomplete: "name", data: { "instant-attribute" => "name", "instant-recheck" => "#registration_user_password" } %>
</div>
</div>

<div class="user-nickname">
<div class="field">
<%= f.text_field :nickname, help_text: t(".nickname_help", organization: current_organization.name), prefix: { value: "@", small: 1, large: 1 } %>
<% unless friendly_override_activated?(:hide_nickname) %>
<div class="user-nickname">
<div class="field">
<%= f.text_field :nickname, help_text: t(".nickname_help", organization: current_organization.name), prefix: { value: "@", small: 1, large: 1 }, autocomplete: "nickname", data: { "instant-attribute" => "nickname", "instant-recheck" => "#registration_user_password" } %>
</div>
</div>
</div>
<% end %>

<div class="field">
<%= f.email_field :email %>
<%= f.email_field :email, autocomplete: "email", data: { "instant-attribute" => "email", "instant-recheck" => "#registration_user_password" } %>
</div>

<div class="field">
<%= f.password_field :password, help_text: t(".password_help", minimun_characters: ::PasswordValidator::MINIMUM_LENGTH), autocomplete: "off" %>
</div>

<div class="field">
<%= f.password_field :password_confirmation %>
</div>
<%= render("decidim/friendly_signup/shared/password_fields", form: f, options: password_field_options_for(:user).merge(data: { "instant-attribute" => "password" } )) %>
</div>
</div>

Expand Down Expand Up @@ -94,4 +90,4 @@
</div>
</div>
</div>
<%= render "decidim/devise/shared/newsletter_modal" %>
<%= render "decidim/devise/shared/newsletter_modal" %>
17 changes: 17 additions & 0 deletions config/initializers/friendly_signup.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

return unless defined?(Decidim::FriendlySignup)

Decidim::FriendlySignup.configure do |config|
# Override password views or leave the originals (default is true):
config.override_passwords = ENV.fetch("FRIENDLY_SIGNUP_OVERRIDE_PASSWORDS", "1") == "1"

# Automatically validate user inputs in the register form (default is true):
config.use_instant_validation = ENV.fetch("FRIENDLY_SIGNUP_INSTANT_VALIDATION", "1") == "1"

# Hide nickname field and create one automatically from user's name or email (default is true)
config.hide_nickname = ENV.fetch("FRIENDLY_SIGNUP_HIDE_NICKNAME", "1") == "1"

# Send the users a 4-digit number that needs to be entered in a confirmation page instead of a confirmation link (default is true)
config.use_confirmation_codes = ENV.fetch("FRIENDLY_SIGNUP_USE_CONFIRMATION_CODES", "1") == "1"
end
1 change: 0 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ en:
newsletter: Newsletter
newsletter_title: Newsletter title
nickname_help: Nickname help
password_help: Password help
sign_in: Sign in
sign_up: Sign up
subtitle: Subtitle
Expand Down
1 change: 0 additions & 1 deletion config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ fr:
newsletter: Pour recevoir tout l'actualité de la démocratie locale de la ville de Lyon, cliquez ici.
newsletter_title: Actualités
nickname_help: Cet identifiant est public et unique sur la plateforme "%{organization}" ; il permet aux autres utilisateurs de vous suivre, de vous contacter ou de vous mentionner dans leurs commentaires. Il s’affichera précédé d’un « @ » ; il ne doit pas contenir d’espace ni de caractère spécial.
password_help: "%{minimun_characters} caractères minimum, ne doit pas être trop commun (par exemple 123456) et doit être différent de votre pseudo et de votre email."
sign_in: Se connecter
sign_up: Créer un compte
subtitle: Inscrivez-vous pour participer aux discussions et pour exprimer votre soutien aux propositions de votre choix.
Expand Down
64 changes: 39 additions & 25 deletions spec/forms/registration_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ module Decidim
let(:attributes) do
{
name: name,
nickname: nickname,
email: email,
password: password,
password_confirmation: password_confirmation,
Expand Down Expand Up @@ -53,12 +52,6 @@ module Decidim
it { is_expected.to be_invalid }
end

context "when the nickname is not present" do
let(:nickname) { nil }

it { is_expected.to be_invalid }
end

context "when the email is not present" do
let(:email) { nil }

Expand All @@ -77,24 +70,6 @@ module Decidim
end
end

context "when the nickname already exists" do
let!(:user) { create(:user, organization: organization, nickname: nickname) }

it { is_expected.to be_invalid }

context "and is pending to accept the invitation" do
let!(:user) { create(:user, organization: organization, nickname: nickname, invitation_token: "foo", invitation_accepted_at: nil) }

it { is_expected.to be_valid }
end
end

context "when the nickname is too long" do
let(:nickname) { "verylongnicknamethatcreatesanerror" }

it { is_expected.to be_invalid }
end

context "when the password is not present" do
let(:password) { nil }

Expand Down Expand Up @@ -124,5 +99,44 @@ module Decidim

it { is_expected.to be_invalid }
end

context "when hide_nickname is active" do
it { is_expected.to be_valid }

it "generates a nickname" do
expect(subject.nickname).to eq("user")
end
end

context "when hide_nickname is inactive" do
before do
allow(Decidim::FriendlySignup).to receive(:hide_nickname).and_return(false)
end

it { is_expected.to be_invalid }

it "does not generate a nickname" do
expect(subject.nickname).to be_blank
end

context "when everything is OK" do
let(:attributes) do
{
name: name,
nickname: nickname,
email: email,
password: password,
password_confirmation: password_confirmation,
tos_agreement: tos_agreement
}
end

it { is_expected.to be_valid }

it "uses params nickname" do
expect(subject.nickname).to eq("justme")
end
end
end
end
end
22 changes: 0 additions & 22 deletions spec/system/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
visit decidim.account_path
end

it_behaves_like "accessible page"

describe "update avatar" do
it "can update avatar" do
dynamically_attach_file(:user_avatar, Decidim::Dev.asset("avatar.jpg"), remove_before: true)
Expand Down Expand Up @@ -101,7 +99,6 @@
page.find(".change-password").click

fill_in :user_password, with: "sekritpass123"
fill_in :user_password_confirmation, with: "sekritpass123"

find("*[type=submit]").click
end
Expand All @@ -113,25 +110,6 @@
expect(user.reload.valid_password?("sekritpass123")).to be(true)
end
end

context "when passwords don't match" do
it "doesn't update the password" do
within "form.edit_user" do
page.find(".change-password").click

fill_in :user_password, with: "sekritpass123"
fill_in :user_password_confirmation, with: "oopseytypo"

find("*[type=submit]").click
end

within_flash_messages do
expect(page).to have_content("There was a problem")
end

expect(user.reload.valid_password?("sekritpass123")).to be(false)
end
end
end

context "when updating the email" do
Expand Down
Loading

0 comments on commit 951c22c

Please sign in to comment.