forked from bguthrie/resource_full
/
xml_spec.rb
98 lines (80 loc) · 3.93 KB
/
xml_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require File.dirname(__FILE__) + '/../../spec_helper'
describe "ResourceFull::Render::XML" , :type => :controller do
controller_name "resource_full_mock_users"
before :each do
ResourceFullMockUser.delete_all
ResourceFullMockUsersController.resource_identifier = :id
end
it "renders the model object" do
user = ResourceFullMockUser.create!
get :show, :id => user.id, :format => 'xml'
response.body.should have_tag("resource-full-mock-user")
response.code.should == '200'
end
it "renders the appropriate error message if it can't find the model object" do
get :show, :id => 1, :format => 'xml'
response.body.should have_tag("errors") { with_tag("error", "ActiveRecord::RecordNotFound: not found: 1") }
response.code.should == '404'
end
it "renders all model objects" do
2.times { ResourceFullMockUser.create! }
get :index, :format => 'xml'
Hash.from_xml(response.body)['resource_full_mock_users'].size.should == 2
response.code.should == '200'
end
it "creates and renders a new model object with an empty body" do
put :create, :resource_full_mock_user => { 'first_name' => 'brian' }, :format => 'xml'
response.body.should == ResourceFullMockUser.find(:first).to_xml
ResourceFullMockUser.find(:first).first_name.should == 'brian'
end
it "creates a new model object and returns a status code of 201 (created)" do
put :create, :resource_full_mock_user => { 'first_name' => 'brian' }, :format => 'xml'
response.code.should == '201'
end
it "creates a new model object and places the location of the new object in the Location header" do
put :create, :resource_full_mock_user => {}, :format => 'xml'
response.headers['Location'].should == resource_full_mock_user_url(ResourceFullMockUser.find(:first))
end
it "renders appropriate errors if a model validation fails" do
ResourceFullMockUser.send :define_method, :validate do
errors.add :first_name, "can't be blank" if self.first_name.blank?
end
put :create, :resource_full_mock_user => {}, :format => 'xml'
response.should have_tag("errors") { with_tag("error", "First name can't be blank")}
ResourceFullMockUser.send :remove_method, :validate
end
it "renders the XML for a new model object" do
get :new, :format => 'xml'
response.body.should == ResourceFullMockUser.new.to_xml
end
class SomeNonsenseException < Exception; end
it "rescues all unhandled exceptions with an XML response" do
ResourceFullMockUser.expects(:find).raises SomeNonsenseException, "sparrow farts"
get :index, :format => 'xml'
response.should have_tag("errors") { with_tag("error", "SomeNonsenseException: sparrow farts") }
end
it "it sends an exception notification email if ExceptionNotifier is enabled and still renders the XML error response" do
cleanup = unless defined? ExceptionNotifier
module ExceptionNotifier; end
module ExceptionNotifiable; end
true
end
ResourceFullMockUsersController.send :include, ExceptionNotifiable
ResourceFullMockUser.expects(:find).raises SomeNonsenseException, "sparrow farts"
ResourceFullMockUsersController.stubs(:exception_data).returns(nil)
ResourceFullMockUsersController.any_instance.stubs(:consider_all_requests_local).returns(false)
ResourceFullMockUsersController.any_instance.stubs(:local_request?).returns(false)
ExceptionNotifier.expects(:deliver_exception_notification)
get :index, :format => 'xml'
response.should have_tag("errors") { with_tag("error", "SomeNonsenseException: sparrow farts") }
if cleanup
Object.send :remove_const, :ExceptionNotifier
Object.send :remove_const, :ExceptionNotifiable
end
end
it "retains the generic error 500 when re-rendering unhandled exceptions" do
ResourceFullMockUser.expects(:find).raises SomeNonsenseException, "sparrow farts"
get :index, :format => 'xml'
response.code.should == '500'
end
end