0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/plugins/discourse-captcha/spec/lib/discourse_captcha/hcaptcha_provider_spec.rb
Juan David Martínez Cubillos 04b5530f34
DEV: Plugin rename discourse captcha (#39350)
####  Description:
This PR renames the `discourse-hcaptcha` plugin to `discourse-captcha`
to better reflect its support for multiple captcha providers (hCaptcha
and reCAPTCHA)

#### Changes

- Renamed plugin directory from plugins/discourse-hcaptcha to
plugins/discourse-captcha
- Updated Ruby module namespace from DiscourseHcaptcha to
DiscourseCaptcha
  - Updated references in:
    - .gitignore
    - pnpm-workspace.yaml
    - tsconfig.json
    - config/official_plugins.json
    - translator.yml
    - migrations/core/config/intermediate_db.yml
    - `hosted-site` plugin
- Migration file preserved to handle existing discourse_hcaptcha_enabled
setting migration

---------

Co-authored-by: Gabriel Grubba <gabriel@discourse.org>
2026-07-21 17:21:39 -05:00

58 lines
1.7 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe DiscourseCaptcha::HcaptchaProvider do
subject(:provider) { described_class.new }
describe "#captcha_verification_url" do
it "returns the hCaptcha verification URL" do
expect(provider.captcha_verification_url).to eq("https://hcaptcha.com/siteverify")
end
end
describe "#fetch_captcha_token" do
let(:token) { "test-hcaptcha-token" }
let(:server_session) { ServerSession.new(SecureRandom.hex) }
before { server_session["hcaptcha_token"] = token }
it "retrieves token from server session" do
result = provider.fetch_captcha_token(server_session)
expect(result).to eq(token)
end
it "deletes the token from server session after fetching" do
provider.fetch_captcha_token(server_session)
expect(server_session["hcaptcha_token"]).to be_nil
end
context "when no token is present" do
let(:empty_session) { ServerSession.new(SecureRandom.hex) }
it "returns nil" do
result = provider.fetch_captcha_token(empty_session)
expect(result).to be_nil
end
end
end
describe "#send_captcha_verification" do
let(:token) { "test-hcaptcha-token" }
before { SiteSetting.hcaptcha_secret_key = "test-secret-key" }
it "returns the response from hCaptcha" do
stub =
stub_request(:post, "https://hcaptcha.com/siteverify").to_return(
status: 200,
body: '{"success":true}',
)
response = provider.send_captcha_verification(token)
expect(stub).to have_been_requested
expect(response.code.to_i).to eq(200)
expect(JSON.parse(response.body)["success"]).to be(true)
end
end
end