Mock verify_captcha

This commit is contained in:
Dominik M. Kwiatek 2020-05-27 21:47:27 +01:00
parent 5650b2eb0b
commit e512d5502d
1 changed files with 65 additions and 29 deletions

View File

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