##// END OF EJS Templates
Convert tests to shoulda...
Eric Davis -
r3651:f484fe855662
parent child
Show More
@@ -46,85 +46,140 class IssuesApiTest < ActionController::IntegrationTest
46 Setting.rest_api_enabled = '1'
46 Setting.rest_api_enabled = '1'
47 end
47 end
48
48
49 def test_index
49 context "/index.xml" do
50 setup do
50 get '/issues.xml'
51 get '/issues.xml'
51 assert_response :success
52 assert_equal 'application/xml', @response.content_type
53 end
52 end
54
53
55 def test_index_with_filter
54 should_respond_with :success
55 should_respond_with_content_type 'application/xml'
56 end
57
58 context "/index.xml with filter" do
59 setup do
56 get '/issues.xml?status_id=5'
60 get '/issues.xml?status_id=5'
57 assert_response :success
61 end
58 assert_equal 'application/xml', @response.content_type
62
63 should_respond_with :success
64 should_respond_with_content_type 'application/xml'
65 should "show only issues with the status_id" do
59 assert_tag :tag => 'issues',
66 assert_tag :tag => 'issues',
60 :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}),
67 :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}),
61 :only => { :tag => 'issue' } }
68 :only => { :tag => 'issue' } }
62 end
69 end
70 end
63
71
64 def test_show
72 context "/issues/1.xml" do
73 setup do
65 get '/issues/1.xml'
74 get '/issues/1.xml'
66 assert_response :success
67 assert_equal 'application/xml', @response.content_type
68 end
75 end
69
76
70 def test_create
77 should_respond_with :success
71 attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
78 should_respond_with_content_type 'application/xml'
72 assert_difference 'Issue.count' do
79 end
73 post '/issues.xml', {:issue => attributes}, :authorization => credentials('jsmith')
80
81 context "POST /issues.xml" do
82 setup do
83 @issue_count = Issue.count
84 @attributes = {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}
85 post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
74 end
86 end
75 assert_response :created
87
76 assert_equal 'application/xml', @response.content_type
88 should_respond_with :created
89 should_respond_with_content_type 'application/xml'
90
91 should "create an issue with the attributes" do
92 assert_equal Issue.count, @issue_count + 1
93
77 issue = Issue.first(:order => 'id DESC')
94 issue = Issue.first(:order => 'id DESC')
78 attributes.each do |attribute, value|
95 @attributes.each do |attribute, value|
79 assert_equal value, issue.send(attribute)
96 assert_equal value, issue.send(attribute)
80 end
97 end
81 end
98 end
99 end
82
100
83 def test_create_failure
101 context "POST /issues.xml with failure" do
84 attributes = {:project_id => 1}
102 setup do
85 assert_no_difference 'Issue.count' do
103 @attributes = {:project_id => 1}
86 post '/issues.xml', {:issue => attributes}, :authorization => credentials('jsmith')
104 post '/issues.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
87 end
105 end
88 assert_response :unprocessable_entity
106
89 assert_equal 'application/xml', @response.content_type
107 should_respond_with :unprocessable_entity
108 should_respond_with_content_type 'application/xml'
109
110 should "have an errors tag" do
90 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
111 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
91 end
112 end
113 end
114
115 context "PUT /issues/1.xml" do
116 setup do
117 @issue_count = Issue.count
118 @journal_count = Journal.count
119 @attributes = {:subject => 'API update'}
120
121 put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
122 end
123
124 should_respond_with :ok
125 should_respond_with_content_type 'application/xml'
92
126
93 def test_update
127 should "not create a new issue" do
94 attributes = {:subject => 'API update'}
128 assert_equal Issue.count, @issue_count
95 assert_no_difference 'Issue.count' do
96 assert_difference 'Journal.count' do
97 put '/issues/1.xml', {:issue => attributes}, :authorization => credentials('jsmith')
98 end
129 end
130
131 should "create a new journal" do
132 assert_equal Journal.count, @journal_count + 1
99 end
133 end
100 assert_response :ok
134
101 assert_equal 'application/xml', @response.content_type
135 should "update the issue" do
102 issue = Issue.find(1)
136 issue = Issue.find(1)
103 attributes.each do |attribute, value|
137 @attributes.each do |attribute, value|
104 assert_equal value, issue.send(attribute)
138 assert_equal value, issue.send(attribute)
105 end
139 end
106 end
140 end
107
141
108 def test_update_failure
109 attributes = {:subject => ''}
110 assert_no_difference 'Issue.count' do
111 assert_no_difference 'Journal.count' do
112 put '/issues/1.xml', {:issue => attributes}, :authorization => credentials('jsmith')
113 end
142 end
143
144 context "PUT /issues/1.xml with failed update" do
145 setup do
146 @attributes = {:subject => ''}
147 @issue_count = Issue.count
148 @journal_count = Journal.count
149
150 put '/issues/1.xml', {:issue => @attributes}, :authorization => credentials('jsmith')
151 end
152
153 should_respond_with :unprocessable_entity
154 should_respond_with_content_type 'application/xml'
155
156 should "not create a new issue" do
157 assert_equal Issue.count, @issue_count
158 end
159
160 should "not create a new journal" do
161 assert_equal Journal.count, @journal_count
114 end
162 end
115 assert_response :unprocessable_entity
163
116 assert_equal 'application/xml', @response.content_type
164 should "have an errors tag" do
117 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
165 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
118 end
166 end
167 end
119
168
120 def test_destroy
169 context "DELETE /issues/1.xml" do
121 assert_difference 'Issue.count', -1 do
170 setup do
171 @issue_count = Issue.count
122 delete '/issues/1.xml', {}, :authorization => credentials('jsmith')
172 delete '/issues/1.xml', {}, :authorization => credentials('jsmith')
123 end
173 end
124 assert_response :ok
174
125 assert_equal 'application/xml', @response.content_type
175 should_respond_with :ok
176 should_respond_with_content_type 'application/xml'
177
178 should "delete the issue" do
179 assert_equal Issue.count, @issue_count -1
126 assert_nil Issue.find_by_id(1)
180 assert_nil Issue.find_by_id(1)
127 end
181 end
182 end
128
183
129 def credentials(user, password=nil)
184 def credentials(user, password=nil)
130 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
185 ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
General Comments 0
You need to be logged in to leave comments. Login now