added support for current_user helper method of authentication

tests for both current_user (Post) and @current_user (Article) implementations
updated gems
This commit is contained in:
cowboycoded 2011-02-10 16:13:30 -05:00
parent 09add973e6
commit cc6c813517
10 changed files with 87 additions and 21 deletions

View File

@ -48,7 +48,10 @@ class ApplicationController < ActionController::Base
Impressionist::Bots::LIST.include? request.user_agent Impressionist::Bots::LIST.include? request.user_agent
end end
#use both @current_user and current_user helper
def user_id 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
end end

View File

@ -1,7 +1,7 @@
PATH PATH
remote: /rails_plugins/mine/impressionist remote: /rails_plugins/mine/impressionist
specs: specs:
impressionist (0.1.1) impressionist (0.1.2)
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
@ -35,13 +35,13 @@ GEM
activesupport (= 3.0.3) activesupport (= 3.0.3)
activesupport (3.0.3) activesupport (3.0.3)
archive-tar-minitar (0.5.2) archive-tar-minitar (0.5.2)
arel (2.0.7) arel (2.0.8)
autotest (4.4.6) autotest (4.4.6)
ZenTest (>= 4.4.1) ZenTest (>= 4.4.1)
autotest-notification (2.3.1) autotest-notification (2.3.1)
autotest (~> 4.3) autotest (~> 4.3)
builder (2.1.2) builder (2.1.2)
capybara (0.4.1.1) capybara (0.4.1.2)
celerity (>= 0.7.9) celerity (>= 0.7.9)
culerity (>= 0.2.4) culerity (>= 0.2.4)
mime-types (>= 1.16) mime-types (>= 1.16)
@ -65,7 +65,7 @@ GEM
cucumber (>= 0.8.0) cucumber (>= 0.8.0)
culerity (0.2.15) culerity (0.2.15)
daemons (1.0.10) daemons (1.0.10)
database_cleaner (0.6.1) database_cleaner (0.6.3)
diff-lcs (1.1.2) diff-lcs (1.1.2)
erubis (2.6.6) erubis (2.6.6)
abstract (>= 1.0.0) abstract (>= 1.0.0)
@ -112,19 +112,19 @@ GEM
rake (>= 0.8.7) rake (>= 0.8.7)
thor (~> 0.14.4) thor (~> 0.14.4)
rake (0.8.7) rake (0.8.7)
rspec (2.4.0) rspec (2.5.0)
rspec-core (~> 2.4.0) rspec-core (~> 2.5.0)
rspec-expectations (~> 2.4.0) rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.4.0) rspec-mocks (~> 2.5.0)
rspec-core (2.4.0) rspec-core (2.5.1)
rspec-expectations (2.4.0) rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2) diff-lcs (~> 1.1.2)
rspec-mocks (2.4.0) rspec-mocks (2.5.0)
rspec-rails (2.4.1) rspec-rails (2.5.0)
actionpack (~> 3.0) actionpack (~> 3.0)
activesupport (~> 3.0) activesupport (~> 3.0)
railties (~> 3.0) railties (~> 3.0)
rspec (~> 2.4.0) rspec (~> 2.5.0)
ruby-debug-base19 (0.11.24) ruby-debug-base19 (0.11.24)
columnize (>= 0.3.1) columnize (>= 0.3.1)
linecache19 (>= 0.5.11) linecache19 (>= 0.5.11)

View File

@ -1,4 +1,13 @@
class ArticlesController < ApplicationController 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 def index
impressionist(Article.first,"this is a test article impression") impressionist(Article.first,"this is a test article impression")
end end

View File

@ -1,4 +1,5 @@
class PostsController < ApplicationController class PostsController < ApplicationController
helper_method :current_user
impressionist impressionist
def index def index
@ -11,4 +12,12 @@ class PostsController < ApplicationController
def edit def edit
end end
def current_user
if session[:user_id]
user = User.new
user.id = session[:user_id]
@current_user ||= user
end
end
end end

View File

@ -0,0 +1,3 @@
class Post < ActiveRecord::Base
is_impressionable
end

View File

@ -0,0 +1,3 @@
class User
attr_accessor :id
end

View File

@ -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

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended to check this file into your version control system. # 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| create_table "articles", :force => true do |t|
t.string "name" t.string "name"
@ -32,4 +32,10 @@ ActiveRecord::Schema.define(:version => 20110201164012) do
t.datetime "updated_at" t.datetime "updated_at"
end end
create_table "posts", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end end

View File

@ -1,7 +1,7 @@
require 'spec_helper.rb' require 'spec_helper.rb'
describe ArticlesController do describe ArticlesController do
fixtures :articles,:impressions fixtures :articles,:impressions,:posts
render_views render_views
it "should make the impressionable_hash available" do it "should make the impressionable_hash available" do
@ -16,25 +16,42 @@ describe ArticlesController do
end end
it "should log an impression without a message" do it "should log an impression without a message" do
get "show", :id=> 1 get "show", id: 1
Impression.all.size.should eq 11 Impression.all.size.should eq 11
Article.first.impressions.last.message.should eq nil Article.first.impressions.last.message.should eq nil
end 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 end
describe PostsController do describe PostsController do
it "should log impression at the action level" 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.all.size.should eq 11
Impression.last.controller_name.should eq "posts" Impression.last.controller_name.should eq "posts"
Impression.last.impressionable_type.should eq "Post" Impression.last.impressionable_type.should eq "Post"
Impression.last.impressionable_id.should eq 1 Impression.last.impressionable_id.should eq 1
end 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 end
describe WidgetsController do describe WidgetsController do
it "should log impression at the per action level" 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 Impression.all.size.should eq 11
get "index" get "index"
Impression.all.size.should eq 12 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 it "should not log impression when user-agent is in wildcard list" do
request.stub!(:user_agent).and_return('somebot') request.stub!(:user_agent).and_return('somebot')
get "show", :id => 1 get "show", id: 1
Impression.all.size.should eq 10 Impression.all.size.should eq 10
end end
it "should not log impression when user-agent is in the bot list" do 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') 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 Impression.all.size.should eq 10
end end
end end

3
test_app/spec/fixtures/posts.yml vendored Normal file
View File

@ -0,0 +1,3 @@
one:
id: 1
name: Test Post