discourse/spec/requests/finish_installation_controller_spec.rb
Penar Musaraj e8c4b80906
DEV: Allow new instances to be set up directly with Discourse ID (#36731)
This relies on https://github.com/discourse/discourse_docker/pull/1010 

When email setup is skipped, we can register the site for Discourse ID.
That means the admin (and other users), can signup/login without the
site having working email at all. The goal of this feature is to
simplify bootstrapping a test site. A well-functioning community will
likely need working email at some point, this simplifies the first step
of getting started.
2025-12-23 12:13:36 -05:00

173 lines
5.8 KiB
Ruby

# frozen_string_literal: true
RSpec.describe FinishInstallationController do
describe "#index" do
context "when has_login_hint is false" do
before { SiteSetting.has_login_hint = false }
it "doesn't allow access" do
get "/finish-installation"
expect(response.status).to eq(403)
end
end
context "when has_login_hint is true" do
before { SiteSetting.has_login_hint = true }
it "allows access" do
get "/finish-installation"
expect(response.status).to eq(200)
end
context "when setting up Discourse ID" do
before do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with("DISCOURSE_SKIP_EMAIL_SETUP").and_return("1")
GlobalSetting.stubs(:developer_emails).returns("admin@example.com")
end
it "enables the enable_discourse_id site setting and shows login button on success" do
stub_request(:post, "https://id.discourse.com/challenge").to_return(
status: 200,
body: { domain: Discourse.current_hostname, token: "test_token" }.to_json,
)
stub_request(:post, "https://id.discourse.com/register").to_return(
status: 200,
body: { client_id: "test_client_id", client_secret: "test_client_secret" }.to_json,
)
get "/finish-installation"
expect(response.status).to eq(200)
expect(SiteSetting.enable_discourse_id).to eq(true)
expect(SiteSetting.enable_local_logins).to eq(false)
expect(response.body).to include("Login with Discourse ID")
expect(response.body).to include("/finish-installation/redirect-discourse-id")
end
it "shows error message and no login button on failure" do
stub_request(:post, "https://id.discourse.com/challenge").to_return(
status: 500,
body: "Internal Server Error",
)
get "/finish-installation"
expect(response.status).to eq(200)
expect(SiteSetting.enable_discourse_id).to eq(false)
expect(response.body).not_to include("Login with Discourse ID")
expect(response.body).to include("alert-error")
end
it "shows error when developer_emails is empty" do
GlobalSetting.stubs(:developer_emails).returns("")
stub_request(:post, "https://id.discourse.com/challenge").to_return(
status: 200,
body: { domain: Discourse.current_hostname, token: "test_token" }.to_json,
)
stub_request(:post, "https://id.discourse.com/register").to_return(
status: 200,
body: { client_id: "test_client_id", client_secret: "test_client_secret" }.to_json,
)
get "/finish-installation"
expect(response.status).to eq(200)
expect(response.body).not_to include("Login with Discourse ID")
expect(response.body).to include("alert-error")
expect(response.body).to include(
I18n.t("finish_installation.discourse_id.no_allowed_emails"),
)
end
end
end
end
describe "#register" do
before do
SiteSetting.has_login_hint = true
GlobalSetting.stubs(:developer_emails).returns("robin@example.com")
end
it "shows no_emails message when developer_emails is empty" do
GlobalSetting.stubs(:developer_emails).returns("")
get "/finish-installation/register"
expect(response.status).to eq(200)
expect(response.body).to include(I18n.t("finish_installation.register.no_emails"))
end
it "returns 400 when email is not in the allowed list" do
post "/finish-installation/register.json",
params: {
email: "notrobin@example.com",
username: "eviltrout",
password: "disismypasswordokay",
}
expect(response.status).to eq(400)
end
end
describe "#confirm_email" do
it "renders without requiring has_login_hint" do
SiteSetting.has_login_hint = false
get "/finish-installation/confirm-email"
expect(response.status).to eq(200)
end
end
describe "#resend_email" do
before do
SiteSetting.has_login_hint = true
GlobalSetting.stubs(:developer_emails).returns("robin@example.com")
end
it "resends activation email for user in session" do
post "/finish-installation/register",
params: {
email: "robin@example.com",
username: "eviltrout",
password: "disismypasswordokay",
}
expect { put "/finish-installation/resend-email" }.to change {
Jobs::CriticalUserEmail.jobs.size
}.by(1)
expect(response.status).to eq(200)
end
it "does nothing when user doesn't exist" do
expect { put "/finish-installation/resend-email" }.not_to change {
Jobs::CriticalUserEmail.jobs.size
}
expect(response.status).to eq(200)
end
end
describe "#redirect_discourse_id" do
context "when has_login_hint is true" do
before do
SiteSetting.has_login_hint = true
GlobalSetting.stubs(:developer_emails).returns("info@test.com")
end
it "creates admin users and redirects to Discourse ID auth" do
get "/finish-installation/redirect-discourse-id"
expect(response.status).to eq(302)
expect(response.location).to include("/auth/discourse_id")
admin_user = User.find_by_email("info@test.com")
expect(admin_user).to be_present
expect(admin_user.admin).to eq(true)
end
end
context "when has_login_hint is false" do
before { SiteSetting.has_login_hint = false }
it "returns 403" do
get "/finish-installation/redirect-discourse-id"
expect(response.status).to eq(403)
end
end
end
end