Skip to content

Instantly share code, notes, and snippets.

@rjharmon
Created March 8, 2009 20:41
Show Gist options
  • Save rjharmon/75896 to your computer and use it in GitHub Desktop.
Save rjharmon/75896 to your computer and use it in GitHub Desktop.
= A sampling of problems found =
== Doesn't generate XML for index.xml ==
'TopicsController responding to GET index when there are topics with mime type of xml should render all topics as xml' FAILED
expected: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<topics type=\"array\">\n <topic>\n <created-at type=\"datetime\">2009-03-08T20:46:47Z</created-at>\n <current-hat>Red</current-hat>\n <current-thought-id type=\"integer\" nil=\"true\"></current-thought-id>\n <id type=\"integer\">1</id>\n <name>generated string # 3</name>\n <summary>generated string # 4</summary>\n <updated-at type=\"datetime\">2009-03-08T20:46:47Z</updated-at>\n <user-id type=\"integer\">4</user-id>\n </topic>\n</topics>\n",
got: " " (using ==)
./spec/controllers/topics_controller_spec.rb:38:
./spec/controllers/topics_controller_spec.rb:3:
== Doesn't restrict user on show, create, update ==
2)
'TopicsController responding to GET show should belong to me - else, should not be actionable' FAILED
expected nil? to return true, got false
/Users/rjh/dev/6hats/spec/spec_helper.rb:178:
./spec/controllers/topics_controller_spec.rb:3:
5)
'TopicsController responding to POST create with valid params - posting to a different userid should redirect to the topic list' FAILED
expected redirect to "http://test.host/topics", got redirect to "http://test.host/topics/1"
./spec/controllers/topics_controller_spec.rb:130:
./spec/controllers/topics_controller_spec.rb:3:
'TopicsController responding to PUT update with valid params - posting to a different userid should not be allowed' FAILED
expected redirect to "http://test.host/topics", got redirect to "http://test.host/topics/1"
./spec/controllers/topics_controller_spec.rb:210:
./spec/controllers/topics_controller_spec.rb:3:
==
map.resources :topics do |topics|
topics.resources :thoughts, :except => [:index,:show]
end
module MyControllerExamples
module ExampleMethods
end
module ExampleGroupMethods
describe "belongs to me", :shared => true do
it "should belong to me - else, should not be actionable" do
@user = Factory(:user)
do_login( @user )
if( ! respond_to?(:assemble_belonging) )
"no supporting factory callback".should == "assemble_belonging(user) method to construct an object owned by this user"
end
do_action()
results = assemble_belonging( Factory(:user))
begin # check the options and give feedback to the developer, if they haven't provided enough info
if ! results.kind_of?(Hash)
"wrong return type".should == "assemble_belonging() should return a hash with :belonging => object, :assigns => :key, :or_redirect => url"
end
unless belonging = results[:belonging]
"no returned belonging object".should == "a generated object belonging to the passed user"
end
unless expectation = results[:assigns]
"no returned assigns symbol".should == "[:assigns] entry with the symbol that will be expected to be set if the object belongs to the passed user"
end
unless redir = results[:or_redirect]
"no returned redirection expectation".should == "[:or_redirect] entry with the url for redirection, if the current user can't access the object"
end
end
do_action(belonging)
assigns[expectation].should be_nil
if( ! response.redirect? )
pp response.body
end
response.should be_redirect
response.should redirect_to( redir )
flash[:warning].should =~ /Permission denied/
end
end
describe "login required", :shared => true do
it "should not be actionable if I'm not logged in" do
# if( ! respond_to?(:no_login_context) )
# "no supporting callback".should == ":no_login_context() method to perform any needed setup"
# end
# no_login_context()
do_login(nil)
do_action
response.should redirect_to( login_url )
flash[:notice].should_not be_blank
end
end
end
end
class TopicsController < ResourceController::Base
belongs_to :user
# def parent_type
# :user
# end
def parent_object
current_user
end
# current_user or redirect to login:
before_filter :fetch_user
public
# GET /topics
# GET /topics.xml
index.wants.html do |wants|
if @topics.size == 0
flash[:notice] = "You don't have any topics yet. Create a new one here."
redirect_to new_topic_url
end
end
# GET /topics/1
# GET /topics/1.xml
show.before do
@thoughts = @topic.thoughts if @topic
end
show.wants.xml do |wants|
render :xml => @topic.to_xml( :include => :thoughts )
end
# new_action.before do
# @topic = @user.topics.build()
# end
end
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe TopicsController do
def no_login_context
# no special context needed for this controller's no-login tests
end
def assemble_belonging(user)
@topic = Factory(:topic, :user => user)
{
:belonging => @topic,
:assigns => :topic,
:or_redirect => topics_url
}
end
describe "responding to GET index" do
it_should_behave_like "login required"
def do_action(user=nil)
get :index
end
describe "when there are topics" do
before do
do_login( @user = Factory( :user ))
@tlist = [1..5].map { |n| Factory( :topic, :user => @user ) }
end
it "should expose all topics as @topics" do
do_action
assigns[:topics].should == @tlist
end
describe "with mime type of xml" do
it "should render all topics as xml" do
request.env["HTTP_ACCEPT"] = "application/xml"
do_action
response.body.should == @tlist.to_xml
end
end
end
describe "when there are no topics" do
it "should render the topic creation page instead" do
do_login( @user = Factory(:user) )
do_action
response.should redirect_to(new_topic_url)
flash[:notice].should_not be_blank
end
end
end
describe "responding to GET show" do
def do_action(topic=nil)
get :show, :id => topic ? topic.id : 2987234
end
it_should_behave_like "login required"
it_should_behave_like "belongs to me"
it "should expose the requested topic as @topic" do
@topic = Factory(:topic)
do_login( @topic.user )
do_action(@topic)
assigns[:topic].should == @topic
assigns[:thoughts].should == []
end
describe "with mime type of xml" do
it "should render the requested topic as xml" do
request.env["HTTP_ACCEPT"] = "application/xml"
@topic = Factory(:topic)
do_login( @topic.user )
do_action( @topic )
response.should have_tag( "topic" ) do
with_tag("thoughts")
end
end
end
end
describe "responding to GET new" do
def do_action(topic=nil)
get :new
end
it_should_behave_like( "login required" )
it "should expose a new topic as @topic" do
do_login( @u = Factory(:user) )
do_action
assigns[:topic].should_not be_nil
assigns[:topic].user_id.should == @u.id
assigns[:topic].should be_new_record
end
end
describe "responding to GET edit" do
def do_action(topic=nil)
get :edit, :id => topic ? topic.id : 239879238742
end
it_should_behave_like "login required"
it_should_behave_like "belongs to me"
it "should expose the requested topic as @topic" do
@t = Factory(:topic)
do_login( @t.user )
do_action( @t )
assigns[:topic].should == @t
end
end
describe "responding to POST create" do
describe "with valid params" do
def do_action(user=nil)
topic = {:name => 'name', :summary => 'summary'}
topic[:user_id] = user.id if user
post :create, :topic => topic
end
it_should_behave_like "login required"
describe " - posting to a different userid" do
before do
do_login( Factory(:user) )
@other = Factory(:user)
do_action(@other)
end
it "should not be allowed" do
response.should_not be_success
end
it "should redirect to the topic list" do
response.should redirect_to(topics_url)
end
it "should show an error message" do
flash[:warning].should =~ /Permission denied/i
end
describe "in an XML request" do
before do
request.env["HTTP_ACCEPT"] = "application/xml"
do_login( Factory(:user) )
@other = Factory(:user)
do_action(@other)
end
it "should not be allowed" do
response.should_not be_success
end
end
end
describe "on success" do
before do
@user = Factory(:user)
@user.state = 'active'; @user.save
do_login(@user)
do_action(@user)
response.should_not redirect_to(topics_url)
@user = User.find_by_id( @user.id )
@topic = @user.topics.first
end
it "should expose a newly created topic as @topic" do
@user.topics.length.should == 1
assigns(:topic).should == @topic
end
it "should redirect to the created topic" do
response.should redirect_to(topic_url(@topic))
end
end
end
describe "with invalid params" do
before do
@topic = Factory.build(:topic, :name => 'f')
do_login( @topic.user )
post :create, :topic => { :name => @topic.name }
end
it "should be testing what we think we're testing" do
@topic.valid?.should_not be_true
end
it "should expose a newly created but unsaved topic as @topic" do
assigns(:topic).new_record?.should be_true
end
it "should re-render the 'new' template" do
response.should render_template('new')
end
end
end
describe "responding to PUT update" do
describe "with valid params" do
def do_action(topic=nil, user=nil)
topic = {:summary => 'updated summary'}
topic[:user_id] = user.id if user
put :update, :id => ( @topic ? @topic.id : 23423243 ) , :topic => topic
end
it_should_behave_like "login required"
before do
@topic = Factory(:topic)
do_login(@topic.user)
end
describe " - posting to a different userid" do
before do
do_login( @other = Factory(:user) )
end
it "should not be allowed" do
do_action( @topic, @other )
response.should_not be_success
response.should redirect_to(topics_url)
end
it "should not be allowed for XML" do
request.env["HTTP_ACCEPT"] = "application/xml"
do_action( @topic, @other )
response.should_not be_success
response.should_not redirect_to(topics_url)
response.should_not be_redirect
end
end
it "should update the requested topic" do
do_action( @topic )
@updated = @topic.user.topics.find_by_id( @topic.id )
@updated.summary.should == "updated summary"
end
it "should redirect to the topic" do
do_action( @topic )
response.should redirect_to( topic_url( @topic ) )
end
# describe "(in-place editing)" do
# it "should respond to an xhr request with the new field value"
# end
end
describe "with invalid params" do
before do
@topic = Factory(:topic)
do_login(@topic.user)
put :update, :id => @topic.id, :topic => { :name => 'f' }
end
it "should expose the topic as @topic" do
assigns(:topic).should == @topic
end
it "should re-render the 'edit' template" do
response.should render_template('edit')
end
end
end
describe "responding to DELETE destroy" do
it_should_behave_like "login required"
it_should_behave_like "belongs to me"
def do_action(topic=nil)
delete :destroy, :id => topic ? topic.id : 2342342
end
describe "when logged in" do
before do
@topic = Factory(:topic)
do_login( @topic.user )
do_action(@topic)
end
it "should destroy the requested topic" do
Topic.find_by_id( @topic.id ).should be_nil
end
it "should redirect to the topics list" do
response.should redirect_to(topics_url)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment