From 7cf5b77d31b4dd0714421db7d1d605702eea1677 Mon Sep 17 00:00:00 2001 From: Dan Croak Date: Fri, 20 Feb 2009 16:48:04 -0500 Subject: [PATCH] raising actual ActionController::Forbidden error, test for the raise in functional tests. test for the response code in acceptance test. --- README.textile | 2 +- Rakefile | 2 +- .../templates/features/password_reset.feature | 9 +- .../step_definitions/clearance_steps.rb | 18 +- .../templates/features/support/paths.rb | 1 - lib/clearance.rb | 4 +- .../app/controllers/application_controller.rb | 6 - .../controllers/confirmations_controller.rb | 12 +- .../app/controllers/passwords_controller.rb | 8 +- lib/clearance/lib/extensions/errors.rb | 4 + lib/clearance/lib/extensions/rescue.rb | 1 + .../confirmations_controller_test.rb | 31 +- .../functional/passwords_controller_test.rb | 32 +- shoulda_macros/clearance.rb | 9 +- test/rails_root/config/environments/test.rb | 16 +- .../.specification | 79 --- .../CONTRIBUTION_GUIDELINES.rdoc | 10 - .../thoughtbot-factory_girl-1.1.5/Changelog | 29 - .../thoughtbot-factory_girl-1.1.5/LICENSE | 19 - .../README.textile | 151 ------ .../thoughtbot-factory_girl-1.1.5/Rakefile | 76 --- .../lib/factory_girl.rb | 27 - .../lib/factory_girl/aliases.rb | 39 -- .../lib/factory_girl/attribute.rb | 38 -- .../lib/factory_girl/attribute_proxy.rb | 92 ---- .../lib/factory_girl/factory.rb | 280 ---------- .../lib/factory_girl/sequence.rb | 58 -- .../test/aliases_test.rb | 29 - .../test/attribute_proxy_test.rb | 121 ----- .../test/attribute_test.rb | 70 --- .../test/factory_test.rb | 494 ------------------ .../test/integration_test.rb | 147 ------ .../test/models.rb | 42 -- .../test/sequence_test.rb | 76 --- .../test/test_helper.rb | 10 - 35 files changed, 87 insertions(+), 1955 deletions(-) create mode 100644 lib/clearance/lib/extensions/errors.rb create mode 100644 lib/clearance/lib/extensions/rescue.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/.specification delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/CONTRIBUTION_GUIDELINES.rdoc delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/Changelog delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/LICENSE delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/README.textile delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/Rakefile delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/aliases.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/attribute.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/attribute_proxy.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/factory.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/sequence.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/aliases_test.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/attribute_proxy_test.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/attribute_test.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/factory_test.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/integration_test.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/models.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/sequence_test.rb delete mode 100644 test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/test_helper.rb diff --git a/README.textile b/README.textile index 133942ce8..331a00fb7 100644 --- a/README.textile +++ b/README.textile @@ -26,7 +26,7 @@ In config/environments/test.rb: config.gem 'thoughtbot-factory_girl', :lib => 'factory_girl', :source => "http://gems.github.com", - :version => '>= 1.1.5' + :version => '>= 1.2.0' Then: diff --git a/Rakefile b/Rakefile index 0118c65c2..40e8c5382 100644 --- a/Rakefile +++ b/Rakefile @@ -13,7 +13,7 @@ namespace :test do end Cucumber::Rake::Task.new(:features) do |t| - t.cucumber_opts = "--format progress" + t.cucumber_opts = "--format pretty" t.feature_pattern = 'test/rails_root/features/*.feature' end end diff --git a/generators/clearance_features/templates/features/password_reset.feature b/generators/clearance_features/templates/features/password_reset.feature index 234a67e3a..6ef94c503 100644 --- a/generators/clearance_features/templates/features/password_reset.feature +++ b/generators/clearance_features/templates/features/password_reset.feature @@ -1,4 +1,4 @@ -Fature: Password Reset +Feature: Password Reset In order to sign in even if he forgot his password A user Should be able to reset it @@ -30,3 +30,10 @@ Fature: Password Reset And I sign in as "email@person.com/newpassword" Then I should be signed in + Scenario: User requests password reset without token + Given a user exists with an email of "user@one.com" + When I try to change the password of "user@one.com" without token + Then I should be forbidden + + + diff --git a/generators/clearance_features/templates/features/step_definitions/clearance_steps.rb b/generators/clearance_features/templates/features/step_definitions/clearance_steps.rb index ab902e846..da9c4bde5 100644 --- a/generators/clearance_features/templates/features/step_definitions/clearance_steps.rb +++ b/generators/clearance_features/templates/features/step_definitions/clearance_steps.rb @@ -4,7 +4,13 @@ Then %{I should see "error(s)? prohibited"} end -# DB +# Database + +Factory.factories.each do |name, factory| + Given /^an? #{name} exists with an? (.*) of "([^"]*)"$/ do |attr, value| + Factory(name, attr.gsub(' ', '_') => value) + end +end Given /^there is no user with "(.*)"$/ do |email| assert_nil User.find_by_email(email) @@ -68,6 +74,16 @@ visit edit_user_password_path(:user_id => user, :token => user.token) end +When /^I try to change the password of "(.*)" without token$/ do |email| + user = User.find_by_email(email) + visit edit_user_password_path(:user_id => user) +end + +Then /^I should be forbidden$/ do + assert_response :forbidden +end + + # Actions When /^I sign in( with "remember me")? as "(.*)\/(.*)"$/ do |remember, email, password| diff --git a/generators/clearance_features/templates/features/support/paths.rb b/generators/clearance_features/templates/features/support/paths.rb index 74305ba2c..f501803a4 100644 --- a/generators/clearance_features/templates/features/support/paths.rb +++ b/generators/clearance_features/templates/features/support/paths.rb @@ -10,7 +10,6 @@ def path_to(page_name) new_session_path when /the password reset request page/i new_password_path - # Add more page name => path mappings here diff --git a/lib/clearance.rb b/lib/clearance.rb index 12cd26c24..1e315f57a 100644 --- a/lib/clearance.rb +++ b/lib/clearance.rb @@ -1,5 +1,5 @@ -class Forbidden < Exception; end - +require 'clearance/lib/extensions/errors' +require 'clearance/lib/extensions/rescue' require 'clearance/app/controllers/application_controller' require 'clearance/app/controllers/confirmations_controller' require 'clearance/app/controllers/passwords_controller' diff --git a/lib/clearance/app/controllers/application_controller.rb b/lib/clearance/app/controllers/application_controller.rb index e2bf49cea..9d77dc2cf 100644 --- a/lib/clearance/app/controllers/application_controller.rb +++ b/lib/clearance/app/controllers/application_controller.rb @@ -7,8 +7,6 @@ def self.included(controller) controller.send(:include, InstanceMethods) controller.class_eval do - rescue_from Forbidden, :with => :forbid - helper_method :current_user helper_method :signed_in? @@ -78,10 +76,6 @@ def deny_access(flash_message = nil, opts = {}) flash[:failure] = flash_message if flash_message render :template => "/sessions/new", :status => :unauthorized end - - def forbid - render :nothing => true, :status => :forbidden - end end end diff --git a/lib/clearance/app/controllers/confirmations_controller.rb b/lib/clearance/app/controllers/confirmations_controller.rb index 3e2186964..49287271c 100644 --- a/lib/clearance/app/controllers/confirmations_controller.rb +++ b/lib/clearance/app/controllers/confirmations_controller.rb @@ -35,15 +35,21 @@ module PrivateMethods def forbid_confirmed_user user = User.find_by_id(params[:user_id]) - raise Forbidden if user && user.email_confirmed? + if user && user.email_confirmed? + raise ActionController::Forbidden, "confirmed user" + end end def forbid_missing_token - raise Forbidden if params[:token].blank? + if params[:token].blank? + raise ActionController::Forbidden, "missing token" + end end def forbid_non_existant_user - raise Forbidden unless User.find_by_id_and_token(params[:user_id], params[:token]) + unless User.find_by_id_and_token(params[:user_id], params[:token]) + raise ActionController::Forbidden, "non-existant user" + end end def url_after_create diff --git a/lib/clearance/app/controllers/passwords_controller.rb b/lib/clearance/app/controllers/passwords_controller.rb index 484fa9f17..2425f850c 100644 --- a/lib/clearance/app/controllers/passwords_controller.rb +++ b/lib/clearance/app/controllers/passwords_controller.rb @@ -52,11 +52,15 @@ module PrivateMethods private def forbid_missing_token - raise Forbidden if params[:token].blank? + if params[:token].blank? + raise ActionController::Forbidden, "missing token" + end end def forbid_non_existant_user - raise Forbidden unless User.find_by_id_and_token(params[:user_id], params[:token]) + unless User.find_by_id_and_token(params[:user_id], params[:token]) + raise ActionController::Forbidden, "non-existant user" + end end def url_after_create diff --git a/lib/clearance/lib/extensions/errors.rb b/lib/clearance/lib/extensions/errors.rb new file mode 100644 index 000000000..285722d3d --- /dev/null +++ b/lib/clearance/lib/extensions/errors.rb @@ -0,0 +1,4 @@ +module ActionController + class Forbidden < StandardError + end +end diff --git a/lib/clearance/lib/extensions/rescue.rb b/lib/clearance/lib/extensions/rescue.rb new file mode 100644 index 000000000..efc984ecd --- /dev/null +++ b/lib/clearance/lib/extensions/rescue.rb @@ -0,0 +1 @@ +ActionController::Base.rescue_responses.update('ActionController::Forbidden' => :forbidden) diff --git a/lib/clearance/test/functional/confirmations_controller_test.rb b/lib/clearance/test/functional/confirmations_controller_test.rb index fbc827cc8..c0b06da2f 100644 --- a/lib/clearance/test/functional/confirmations_controller_test.rb +++ b/lib/clearance/test/functional/confirmations_controller_test.rb @@ -27,42 +27,39 @@ def self.included(controller_test) should_redirect_to_url_after_create end - context "on GET to #new with incorrect token" do + context "with an incorrect token" do setup do - bad_token = "bad token" - assert_not_equal bad_token, @user.token - get :new, :user_id => @user.to_param, :token => bad_token + @bad_token = "bad token" + assert_not_equal @bad_token, @user.token end - should_forbid + should_forbid "on GET to #new with incorrect token" do + get :new, :user_id => @user.to_param, :token => @bad_token + end end - context "on GET to #new with blank token" do - setup { get :new, :user_id => @user.to_param, :token => "" } - should_forbid + should_forbid "on GET to #new with blank token" do + get :new, :user_id => @user.to_param, :token => "" end - context "on GET to #new with no token" do - setup { get :new, :user_id => @user.to_param } - should_forbid + should_forbid "on GET to #new with no token" do + get :new, :user_id => @user.to_param end end context "a user with email confirmed" do setup { @user = Factory(:email_confirmed_user) } - context "on GET to #new with correct id" do - setup { get :new, :user_id => @user.to_param } - should_forbid + should_forbid "on GET to #new with correct id" do + get :new, :user_id => @user.to_param end end context "no users" do setup { assert_equal 0, User.count } - context "on GET to #new with nonexistent id and token" do - setup { get :new, :user_id => '123', :token => '123' } - should_forbid + should_forbid "on GET to #new with nonexistent id and token" do + get :new, :user_id => '123', :token => '123' end end diff --git a/lib/clearance/test/functional/passwords_controller_test.rb b/lib/clearance/test/functional/passwords_controller_test.rb index e8f6f894b..2149dcd94 100644 --- a/lib/clearance/test/functional/passwords_controller_test.rb +++ b/lib/clearance/test/functional/passwords_controller_test.rb @@ -90,14 +90,12 @@ def self.included(controller_test) should_display_a_password_update_form end - context "on GET to #edit with correct id but blank token" do - setup { get :edit, :user_id => @user.to_param, :token => "" } - should_forbid + should_forbid "on GET to #edit with correct id but blank token" do + get :edit, :user_id => @user.to_param, :token => "" end - context "on GET to #edit with correct id but no token" do - setup { get :edit, :user_id => @user.to_param } - should_forbid + should_forbid "on GET to #edit with correct id but no token" do + get :edit, :user_id => @user.to_param end context "on PUT to #update with matching password and password confirmation" do @@ -158,30 +156,20 @@ def self.included(controller_test) should_display_a_password_update_form end - context "on PUT to #update with id but no token" do - setup { put :update, :user_id => @user.to_param, :token => "" } - - should "not update password" do - assert_not_equal @encrypted_new_password, @user.encrypted_password - end - - should_forbid + should_forbid "on PUT to #update with id but no token" do + put :update, :user_id => @user.to_param, :token => "" end end - context "given two users" do + context "given two users and user one signs in" do setup do @user_one = Factory(:user) @user_two = Factory(:user) + sign_in_as @user_one end - context "when user one signs in" do - setup { sign_in_as @user_one } - - context "and tries to change user two's password" do - setup { get :edit, :user_id => @user_two.to_param } - should_forbid - end + should_forbid "when user one tries to change user two's password on GET with no token" do + get :edit, :user_id => @user_two.to_param end end end diff --git a/shoulda_macros/clearance.rb b/shoulda_macros/clearance.rb index 002f7d1c9..05e9b011e 100644 --- a/shoulda_macros/clearance.rb +++ b/shoulda_macros/clearance.rb @@ -55,9 +55,12 @@ def should_deny_access(opts = {}) # HTTP FLUENCY - def should_forbid - should_respond_with :forbidden - should_render_nothing + def should_forbid(description, &block) + should "forbid #{description}" do + assert_raises ActionController::Forbidden do + instance_eval(&block) + end + end end # CONTEXTS diff --git a/test/rails_root/config/environments/test.rb b/test/rails_root/config/environments/test.rb index aba8f5b83..5cebf1244 100644 --- a/test/rails_root/config/environments/test.rb +++ b/test/rails_root/config/environments/test.rb @@ -23,11 +23,11 @@ HOST = "localhost" - config.gem 'thoughtbot-shoulda', - :lib => 'shoulda', - :source => "http://gems.github.com", - :version => '>= 2.9.1' - config.gem 'thoughtbot-factory_girl', - :lib => 'factory_girl', - :source => "http://gems.github.com", - :version => '>= 1.1.5' +config.gem 'thoughtbot-shoulda', + :lib => 'shoulda', + :source => "http://gems.github.com", + :version => '>= 2.9.1' +config.gem 'thoughtbot-factory_girl', + :lib => 'factory_girl', + :source => "http://gems.github.com", + :version => '>= 1.2.0' diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/.specification b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/.specification deleted file mode 100644 index bea58e865..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/.specification +++ /dev/null @@ -1,79 +0,0 @@ ---- !ruby/object:Gem::Specification -name: thoughtbot-factory_girl -version: !ruby/object:Gem::Version - version: 1.1.5 -platform: ruby -authors: -- Joe Ferris -autorequire: -bindir: bin -cert_chain: [] - -date: 2008-12-11 00:00:00 -05:00 -default_executable: -dependencies: [] - -description: factory_girl provides a framework and DSL for defining and using factories - less error-prone, more explicit, and all-around easier to work with than fixtures. -email: jferris@thoughtbot.com -executables: [] - -extensions: [] - -extra_rdoc_files: -- README.textile -files: -- Changelog -- CONTRIBUTION_GUIDELINES.rdoc -- LICENSE -- Rakefile -- README.textile -- lib/factory_girl/aliases.rb -- lib/factory_girl/attribute.rb -- lib/factory_girl/attribute_proxy.rb -- lib/factory_girl/factory.rb -- lib/factory_girl/sequence.rb -- lib/factory_girl.rb -- test/aliases_test.rb -- test/attribute_proxy_test.rb -- test/attribute_test.rb -- test/factory_test.rb -- test/integration_test.rb -- test/models.rb -- test/sequence_test.rb -- test/test_helper.rb -has_rdoc: true -homepage: -post_install_message: -rdoc_options: -- --line-numbers -- --inline-source -- --main -- README.textile -require_paths: -- lib -required_ruby_version: !ruby/object:Gem::Requirement - requirements: - - - ">=" - - !ruby/object:Gem::Version - version: "0" - version: -required_rubygems_version: !ruby/object:Gem::Requirement - requirements: - - - ">=" - - !ruby/object:Gem::Version - version: "0" - version: -requirements: [] - -rubyforge_project: -rubygems_version: 1.3.1 -signing_key: -specification_version: 2 -summary: factory_girl provides a framework and DSL for defining and using model instance factories. -test_files: -- test/aliases_test.rb -- test/attribute_proxy_test.rb -- test/attribute_test.rb -- test/factory_test.rb -- test/integration_test.rb -- test/sequence_test.rb diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/CONTRIBUTION_GUIDELINES.rdoc b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/CONTRIBUTION_GUIDELINES.rdoc deleted file mode 100644 index a1778f2d9..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/CONTRIBUTION_GUIDELINES.rdoc +++ /dev/null @@ -1,10 +0,0 @@ -We're using GitHub[http://github.com/thoughtbot/factory_girl/tree/master] and Lighthouse[http://thoughtbot.lighthouseapp.com/projects/14354], and we've been getting any combination of github pull requests, Lighthouse tickets, patches, emails, etc. We need to normalize this workflow to make sure we don't miss any fixes. - -* Make sure you're accessing the source from the {official repository}[http://github.com/thoughtbot/factory_girl/tree/master]. -* We prefer git branches over patches, but we can take either. -* If you're using git, please make a branch for each separate contribution. We can cherry pick your commits, but pulling from a branch is easier. -* If you're submitting patches, please cut each fix or feature into a separate patch. -* There should be a Lighthouse[http://thoughtbot.lighthouseapp.com/projects/14354] ticket for any submission. If you've found a bug and want to fix it, open a new ticket at the same time. -* Please don't send pull requests Just update the lighthouse ticket with the url for your fix (or attach the patch) when it's ready. The github pull requests pretty much get dropped on the floor until someone with commit rights notices them in the mailbox. -* Contributions without tests won't be accepted. -* Please don't submit patches or branches that update the Gem version. diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/Changelog b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/Changelog deleted file mode 100644 index d775959e3..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/Changelog +++ /dev/null @@ -1,29 +0,0 @@ -1.1.4 (November 28, 2008) - Factory.build now uses Factory.create for associations of the built object - Factory definitions are now detected in subdirectories, such as - factories/person_factory.rb (thanks to Josh Nichols) - Factory definitions are now loaded after the environment in a Rails project - (fixes some issues with dependencies being loaded too early) (thanks to - Josh Nichols) - Factory names ending in 's' no longer cause problems (thanks to Alex Sharp - and Josh Owens) - -1.1.3 (September 12, 2008) - Automatically pull in definitions from factories.rb, test/factories.rb, or - spec/factories.rb -1.1.2 (July 30, 2008) - Improved error handling for invalid and undefined factories/attributes - Improved handling of strings vs symbols vs classes - Added a prettier syntax for handling associations - Updated documentation and fixed compatibility with Rails 2.1 - -1.1.1 (June 23, 2008) - The attribute "name" no longer requires using #add_attribute - -1.1.0 (June 3, 2008) - Added support for dependent attributes - Fixed the attributes_for build strategy to not build associations - Added support for sequences - -1.0.0 (May 31, 208) - First version diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/LICENSE b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/LICENSE deleted file mode 100644 index 182911bea..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -Copyright (c) 2008 Joe Ferris and thoughtbot, inc. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/README.textile b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/README.textile deleted file mode 100644 index b6372d18c..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/README.textile +++ /dev/null @@ -1,151 +0,0 @@ -h1. factory_girl - -Written by "Joe Ferris":mailto:jferris@thoughtbot.com. - -Thanks to Tammer Saleh, Dan Croak, and Jon Yurek of thoughtbot, inc. - -Copyright 2008 Joe Ferris and thoughtbot, inc. - -h2. Download - -Github: "Page":http://github.com/thoughtbot/factory_girl/tree/master "Clone":git://github.com/thoughtbot/factory_girl.git - -Gem:
gem install thoughtbot-factory_girl --source http://gems.github.com
- -Note: if you install factory_girl using the gem from Github, you'll need this -in your environment.rb if you want to use Rails 2.1's dependency manager: - -config.gem "thoughtbot-factory_girl", - :lib => "factory_girl", - :source => "http://gems.github.com" - -h2. Contributing - -Please read the contribution guidelines before submitting patches or pull requests. - -h2. Defining factories - -
# This will guess the User class
-Factory.define :user do |u|
-  u.first_name 'John'
-  u.last_name  'Doe'
-  u.admin false
-end
-
-# This will use the User class (Admin would have been guessed)
-Factory.define :admin, :class => User do |u|
-  u.first_name 'Admin'
-  u.last_name  'User'
-  u.admin true
-end
- - -It is recommended that you create a test/factories.rb file and define your -factories there. This file can be included from test_helper or directly from -your test files. Don't forget: -
require 'factory_girl'
- - -h2. Lazy Attributes - -Most attributes can be added using static values that are evaluated when the -factory is defined, but some attributes (such as associations and other -attributes that must be dynamically generated) will need values assigned each -time an instance is generated. These "lazy" attributes can be added by passing -a block instead of a parameter: - -
Factory.define :user do |u|
-  # ...
-  u.activation_code { User.generate_activation_code }
-end
- - -h2. Dependent Attributes - -Some attributes may need to be generated based on the values of other -attributes. This can be done by calling the attribute name on -Factory::AttributeProxy, which is yielded to lazy attribute blocks: - -
Factory.define :user do |u|
-  u.first_name 'Joe'
-  u.last_name  'Blow'
-  u.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
-end
-
-Factory(:user, :last_name => 'Doe').email
-# => "joe.doe@example.com"
- - -h2. Associations - -Associated instances can be generated by using the association method when -defining a lazy attribute: - -
Factory.define :post do |p|
-  # ...
-  p.author {|author| author.association(:user, :last_name => 'Writely') }
-end
- - -When using the association method, the same build strategy (build, create, or attributes_for) will be used for all generated instances: - -
# Builds and saves a User and a Post
-post = Factory(:post)
-post.new_record?       # => false
-post.author.new_record # => false
-
-# Builds but does not save a User and a Post
-Factory.build(:post)
-post.new_record?       # => true
-post.author.new_record # => true
- -Because this pattern is so common, a prettier syntax is available for defining -associations: - -
# The following definitions are equivilent:
-Factory.define :post do |p|
-  p.author {|a| a.association(:user) }
-end
-
-Factory.define :post do |p|
-  p.association :author, :factory => :user
-end
- -If the factory name is the same as the association name, the factory name can -be left out. - - -h2. Sequences - -Unique values in a specific format (for example, e-mail addresses) can be -generated using sequences. Sequences are defined by calling Factory.sequence, -and values in a sequence are generated by calling Factory.next: - -
# Defines a new sequence
-Factory.sequence :email do |n|
-  "person#{n}@example.com"
-end
-
-Factory.next :email
-# => "person1@example.com"
-
-Factory.next :email
-# => "person2@example.com"
- - -h2. Using factories - -
# Build and save a User instance
-Factory(:user)
-
-# Build a User instance and override the first_name property
-Factory.build(:user, :first_name => 'Joe')
-
-# Return an attributes Hash that can be used to build a User instance
-attrs = Factory.attributes_for(:user)
- -h2. More Information - -"Our blog":http://giantrobots.thoughtbot.com - -"factory_girl rdoc":http://dev.thoughtbot.com/factory_girl diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/Rakefile b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/Rakefile deleted file mode 100644 index adf6d25ff..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/Rakefile +++ /dev/null @@ -1,76 +0,0 @@ -require 'rubygems' -require 'rake' -require 'rake/testtask' -require 'rake/rdoctask' -require 'rake/gempackagetask' -require 'rcov/rcovtask' -require 'date' - -desc 'Default: run unit tests.' -task :default => :test - -desc 'Test the factory_girl plugin.' -Rake::TestTask.new(:test) do |t| - t.libs << 'lib' - t.pattern = 'test/**/*_test.rb' - t.verbose = true -end - -desc 'Performs code coverage on the factory_girl plugin.' -Rcov::RcovTask.new do |t| - t.libs << "test" - t.test_files = FileList['test/*_test.rb'] - t.verbose = true -end - -desc 'Generate documentation for the factory_girl plugin.' -Rake::RDocTask.new(:rdoc) do |rdoc| - rdoc.rdoc_dir = 'rdoc' - rdoc.title = 'Factory Girl' - rdoc.options << '--line-numbers' << '--inline-source' << "--main" << "README.textile" - rdoc.rdoc_files.include('README.textile') - rdoc.rdoc_files.include('lib/**/*.rb') -end - -desc 'Update documentation on website' -task :sync_docs => 'rdoc' do - `rsync -ave ssh rdoc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/factory_girl` -end - -spec = Gem::Specification.new do |s| - s.name = %q{factory_girl} - s.version = "1.1.5" - s.summary = %q{factory_girl provides a framework and DSL for defining and - using model instance factories.} - s.description = %q{factory_girl provides a framework and DSL for defining and - using factories - less error-prone, more explicit, and - all-around easier to work with than fixtures.} - - s.files = FileList['[A-Z]*', 'lib/**/*.rb', 'test/**/*.rb'] - s.require_path = 'lib' - s.test_files = Dir[*['test/**/*_test.rb']] - - s.has_rdoc = true - s.extra_rdoc_files = ["README.textile"] - s.rdoc_options = ['--line-numbers', '--inline-source', "--main", "README.textile"] - - s.authors = ["Joe Ferris"] - s.email = %q{jferris@thoughtbot.com} - - s.platform = Gem::Platform::RUBY -end - -Rake::GemPackageTask.new spec do |pkg| - pkg.need_tar = true - pkg.need_zip = true -end - -desc "Clean files generated by rake tasks" -task :clobber => [:clobber_rdoc, :clobber_package] - -desc "Generate a gemspec file" -task :gemspec do - File.open("#{spec.name}.gemspec", 'w') do |f| - f.write spec.to_ruby - end -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl.rb deleted file mode 100644 index 248171625..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'active_support' -require 'factory_girl/factory' -require 'factory_girl/attribute_proxy' -require 'factory_girl/attribute' -require 'factory_girl/sequence' -require 'factory_girl/aliases' - -# Shortcut for Factory.create. -# -# Example: -# Factory(:user, :name => 'Joe') -def Factory (name, attrs = {}) - Factory.create(name, attrs) -end - -if defined? Rails - Rails.configuration.after_initialize do - Factory.definition_file_paths = [ - File.join(RAILS_ROOT, 'test', 'factories'), - File.join(RAILS_ROOT, 'spec', 'factories') - ] - Factory.find_definitions - end -else - Factory.find_definitions -end - diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/aliases.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/aliases.rb deleted file mode 100644 index c9bd9c719..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/aliases.rb +++ /dev/null @@ -1,39 +0,0 @@ -class Factory - - class << self - attr_accessor :aliases #:nodoc: - end - self.aliases = [ - [/(.*)_id/, '\1'], - [/(.*)/, '\1_id'] - ] - - # Defines a new alias for attributes - # - # Arguments: - # pattern: (Regexp) - # A pattern that will be matched against attributes when looking for - # aliases. Contents captured in the pattern can be used in the alias. - # replace: (String) - # The alias that results from the matched pattern. Captured strings can - # be insert like String#sub. - # - # Example: - # - # Factory.alias /(.*)_confirmation/, '\1' - def self.alias (pattern, replace) - self.aliases << [pattern, replace] - end - - def self.aliases_for (attribute) #:nodoc: - aliases.collect do |params| - pattern, replace = *params - if pattern.match(attribute.to_s) - attribute.to_s.sub(pattern, replace).to_sym - else - nil - end - end.compact << attribute - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/attribute.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/attribute.rb deleted file mode 100644 index 7079eaaf9..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/attribute.rb +++ /dev/null @@ -1,38 +0,0 @@ -class Factory - - class AttributeDefinitionError < RuntimeError - end - - class Attribute #:nodoc: - - attr_reader :name - - def initialize (name, static_value, lazy_block) - name = name.to_sym - - if name.to_s =~ /=$/ - raise AttributeDefinitionError, - "factory_girl uses 'f.#{name.to_s.chop} value' syntax " + - "rather than 'f.#{name} = value'" - end - - unless static_value.nil? || lazy_block.nil? - raise AttributeDefinitionError, "Both value and block given" - end - - @name = name - @static_value = static_value - @lazy_block = lazy_block - end - - def value (proxy) - if @lazy_block.nil? - @static_value - else - @lazy_block.call(proxy) - end - end - - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/attribute_proxy.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/attribute_proxy.rb deleted file mode 100644 index c98d45978..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/attribute_proxy.rb +++ /dev/null @@ -1,92 +0,0 @@ -class Factory - - class AttributeProxy - - attr_accessor :factory, :attribute_name, :strategy, :current_values #:nodoc: - - def initialize (factory, attr, strategy, values) #:nodoc: - @factory = factory - @attribute_name = attr - @strategy = strategy - @current_values = values - end - - # Generates an association using the current build strategy. - # - # Arguments: - # name: (Symbol) - # The name of the factory that should be used to generate this - # association. - # attributes: (Hash) - # A hash of attributes that should be overridden for this association. - # - # Returns: - # The generated association for the current build strategy. Note that - # assocaitions are not generated for the attributes_for strategy. Returns - # nil in this case. - # - # Example: - # - # Factory.define :user do |f| - # # ... - # end - # - # Factory.define :post do |f| - # # ... - # f.author {|a| a.association :user, :name => 'Joe' } - # end - # - # # Builds (but doesn't save) a Post and a User - # Factory.build(:post) - # - # # Builds and saves a User, builds a Post, assigns the User to the - # # author association, and saves the User. - # Factory.create(:post) - # - def association (name, attributes = {}) - if strategy == :attributes_for - nil - else - Factory.create(name, attributes) - end - end - - # Returns the value for specified attribute. A value will only be available - # if it was overridden when calling the factory, or if a value is added - # earlier in the definition of the factory. - # - # Arguments: - # attribute: (Symbol) - # The attribute whose value should be returned. - # - # Returns: - # The value of the requested attribute. (Object) - def value_for (attribute) - unless current_values.key?(attribute) - raise ArgumentError, "No such attribute: #{attribute.inspect}" - end - current_values[attribute] - end - - # Undefined methods are delegated to value_for, which means that: - # - # Factory.define :user do |f| - # f.first_name 'Ben' - # f.last_name {|a| a.value_for(:first_name) } - # end - # - # and: - # - # Factory.define :user do |f| - # f.first_name 'Ben' - # f.last_name {|a| a.first_name } - # end - # - # are equivilent. - def method_missing (name, *args, &block) - current_values[name] - end - - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/factory.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/factory.rb deleted file mode 100644 index 0749c4f4f..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/factory.rb +++ /dev/null @@ -1,280 +0,0 @@ -class Factory - - class << self - attr_accessor :factories #:nodoc: - - # An Array of strings specifying locations that should be searched for - # factory definitions. By default, factory_girl will attempt to require - # "factories," "test/factories," and "spec/factories." Only the first - # existing file will be loaded. - attr_accessor :definition_file_paths - end - - self.factories = {} - self.definition_file_paths = %w(factories test/factories spec/factories) - - attr_reader :factory_name - - # Defines a new factory that can be used by the build strategies (create and - # build) to build new objects. - # - # Arguments: - # name: (Symbol) - # A unique name used to identify this factory. - # options: (Hash) - # class: the class that will be used when generating instances for this - # factory. If not specified, the class will be guessed from the - # factory name. - # - # Yields: - # The newly created factory (Factory) - def self.define (name, options = {}) - instance = Factory.new(name, options) - yield(instance) - self.factories[instance.factory_name] = instance - end - - def build_class #:nodoc: - @build_class ||= class_for(@options[:class] || factory_name) - end - - def initialize (name, options = {}) #:nodoc: - assert_valid_options(options) - @factory_name = factory_name_for(name) - @options = options - @attributes = [] - end - - # Adds an attribute that should be assigned on generated instances for this - # factory. - # - # This method should be called with either a value or block, but not both. If - # called with a block, the attribute will be generated "lazily," whenever an - # instance is generated. Lazy attribute blocks will not be called if that - # attribute is overriden for a specific instance. - # - # When defining lazy attributes, an instance of Factory::AttributeProxy will - # be yielded, allowing associations to be built using the correct build - # strategy. - # - # Arguments: - # name: (Symbol) - # The name of this attribute. This will be assigned using :"#{name}=" for - # generated instances. - # value: (Object) - # If no block is given, this value will be used for this attribute. - def add_attribute (name, value = nil, &block) - attribute = Attribute.new(name, value, block) - - if attribute_defined?(attribute.name) - raise AttributeDefinitionError, "Attribute already defined: #{name}" - end - - @attributes << attribute - end - - # Calls add_attribute using the missing method name as the name of the - # attribute, so that: - # - # Factory.define :user do |f| - # f.name 'Billy Idol' - # end - # - # and: - # - # Factory.define :user do |f| - # f.add_attribute :name, 'Billy Idol' - # end - # - # are equivilent. - def method_missing (name, *args, &block) - add_attribute(name, *args, &block) - end - - # Adds an attribute that builds an association. The associated instance will - # be built using the same build strategy as the parent instance. - # - # Example: - # Factory.define :user do |f| - # f.name 'Joey' - # end - # - # Factory.define :post do |f| - # f.association :author, :factory => :user - # end - # - # Arguments: - # name: (Symbol) - # The name of this attribute. - # options: (Hash) - # factory: (Symbol) - # The name of the factory to use when building the associated instance. - # If no name is given, the name of the attribute is assumed to be the - # name of the factory. For example, a "user" association will by - # default use the "user" factory. - def association (name, options = {}) - name = name.to_sym - options = symbolize_keys(options) - association_factory = options[:factory] || name - - add_attribute(name) {|a| a.association(association_factory) } - end - - def attributes_for (attrs = {}) #:nodoc: - build_attributes_hash(attrs, :attributes_for) - end - - def build (attrs = {}) #:nodoc: - build_instance(attrs, :build) - end - - def create (attrs = {}) #:nodoc: - instance = build_instance(attrs, :create) - instance.save! - instance - end - - class << self - - # Generates and returns a Hash of attributes from this factory. Attributes - # can be individually overridden by passing in a Hash of attribute => value - # pairs. - # - # Arguments: - # attrs: (Hash) - # Attributes to overwrite for this set. - # - # Returns: - # A set of attributes that can be used to build an instance of the class - # this factory generates. (Hash) - def attributes_for (name, attrs = {}) - factory_by_name(name).attributes_for(attrs) - end - - # Generates and returns an instance from this factory. Attributes can be - # individually overridden by passing in a Hash of attribute => value pairs. - # - # Arguments: - # attrs: (Hash) - # See attributes_for - # - # Returns: - # An instance of the class this factory generates, with generated - # attributes assigned. - def build (name, attrs = {}) - factory_by_name(name).build(attrs) - end - - # Generates, saves, and returns an instance from this factory. Attributes can - # be individually overridden by passing in a Hash of attribute => value - # pairs. - # - # If the instance is not valid, an ActiveRecord::Invalid exception will be - # raised. - # - # Arguments: - # attrs: (Hash) - # See attributes_for - # - # Returns: - # A saved instance of the class this factory generates, with generated - # attributes assigned. - def create (name, attrs = {}) - factory_by_name(name).create(attrs) - end - - def find_definitions #:nodoc: - definition_file_paths.each do |path| - require("#{path}.rb") if File.exists?("#{path}.rb") - - if File.directory? path - Dir[File.join(path, '*.rb')].each do |file| - require file - end - end - end - end - - private - - def factory_by_name (name) - factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}") - end - - end - - private - - def build_attributes_hash (values, strategy) - values = symbolize_keys(values) - passed_keys = values.keys.collect {|key| Factory.aliases_for(key) }.flatten - @attributes.each do |attribute| - unless passed_keys.include?(attribute.name) - proxy = AttributeProxy.new(self, attribute.name, strategy, values) - values[attribute.name] = attribute.value(proxy) - end - end - values - end - - def build_instance (override, strategy) - instance = build_class.new - attrs = build_attributes_hash(override, strategy) - attrs.each do |attr, value| - instance.send(:"#{attr}=", value) - end - instance - end - - def class_for (class_or_to_s) - if class_or_to_s.respond_to?(:to_sym) - Object.const_get(variable_name_to_class_name(class_or_to_s)) - else - class_or_to_s - end - end - - def factory_name_for (class_or_to_s) - if class_or_to_s.respond_to?(:to_sym) - class_or_to_s.to_sym - else - class_name_to_variable_name(class_or_to_s).to_sym - end - end - - def attribute_defined? (name) - !@attributes.detect {|attr| attr.name == name }.nil? - end - - def assert_valid_options(options) - invalid_keys = options.keys - [:class] - unless invalid_keys == [] - raise ArgumentError, "Unknown arguments: #{invalid_keys.inspect}" - end - end - - # Based on ActiveSupport's underscore inflector - def class_name_to_variable_name(name) - name.to_s.gsub(/::/, '/'). - gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). - gsub(/([a-z\d])([A-Z])/,'\1_\2'). - tr("-", "_"). - downcase - end - - # Based on ActiveSupport's camelize inflector - def variable_name_to_class_name(name) - name.to_s. - gsub(/\/(.?)/) { "::#{$1.upcase}" }. - gsub(/(?:^|_)(.)/) { $1.upcase } - end - - # From ActiveSupport - def symbolize_keys(hash) - hash.inject({}) do |options, (key, value)| - options[(key.to_sym rescue key) || key] = value - options - end - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/sequence.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/sequence.rb deleted file mode 100644 index cccbe7bcf..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/lib/factory_girl/sequence.rb +++ /dev/null @@ -1,58 +0,0 @@ -class Factory - - class Sequence - - def initialize (&proc) #:nodoc: - @proc = proc - @value = 0 - end - - # Returns the next value for this sequence - def next - @value += 1 - @proc.call(@value) - end - - end - - class << self - attr_accessor :sequences #:nodoc: - end - self.sequences = {} - - # Defines a new sequence that can be used to generate unique values in a specific format. - # - # Arguments: - # name: (Symbol) - # A unique name for this sequence. This name will be referenced when - # calling next to generate new values from this sequence. - # block: (Proc) - # The code to generate each value in the sequence. This block will be - # called with a unique number each time a value in the sequence is to be - # generated. The block should return the generated value for the - # sequence. - # - # Example: - # - # Factory.sequence(:email) {|n| "somebody_#{n}@example.com" } - def self.sequence (name, &block) - self.sequences[name] = Sequence.new(&block) - end - - # Generates and returns the next value in a sequence. - # - # Arguments: - # name: (Symbol) - # The name of the sequence that a value should be generated for. - # - # Returns: - # The next value in the sequence. (Object) - def self.next (sequence) - unless self.sequences.key?(sequence) - raise "No such sequence: #{sequence}" - end - - self.sequences[sequence].next - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/aliases_test.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/aliases_test.rb deleted file mode 100644 index b6c0d50f2..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/aliases_test.rb +++ /dev/null @@ -1,29 +0,0 @@ -require(File.join(File.dirname(__FILE__), 'test_helper')) - -class AliasesTest < Test::Unit::TestCase - - should "include an attribute as an alias for itself by default" do - assert Factory.aliases_for(:test).include?(:test) - end - - should "include the root of a foreign key as an alias by default" do - assert Factory.aliases_for(:test_id).include?(:test) - end - - should "include an attribute's foreign key as an alias by default" do - assert Factory.aliases_for(:test).include?(:test_id) - end - - context "after adding an alias" do - - setup do - Factory.alias(/(.*)_suffix/, '\1') - end - - should "return the alias in the aliases list" do - assert Factory.aliases_for(:test_suffix).include?(:test) - end - - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/attribute_proxy_test.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/attribute_proxy_test.rb deleted file mode 100644 index d392c07d4..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/attribute_proxy_test.rb +++ /dev/null @@ -1,121 +0,0 @@ -require(File.join(File.dirname(__FILE__), 'test_helper')) - -class AttributeProxyTest < Test::Unit::TestCase - - context "an association proxy" do - - setup do - @factory = mock('factory') - @attr = :user - @attrs = { :first_name => 'John' } - @strategy = :create - @proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs) - end - - should "have a factory" do - assert_equal @factory, @proxy.factory - end - - should "have an attribute name" do - assert_equal @attr, @proxy.attribute_name - end - - should "have a build strategy" do - assert_equal @strategy, @proxy.strategy - end - - should "have attributes" do - assert_equal @attrs, @proxy.current_values - end - - context "building an association" do - - setup do - @association = mock('built-user') - @name = :user - @attribs = { :first_name => 'Billy' } - - Factory.stubs(@strategy).returns(@association) - end - - should "delegate to the appropriate method on Factory" do - Factory.expects(@strategy).with(@name, @attribs).returns(@association) - @proxy.association(@name, @attribs) - end - - should "return the built association" do - assert_equal @association, @proxy.association(@name) - end - - end - - context "building an association using the attributes_for strategy" do - - setup do - @strategy = :attributes_for - @proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs) - end - - should "not build the association" do - Factory.expects(@strategy).never - @proxy.association(:user) - end - - should "return nil for the association" do - Factory.stubs(@strategy).returns(:user) - assert_nil @proxy.association(:user) - end - - end - - context "building an association using the build strategy" do - - setup do - @strategy = :build - @built = 'object' - @proxy = Factory::AttributeProxy.new(@factory, @attr, @strategy, @attrs) - Factory.stubs(:create).returns(@built) - end - - should "create the association" do - Factory.expects(:create).with(:user, {}).returns(@built) - @proxy.association(:user) - end - - should "return the created object" do - assert_equal @built, @proxy.association(:user) - end - - end - - context "fetching the value of an attribute" do - - setup do - @attr = :first_name - end - - should "return the correct value" do - assert_equal @attrs[@attr], @proxy.value_for(@attr) - end - - should "call value_for for undefined methods" do - assert_equal @attrs[@attr], @proxy.send(@attr) - end - - end - - context "fetching the value of an undefined attribute" do - - setup do - @attr = :beachball - end - - should "raise an ArgumentError" do - assert_raise(ArgumentError) { @proxy.value_for(@attr) } - end - - end - - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/attribute_test.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/attribute_test.rb deleted file mode 100644 index 1805d2df5..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/attribute_test.rb +++ /dev/null @@ -1,70 +0,0 @@ -require(File.join(File.dirname(__FILE__), 'test_helper')) - -class AttributeTest < Test::Unit::TestCase - - def setup - @proxy = mock('attribute-proxy') - end - - context "an attribute" do - - setup do - @name = :user - @attr = Factory::Attribute.new(@name, 'test', nil) - end - - should "have a name" do - assert_equal @name, @attr.name - end - - end - - context "an attribute with a static value" do - - setup do - @value = 'test' - @attr = Factory::Attribute.new(:user, @value, nil) - end - - should "return the value" do - assert_equal @value, @attr.value(@proxy) - end - - end - - context "an attribute with a lazy value" do - - setup do - @block = lambda { 'value' } - @attr = Factory::Attribute.new(:user, nil, @block) - end - - should "call the block to return a value" do - assert_equal 'value', @attr.value(@proxy) - end - - should "yield the attribute proxy to the block" do - @block = lambda {|a| a } - @attr = Factory::Attribute.new(:user, nil, @block) - assert_equal @proxy, @attr.value(@proxy) - end - - end - - should "raise an error when defining an attribute writer" do - assert_raise Factory::AttributeDefinitionError do - Factory::Attribute.new('test=', nil, nil) - end - end - - should "not allow attributes to be added with both a value parameter and a block" do - assert_raise(Factory::AttributeDefinitionError) do - Factory::Attribute.new(:name, 'value', lambda {}) - end - end - - should "convert names to symbols" do - assert_equal :name, Factory::Attribute.new('name', nil, nil).name - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/factory_test.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/factory_test.rb deleted file mode 100644 index 8e3bbf782..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/factory_test.rb +++ /dev/null @@ -1,494 +0,0 @@ -require(File.join(File.dirname(__FILE__), 'test_helper')) - -class FactoryTest < Test::Unit::TestCase - - def self.should_instantiate_class - - should "instantiate the build class" do - assert_kind_of @class, @instance - end - - should "assign attributes on the instance" do - assert_equal @first_name, @instance.first_name - assert_equal @last_name, @instance.last_name - end - - should "override attributes using the passed hash" do - @value = 'Davis' - @instance = @factory.build(:first_name => @value) - assert_equal @value, @instance.first_name - end - - end - - context "defining a factory" do - - setup do - @name = :user - @factory = mock('factory') - @factory.stubs(:factory_name).returns(@name) - @options = { :class => 'magic' } - Factory.stubs(:new).returns(@factory) - end - - should "create a new factory using the specified name and options" do - Factory.expects(:new).with(@name, @options).returns(@factory) - Factory.define(@name, @options) {|f| } - end - - should "pass the factory do the block" do - yielded = nil - Factory.define(@name) do |y| - yielded = y - end - assert_equal @factory, yielded - end - - should "add the factory to the list of factories" do - Factory.define(@name) {|f| } - assert_equal Factory.factories[@name], - @factory, - "Factories: #{Factory.factories.inspect}" - end - - end - - context "a factory" do - - setup do - @name = :user - @class = User - @factory = Factory.new(@name) - end - - should "have a factory name" do - assert_equal @name, @factory.factory_name - end - - should "have a build class" do - assert_equal @class, @factory.build_class - end - - should "not allow the same attribute to be added twice" do - assert_raise(Factory::AttributeDefinitionError) do - 2.times { @factory.add_attribute @name } - end - end - - context "when adding an attribute with a value parameter" do - - setup do - @attr = :name - @value = 'Elvis lives!' - @factory.add_attribute(@attr, @value) - end - - should "include that value in the generated attributes hash" do - assert_equal @value, @factory.attributes_for[@attr] - end - - end - - context "when adding an attribute with a block" do - - setup do - @attr = :name - @attrs = {} - @proxy = mock('attr-proxy') - Factory::AttributeProxy.stubs(:new).returns(@proxy) - end - - should "not evaluate the block when the attribute is added" do - @factory.add_attribute(@attr) { flunk } - end - - should "evaluate the block when attributes are generated" do - called = false - @factory.add_attribute(@attr) do - called = true - end - @factory.attributes_for - assert called - end - - should "use the result of the block as the value of the attribute" do - value = "Watch out for snakes!" - @factory.add_attribute(@attr) { value } - assert_equal value, @factory.attributes_for[@attr] - end - - should "build an attribute proxy" do - Factory::AttributeProxy.expects(:new).with(@factory, @attr, :attributes_for, @attrs) - @factory.add_attribute(@attr) {} - @factory.attributes_for - end - - should "yield an attribute proxy to the block" do - yielded = nil - @factory.add_attribute(@attr) {|y| yielded = y } - @factory.attributes_for - assert_equal @proxy, yielded - end - - context "when other attributes have previously been defined" do - - setup do - @attr = :unimportant - @attrs = { - :one => 'whatever', - :another => 'soup' - } - @factory.add_attribute(:one, 'whatever') - @factory.add_attribute(:another) { 'soup' } - @factory.add_attribute(@attr) {} - end - - should "provide previously set attributes" do - Factory::AttributeProxy.expects(:new).with(@factory, @attr, :attributes_for, @attrs) - @factory.attributes_for - end - - end - - end - - context "when adding an association without a factory name" do - - setup do - @factory = Factory.new(:post) - @name = :user - @factory.association(@name) - Post.any_instance.stubs(:user=) - Factory.stubs(:create) - end - - should "add an attribute with the name of the association" do - assert @factory.attributes_for.key?(@name) - end - - should "create a block that builds the association" do - Factory.expects(:create).with(@name, {}) - @factory.build - end - - end - - context "when adding an association with a factory name" do - - setup do - @factory = Factory.new(:post) - @name = :author - @factory_name = :user - @factory.association(@name, :factory => @factory_name) - Factory.stubs(:create) - end - - should "add an attribute with the name of the association" do - assert @factory.attributes_for.key?(@name) - end - - should "create a block that builds the association" do - Factory.expects(:create).with(@factory_name, {}) - @factory.build - end - - end - - should "add an attribute using the method name when passed an undefined method" do - @attr = :first_name - @value = 'Sugar' - @factory.send(@attr, @value) - assert_equal @value, @factory.attributes_for[@attr] - end - - should "allow attributes to be added with strings as names" do - @factory.add_attribute('name', 'value') - assert_equal 'value', @factory.attributes_for[:name] - end - - context "when overriding generated attributes with a hash" do - - setup do - @attr = :name - @value = 'The price is right!' - @hash = { @attr => @value } - end - - should "return the overridden value in the generated attributes" do - @factory.add_attribute(@attr, 'The price is wrong, Bob!') - assert_equal @value, @factory.attributes_for(@hash)[@attr] - end - - should "not call a lazy attribute block for an overridden attribute" do - @factory.add_attribute(@attr) { flunk } - @factory.attributes_for(@hash) - end - - should "override a symbol parameter with a string parameter" do - @factory.add_attribute(@attr, 'The price is wrong, Bob!') - @hash = { @attr.to_s => @value } - assert_equal @value, @factory.attributes_for(@hash)[@attr] - end - - end - - context "overriding an attribute with an alias" do - - setup do - @factory.add_attribute(:test, 'original') - Factory.alias(/(.*)_alias/, '\1') - @result = @factory.attributes_for(:test_alias => 'new') - end - - should "use the passed in value for the alias" do - assert_equal 'new', @result[:test_alias] - end - - should "discard the predefined value for the attribute" do - assert_nil @result[:test] - end - - end - - should "guess the build class from the factory name" do - assert_equal User, @factory.build_class - end - - context "when defined with a custom class" do - - setup do - @class = User - @factory = Factory.new(:author, :class => @class) - end - - should "use the specified class as the build class" do - assert_equal @class, @factory.build_class - end - - end - - context "when defined with a class instead of a name" do - - setup do - @class = ArgumentError - @name = :argument_error - @factory = Factory.new(@class) - end - - should "guess the name from the class" do - assert_equal @name, @factory.factory_name - end - - should "use the class as the build class" do - assert_equal @class, @factory.build_class - end - - end - context "when defined with a custom class name" do - - setup do - @class = ArgumentError - @factory = Factory.new(:author, :class => :argument_error) - end - - should "use the specified class as the build class" do - assert_equal @class, @factory.build_class - end - - end - - context "with some attributes added" do - - setup do - @first_name = 'Billy' - @last_name = 'Idol' - @email = 'test@something.com' - - @factory.add_attribute(:first_name, @first_name) - @factory.add_attribute(:last_name, @last_name) - @factory.add_attribute(:email, @email) - end - - context "when building an instance" do - - setup do - @instance = @factory.build - end - - should_instantiate_class - - should "not save the instance" do - assert @instance.new_record? - end - - end - - context "when creating an instance" do - - setup do - @instance = @factory.create - end - - should_instantiate_class - - should "save the instance" do - assert !@instance.new_record? - end - - end - - should "raise an error for invalid instances" do - assert_raise(ActiveRecord::RecordInvalid) do - @factory.create(:first_name => nil) - end - end - - end - - end - - context "a factory with a name ending in s" do - - setup do - @name = :business - @class = Business - @factory = Factory.new(@name) - end - - should "have a factory name" do - assert_equal @name, @factory.factory_name - end - - should "have a build class" do - assert_equal @class, @factory.build_class - end - - end - - context "a factory with a string for a name" do - - setup do - @name = :user - @factory = Factory.new(@name.to_s) {} - end - - should "convert the string to a symbol" do - assert_equal @name, @factory.factory_name - end - - end - - context "a factory defined with a string name" do - - setup do - Factory.factories = {} - @name = :user - @factory = Factory.define(@name.to_s) {} - end - - should "store the factory using a symbol" do - assert_equal @factory, Factory.factories[@name] - end - - end - - context "Factory class" do - - setup do - @name = :user - @attrs = { :last_name => 'Override' } - @first_name = 'Johnny' - @last_name = 'Winter' - @class = User - - Factory.define(@name) do |u| - u.first_name @first_name - u.last_name { @last_name } - u.email 'jwinter@guitar.org' - end - - @factory = Factory.factories[@name] - end - - [:build, :create, :attributes_for].each do |method| - - should "delegate the #{method} method to the factory instance" do - @factory.expects(method).with(@attrs) - Factory.send(method, @name, @attrs) - end - - should "raise an ArgumentError when #{method} is called with a nonexistant factory" do - assert_raise(ArgumentError) { Factory.send(method, :bogus) } - end - - should "recognize either 'name' or :name for Factory.#{method}" do - assert_nothing_raised { Factory.send(method, @name.to_s) } - assert_nothing_raised { Factory.send(method, @name.to_sym) } - end - - end - - should "call the create method from the top-level Factory() method" do - @factory.expects(:create).with(@attrs) - Factory(@name, @attrs) - end - - end - - def self.context_in_directory_with_files(*files) - context "in a directory with #{files.to_sentence}" do - setup do - @pwd = Dir.pwd - @tmp_dir = File.join(File.dirname(__FILE__), 'tmp') - FileUtils.mkdir_p @tmp_dir - Dir.chdir(@tmp_dir) - - files.each do |file| - FileUtils.mkdir_p File.dirname(file) - FileUtils.touch file - Factory.stubs(:require).with(file) - end - end - - teardown do - Dir.chdir(@pwd) - FileUtils.rm_rf(@tmp_dir) - end - - yield - end - end - - def self.should_require_definitions_from(file) - should "load definitions from #{file}" do - Factory.expects(:require).with(file) - Factory.find_definitions - end - end - - context_in_directory_with_files 'factories.rb' do - should_require_definitions_from 'factories.rb' - end - - %w(spec test).each do |dir| - context_in_directory_with_files File.join(dir, 'factories.rb') do - should_require_definitions_from "#{dir}/factories.rb" - end - - context_in_directory_with_files File.join(dir, 'factories', 'post_factory.rb') do - should_require_definitions_from "#{dir}/factories/post_factory.rb" - end - - context_in_directory_with_files File.join(dir, 'factories', 'post_factory.rb'), File.join(dir, 'factories', 'person_factory.rb') do - should_require_definitions_from "#{dir}/factories/post_factory.rb" - should_require_definitions_from "#{dir}/factories/person_factory.rb" - end - - context_in_directory_with_files File.join(dir, 'factories.rb'), File.join(dir, 'factories', 'post_factory.rb'), File.join(dir, 'factories', 'person_factory.rb') do - should_require_definitions_from "#{dir}/factories.rb" - should_require_definitions_from "#{dir}/factories/post_factory.rb" - should_require_definitions_from "#{dir}/factories/person_factory.rb" - end - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/integration_test.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/integration_test.rb deleted file mode 100644 index 798a2971a..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/integration_test.rb +++ /dev/null @@ -1,147 +0,0 @@ -require(File.join(File.dirname(__FILE__), 'test_helper')) - -class IntegrationTest < Test::Unit::TestCase - - def setup - Factory.define :user, :class => 'user' do |f| - f.first_name 'Jimi' - f.last_name 'Hendrix' - f.admin false - f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase } - end - - Factory.define Post do |f| - f.name 'Test Post' - f.association :author, :factory => :user - end - - Factory.define :admin, :class => User do |f| - f.first_name 'Ben' - f.last_name 'Stein' - f.admin true - f.email { Factory.next(:email) } - end - - Factory.sequence :email do |n| - "somebody#{n}@example.com" - end - end - - def teardown - Factory.factories.clear - end - - context "a generated attributes hash" do - - setup do - @attrs = Factory.attributes_for(:user, :first_name => 'Bill') - end - - should "assign all attributes" do - assert_equal [:admin, :email, :first_name, :last_name], - @attrs.keys.sort {|a, b| a.to_s <=> b.to_s } - end - - should "correctly assign lazy, dependent attributes" do - assert_equal "bill.hendrix@example.com", @attrs[:email] - end - - should "override attrbutes" do - assert_equal 'Bill', @attrs[:first_name] - end - - should "not assign associations" do - assert_nil Factory.attributes_for(:post)[:author] - end - - end - - context "a built instance" do - - setup do - @instance = Factory.build(:post) - end - - should "not be saved" do - assert @instance.new_record? - end - - should "assign associations" do - assert_kind_of User, @instance.author - end - - should "save associations" do - assert !@instance.author.new_record? - end - - should "not assign both an association and its foreign key" do - assert_equal 1, Factory.build(:post, :author_id => 1).author_id - end - - end - - context "a created instance" do - - setup do - @instance = Factory.create('post') - end - - should "be saved" do - assert !@instance.new_record? - end - - should "assign associations" do - assert_kind_of User, @instance.author - end - - should "save associations" do - assert !@instance.author.new_record? - end - - end - - context "an instance generated by a factory with a custom class name" do - - setup do - @instance = Factory.create(:admin) - end - - should "use the correct class name" do - assert_kind_of User, @instance - end - - should "use the correct factory definition" do - assert @instance.admin? - end - - end - - context "an attribute generated by a sequence" do - - setup do - @email = Factory.attributes_for(:admin)[:email] - end - - should "match the correct format" do - assert_match /^somebody\d+@example\.com$/, @email - end - - context "after the attribute has already been generated once" do - - setup do - @another_email = Factory.attributes_for(:admin)[:email] - end - - should "match the correct format" do - assert_match /^somebody\d+@example\.com$/, @email - end - - should "not be the same as the first generated value" do - assert_not_equal @email, @another_email - end - - end - - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/models.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/models.rb deleted file mode 100644 index 90af55844..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/models.rb +++ /dev/null @@ -1,42 +0,0 @@ -ActiveRecord::Base.establish_connection( - :adapter => 'sqlite3', - :database => File.join(File.dirname(__FILE__), 'test.db') -) - -class CreateSchema < ActiveRecord::Migration - def self.up - create_table :users, :force => true do |t| - t.string :first_name - t.string :last_name - t.string :email - t.boolean :admin, :default => false - end - - create_table :posts, :force => true do |t| - t.string :name - t.integer :author_id - end - - create_table :business, :force => true do |t| - t.string :name - t.integer :owner_id - end - end -end - -CreateSchema.suppress_messages { CreateSchema.migrate(:up) } - -class User < ActiveRecord::Base - validates_presence_of :first_name, :last_name, :email - has_many :posts, :foreign_key => 'author_id' -end - -class Business < ActiveRecord::Base - validates_presence_of :name, :owner_id - belongs_to :owner, :class_name => 'User' -end - -class Post < ActiveRecord::Base - validates_presence_of :name, :author_id - belongs_to :author, :class_name => 'User' -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/sequence_test.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/sequence_test.rb deleted file mode 100644 index 5f7c525d4..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/sequence_test.rb +++ /dev/null @@ -1,76 +0,0 @@ -require(File.join(File.dirname(__FILE__), 'test_helper')) - -class SequenceTest < Test::Unit::TestCase - - context "a sequence" do - - setup do - @sequence = Factory::Sequence.new {|n| "=#{n}" } - end - - should "start with a value of 1" do - assert_equal "=1", @sequence.next - end - - context "after being called" do - - setup do - @sequence.next - end - - should "use the next value" do - assert_equal "=2", @sequence.next - end - - end - - end - - context "defining a sequence" do - - setup do - @sequence = mock('sequence') - @name = :count - Factory::Sequence.stubs(:new).returns(@sequence) - end - - should "create a new sequence" do - Factory::Sequence.expects(:new).with().returns(@sequence) - Factory.sequence(@name) - end - - should "use the supplied block as the sequence generator" do - Factory::Sequence.stubs(:new).yields(1) - yielded = false - Factory.sequence(@name) {|n| yielded = true } - assert yielded - end - - end - - context "after defining a sequence" do - - setup do - @sequence = mock('sequence') - @name = :test - @value = '1 2 5' - - @sequence. stubs(:next).returns(@value) - Factory::Sequence.stubs(:new). returns(@sequence) - - Factory.sequence(@name) {} - end - - should "call next on the sequence when sent next" do - @sequence.expects(:next) - - Factory.next(@name) - end - - should "return the value from the sequence" do - assert_equal @value, Factory.next(@name) - end - - end - -end diff --git a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/test_helper.rb b/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/test_helper.rb deleted file mode 100644 index 9a94449bc..000000000 --- a/test/rails_root/vendor/gems/thoughtbot-factory_girl-1.1.5/test/test_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -$: << File.join(File.dirname(__FILE__), '..', 'lib') -$: << File.join(File.dirname(__FILE__)) - -require 'rubygems' -require 'test/unit' -require 'activerecord' -require 'factory_girl' -require 'shoulda' -require 'mocha' -require 'models'