From 4c7ec05198568ff67932a62fe231504fcef65666 Mon Sep 17 00:00:00 2001 From: "Dominik M. Kwiatek" Date: Wed, 27 May 2020 22:27:54 +0100 Subject: [PATCH] Address @nilsding's review comments --- config/initializers/hcaptcha.rb | 2 +- config/justask.yml.example | 1 + .../user/registration_controller_spec.rb | 33 +++++++++---------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/config/initializers/hcaptcha.rb b/config/initializers/hcaptcha.rb index 24454368..b8c22200 100644 --- a/config/initializers/hcaptcha.rb +++ b/config/initializers/hcaptcha.rb @@ -5,4 +5,4 @@ Hcaptcha.configure do |config| config.secret_key = APP_CONFIG.dig(:hcaptcha, :secret_key) config.skip_verify_env.delete 'test' -end \ No newline at end of file +end diff --git a/config/justask.yml.example b/config/justask.yml.example index 3e3c42b4..57ce7c8a 100644 --- a/config/justask.yml.example +++ b/config/justask.yml.example @@ -63,6 +63,7 @@ admins: # bucket name, required # directory: 'retrospring' +# hCaptcha -- get keys from https://www.hcaptcha.com/ hcaptcha: enabled: false site_key: '' diff --git a/spec/controllers/user/registration_controller_spec.rb b/spec/controllers/user/registration_controller_spec.rb index 0d3bf263..3e7670fc 100644 --- a/spec/controllers/user/registration_controller_spec.rb +++ b/spec/controllers/user/registration_controller_spec.rb @@ -5,13 +5,15 @@ 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 context "valid user sign up" do + before do + allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(true) + allow(controller).to receive(:verify_hcaptcha).and_return(captcha_successful) + end + let :registration_params do { user: { @@ -25,15 +27,15 @@ describe User::RegistrationsController, type: :controller do subject { post :create, params: registration_params } - context "when captcha was invalid" do + context "when captcha is invalid" do let(:captcha_successful) { false } - it "doesn't allow a registration without an invalid captcha" do + it "doesn't allow a registration with an invalid captcha" do expect { subject }.not_to(change { User.count }) expect(response).to redirect_to :new_user_registration end end - context "when captcha was valid" do + context "when captcha is valid" do let(:captcha_successful) { true } it "creates a user" do allow(controller).to receive(:verify_hcaptcha).and_return(true) @@ -43,6 +45,10 @@ describe User::RegistrationsController, type: :controller do end context "invalid user sign up" do + before do + allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(false) + end + subject { post :create, params: registration_params } context "when registration params are empty" do @@ -56,11 +62,8 @@ describe User::RegistrationsController, type: :controller do } } end - let(:captcha_successful) { true } - - it "rejects unfilled registration forms" do - allow(APP_CONFIG).to receive(:dig).with(:hcaptcha, :enabled).and_return(false) + it "does not create a user" do expect { subject }.not_to(change { User.count }) end end @@ -74,11 +77,8 @@ describe User::RegistrationsController, type: :controller do 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) + it "does not create a user" do expect { subject }.not_to(change { User.count }) end end @@ -92,11 +92,8 @@ describe User::RegistrationsController, type: :controller do 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) + it "does not create a user" do expect { subject }.not_to(change { User.count }) end end