Mock verify_captcha

This commit is contained in:
Dominik M. Kwiatek 2020-05-27 21:47:27 +01:00
parent 5650b2eb0b
commit e512d5502d

View file

@ -5,6 +5,9 @@ require "rails_helper"
describe User::RegistrationsController, type: :controller do describe User::RegistrationsController, type: :controller do
before do before do
@request.env["devise.mapping"] = Devise.mappings[:user] @request.env["devise.mapping"] = Devise.mappings[:user]
allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(true)
allow(controller).to receive(:verify_hcaptcha).and_return(captcha_successful)
end end
describe "#create" do describe "#create" do
@ -22,47 +25,80 @@ describe User::RegistrationsController, type: :controller do
subject { post :create, params: registration_params } subject { post :create, params: registration_params }
it "doesn't allow a registration without solving the captcha" do context "when captcha was invalid" do
expect { subject }.not_to(change { User.count }) let(:captcha_successful) { false }
expect(response).to redirect_to :new_user_registration it "doesn't allow a registration without an invalid captcha" do
expect { subject }.not_to(change { User.count })
expect(response).to redirect_to :new_user_registration
end
end end
it "creates a user" do context "when captcha was valid" do
allow(controller).to receive(:verify_hcaptcha).and_return(true) let(:captcha_successful) { true }
expect { subject }.to change { User.count }.by(1) it "creates a user" do
allow(controller).to receive(:verify_hcaptcha).and_return(true)
expect { subject }.to change { User.count }.by(1)
end
end end
end end
context "invalid user sign up" do context "invalid user sign up" do
subject { post :create, params: registration_params } subject { post :create, params: registration_params }
let!(:registration_params) { {} } context "when registration params are empty" do
it "rejects unfilled registration forms" do let(:registration_params) do
expect { subject }.not_to(change { User.count }) {
user: {
screen_name: '',
email: '',
password: '',
password_confirmation: ''
}
}
end
let(:captcha_successful) { true }
it "rejects unfilled registration forms" do
allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(false)
expect { subject }.not_to(change { User.count })
end
end end
let!(:registration_params) { { context "when username contains invalid characters" do
user: { let(:registration_params) { {
screen_name: 'Dio Brando', user: {
email: 'the-world-21@somewhere.everywhere', screen_name: 'Dio Brando',
password: 'AReallySecurePassword456!', email: 'the-world-21@somewhere.everywhere',
password_confirmation: 'AReallySecurePassword456!' password: 'AReallySecurePassword456!',
} password_confirmation: 'AReallySecurePassword456!'
} } }
it "rejects registrations with invalid usernames" do } }
expect { subject }.not_to(change { User.count }) let(:captcha_successful) { true }
it "rejects registrations with invalid usernames" do
allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(false)
expect { subject }.not_to(change { User.count })
end
end end
let!(:registration_params) { { context "when username is forbidden" do
user: { let(:registration_params) { {
screen_name: 'inbox', user: {
email: 'the-world-21@somewhere.everywhere', screen_name: 'inbox',
password: 'AReallySecurePassword456!', email: 'the-world-21@somewhere.everywhere',
password_confirmation: 'AReallySecurePassword456!' password: 'AReallySecurePassword456!',
} password_confirmation: 'AReallySecurePassword456!'
} } }
it "rejects registrations with reserved usernames" do } }
expect { subject }.not_to(change { User.count }) let(:captcha_successful) { true }
it "rejects registrations with reserved usernames" do
allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(false)
expect { subject }.not_to(change { User.count })
end
end end
end end
end end