2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe UserAuthenticator do
|
2021-03-02 01:13:04 -06:00
|
|
|
def github_auth(email_valid)
|
|
|
|
{
|
|
|
|
email: "user53@discourse.org",
|
|
|
|
username: "joedoe546",
|
|
|
|
email_valid: email_valid,
|
|
|
|
omit_username: nil,
|
|
|
|
name: "Joe Doe 546",
|
|
|
|
authenticator_name: "github",
|
|
|
|
extra_data: {
|
|
|
|
provider: "github",
|
2023-01-09 05:18:21 -06:00
|
|
|
uid: "100",
|
2021-03-02 01:13:04 -06:00
|
|
|
},
|
2023-01-09 05:18:21 -06:00
|
|
|
skip_email_validation: false,
|
2021-03-02 01:13:04 -06:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
before { SiteSetting.enable_github_logins = true }
|
2021-03-02 01:13:04 -06:00
|
|
|
|
|
|
|
describe "#start" do
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "without authentication session" do
|
2021-03-02 01:13:04 -06:00
|
|
|
it "should apply the right user attributes" do
|
|
|
|
user = User.new
|
|
|
|
UserAuthenticator.new(user, {}).start
|
2018-05-22 18:26:07 -05:00
|
|
|
|
2021-03-02 01:13:04 -06:00
|
|
|
expect(user.password_required?).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows password requirement to be skipped" do
|
|
|
|
user = User.new
|
|
|
|
UserAuthenticator.new(user, {}, require_password: false).start
|
|
|
|
|
|
|
|
expect(user.password_required?).to eq(false)
|
|
|
|
end
|
2018-05-22 18:26:07 -05:00
|
|
|
end
|
2021-03-02 01:13:04 -06:00
|
|
|
end
|
|
|
|
|
2022-07-27 05:21:10 -05:00
|
|
|
describe "#finish" do
|
2021-03-02 01:13:04 -06:00
|
|
|
fab!(:group) { Fabricate(:group, automatic_membership_email_domains: "discourse.org") }
|
2018-05-22 18:26:07 -05:00
|
|
|
|
2021-05-20 20:43:47 -05:00
|
|
|
it "confirms email and adds the user to appropriate groups based on email" do
|
2018-05-22 18:26:07 -05:00
|
|
|
user = Fabricate(:user, email: "user53@discourse.org")
|
|
|
|
expect(group.usernames).not_to include(user.username)
|
|
|
|
|
|
|
|
authentication = github_auth(true)
|
|
|
|
|
2021-09-27 07:45:05 -05:00
|
|
|
UserAuthenticator.new(user, { authentication: authentication }).finish
|
2018-05-22 18:26:07 -05:00
|
|
|
expect(user.email_confirmed?).to be_truthy
|
|
|
|
expect(group.usernames).to include(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't confirm email if email is invalid" do
|
|
|
|
user = Fabricate(:user, email: "user53@discourse.org")
|
|
|
|
|
|
|
|
authentication = github_auth(false)
|
|
|
|
|
2021-09-27 07:45:05 -05:00
|
|
|
UserAuthenticator.new(user, { authentication: authentication }).finish
|
2018-05-22 18:26:07 -05:00
|
|
|
expect(user.email_confirmed?).to be_falsey
|
|
|
|
expect(group.usernames).not_to include(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't confirm email if it was changed" do
|
|
|
|
user = Fabricate(:user, email: "changed@discourse.org")
|
|
|
|
|
|
|
|
authentication = github_auth(true)
|
|
|
|
|
2021-09-27 07:45:05 -05:00
|
|
|
UserAuthenticator.new(user, { authentication: authentication }).finish
|
2018-05-22 18:26:07 -05:00
|
|
|
expect(user.email_confirmed?).to be_falsey
|
|
|
|
expect(group.usernames).not_to include(user.username)
|
|
|
|
end
|
2019-09-12 06:11:12 -05:00
|
|
|
|
|
|
|
it "clears the authentication info from the session" do
|
|
|
|
user = Fabricate(:user, email: "user53@discourse.org")
|
|
|
|
session = { authentication: github_auth(true) }
|
|
|
|
|
|
|
|
UserAuthenticator.new(user, session).finish
|
|
|
|
expect(user.email_confirmed?).to be_truthy
|
|
|
|
|
|
|
|
expect(session[:authentication]).to eq(nil)
|
|
|
|
end
|
2021-02-22 06:05:36 -06:00
|
|
|
|
|
|
|
it "raises an error for non-boolean values" do
|
|
|
|
user = Fabricate(:user, email: "user53@discourse.org")
|
2023-01-09 05:18:21 -06:00
|
|
|
session = { authentication: github_auth("string") }
|
2021-02-22 06:05:36 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect do UserAuthenticator.new(user, session).finish end.to raise_error ArgumentError
|
2021-02-22 06:05:36 -06:00
|
|
|
end
|
2018-05-22 18:26:07 -05:00
|
|
|
end
|
|
|
|
end
|