Retrospring/spec/controllers/user/registration_controller_spe...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

160 lines
5.0 KiB
Ruby
Raw Normal View History

2020-05-27 12:04:49 -07:00
# frozen_string_literal: true
require "rails_helper"
describe User::RegistrationsController, type: :controller do
before do
2020-10-23 11:45:06 -07:00
# Required for devise to register routes
2020-05-27 12:04:49 -07:00
@request.env["devise.mapping"] = Devise.mappings[:user]
stub_const("APP_CONFIG", {
"hostname" => "example.com",
"https" => true,
"items_per_page" => 5,
"forbidden_screen_names" => %w[
justask_admin retrospring_admin admin justask retrospring
moderation moderator mod administrator siteadmin site_admin
help retro_spring retroospring retrosprlng
],
},)
2020-05-27 12:04:49 -07:00
end
describe "#create" do
context "valid user sign up" do
2020-05-27 14:27:54 -07:00
before do
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)
2020-05-27 14:27:54 -07:00
end
2020-05-27 12:04:49 -07:00
let :registration_params do
{
user: {
screen_name: "dio",
email: "the-world-21@somewhere.everywhere.now",
password: "AReallySecurePassword456!",
password_confirmation: "AReallySecurePassword456!",
},
2020-05-27 12:04:49 -07:00
}
end
subject { post :create, params: registration_params }
2020-05-27 14:27:54 -07:00
context "when captcha is invalid" do
2024-08-09 01:57:08 -07:00
before do
allow(controller).to receive(:verify_hcaptcha).and_return(false)
end
2020-05-27 14:27:54 -07:00
it "doesn't allow a registration with an invalid captcha" do
2020-05-27 13:47:27 -07:00
expect { subject }.not_to(change { User.count })
expect(response).to redirect_to :new_user_registration
end
2020-05-27 12:04:49 -07:00
end
2020-05-27 14:27:54 -07:00
context "when captcha is valid" do
2024-08-09 01:57:08 -07:00
before do
allow(controller).to receive(:verify_hcaptcha).and_return(true)
end
2020-05-27 13:47:27 -07:00
it "creates a user" do
allow(controller).to receive(:verify_hcaptcha).and_return(true)
expect { subject }.to change { User.count }.by(1)
end
2020-05-27 12:04:49 -07:00
end
2024-08-09 01:57:08 -07:00
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
2020-05-27 12:04:49 -07:00
end
context "invalid user sign up" do
2020-05-27 14:27:54 -07:00
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(true)
2020-05-27 14:27:54 -07:00
end
2020-05-27 12:04:49 -07:00
subject { post :create, params: registration_params }
2020-05-27 13:47:27 -07:00
context "when registration params are empty" do
let(:registration_params) do
{
user: {
screen_name: "",
email: "",
password: "",
password_confirmation: "",
},
2020-05-27 13:47:27 -07:00
}
end
2020-05-27 14:27:54 -07:00
it "does not create a user" do
2020-05-27 13:47:27 -07:00
expect { subject }.not_to(change { User.count })
end
2020-05-27 12:04:49 -07:00
end
2020-05-27 13:47:27 -07:00
context "when username contains invalid characters" do
let(:registration_params) do
{
2020-05-27 13:47:27 -07:00
user: {
screen_name: "Dio Brando",
email: "the-world-21@somewhere.everywhere.now",
password: "AReallySecurePassword456!",
password_confirmation: "AReallySecurePassword456!",
},
}
end
2020-05-27 13:47:27 -07:00
2020-05-27 14:27:54 -07:00
it "does not create a user" do
2020-05-27 13:47:27 -07:00
expect { subject }.not_to(change { User.count })
end
2020-05-27 12:04:49 -07:00
end
2020-05-27 13:47:27 -07:00
context "when username is forbidden" do
let(:registration_params) do
{
2020-05-27 13:47:27 -07:00
user: {
screen_name: "moderator",
email: "the-world-21@somewhere.everywhere.now",
password: "AReallySecurePassword456!",
password_confirmation: "AReallySecurePassword456!",
},
}
end
2020-05-27 13:47:27 -07:00
2020-05-27 14:27:54 -07:00
it "does not create a user" do
2020-05-27 13:47:27 -07:00
expect { subject }.not_to(change { User.count })
end
2020-05-27 12:04:49 -07:00
end
end
end
describe "#new" do
subject { get :new }
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
2020-05-27 12:04:49 -07:00
end
end