@@ -26,42 +26,96 class ApiTest::ProjectsTest < ActionController::IntegrationTest | |||
|
26 | 26 | Setting.rest_api_enabled = '1' |
|
27 | 27 | end |
|
28 | 28 |
|
|
29 | def test_index | |
|
29 | context "GET /projects" do | |
|
30 | context ".xml" do | |
|
31 | should "return projects" do | |
|
30 | 32 | get '/projects.xml' |
|
31 | 33 | assert_response :success |
|
32 | 34 | assert_equal 'application/xml', @response.content_type |
|
35 | ||
|
36 | assert_tag :tag => 'projects', | |
|
37 | :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}} | |
|
38 | end | |
|
33 | 39 | end |
|
34 | 40 | |
|
35 | context "GET /projects/2.xml" do | |
|
41 | context ".json" do | |
|
42 | should "return projects" do | |
|
43 | get '/projects.json' | |
|
44 | assert_response :success | |
|
45 | assert_equal 'application/json', @response.content_type | |
|
46 | ||
|
47 | json = ActiveSupport::JSON.decode(response.body) | |
|
48 | assert_kind_of Hash, json | |
|
49 | assert_kind_of Array, json['projects'] | |
|
50 | assert_kind_of Hash, json['projects'].first | |
|
51 | assert json['projects'].first.has_key?('id') | |
|
52 | end | |
|
53 | end | |
|
54 | end | |
|
55 | ||
|
56 | context "GET /projects/:id" do | |
|
57 | context ".xml" do | |
|
36 | 58 | # TODO: A private project is needed because should_allow_api_authentication |
|
37 | 59 | # actually tests that authentication is *required*, not just allowed |
|
38 | 60 | should_allow_api_authentication(:get, "/projects/2.xml") |
|
39 | end | |
|
40 | 61 | |
|
41 | def test_show | |
|
62 | should "return requested project" do | |
|
42 | 63 | get '/projects/1.xml' |
|
43 | 64 | assert_response :success |
|
44 | 65 | assert_equal 'application/xml', @response.content_type |
|
45 | assert_tag 'custom_field', :attributes => {:name => 'Development status'}, :content => 'Stable' | |
|
66 | ||
|
67 | assert_tag :tag => 'project', | |
|
68 | :child => {:tag => 'id', :content => '1'} | |
|
69 | assert_tag :tag => 'custom_field', | |
|
70 | :attributes => {:name => 'Development status'}, :content => 'Stable' | |
|
46 | 71 | end |
|
47 | 72 | |
|
48 | def test_show_should_not_display_hidden_custom_fields | |
|
73 | context "with hidden custom fields" do | |
|
74 | setup do | |
|
49 | 75 | ProjectCustomField.find_by_name('Development status').update_attribute :visible, false |
|
76 | end | |
|
77 | ||
|
78 | should "not display hidden custom fields" do | |
|
50 | 79 | get '/projects/1.xml' |
|
51 | 80 | assert_response :success |
|
52 | 81 | assert_equal 'application/xml', @response.content_type |
|
53 | assert_no_tag 'custom_field', :attributes => {:name => 'Development status'} | |
|
82 | ||
|
83 | assert_no_tag 'custom_field', | |
|
84 | :attributes => {:name => 'Development status'} | |
|
85 | end | |
|
54 | 86 | end |
|
87 | end | |
|
88 | ||
|
89 | context ".json" do | |
|
90 | should_allow_api_authentication(:get, "/projects/2.json") | |
|
91 | ||
|
92 | should "return requested project" do | |
|
93 | get '/projects/1.json' | |
|
55 | 94 | |
|
56 | context "POST /projects.xml" do | |
|
95 | json = ActiveSupport::JSON.decode(response.body) | |
|
96 | assert_kind_of Hash, json | |
|
97 | assert_kind_of Hash, json['project'] | |
|
98 | assert_equal 1, json['project']['id'] | |
|
99 | end | |
|
100 | end | |
|
101 | end | |
|
102 | ||
|
103 | context "POST /projects" do | |
|
104 | context "with valid parameters" do | |
|
105 | setup do | |
|
106 | @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}} | |
|
107 | end | |
|
108 | ||
|
109 | context ".xml" do | |
|
57 | 110 | should_allow_api_authentication(:post, |
|
58 | 111 | '/projects.xml', |
|
59 | 112 | {:project => {:name => 'API test', :identifier => 'api-test'}}, |
|
60 | 113 | {:success_code => :created}) |
|
61 | 114 | |
|
115 | ||
|
62 | 116 | should "create a project with the attributes" do |
|
63 | 117 | assert_difference('Project.count') do |
|
64 |
post '/projects.xml', |
|
|
118 | post '/projects.xml', @parameters, :authorization => credentials('admin') | |
|
65 | 119 | end |
|
66 | 120 | |
|
67 | 121 | project = Project.first(:order => 'id DESC') |
@@ -73,26 +127,42 class ApiTest::ProjectsTest < ActionController::IntegrationTest | |||
|
73 | 127 | assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} |
|
74 | 128 | end |
|
75 | 129 | end |
|
130 | end | |
|
76 | 131 | |
|
77 | def test_create_failure | |
|
78 | attributes = {:name => 'API test'} | |
|
79 | assert_no_difference 'Project.count' do | |
|
80 | post '/projects.xml', {:project => attributes}, :authorization => credentials('admin') | |
|
132 | context "with invalid parameters" do | |
|
133 | setup do | |
|
134 | @parameters = {:project => {:name => 'API test'}} | |
|
135 | end | |
|
136 | ||
|
137 | context ".xml" do | |
|
138 | should "return errors" do | |
|
139 | assert_no_difference('Project.count') do | |
|
140 | post '/projects.xml', @parameters, :authorization => credentials('admin') | |
|
81 | 141 | end |
|
142 | ||
|
82 | 143 | assert_response :unprocessable_entity |
|
83 | 144 | assert_equal 'application/xml', @response.content_type |
|
84 |
assert_tag |
|
|
145 | assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"} | |
|
146 | end | |
|
147 | end | |
|
148 | end | |
|
85 | 149 | end |
|
86 | 150 | |
|
87 |
context "PUT /projects/ |
|
|
151 | context "PUT /projects/:id" do | |
|
152 | context "with valid parameters" do | |
|
153 | setup do | |
|
154 | @parameters = {:project => {:name => 'API update'}} | |
|
155 | end | |
|
156 | ||
|
157 | context ".xml" do | |
|
88 | 158 | should_allow_api_authentication(:put, |
|
89 | 159 | '/projects/2.xml', |
|
90 |
{:project => {:name => 'API te |
|
|
160 | {:project => {:name => 'API update'}}, | |
|
91 | 161 | {:success_code => :ok}) |
|
92 | 162 | |
|
93 | 163 | should "update the project" do |
|
94 | 164 | assert_no_difference 'Project.count' do |
|
95 |
put '/projects/2.xml', |
|
|
165 | put '/projects/2.xml', @parameters, :authorization => credentials('jsmith') | |
|
96 | 166 | end |
|
97 | 167 | assert_response :ok |
|
98 | 168 | assert_equal 'application/xml', @response.content_type |
@@ -100,18 +170,29 class ApiTest::ProjectsTest < ActionController::IntegrationTest | |||
|
100 | 170 | assert_equal 'API update', project.name |
|
101 | 171 | end |
|
102 | 172 | end |
|
173 | end | |
|
103 | 174 | |
|
104 | def test_update_failure | |
|
105 | attributes = {:name => ''} | |
|
106 | assert_no_difference 'Project.count' do | |
|
107 | put '/projects/1.xml', {:project => attributes}, :authorization => credentials('jsmith') | |
|
175 | context "with invalid parameters" do | |
|
176 | setup do | |
|
177 | @parameters = {:project => {:name => ''}} | |
|
178 | end | |
|
179 | ||
|
180 | context ".xml" do | |
|
181 | should "return errors" do | |
|
182 | assert_no_difference('Project.count') do | |
|
183 | put '/projects/2.xml', @parameters, :authorization => credentials('admin') | |
|
108 | 184 | end |
|
185 | ||
|
109 | 186 | assert_response :unprocessable_entity |
|
110 | 187 | assert_equal 'application/xml', @response.content_type |
|
111 |
assert_tag |
|
|
188 | assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"} | |
|
189 | end | |
|
190 | end | |
|
191 | end | |
|
112 | 192 | end |
|
113 | 193 | |
|
114 |
context "DELETE /projects/ |
|
|
194 | context "DELETE /projects/:id" do | |
|
195 | context ".xml" do | |
|
115 | 196 | should_allow_api_authentication(:delete, |
|
116 | 197 | '/projects/2.xml', |
|
117 | 198 | {}, |
@@ -125,6 +206,7 class ApiTest::ProjectsTest < ActionController::IntegrationTest | |||
|
125 | 206 | assert_nil Project.find_by_id(2) |
|
126 | 207 | end |
|
127 | 208 | end |
|
209 | end | |
|
128 | 210 | |
|
129 | 211 | def credentials(user, password=nil) |
|
130 | 212 | ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user) |
General Comments 0
You need to be logged in to leave comments.
Login now