Skip to content

Commit

Permalink
Fix db:seed - only run some validations when the field was changed (#…
Browse files Browse the repository at this point in the history
…3592)

* Fix db:seed - only run some validations when the field was changed

* Add tests
  • Loading branch information
Gargron authored and mjankowski committed Jun 8, 2017
1 parent b87eb8e commit c207b4b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
12 changes: 8 additions & 4 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ class Account < ApplicationRecord
has_one :user, inverse_of: :account

validates :username, presence: true
validates :username, uniqueness: { scope: :domain, case_sensitive: true }, unless: :local?

# Remote user validations
with_options unless: :local? do
validates :username, uniqueness: { scope: :domain, case_sensitive: true }, if: :username_changed?
end

# Local user validations
with_options if: :local? do
validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, unreserved: true
validates :display_name, length: { maximum: 30 }
validates :note, length: { maximum: 160 }
validates :username, format: { with: /\A[a-z0-9_]+\z/i }, uniqueness: { scope: :domain, case_sensitive: false }, length: { maximum: 30 }, unreserved: true, if: :username_changed?
validates :display_name, length: { maximum: 30 }, if: :display_name_changed?
validates :note, length: { maximum: 160 }, if: :note_changed?
end

# Timelines
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class User < ApplicationRecord
accepts_nested_attributes_for :account

validates :locale, inclusion: I18n.available_locales.map(&:to_s), if: :locale?
validates :email, email: true
validates :email, email: true, if: :email_changed?

scope :recent, -> { order(id: :desc) }
scope :admins, -> { where(admin: true) }
Expand Down
3 changes: 2 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

if Rails.env.development?
domain = ENV['LOCAL_DOMAIN'] || Rails.configuration.x.local_domain
admin = Account.where(username: 'admin').first_or_create!(username: 'admin')
admin = Account.where(username: 'admin').first_or_initialize(username: 'admin')
admin.save(validate: false)
User.where(email: "admin@#{domain}").first_or_initialize(email: "admin@#{domain}", password: 'mastodonadmin', password_confirmation: 'mastodonadmin', confirmed_at: Time.now.utc, admin: true, account: admin).save!
end
6 changes: 6 additions & 0 deletions spec/models/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@
expect(account).to model_have_error_on_field(:username)
end

it 'is valid when username is reserved but record has already been created' do
account = Fabricate.build(:account, username: 'support')
account.save(validate: false)
expect(account.valid?).to be true
end

context 'when is local' do
it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
account = Fabricate.build(:account, username: 'the-doctor')
Expand Down
6 changes: 6 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
expect(user).to model_have_error_on_field(:email)
end

it 'is valid with an invalid e-mail that has already been saved' do
user = Fabricate.build(:user, email: 'invalid-email')
user.save(validate: false)
expect(user.valid?).to be true
end

it 'cleans out empty string from languages' do
user = Fabricate.build(:user, filtered_languages: [''])
user.valid?
Expand Down

0 comments on commit c207b4b

Please sign in to comment.