From cc6c813517a78a949e73f98e63d9b545258214c5 Mon Sep 17 00:00:00 2001 From: cowboycoded Date: Thu, 10 Feb 2011 16:13:30 -0500 Subject: [PATCH] added support for current_user helper method of authentication tests for both current_user (Post) and @current_user (Article) implementations updated gems --- .../impressionist_appliation_controller.rb | 5 +++- test_app/Gemfile.lock | 26 ++++++++--------- .../app/controllers/articles_controller.rb | 9 ++++++ test_app/app/controllers/posts_controller.rb | 9 ++++++ test_app/app/models/post.rb | 3 ++ test_app/app/models/user.rb | 3 ++ .../db/migrate/20110210205028_create_posts.rb | 13 +++++++++ test_app/db/schema.rb | 8 ++++- test_app/spec/controllers/controller_spec.rb | 29 +++++++++++++++---- test_app/spec/fixtures/posts.yml | 3 ++ 10 files changed, 87 insertions(+), 21 deletions(-) create mode 100644 test_app/app/models/post.rb create mode 100644 test_app/app/models/user.rb create mode 100644 test_app/db/migrate/20110210205028_create_posts.rb create mode 100644 test_app/spec/fixtures/posts.yml diff --git a/app/controllers/impressionist_appliation_controller.rb b/app/controllers/impressionist_appliation_controller.rb index 52904c8..d006aa7 100644 --- a/app/controllers/impressionist_appliation_controller.rb +++ b/app/controllers/impressionist_appliation_controller.rb @@ -48,7 +48,10 @@ class ApplicationController < ActionController::Base Impressionist::Bots::LIST.include? request.user_agent end + #use both @current_user and current_user helper def user_id - @current_user ? @current_user.id : nil + user_id = @current_user ? @current_user.id : nil rescue nil + user_id = current_user ? current_user.id : nil rescue nil if user_id.blank? + user_id end end \ No newline at end of file diff --git a/test_app/Gemfile.lock b/test_app/Gemfile.lock index cdad697..2a76d08 100644 --- a/test_app/Gemfile.lock +++ b/test_app/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: /rails_plugins/mine/impressionist specs: - impressionist (0.1.1) + impressionist (0.1.2) GEM remote: http://rubygems.org/ @@ -35,13 +35,13 @@ GEM activesupport (= 3.0.3) activesupport (3.0.3) archive-tar-minitar (0.5.2) - arel (2.0.7) + arel (2.0.8) autotest (4.4.6) ZenTest (>= 4.4.1) autotest-notification (2.3.1) autotest (~> 4.3) builder (2.1.2) - capybara (0.4.1.1) + capybara (0.4.1.2) celerity (>= 0.7.9) culerity (>= 0.2.4) mime-types (>= 1.16) @@ -65,7 +65,7 @@ GEM cucumber (>= 0.8.0) culerity (0.2.15) daemons (1.0.10) - database_cleaner (0.6.1) + database_cleaner (0.6.3) diff-lcs (1.1.2) erubis (2.6.6) abstract (>= 1.0.0) @@ -112,19 +112,19 @@ GEM rake (>= 0.8.7) thor (~> 0.14.4) rake (0.8.7) - rspec (2.4.0) - rspec-core (~> 2.4.0) - rspec-expectations (~> 2.4.0) - rspec-mocks (~> 2.4.0) - rspec-core (2.4.0) - rspec-expectations (2.4.0) + rspec (2.5.0) + rspec-core (~> 2.5.0) + rspec-expectations (~> 2.5.0) + rspec-mocks (~> 2.5.0) + rspec-core (2.5.1) + rspec-expectations (2.5.0) diff-lcs (~> 1.1.2) - rspec-mocks (2.4.0) - rspec-rails (2.4.1) + rspec-mocks (2.5.0) + rspec-rails (2.5.0) actionpack (~> 3.0) activesupport (~> 3.0) railties (~> 3.0) - rspec (~> 2.4.0) + rspec (~> 2.5.0) ruby-debug-base19 (0.11.24) columnize (>= 0.3.1) linecache19 (>= 0.5.11) diff --git a/test_app/app/controllers/articles_controller.rb b/test_app/app/controllers/articles_controller.rb index 45e8c0e..d154f1d 100644 --- a/test_app/app/controllers/articles_controller.rb +++ b/test_app/app/controllers/articles_controller.rb @@ -1,4 +1,13 @@ class ArticlesController < ApplicationController + before_filter :test_current_user_var + + def test_current_user_var + if session[:user_id] + @current_user = User.new + @current_user.id = session[:user_id] + end + end + def index impressionist(Article.first,"this is a test article impression") end diff --git a/test_app/app/controllers/posts_controller.rb b/test_app/app/controllers/posts_controller.rb index 829fab4..9acf731 100644 --- a/test_app/app/controllers/posts_controller.rb +++ b/test_app/app/controllers/posts_controller.rb @@ -1,4 +1,5 @@ class PostsController < ApplicationController + helper_method :current_user impressionist def index @@ -11,4 +12,12 @@ class PostsController < ApplicationController def edit end + + def current_user + if session[:user_id] + user = User.new + user.id = session[:user_id] + @current_user ||= user + end + end end \ No newline at end of file diff --git a/test_app/app/models/post.rb b/test_app/app/models/post.rb new file mode 100644 index 0000000..ccc43da --- /dev/null +++ b/test_app/app/models/post.rb @@ -0,0 +1,3 @@ +class Post < ActiveRecord::Base + is_impressionable +end diff --git a/test_app/app/models/user.rb b/test_app/app/models/user.rb new file mode 100644 index 0000000..6738fc6 --- /dev/null +++ b/test_app/app/models/user.rb @@ -0,0 +1,3 @@ +class User + attr_accessor :id +end \ No newline at end of file diff --git a/test_app/db/migrate/20110210205028_create_posts.rb b/test_app/db/migrate/20110210205028_create_posts.rb new file mode 100644 index 0000000..f018e1c --- /dev/null +++ b/test_app/db/migrate/20110210205028_create_posts.rb @@ -0,0 +1,13 @@ +class CreatePosts < ActiveRecord::Migration + def self.up + create_table :posts do |t| + t.string :name + + t.timestamps + end + end + + def self.down + drop_table :posts + end +end diff --git a/test_app/db/schema.rb b/test_app/db/schema.rb index ea8a878..05581aa 100644 --- a/test_app/db/schema.rb +++ b/test_app/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110201164012) do +ActiveRecord::Schema.define(:version => 20110210205028) do create_table "articles", :force => true do |t| t.string "name" @@ -32,4 +32,10 @@ ActiveRecord::Schema.define(:version => 20110201164012) do t.datetime "updated_at" end + create_table "posts", :force => true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + end + end diff --git a/test_app/spec/controllers/controller_spec.rb b/test_app/spec/controllers/controller_spec.rb index cf411c3..fd34538 100644 --- a/test_app/spec/controllers/controller_spec.rb +++ b/test_app/spec/controllers/controller_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper.rb' describe ArticlesController do - fixtures :articles,:impressions + fixtures :articles,:impressions,:posts render_views it "should make the impressionable_hash available" do @@ -16,25 +16,42 @@ describe ArticlesController do end it "should log an impression without a message" do - get "show", :id=> 1 + get "show", id: 1 Impression.all.size.should eq 11 Article.first.impressions.last.message.should eq nil end + + it "should log the user_id if user is authenticated (@current_user before_filter method)" do + session[:user_id] = 123 + get "show", id: 1 + Article.first.impressions.last.user_id.should eq 123 + end + + it "should not log the user_id if user is authenticated" do + get "show", id: 1 + Article.first.impressions.last.user_id.should eq nil + end end describe PostsController do it "should log impression at the action level" do - get "show", :id=> 1 + get "show", id: 1 Impression.all.size.should eq 11 Impression.last.controller_name.should eq "posts" Impression.last.impressionable_type.should eq "Post" Impression.last.impressionable_id.should eq 1 end + + it "should log the user_id if user is authenticated (current_user helper method)" do + session[:user_id] = 123 + get "show", id: 1 + Post.first.impressions.last.user_id.should eq 123 + end end describe WidgetsController do it "should log impression at the per action level" do - get "show", :id => 1 + get "show", id: 1 Impression.all.size.should eq 11 get "index" Impression.all.size.should eq 12 @@ -44,13 +61,13 @@ describe WidgetsController do it "should not log impression when user-agent is in wildcard list" do request.stub!(:user_agent).and_return('somebot') - get "show", :id => 1 + get "show", id: 1 Impression.all.size.should eq 10 end it "should not log impression when user-agent is in the bot list" do request.stub!(:user_agent).and_return('Acoon Robot v1.50.001') - get "show", :id => 1 + get "show", id: 1 Impression.all.size.should eq 10 end end diff --git a/test_app/spec/fixtures/posts.yml b/test_app/spec/fixtures/posts.yml new file mode 100644 index 0000000..6b70243 --- /dev/null +++ b/test_app/spec/fixtures/posts.yml @@ -0,0 +1,3 @@ +one: + id: 1 + name: Test Post \ No newline at end of file