Fix styling and tidy validations

This commit is contained in:
Prommah 2015-09-24 10:19:00 +01:00
parent 4d83b2a06d
commit c4a0dd62ff
3 changed files with 39 additions and 43 deletions

11
app/models/ban.rb Normal file → Executable file
View file

@ -33,14 +33,13 @@ class Ban < ActiveRecord::Base
scope :effective, conditions: "expiry > UTC_TIMESTAMP()" scope :effective, conditions: "expiry > UTC_TIMESTAMP()"
scope :ineffective, conditions: "expiry < UTC_TIMESTAMP()" scope :ineffective, conditions: "expiry < UTC_TIMESTAMP()"
before_validation :check_user
validate :validate_type validate :validate_type
validate :validate_ventban validate :validate_ventban
validates_length_of :steamid, maximum: 14, allow_blank: true validates :steamid, length: {maximum: 14}, format: /\A0:[01]:[0-9]{1,10}\Z/, allow_blank: true
validates_format_of :steamid, with: /\A0:[01]:[0-9]{1,10}\Z/, allow_blank: true validates :addr, format: /\A([0-9]{1,3}\.){3}[0-9]{1,3}:?[0-9]{0,5}\z/, allow_blank: true
validates_format_of :addr, with: /\A([0-9]{1,3}\.){3}[0-9]{1,3}:?[0-9]{0,5}\z/, allow_blank: true validates :reason, length: {maximum: 255}, allow_blank: true
validates_length_of :reason, maximum: 255, allow_nil: true, allow_blank: true
before_validation :check_user
belongs_to :user belongs_to :user
belongs_to :server belongs_to :server

View file

@ -100,24 +100,20 @@ class User < ActiveRecord::Base
scope :idle, scope :idle,
:conditions => ["lastvisit < ?", 30.minutes.ago.utc] :conditions => ["lastvisit < ?", 30.minutes.ago.utc]
validates_uniqueness_of :username, :email before_validation :update_password
validates_uniqueness_of :steamid, :allow_nil => true
validates_length_of :firstname, :in => 1..15, :allow_blank => true validates :username, uniqueness: true, length: {in: 2..20}, format: /\A[A-Za-z0-9_\-\+]{2,20}\Z/
validates_length_of :lastname, :in => 1..25, :allow_blank => true validates :firstname, length: {in: 1..15}, allow_blank: true
validates_length_of :username, :in => 2..20 validates :lastname, length: {in: 1..25}, allow_blank: true
validates_format_of :username, :with => /\A[A-Za-z0-9_\-\+]{2,20}\Z/ validates :raw_password, presence: {on: :create}
validates_presence_of :raw_password, :on => :create validates :email, uniqueness: true, length: {maximum: 50}, format: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_length_of :email, :maximum => 50 validates :steamid, uniqueness: {allow_nil: true}, length: {maximum: 14}, presence: {on: :create}
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
validates_length_of :steamid, :maximum => 14
validates :steamid, presence: true, on: :create
validate :validate_steamid validate :validate_steamid
validates_length_of :time_zone, :maximum => 100, :allow_blank => true, :allow_nil => true validates :time_zone, length: {maximum: 100}, allow_blank: true
validates_inclusion_of [:public_email], :in => [true, false], :allow_nil => true validates :public_email, inclusion: [true, false], allow_nil: true
validate :validate_team validate :validate_team
before_create :init_variables before_create :init_variables
before_validation :update_password
before_save :correct_steamid_universe before_save :correct_steamid_universe
@ -243,15 +239,16 @@ class User < ActiveRecord::Base
end end
def validate_steamid def validate_steamid
errors.add :steamid unless self.steamid.nil? || errors.add :steamid unless
(m = self.steamid.match(/\A([01]):([01]):(\d{1,10})\Z/)) && steamid.nil? ||
(m = steamid.match(/\A([01]):([01]):(\d{1,10})\Z/)) &&
(id = m[3].to_i) && (id = m[3].to_i) &&
id >= 1 && id <= 2147483647 id >= 1 && id <= 2147483647
end end
def correct_steamid_universe def correct_steamid_universe
if self.steamid.present? if steamid.present?
self.steamid[0] = "0" steamid[0] = "0"
end end
end end

View file

@ -1,64 +1,64 @@
require 'spec_helper' require "spec_helper"
feature 'Visitor signs up', js: :true do feature "Visitor signs up", js: :true do
let(:user) { attributes_for(:user) } let(:user) { attributes_for(:user) }
before do before do
visit new_user_path visit new_user_path
end end
scenario 'with valid Username, Email, Password and Steam ID' do scenario "with valid Username, Email, Password and Steam ID" do
within registration_form do within registration_form do
fill_form(:user, user.slice(*sign_up_attributes)) fill_form(:user, user.slice(*sign_up_attributes))
click_button submit(:user, :create) click_button submit(:user, :create)
end end
expect(user_status).to have_content('ACCOUNT') expect(user_status).to have_content("ACCOUNT")
end end
scenario 'with invalid Email' do scenario "with invalid Email" do
within registration_form do within registration_form do
fill_form(:user, user.slice(*sign_up_attributes).merge({ email: "invalid" })) fill_form(:user, user.slice(*sign_up_attributes).merge(email: "invalid"))
click_button submit(:user, :create) click_button submit(:user, :create)
end end
expect(page).to have_content(error_message('email.invalid')) expect(page).to have_content(error_message("email.invalid"))
end end
scenario 'with blank Password' do scenario "with blank Password" do
within registration_form do within registration_form do
fill_form(:user, user.slice(*sign_up_attributes).merge({ raw_password: "" })) fill_form(:user, user.slice(*sign_up_attributes).merge(raw_password: ""))
click_button submit(:user, :create) click_button submit(:user, :create)
end end
expect(page).to have_content(error_message('raw_password.blank')) expect(page).to have_content(error_message("raw_password.blank"))
end end
scenario 'with invalid Steam ID' do scenario "with invalid Steam ID" do
within registration_form do within registration_form do
fill_form(:user, user.slice(*sign_up_attributes).merge({ steamid: "invalid" })) fill_form(:user, user.slice(*sign_up_attributes).merge(steamid: "invalid"))
click_button submit(:user, :create) click_button submit(:user, :create)
end end
expect(page).to have_content(error_message('steamid.invalid')) expect(page).to have_content(error_message("steamid.invalid"))
end end
scenario 'with out of range Steam ID' do scenario "with out of range Steam ID" do
within registration_form do within registration_form do
fill_form(:user, user.slice(*sign_up_attributes).merge({ steamid: "0:0:2147483648" })) fill_form(:user, user.slice(*sign_up_attributes).merge(steamid: "0:0:2147483648"))
click_button submit(:user, :create) click_button submit(:user, :create)
end end
expect(page).to have_content(error_message('steamid.invalid')) expect(page).to have_content(error_message("steamid.invalid"))
end end
scenario 'with nil Steam ID' do scenario "with nil Steam ID" do
within registration_form do within registration_form do
fill_form(:user, user.slice(*sign_up_attributes).merge({ steamid: nil })) fill_form(:user, user.slice(*sign_up_attributes).merge(steamid: nil))
click_button submit(:user, :create) click_button submit(:user, :create)
end end
expect(page).to have_content(error_message('steamid.invalid')) expect(page).to have_content(error_message("steamid.invalid"))
end end
def sign_up_attributes def sign_up_attributes