Skip to content

Commit 7cb6210

Browse files
authored
Merge pull request #5268 from manyfold3d/allow-saving-invalid-user-tour-state
Allow saving tour state without validation
2 parents ba94676 + 06905a1 commit 7cb6210

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

app/models/user.rb

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class User < ApplicationRecord
1212
has_many :creators, -> { where("caber_relations.permission": "own") }, through: :caber_relations, source_type: "Creator", source: :object
1313
accepts_nested_attributes_for :creators
1414

15-
before_validation :set_json_field_defaults, on: :create
15+
before_validation :set_json_field_defaults
1616
before_save :set_quota
1717

1818
acts_as_federails_actor(
@@ -29,22 +29,22 @@ class User < ApplicationRecord
2929

3030
devise :omniauthable, omniauth_providers: %i[openid_connect] if SiteSettings.oidc_enabled?
3131

32-
validates :username, multimodel_uniqueness: {punctuation_sensitive: false, case_sensitive: false, check: FederailsCommon::FEDIVERSE_USERNAMES}
33-
34-
validates :username,
35-
presence: true,
36-
uniqueness: {case_sensitive: false},
37-
format: {with: /\A[[:alnum:].\-_;]+\z/}
38-
39-
validates :email,
40-
presence: true,
41-
uniqueness: {case_sensitive: false},
42-
format: {with: URI::MailTo::EMAIL_REGEXP}
43-
44-
validates :password,
45-
presence: true,
46-
confirmation: true,
47-
if: :password_required?
32+
# Reduce validations if only safe settings are being changed
33+
with_options unless: :only_settings_changed? do
34+
validates :username,
35+
presence: true,
36+
uniqueness: {case_sensitive: false},
37+
format: {with: /\A[[:alnum:].\-_;]+\z/},
38+
multimodel_uniqueness: {punctuation_sensitive: false, case_sensitive: false, check: FederailsCommon::FEDIVERSE_USERNAMES}
39+
validates :email,
40+
presence: true,
41+
uniqueness: {case_sensitive: false},
42+
format: {with: URI::MailTo::EMAIL_REGEXP}
43+
validates :password,
44+
presence: true,
45+
confirmation: true,
46+
if: :password_required?
47+
end
4848

4949
after_create :assign_default_role
5050

@@ -234,4 +234,12 @@ def set_json_field_defaults
234234
self.file_list_settings ||= SiteSettings::UserDefaults::FILE_LIST
235235
self.tour_state ||= DEFAULT_TOUR_STATE
236236
end
237+
238+
def only_settings_changed?
239+
return false unless changed?
240+
settings_attributes = [
241+
"tour_state"
242+
].freeze
243+
(changed - settings_attributes).empty?
244+
end
237245
end

spec/requests/users/registrations_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,5 +452,30 @@
452452
patch "/users", params: completion_params, as: :json
453453
expect(User.first.tour_state["completed"]).to include("new-item", "old-item")
454454
end
455+
456+
context "with an invalid user" do # rubocop:disable RSpec/MultipleMemoizedHelpers
457+
let(:user) { create(:contributor) }
458+
let(:creator) { create(:creator) }
459+
460+
before do
461+
sign_in user
462+
user.username = "@@@"
463+
user.save validate: false
464+
end
465+
466+
it "has an invalid user" do
467+
expect(user).not_to be_valid
468+
end
469+
470+
it "doesn't validate if only saving tour state or other settings" do
471+
patch "/users", params: completion_params, as: :json
472+
expect(response).to have_http_status :success
473+
end
474+
475+
it "validates if important data is also entered" do
476+
patch "/users", params: {user: {email: "[email protected]", tour_state: {completed: {add: ["new-item"]}}}}, as: :json
477+
expect(response).to have_http_status :unprocessable_content
478+
end
479+
end
455480
end
456481
end

0 commit comments

Comments
 (0)