Skip to content

Commit

Permalink
add more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsusser committed May 28, 2008
1 parent b7baac0 commit 9acfe0a
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 2 deletions.
79 changes: 79 additions & 0 deletions spec/controllers/articles_controller_spec.rb
@@ -0,0 +1,79 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Admin::ArticlesController do
scenario :basic

setup do
use_controller Admin::ArticlesController
login_as :admin
end

it "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:articles)
end

it "should show article with comments" do
get :show, :id => articles(:body_post).id
assert_response :success
assert_not_nil assigns(:article)
assert_equal articles(:body_post).comments.size, assigns(:comments).size
end

def test_should_get_new
get :new
assert_response :success
end

def test_should_create_post
assert_difference('Article.count') do
post :create, :article => articles(:body_post).attributes.except("id")
assert_equal session[:user_id], assigns(:article).user.id
assert_not_nil assigns(:article).kind
assert_not_nil assigns(:article).published_at
end

assert_redirected_to admin_article_url(assigns(:article))
end

def test_should_show_post
get :show, :id => articles(:body_post).id
assert_response :success
end

def test_should_get_edit
get :edit, :id => articles(:body_post).id
assert_response :success
end

def test_should_update_post
article = articles(:body_post)
versions = article.versions.size
put :update, :id => article.id, :article => { :extended => "and more content" }, :commit => "Save new version"
article.reload
assert_equal article, assigns(:article)
assert_equal (versions + 1), article.versions.size
assert_equal "and more content", article.extended
assert_redirected_to edit_admin_article_url(article)
end

def test_should_update_post_without_new_version
article = articles(:body_post)
versions = article.versions.size
put :update, :id => article.id, :article => { :extended => "and more content" }, :commit => "Save and keep same version"
article.reload
assert_equal article, assigns(:article)
assert_equal versions, article.versions.size
assert_equal "and more content", article.extended
assert_redirected_to edit_admin_article_url(article)
end

def test_should_destroy_post
assert_difference('Article.count', -1) do
delete :destroy, :id => articles(:body_post).id
end

assert_redirected_to admin_articles_url
end
end
167 changes: 167 additions & 0 deletions spec/controllers/blog_controller_spec.rb
@@ -0,0 +1,167 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe BlogController do
scenario :blog

describe "basic viewing" do
it "should show archives in index view" do
get :index
assert_not_nil assigns(:archives)
assert_select "ul#archives" do
assert_select "li a[href=#{post_path(*articles(:welcome).post_path_params)}]"
end
end

it "should not show meta section on Page view" do
about = articles(:about)
post :page, :slug => about.slug
assert_response :success
assert_template 'show'
assert_select 'ul.meta', false
end

it "should show meta section on Post view" do
welcome = articles(:welcome)
year, month, day, slug = *welcome.post_path_params
post :show, :year => year, :month => month, :day => day, :slug => slug
assert_response :success
assert_template 'show'
assert_select 'p.meta'
end

it "should not show comments on Page view" do
about = articles(:about)
post :page, :slug => about.slug
assert_response :success
assert_template 'show'
assert_select 'ol#comments', false
end

it "should show comments on Post view" do
welcome = articles(:welcome)
year, month, day, slug = *welcome.post_path_params
post :show, :year => year, :month => month, :day => day, :slug => slug
assert_response :success
assert_template 'show'
assert_select 'ol#comments'
end

it "should list all tags that have articles" do
tags = Tag.find_all_popular
get :tags
assert_equal tags, assigns(:tags)
end

it "should list recent articles for a tag" do
tag = tags(:meta)
get :tag, :tag => tag.name
assert_equal tag.articles.recent, assigns(:articles)
end
end

describe "sidebar" do
it "should show search box" do
get :index
assert_select "div#search input#q"
end

it "should return search results" do
get :search, :q => "body"
assert_template "results"
assert_not_nil assigns(:articles)
end

it "should return empty results for blank query" do
get :search, :q => ""
assert_template "results"
assert_equal [], assigns(:articles)
end

it "should show tags in tag cloud" do
get :index
assert_not_nil assigns(:tag_cloud)
assert_select "p#tags"
end
end

describe "entering new comment" do
it "should create new comment on Post view" do
update_post = articles(:update)
assert_difference('Comment.count') do
assert_difference('update_post.comments.count') do
post :create_comment, :article_id => update_post.to_param, :comment => comment_options
end
end

assert_redirected_to post_path(*assigns(:article).post_path_params)

# should be follow_redirect
ps = update_post.post_path_params
get :show, :year => ps[0], :month => ps[1], :day => ps[2], :slug => ps[3]

new_comment = Comment.find_by_author_name(comment_options[:author_name])
assert !new_comment.author_ip.blank?
assert_select "li#comment_#{new_comment.id}"
end

it "should not create new comment on spammy submission" do
update_post = articles(:update)
assert_no_difference('Comment.count') do
assert_no_difference('update_post.comments.count') do
assert_difference('Reject.count') do
post :create_comment, :article_id => update_post.to_param, :comment => comment_options(:body => "spam spam spam")
end
end
end

assert_redirected_to post_path(*assigns(:article).post_path_params)
end

it "should set user_id of logged in author on comment creation" do
update_post = articles(:update)

post :create_comment, :article_id => update_post.to_param, :comment => comment_options
assert_nil assigns(:comment).user_id

login_as(:admin)
post :create_comment, :article_id => update_post.to_param, :comment => comment_options
assert_equal users(:admin).id, assigns(:comment).user_id
end

def comment_options(options = {})
{ :author_name => "Bob Dobbs",
:author_email => "bob@slack.com",
:author_url => "http://bob.slack.com",
:tofu => "Slack off! Quit your job!"
}.merge(options)
end
end

describe "feed" do

ATOM_CONTENT_TYPE = "application/atom+xml; charset=utf-8"

it "should respond to main atom feed request with atom xml document" do
get :index, :format => 'atom'
assert_equal ATOM_CONTENT_TYPE, @response.headers["type"]
assert_select "entry", assigns(:articles).size
end

it "should respond to tag atom feed request with atom xml document" do
get :tag, :tag => 'meta', :format => 'atom'
assert_equal ATOM_CONTENT_TYPE, @response.headers["type"]
assert_select "entry", assigns(:articles).size
end

it "should respond with 404 for a non-existent tag feed" do
get :tag, :tag => 'meat', :format => 'atom'
assert_response :not_found
end

it "should not load sidebar data for atom feeds" do
get :index, :format => 'atom'
assert_nil assigns(:tag_cloud)
assert_nil assigns(:archives)
end
end
end
4 changes: 2 additions & 2 deletions spec/models/article_spec.rb
Expand Up @@ -18,7 +18,7 @@
end
end

describe "Finding posts with #find_post_by_date_and_slug" do
describe "#find_post_by_date_and_slug" do
scenario :blog

it "should find the post with slug published on given date" do
Expand All @@ -37,7 +37,7 @@
end
end

describe "Finding posts with #find_post_by_date_and_slug" do
describe "#find_page_by_slug" do
scenario :blog

it "should find the page with slug" do
Expand Down
40 changes: 40 additions & 0 deletions spec/models/tag_spec.rb
@@ -0,0 +1,40 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "Tag parsing" do
it "should parse list of tags" do
Tag.parse("").should be_empty
Tag.parse("a, b, c").should == %w(a b c)
Tag.parse("a, b, c, a, b, a").should == %w(a b c)
Tag.parse(" O'NE , two, - , // thrEE,, 1337").should == %w(one two three 1337)
end
end

describe Tag do
scenario :tags

describe "#find_all_popular" do
it "should return all tags sorted descending by number of taggings" do
tags = Tag.find_all_popular
tags.should have(Tag.count).items
in_order = true
0.upto(tags.size - 2) { |i| in_order &&= (tags[i].taggings_count >= tags[i+1].taggings_count) }
in_order.should be_true
end

it "should not return tags with no taggings" do
lonely = Tag.create!(:name => "lonely")
tags = Tag.find_all_popular
tags.should_not include(lonely)
tags.all? { |t| t.taggings_count > 0 }.should be_true
end
end

describe "posts.recent" do
it "should return the tag's posts in revers chron order" do
sf = tags(:san_francisco)
posts = sf.articles.recent
posts.should have(sf.articles.size).articles
posts.should == posts.sort { |a,b| b.published_at <=> a.published_at }
end
end
end
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -46,3 +46,16 @@
#
# For more information take a look at Spec::Example::Configuration and Spec::Runner
end

Spec::Runner.configuration.before(:all, :behaviour_type => :controller) do
@integrate_views = true
end

# Sets the current user in the session from the user fixtures.
def login_as(user)
@request.session[:user_id] = user ? users(user).id : nil
end

def authorize_as(user)
@request.env["HTTP_AUTHORIZATION"] = user ? "Basic #{Base64.encode64("#{users(user).login}:test")}" : nil
end

0 comments on commit 9acfe0a

Please sign in to comment.