-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow to configure notifications_sending_frequency (#183)
* feat: Allow to configure notifications_sending_frequency * lint: Fix rubocop offense * fix: Create registration spec
- Loading branch information
1 parent
c1bf603
commit 7687ef5
Showing
4 changed files
with
115 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,120 +3,124 @@ | |
require "spec_helper" | ||
|
||
module Decidim | ||
module Comments | ||
describe CreateRegistration do | ||
describe "call" do | ||
let(:organization) { create(:organization) } | ||
|
||
let(:name) { "Username" } | ||
let(:nickname) { "nickname" } | ||
let(:email) { "[email protected]" } | ||
let(:password) { "Y1fERVzL2F" } | ||
let(:password_confirmation) { password } | ||
let(:tos_agreement) { "1" } | ||
let(:newsletter) { "1" } | ||
let(:current_locale) { "fr" } | ||
|
||
let(:form_params) do | ||
{ | ||
"user" => { | ||
"name" => name, | ||
"nickname" => nickname, | ||
"email" => email, | ||
"password" => password, | ||
"password_confirmation" => password_confirmation, | ||
"tos_agreement" => tos_agreement, | ||
"newsletter_at" => newsletter | ||
} | ||
describe CreateRegistration do | ||
describe "call" do | ||
let(:organization) { create(:organization) } | ||
|
||
let(:name) { "Username" } | ||
let(:nickname) { "nickname" } | ||
let(:email) { "[email protected]" } | ||
let(:password) { "Y1fERVzL2F" } | ||
let(:password_confirmation) { password } | ||
let(:tos_agreement) { "1" } | ||
let(:newsletter) { "1" } | ||
let(:current_locale) { "fr" } | ||
let(:notifications_sending_frequency) { "none" } | ||
|
||
let(:form_params) do | ||
{ | ||
"user" => { | ||
"name" => name, | ||
"nickname" => nickname, | ||
"email" => email, | ||
"password" => password, | ||
"password_confirmation" => password_confirmation, | ||
"tos_agreement" => tos_agreement, | ||
"newsletter_at" => newsletter | ||
} | ||
} | ||
end | ||
let(:form) do | ||
RegistrationForm.from_params( | ||
form_params, | ||
current_locale: current_locale | ||
).with_context( | ||
current_organization: organization | ||
) | ||
end | ||
let(:command) { described_class.new(form) } | ||
|
||
describe "when the form is not valid" do | ||
before do | ||
allow(form).to receive(:invalid?).and_return(true) | ||
end | ||
let(:form) do | ||
RegistrationForm.from_params( | ||
form_params, | ||
current_locale: current_locale | ||
).with_context( | ||
current_organization: organization | ||
) | ||
|
||
it "broadcasts invalid" do | ||
expect { command.call }.to broadcast(:invalid) | ||
end | ||
let(:command) { described_class.new(form) } | ||
|
||
describe "when the form is not valid" do | ||
before do | ||
allow(form).to receive(:invalid?).and_return(true) | ||
end | ||
it "doesn't create a user" do | ||
expect do | ||
command.call | ||
end.not_to change(User, :count) | ||
end | ||
|
||
context "when the user was already invited" do | ||
let(:user) { build(:user, email: email, organization: organization) } | ||
|
||
it "broadcasts invalid" do | ||
expect { command.call }.to broadcast(:invalid) | ||
before do | ||
user.invite! | ||
clear_enqueued_jobs | ||
end | ||
|
||
it "doesn't create a user" do | ||
# rubocop:disable RSpec/ChangeByZero | ||
it "receives the invitation email again" do | ||
expect do | ||
command.call | ||
end.not_to change(User, :count) | ||
user.reload | ||
end.to change(User, :count).by(0) | ||
.and broadcast(:invalid) | ||
.and change(user.reload, :invitation_token) | ||
expect(ActionMailer::MailDeliveryJob).to have_been_enqueued.on_queue("mailers") | ||
end | ||
# rubocop:enable RSpec/ChangeByZero | ||
end | ||
end | ||
|
||
context "when the user was already invited" do | ||
let(:user) { build(:user, email: email, organization: organization) } | ||
|
||
before do | ||
user.invite! | ||
clear_enqueued_jobs | ||
end | ||
|
||
# rubocop:disable RSpec/ChangeByZero | ||
it "receives the invitation email again" do | ||
expect do | ||
command.call | ||
user.reload | ||
end.to change(User, :count).by(0) | ||
.and broadcast(:invalid) | ||
.and change(user.reload, :invitation_token) | ||
expect(ActionMailer::MailDeliveryJob).to have_been_enqueued.on_queue("mailers") | ||
end | ||
# rubocop:enable RSpec/ChangeByZero | ||
end | ||
describe "when the form is valid" do | ||
before do | ||
Decidim::ExtraUserFields.notifications_sending_frequency = "none" | ||
end | ||
|
||
describe "when the form is valid" do | ||
it "broadcasts ok" do | ||
expect { command.call }.to broadcast(:ok) | ||
end | ||
it "broadcasts ok" do | ||
expect { command.call }.to broadcast(:ok) | ||
end | ||
|
||
it "creates a new user" do | ||
expect(User).to receive(:create!).with( | ||
name: form.name, | ||
nickname: form.nickname, | ||
email: form.email, | ||
password: form.password, | ||
password_confirmation: form.password_confirmation, | ||
tos_agreement: form.tos_agreement, | ||
newsletter_notifications_at: form.newsletter_at, | ||
organization: organization, | ||
accepted_tos_version: organization.tos_version, | ||
locale: form.current_locale, | ||
password_updated_at: an_instance_of(ActiveSupport::TimeWithZone), | ||
extended_data: { | ||
country: nil, | ||
date_of_birth: nil, | ||
gender: nil, | ||
location: nil, | ||
phone_number: nil, | ||
postal_code: nil | ||
} | ||
).and_call_original | ||
|
||
expect { command.call }.to change(User, :count).by(1) | ||
end | ||
it "creates a new user" do | ||
expect(User).to receive(:create!).with( | ||
name: form.name, | ||
nickname: form.nickname, | ||
email: form.email, | ||
password: form.password, | ||
password_confirmation: form.password_confirmation, | ||
tos_agreement: form.tos_agreement, | ||
newsletter_notifications_at: form.newsletter_at, | ||
organization: organization, | ||
accepted_tos_version: organization.tos_version, | ||
locale: form.current_locale, | ||
password_updated_at: an_instance_of(ActiveSupport::TimeWithZone), | ||
notifications_sending_frequency: "none", | ||
extended_data: { | ||
country: nil, | ||
date_of_birth: nil, | ||
gender: nil, | ||
location: nil, | ||
phone_number: nil, | ||
postal_code: nil | ||
} | ||
).and_call_original | ||
|
||
expect { command.call }.to change(User, :count).by(1) | ||
end | ||
|
||
describe "when user keeps the newsletter unchecked" do | ||
let(:newsletter) { "0" } | ||
describe "when user keeps the newsletter unchecked" do | ||
let(:newsletter) { "0" } | ||
|
||
it "creates a user with no newsletter notifications" do | ||
expect do | ||
command.call | ||
expect(User.last.newsletter_notifications_at).to be_nil | ||
end.to change(User, :count).by(1) | ||
end | ||
it "creates a user with no newsletter notifications" do | ||
expect do | ||
command.call | ||
expect(User.last.newsletter_notifications_at).to be_nil | ||
end.to change(User, :count).by(1) | ||
end | ||
end | ||
end | ||
|