2021-12-23 05:53:17 -05:00
|
|
|
# frozen_string_literal: true
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Auth::Result do
|
2021-12-23 05:53:17 -05:00
|
|
|
fab!(:initial_email) { "initialemail@example.org" }
|
|
|
|
fab!(:initial_username) { "initialusername" }
|
|
|
|
fab!(:initial_name) { "Initial Name" }
|
|
|
|
fab!(:user) do
|
|
|
|
Fabricate(:user, email: initial_email, username: initial_username, name: initial_name)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2021-12-23 05:53:17 -05:00
|
|
|
|
|
|
|
let(:new_email) { "newemail@example.org" }
|
|
|
|
let(:new_username) { "newusername" }
|
|
|
|
let(:new_name) { "New Name" }
|
|
|
|
|
|
|
|
let(:result) do
|
|
|
|
result = Auth::Result.new
|
|
|
|
result.email = new_email
|
|
|
|
result.username = new_username
|
|
|
|
result.name = new_name
|
|
|
|
result.user = user
|
|
|
|
result.email_valid = true
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't override user attributes by default" do
|
|
|
|
result.apply_user_attributes!
|
|
|
|
expect(user.email).to eq(initial_email)
|
|
|
|
expect(user.username).to eq(initial_username)
|
|
|
|
expect(user.name).to eq(initial_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "overrides user attributes when site settings enabled" do
|
|
|
|
SiteSetting.email_editable = false
|
|
|
|
SiteSetting.auth_overrides_email = true
|
|
|
|
SiteSetting.auth_overrides_name = true
|
|
|
|
SiteSetting.auth_overrides_username = true
|
|
|
|
|
|
|
|
result.apply_user_attributes!
|
|
|
|
|
|
|
|
expect(user.email).to eq(new_email)
|
|
|
|
expect(user.username).to eq(new_username)
|
|
|
|
expect(user.name).to eq(new_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "overrides user attributes when result attributes set" do
|
|
|
|
result.overrides_email = true
|
|
|
|
result.overrides_name = true
|
|
|
|
result.overrides_username = true
|
|
|
|
|
|
|
|
result.apply_user_attributes!
|
|
|
|
|
|
|
|
expect(user.email).to eq(new_email)
|
|
|
|
expect(user.username).to eq(new_username)
|
|
|
|
expect(user.name).to eq(new_name)
|
|
|
|
end
|
|
|
|
|
2022-05-04 06:22:09 -04:00
|
|
|
it "overrides username with suggested value if missing" do
|
|
|
|
SiteSetting.auth_overrides_username = true
|
|
|
|
|
|
|
|
result.username = nil
|
|
|
|
result.apply_user_attributes!
|
|
|
|
|
|
|
|
expect(user.username).to eq("New_Name")
|
|
|
|
end
|
|
|
|
|
2021-12-23 05:53:17 -05:00
|
|
|
it "updates the user's email if currently invalid" do
|
|
|
|
user.update!(email: "someemail@discourse.org")
|
|
|
|
expect { result.apply_user_attributes! }.not_to change { user.email }
|
|
|
|
|
|
|
|
user.update!(email: "someemail@discourse.invalid")
|
|
|
|
expect { result.apply_user_attributes! }.to change { user.email }
|
|
|
|
|
|
|
|
expect(user.email).to eq(new_email)
|
|
|
|
end
|
|
|
|
end
|