fix registration controller spec

This commit is contained in:
Jyrki Gadinger 2024-08-09 10:57:08 +02:00 committed by Andreas Nedbal
parent 4e5b0a8562
commit 5cee6e3e3c
1 changed files with 26 additions and 15 deletions

View File

@ -24,7 +24,6 @@ describe User::RegistrationsController, type: :controller do
before do before do
allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(true) allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(true)
allow(APP_CONFIG).to receive(:dig).with(:features, :registration, :enabled).and_return(true) allow(APP_CONFIG).to receive(:dig).with(:features, :registration, :enabled).and_return(true)
allow(controller).to receive(:verify_hcaptcha).and_return(captcha_successful)
end end
let :registration_params do let :registration_params do
@ -41,7 +40,10 @@ describe User::RegistrationsController, type: :controller do
subject { post :create, params: registration_params } subject { post :create, params: registration_params }
context "when captcha is invalid" do context "when captcha is invalid" do
let(:captcha_successful) { false } before do
allow(controller).to receive(:verify_hcaptcha).and_return(false)
end
it "doesn't allow a registration with an invalid captcha" do it "doesn't allow a registration with an invalid captcha" do
expect { subject }.not_to(change { User.count }) expect { subject }.not_to(change { User.count })
expect(response).to redirect_to :new_user_registration expect(response).to redirect_to :new_user_registration
@ -49,12 +51,33 @@ describe User::RegistrationsController, type: :controller do
end end
context "when captcha is valid" do context "when captcha is valid" do
let(:captcha_successful) { true } before do
allow(controller).to receive(:verify_hcaptcha).and_return(true)
end
it "creates a user" do it "creates a user" do
allow(controller).to receive(:verify_hcaptcha).and_return(true) allow(controller).to receive(:verify_hcaptcha).and_return(true)
expect { subject }.to change { User.count }.by(1) expect { subject }.to change { User.count }.by(1)
end end
end end
context "when registrations are disabled" do
before do
allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(false)
allow(APP_CONFIG).to receive(:dig).with(:features, :registration, :enabled).and_return(false)
end
it "redirects to the root page" do
allow(controller).to receive(:verify_hcaptcha).and_return(true)
subject
expect(response).to redirect_to(root_path)
end
it "does not create a user" do
allow(controller).to receive(:verify_hcaptcha).and_return(true)
expect { subject }.not_to(change { User.count })
end
end
end end
context "invalid user sign up" do context "invalid user sign up" do
@ -116,18 +139,6 @@ describe User::RegistrationsController, type: :controller do
end end
end end
end end
context "when registrations are disabled" do
before do
allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(false)
allow(APP_CONFIG).to receive(:dig).with(:features, :registration, :enabled).and_return(false)
end
it "redirects to the root page" do
subject
expect(response).to redirect_to(root_path)
end
end
end end
describe "#new" do describe "#new" do