Skip to content

Commit

Permalink
Some work on funcional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alce committed May 26, 2010
1 parent 28be685 commit c55b621
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 128 deletions.
2 changes: 1 addition & 1 deletion config/initializers/backtrace_silencers.rb
Expand Up @@ -4,4 +4,4 @@
# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }

# You can also remove all the silencers if you're trying do debug a problem that might steem from framework code.
# Rails.backtrace_cleaner.remove_silencers!
Rails.backtrace_cleaner.remove_silencers!
4 changes: 4 additions & 0 deletions test/factories.rb
Expand Up @@ -8,6 +8,10 @@
Factory.define :group_type do |m|
m.sequence(:name) {|n| "TestGroupType#{n}" }
end

Factory.define :permission do |m|
m.name "edit_content"
end

Factory.define :user do |m|
m.first_name "Test"
Expand Down
25 changes: 13 additions & 12 deletions test/functional/blog_post_test.rb
Expand Up @@ -4,13 +4,14 @@ class BlogPostTest < ActionController::TestCase
tests Cms::ContentController

def setup
setup_stubs
create_baseline_data
end

def teardown
destroy_baseline_data
end

# def teardown
# destroy_baseline_data
# end
def test_show_post
get :show, :path => ["blog", "post"],
:year => @first_post.year,
Expand All @@ -21,7 +22,7 @@ def test_show_post
assert_response :success
assert_select "title", @first_post.name
assert_select ".blog_post", 1

assert_select "#blog_post_#{@first_post.id}" do
assert_select "h2 a", @first_post.name
assert_select "p.body", @first_post.body
Expand All @@ -30,12 +31,12 @@ def test_show_post
end

end

def test_non_existent_slug_should_return_404
get :show, :path => ["blog", "post"],
:year => 2005, :month => 6, :day => 14,
:slug => "not-here"
assert_response :not_found
end
# def test_non_existent_slug_should_return_404
# get :show, :path => ["blog", "post"],
# :year => 2005, :month => 6, :day => 14,
# :slug => "not-here"
# assert_response :not_found
# end

end
117 changes: 61 additions & 56 deletions test/functional/blog_test.rb
Expand Up @@ -4,66 +4,71 @@ class BlogTest < ActionController::TestCase
tests Cms::ContentController

def setup
# create_baseline_data
# `rake db:test:purge`
# `rake db:migrate`
create_baseline_data
end

def teardown
destroy_baseline_data
end

#
# def teardown
# destroy_baseline_data
# end
#
def test_list_of_blog_posts
get :show
#log @response.body
assert_response :success
assert_select ".blog_post", 5

assert_select "#blog_post_#{@first_post.id}" do
assert_select "h2 a", @first_post.name
assert_select "p.body", @first_post.body
assert_select "p.meta a", "General"
assert_select "p.meta a", "0 Comments"
end

assert_select "#blog_post_#{@foo_post_1.id}" do
assert_select "h2 a", @foo_post_1.name
assert_select "p.body", @foo_post_1.body
assert_select "p.meta .tags a", "foo"
assert_select "p.meta .tags a", "stuff"
end
end
get :show
log @response.body
assert_response :success
assert_select ".blog_post", 5

assert_select "#blog_post_#{@first_post.id}" do
assert_select "h2 a", @first_post.name
assert_select "p.body", @first_post.body
assert_select "p.meta a", "General"
assert_select "p.meta a", "0 Comments"
end

assert_select "#blog_post_#{@foo_post_1.id}" do
assert_select "h2 a", @foo_post_1.name
assert_select "p.body", @foo_post_1.body
assert_select "p.meta .tags a", "foo"
assert_select "p.meta .tags a", "stuff"
end
end

def test_list_of_tagged_blog_posts
get :show, :category => "General"
#log @response.body
assert_response :success
assert_select ".blog_post", 3
end

def test_list_of_categorized_blog_posts
get :show, :tag => "foo"
#log @response.body
assert_response :success
assert_select ".blog_post", 2
end

def test_list_of_blog_posts_in_day
get :show, :path => ["blog", "posts_in_day"],
:year => 2008, :month => 7, :day => 5
assert_response :success
assert_select ".blog_post", 2
end

def test_list_of_blog_posts_in_month
get :show, :path => ["blog", "posts_in_month"],
:year => 2008, :month => 7
assert_response :success
assert_select ".blog_post", 3
end

def test_list_of_blog_posts_in_year
get :show, :path => ["blog", "posts_in_year"],
:year => 2008
assert_response :success
assert_select ".blog_post", 4
end
# def test_list_of_tagged_blog_posts
# get :show, :category => "General"
# puts @response.body
# assert_response :success
# assert_select ".blog_post", 3
# end
#
# def test_list_of_categorized_blog_posts
# get :show, :tag => "foo"
# #log @response.body
# assert_response :success
# assert_select ".blog_post", 2
# end
#
# def test_list_of_blog_posts_in_day
# get :show, :path => ["blog", "posts_in_day"],
# :year => 2008, :month => 7, :day => 5
# assert_response :success
# assert_select ".blog_post", 2
# end
#
# def test_list_of_blog_posts_in_month
# get :show, :path => ["blog", "posts_in_month"],
# :year => 2008, :month => 7
# assert_response :success
# assert_select ".blog_post", 3
# end
#
# def test_list_of_blog_posts_in_year
# get :show, :path => ["blog", "posts_in_year"],
# :year => 2008
# assert_response :success
# assert_select ".blog_post", 4
# end
end
53 changes: 28 additions & 25 deletions test/functional/cms/blog_posts_controller_test.rb
Expand Up @@ -2,42 +2,45 @@

class Cms::BlogPostsControllerTest < ActionController::TestCase
def setup
create_non_admin_user
login_as(@user)
setup_stubs
ContentType.create!(:name => 'BlogPost', :group_name => 'Blog')
login_as(create_user)
end

def test_access_denied_on_create_if_blog_not_user_editable
@editable = Factory.create(:blog, :groups => [@group])
@non_editable = Factory.create(:blog)
flunk
@editable = Factory(:blog, :groups => [@group])
@non_editable = Factory(:blog)
post :create, :blog_post => { :blog_id => @non_editable.id }
assert @response.body.include?("AccessDenied")
end

def test_access_denied_on_update_if_blog_not_user_editable
flunk
@editable = Factory.create(:blog, :groups => [@group])
@non_editable = Factory.create(:blog)
@blog_post = Factory.create(:blog_post, :blog => @non_editable)
put :update, :id => @blog_post, :blog_post => { :name => "Foo" }
assert @response.body.include?("AccessDenied")
end

def test_no_access_if_no_editable_blogs
@blog = Factory.create(:blog)
get :index
assert_template "no_access"
end

def test_index_shouldnt_show_non_editable_posts
@editable = Factory.create(:blog, :groups => [@group])
@non_editable = Factory.create(:blog)
@blog_post = Factory.create(:blog_post, :name => "Non-editable", :blog => @non_editable)
get :index
assert !@response.body.include?("Non-editable")
end

def test_create_sets_author
@blog = Factory.create(:blog, :groups => [@group])
post :create, :blog_post => { :blog_id => @blog.id }
assert_equal @user, assigns(:block).author
end
end
def test_no_access_if_no_editable_blogs
@blog = Factory.create(:blog)
get :index
assert_template "no_access"
end
def test_index_shouldnt_show_non_editable_posts
@editable = Factory.create(:blog, :groups => [@group])
@non_editable = Factory.create(:blog)
@blog_post = Factory.create(:blog_post, :name => "Non-editable", :blog => @non_editable)
get :index
assert !@response.body.include?("Non-editable")
end
def test_create_sets_author
@blog = Factory.create(:blog, :groups => [@group])
post :create, :blog_post => { :blog_id => @blog.id }
assert_equal @user, assigns(:block).author
end
end
23 changes: 18 additions & 5 deletions test/functional/cms/blogs_controller_test.rb
@@ -1,12 +1,25 @@
require File.dirname(__FILE__) + '/../../test_helper'

class Cms::BlogsControllerTest < ActionController::TestCase
def test_list_blog_posts_as_non_admin
create_non_admin_user
login_as(@user)


def setup
setup_stubs
ContentType.create!(:name => 'Blog', :group_name => 'Blog')
Factory(:blog)
end

test "should allow access to admin users" do
login_as(create_user(:admin => true))
get :index
assert_response :success
assert assigns(:blocks)
assert_template("index")
end

test "should not allow access to non-admin users" do
login_as(create_user)
get :index
assert_response :success
assert @response.body.include? "Sorry, this section is restricted to administrators."
assert_template("admin_only.html.erb")
end
end
48 changes: 19 additions & 29 deletions test/test_helper.rb
Expand Up @@ -3,18 +3,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'

# unless $database_initialized
# $database_initialized = true
#
# # Empty the database and load in the default seed data for browsercms
# # and the blog module
# `rake db:test:purge`
# `rake db:migrate`
#
# # Publish all pages, as they are drafts after migrating
# Page.find(:all).each(&:publish!)
# end

class ActiveSupport::TestCase
require File.dirname(__FILE__) + '/test_logging'
include TestLogging
Expand Down Expand Up @@ -50,14 +38,6 @@ class ActiveSupport::TestCase
fixtures :all

# Add more helper methods to be used by all tests here...
def setup_stubs
Blog.any_instance.stubs(:reload_routes)
@section = Section.new
Section.stubs(:create! => @section)
@section.stubs(:groups => [], :save! => true)
Page.stubs(:create! => Page.new)
Page.any_instance.stubs(:create_connector)
end

def create_baseline_data
# Find the seed data items
Expand Down Expand Up @@ -94,16 +74,26 @@ def destroy_baseline_data
Category.destroy_all
BlogPost.destroy_all
end

def setup_stubs
Blog.any_instance.stubs(:reload_routes)
@section = Section.new
Section.stubs(:create! => @section)
@section.stubs(:groups => [], :save! => true)
Page.stubs(:create! => Page.new)
Page.any_instance.stubs(:create_connector)
end

def create_non_admin_user
@group = Factory(:group, :name => "Test", :group_type => Factory(:group_type, :name => "CMS User", :cms_access => true))
@group.permissions << Permission.find_by_name("edit_content")
@group.permissions << Permission.find_by_name("publish_content")
@group.save!

@user = Factory(:user)
@user.groups << @group
@user.save!
def create_group
@group = Factory(:group, :name => "Test", :group_type => Factory(:group_type, :name => "CMS User", :cms_access => true))
@group.permissions << Factory(:permission, :name => "edit_content")
@group.permissions << Factory(:permission, :name => "publish_content")
end

def create_user(opts = {})
create_group
@group.permissions << Factory(:permission, :name => "administrate") if opts[:admin]
@user = Factory(:user, :groups => [@group])
end

def login_as(user)
Expand Down

0 comments on commit c55b621

Please sign in to comment.