From 722a99a1204dc037c7d72c138fe3f09a98c68599 Mon Sep 17 00:00:00 2001 From: Nicolae Claudius Date: Sat, 8 Oct 2011 06:37:20 -0700 Subject: [PATCH] spec, ensurances, tweaks --- Gemfile | 16 ++++------ Gemfile.lock | 19 ++++++++--- Guardfile | 32 +++++++++++++++++++ app/controllers/domains_controller.rb | 12 ++++++- app/models/domain.rb | 10 ++++++ app/models/soa.rb | 18 ++++++++--- ...e.yml.sample => database.yml.mysql.sample} | 25 ++++++++------- db/seeds.rb | 4 ++- spec/factories.rb | 25 +++++++++++++++ ..._as_helper_spec.rb => soas_helper_spec.rb} | 2 +- spec/models/domain_spec.rb | 5 ++- spec/spec_helper.rb | 8 ++--- spec/support/rspec.rb | 8 +++++ 13 files changed, 144 insertions(+), 40 deletions(-) create mode 100644 Guardfile rename config/{database.yml.sample => database.yml.mysql.sample} (59%) rename spec/helpers/{so_as_helper_spec.rb => soas_helper_spec.rb} (94%) create mode 100644 spec/support/rspec.rb diff --git a/Gemfile b/Gemfile index 581b1c9..90120dd 100644 --- a/Gemfile +++ b/Gemfile @@ -5,15 +5,8 @@ gem 'rails', '3.1.1.rc1' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' -group :development, :test do - gem 'sqlite3' -end - -group :production do - # gem 'pg' - gem 'mysql2' -end - +# gem 'pg' +gem 'mysql2' gem 'devise', '~> 1.4.5' gem 'cancan', '~> 1.6.5' # gem "meta_where", "~> 1.0" # squeel ? @@ -41,9 +34,12 @@ gem 'jquery-rails' gem 'rspec-rails', '~> 2.6.1', :group => [:test, :development] group :test do - gem 'forgery','~> 0.3.12' + gem 'faker','~> 1.0.1' gem 'factory_girl_rails', '~> 1.2' gem 'capybara', '~> 1.1.1' gem 'database_cleaner', '~> 0.6.7' gem 'spork', '~> 0.9.0.rc' + gem 'rb-fsevent', '~> 0.4.3.1' + gem 'guard-rspec', '~> 0.4.5' + gem 'guard-spork', '~> 0.3.0' end diff --git a/Gemfile.lock b/Gemfile.lock index 6fd3edb..5526016 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -87,10 +87,17 @@ GEM factory_girl_rails (1.2.0) factory_girl (~> 2.1.0) railties (>= 3.0.0) + faker (1.0.1) + i18n (~> 0.4) ffi (1.0.9) - forgery (0.3.12) - nokogiri (~> 1.4) fssm (0.2.7) + guard (0.8.4) + thor (~> 0.14.6) + guard-rspec (0.4.5) + guard (>= 0.4.0) + guard-spork (0.3.0) + guard (>= 0.8.2) + spork (>= 0.8.4) highline (1.6.2) hike (1.2.1) i18n (0.6.0) @@ -148,6 +155,7 @@ GEM rdoc (~> 3.4) thor (~> 0.14.6) rake (0.9.2) + rb-fsevent (0.4.3.1) rdoc (3.9.4) rspec (2.6.0) rspec-core (~> 2.6.0) @@ -181,7 +189,6 @@ GEM hike (~> 1.2) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) - sqlite3 (1.3.4) therubyracer (0.9.8) libv8 (~> 3.3.10) thor (0.14.6) @@ -211,18 +218,20 @@ DEPENDENCIES database_cleaner (~> 0.6.7) devise (~> 1.4.5) factory_girl_rails (~> 1.2) - forgery (~> 0.3.12) + faker (~> 1.0.1) + guard-rspec (~> 0.4.5) + guard-spork (~> 0.3.0) jquery-rails mysql2 nilify_blanks (~> 1.0.0) pjax_rails (~> 0.1.10) rails (= 3.1.1.rc1) rails_config (~> 0.2.4) + rb-fsevent (~> 0.4.3.1) rspec-rails (~> 2.6.1) sass-rails (~> 3.1.0) sentient_user (~> 0.3.2) spork (~> 0.9.0.rc) - sqlite3 therubyracer uglifier validates_hostname (~> 1.0.0)! diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..089f4d9 --- /dev/null +++ b/Guardfile @@ -0,0 +1,32 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do + watch('config/application.rb') + watch('config/environment.rb') + watch(%r{^config/environments/.+\.rb$}) + watch(%r{^config/initializers/.+\.rb$}) + watch('Gemfile') + watch('Gemfile.lock') + watch('spec/spec_helper.rb') + watch('test/test_helper.rb') + watch(%r{^spec/support/.+\.rb$}) +end + +guard 'rspec', :version => 2, :cli => '--drb', :all_on_start => false, :all_after_pass => false do + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch('spec/spec_helper.rb') { "spec" } + + # Rails example + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } + watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] } + watch(%r{^spec/support/(.+)\.rb$}) { "spec" } + watch('spec/spec_helper.rb') { "spec" } + watch('config/routes.rb') { "spec/routing" } + watch('app/controllers/application_controller.rb') { "spec/controllers" } + # Capybara request specs + watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" } +end diff --git a/app/controllers/domains_controller.rb b/app/controllers/domains_controller.rb index 2867ab9..91a33f8 100644 --- a/app/controllers/domains_controller.rb +++ b/app/controllers/domains_controller.rb @@ -29,5 +29,15 @@ class DomainsController < ApplicationController def after_create_save(record) @record.reload end - + + def before_update_save(record) # just to make sure of params tampering + record.type = 'NATIVE' + end + + def after_update_save(record) + if @record.nam_changed? + flash[:warning] = "Changing the name of a domain migrates all it's records to the new name!" + end + end + end diff --git a/app/models/domain.rb b/app/models/domain.rb index 2f83eef..28c994f 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -41,6 +41,16 @@ class Domain < ActiveRecord::Base a_records.build(:content => ip) if ip.present? end + before_validation(:on => :update) do + if name_changed? + soa_record.reset_serial + # soa_record.name = soa_record.name.gsub(/#{name_was}$/, name) + for record in records + record.name = record.name.gsub(/#{name_was}$/, name) + end + end + end + def setup(email) build_soa_record soa = soa_record diff --git a/app/models/soa.rb b/app/models/soa.rb index bb4024a..ea793bf 100644 --- a/app/models/soa.rb +++ b/app/models/soa.rb @@ -20,6 +20,7 @@ class SOA < Record before_validation :assemble_content before_update :update_serial after_initialize :disassemble_content + before_validation do self.primary_ns ||= domain.ns_records.first.content end @@ -50,8 +51,12 @@ class SOA < Record # generation, that gets triggered by updating the change_date def update_serial return if self.content_changed? - date_serial = Time.now.strftime( "%Y%m%d00" ).to_i - @serial = (@serial.nil? || date_serial > @serial) ? date_serial : @serial + 1 + compute_serial + end + + def reset_serial + @serial = nil + compute_serial end # Same as #update_serial and saves the record @@ -76,9 +81,12 @@ class SOA < Record end def name_equals_domain_name? - unless name == domain.name - errors.add :name, "must be equal to domain's name" - end + errors.add :name, "must be equal to domain's name" unless name == domain.name + end + + def compute_serial + date_serial = Time.now.strftime( "%Y%m%d00" ).to_i + @serial = (@serial.nil? || date_serial > @serial) ? date_serial : @serial + 1 end end diff --git a/config/database.yml.sample b/config/database.yml.mysql.sample similarity index 59% rename from config/database.yml.sample rename to config/database.yml.mysql.sample index b68f8a3..51f75b1 100644 --- a/config/database.yml.sample +++ b/config/database.yml.mysql.sample @@ -1,22 +1,25 @@ -# SQLite version 3.x -# gem install sqlite3 -# -# Ensure the SQLite 3 gem is defined in your Gemfile -# gem 'sqlite3' development: - adapter: sqlite3 - database: db/development.sqlite3 + adapter: mysql2 + encoding: utf8 + reconnect: false + database: entrydns_development pool: 5 - timeout: 5000 + username: root + password: root + host: localhost # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: - adapter: sqlite3 - database: db/test.sqlite3 + adapter: mysql2 + encoding: utf8 + reconnect: false + database: entrydns_test pool: 5 - timeout: 5000 + username: root + password: root + host: localhost production: adapter: mysql2 diff --git a/db/seeds.rb b/db/seeds.rb index 21b4ed7..bbe05e6 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,4 +1,6 @@ -user = User.create :email => 'user@app.com', +user = User.create( + :email => 'user@app.com', :password => 'useruser', :password_confirmation => 'useruser' +) user.confirm! diff --git a/spec/factories.rb b/spec/factories.rb index c8a6136..ac8b51d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,12 +1,37 @@ FactoryGirl.define do + sequence(:email){|n| "#{Faker::Internet.user_name}#{n}@example.com"} + sequence(:password){|n| "password#{n}"} + sequence(:domain_name){|n| "#{n}#{Faker::Internet.domain_name}"} + factory :user do + email + password + password_confirmation {password} end factory :domain do + name {FactoryGirl.generate(:domain_name)} + type 'NATIVE' + association :soa_record, :factory => :soa_record, :method => :build + ns_records do |ns_records| + ns1 = ns_records.association(:ns_record, :method => :build) + ns2 = ns_records.association(:ns_record, :method => :build) + ns1.content = Settings.ns.first + ns2.content = (Settings.ns - [ns1.content]).sample + [ns1, ns2] + end end factory :record do end + + factory :soa_record, :class => 'SOA' do + contact {Faker::Internet.email} + end + + factory :ns_record, :class => 'NS' do + content {Settings.ns.sample} + end end \ No newline at end of file diff --git a/spec/helpers/so_as_helper_spec.rb b/spec/helpers/soas_helper_spec.rb similarity index 94% rename from spec/helpers/so_as_helper_spec.rb rename to spec/helpers/soas_helper_spec.rb index 5cdd05c..26c9fa6 100644 --- a/spec/helpers/so_as_helper_spec.rb +++ b/spec/helpers/soas_helper_spec.rb @@ -10,6 +10,6 @@ require 'spec_helper' # end # end # end -describe SoAsHelper do +describe SoasHelper do pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/models/domain_spec.rb b/spec/models/domain_spec.rb index 792e493..f1a9aa8 100644 --- a/spec/models/domain_spec.rb +++ b/spec/models/domain_spec.rb @@ -1,5 +1,8 @@ require 'spec_helper' describe Domain do - pending "add some examples to (or delete) #{__FILE__}" + let(:domain){Factory(:domain)} + it "is" do + domain.should be_valid + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a3c1989..38e8077 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,12 +7,10 @@ Spork.prefork do require 'rspec/rails' require 'capybara/rspec' - RSpec.configure do |config| - config.mock_with :rspec - config.use_transactional_fixtures = true - end + Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} + Spork.trap_method(Rails::Application::RoutesReloader, :reload!) end Spork.each_run do - Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} + FactoryGirl.reload end diff --git a/spec/support/rspec.rb b/spec/support/rspec.rb new file mode 100644 index 0000000..15dce4c --- /dev/null +++ b/spec/support/rspec.rb @@ -0,0 +1,8 @@ +RSpec.configure do |config| + config.mock_with :rspec + config.use_transactional_fixtures = true + + config.treat_symbols_as_metadata_keys_with_true_values = true + config.filter_run :focus => true + config.run_all_when_everything_filtered = true +end