Removed some shoulda context.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11862 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2013-05-18 08:27:53 +00:00
parent 5b29964512
commit 0d4bb7558f
7 changed files with 403 additions and 527 deletions

View File

@ -24,20 +24,14 @@ class Redmine::ApiTest::EnumerationsTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "/enumerations/issue_priorities" do test "GET /enumerations/issue_priorities.xml should return priorities" do
context "GET" do get '/enumerations/issue_priorities.xml'
assert_response :success
should "return priorities" do assert_equal 'application/xml', response.content_type
get '/enumerations/issue_priorities.xml' assert_select 'issue_priorities[type=array]' do
assert_select 'issue_priority' do
assert_response :success assert_select 'id', :text => '6'
assert_equal 'application/xml', response.content_type assert_select 'name', :text => 'High'
assert_select 'issue_priorities[type=array]' do
assert_select 'issue_priority' do
assert_select 'id', :text => '6'
assert_select 'name', :text => 'High'
end
end
end end
end end
end end

View File

@ -24,189 +24,147 @@ class Redmine::ApiTest::GroupsTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "GET /groups" do test "GET /groups.xml should require authentication" do
context ".xml" do get '/groups.xml'
should "require authentication" do assert_response 401
get '/groups.xml' end
assert_response 401
end
should "return groups" do test "GET /groups.xml should return groups" do
get '/groups.xml', {}, credentials('admin') get '/groups.xml', {}, credentials('admin')
assert_response :success assert_response :success
assert_equal 'application/xml', response.content_type assert_equal 'application/xml', response.content_type
assert_select 'groups' do assert_select 'groups' do
assert_select 'group' do assert_select 'group' do
assert_select 'name', :text => 'A Team' assert_select 'name', :text => 'A Team'
assert_select 'id', :text => '10' assert_select 'id', :text => '10'
end
end
end
end
context ".json" do
should "require authentication" do
get '/groups.json'
assert_response 401
end
should "return groups" do
get '/groups.json', {}, credentials('admin')
assert_response :success
assert_equal 'application/json', response.content_type
json = MultiJson.load(response.body)
groups = json['groups']
assert_kind_of Array, groups
group = groups.detect {|g| g['name'] == 'A Team'}
assert_not_nil group
assert_equal({'id' => 10, 'name' => 'A Team'}, group)
end end
end end
end end
context "GET /groups/:id" do test "GET /groups.json should require authentication" do
context ".xml" do get '/groups.json'
should "return the group with its users" do assert_response 401
get '/groups/10.xml', {}, credentials('admin') end
assert_response :success
assert_equal 'application/xml', response.content_type
assert_select 'group' do test "GET /groups.json should return groups" do
assert_select 'name', :text => 'A Team' get '/groups.json', {}, credentials('admin')
assert_select 'id', :text => '10' assert_response :success
end assert_equal 'application/json', response.content_type
end
should "include users if requested" do json = MultiJson.load(response.body)
get '/groups/10.xml?include=users', {}, credentials('admin') groups = json['groups']
assert_response :success assert_kind_of Array, groups
assert_equal 'application/xml', response.content_type group = groups.detect {|g| g['name'] == 'A Team'}
assert_not_nil group
assert_equal({'id' => 10, 'name' => 'A Team'}, group)
end
assert_select 'group' do test "GET /groups/:id.xml should return the group with its users" do
assert_select 'users' do get '/groups/10.xml', {}, credentials('admin')
assert_select 'user', Group.find(10).users.count assert_response :success
assert_select 'user[id=8]' assert_equal 'application/xml', response.content_type
end
end
end
should "include memberships if requested" do assert_select 'group' do
get '/groups/10.xml?include=memberships', {}, credentials('admin') assert_select 'name', :text => 'A Team'
assert_response :success assert_select 'id', :text => '10'
assert_equal 'application/xml', response.content_type end
end
assert_select 'group' do test "GET /groups/:id.xml should include users if requested" do
assert_select 'memberships' get '/groups/10.xml?include=users', {}, credentials('admin')
end assert_response :success
assert_equal 'application/xml', response.content_type
assert_select 'group' do
assert_select 'users' do
assert_select 'user', Group.find(10).users.count
assert_select 'user[id=8]'
end end
end end
end end
context "POST /groups" do test "GET /groups/:id.xml include memberships if requested" do
context "with valid parameters" do get '/groups/10.xml?include=memberships', {}, credentials('admin')
context ".xml" do assert_response :success
should "create groups" do assert_equal 'application/xml', response.content_type
assert_difference('Group.count') do
post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
assert_response :created
assert_equal 'application/xml', response.content_type
end
group = Group.order('id DESC').first assert_select 'group' do
assert_equal 'Test', group.name assert_select 'memberships'
assert_equal [2, 3], group.users.map(&:id).sort
assert_select 'group' do
assert_select 'name', :text => 'Test'
end
end
end
end
context "with invalid parameters" do
context ".xml" do
should "return errors" do
assert_no_difference('Group.count') do
post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
end
assert_response :unprocessable_entity
assert_equal 'application/xml', response.content_type
assert_select 'errors' do
assert_select 'error', :text => /Name can't be blank/
end
end
end
end end
end end
context "PUT /groups/:id" do test "POST /groups.xml with valid parameters should create the group" do
context "with valid parameters" do assert_difference('Group.count') do
context ".xml" do post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
should "update the group" do assert_response :created
put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin') assert_equal 'application/xml', response.content_type
assert_response :ok
assert_equal '', @response.body
group = Group.find(10)
assert_equal 'New name', group.name
assert_equal [2, 3], group.users.map(&:id).sort
end
end
end end
context "with invalid parameters" do group = Group.order('id DESC').first
context ".xml" do assert_equal 'Test', group.name
should "return errors" do assert_equal [2, 3], group.users.map(&:id).sort
put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin')
assert_response :unprocessable_entity
assert_equal 'application/xml', response.content_type
assert_select 'errors' do assert_select 'group' do
assert_select 'error', :text => /Name can't be blank/ assert_select 'name', :text => 'Test'
end
end
end
end end
end end
context "DELETE /groups/:id" do test "POST /groups.xml with invalid parameters should return errors" do
context ".xml" do assert_no_difference('Group.count') do
should "delete the group" do post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
assert_difference 'Group.count', -1 do end
delete '/groups/10.xml', {}, credentials('admin') assert_response :unprocessable_entity
assert_response :ok assert_equal 'application/xml', response.content_type
assert_equal '', @response.body
end assert_select 'errors' do
end assert_select 'error', :text => /Name can't be blank/
end end
end end
context "POST /groups/:id/users" do test "PUT /groups/:id.xml with valid parameters should update the group" do
context ".xml" do put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin')
should "add user to the group" do assert_response :ok
assert_difference 'Group.find(10).users.count' do assert_equal '', @response.body
post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
assert_response :ok group = Group.find(10)
assert_equal '', @response.body assert_equal 'New name', group.name
end assert_equal [2, 3], group.users.map(&:id).sort
assert_include User.find(5), Group.find(10).users end
end
test "PUT /groups/:id.xml with invalid parameters should return errors" do
put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin')
assert_response :unprocessable_entity
assert_equal 'application/xml', response.content_type
assert_select 'errors' do
assert_select 'error', :text => /Name can't be blank/
end end
end end
context "DELETE /groups/:id/users/:user_id" do test "DELETE /groups/:id.xml should delete the group" do
context ".xml" do assert_difference 'Group.count', -1 do
should "remove user from the group" do delete '/groups/10.xml', {}, credentials('admin')
assert_difference 'Group.find(10).users.count', -1 do assert_response :ok
delete '/groups/10/users/8.xml', {}, credentials('admin') assert_equal '', @response.body
assert_response :ok
assert_equal '', @response.body
end
assert_not_include User.find(8), Group.find(10).users
end
end end
end end
test "POST /groups/:id/users.xml should add user to the group" do
assert_difference 'Group.find(10).users.count' do
post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
assert_response :ok
assert_equal '', @response.body
end
assert_include User.find(5), Group.find(10).users
end
test "DELETE /groups/:id/users/:user_id.xml should remove user from the group" do
assert_difference 'Group.find(10).users.count', -1 do
delete '/groups/10/users/8.xml', {}, credentials('admin')
assert_response :ok
assert_equal '', @response.body
end
assert_not_include User.find(8), Group.find(10).users
end
end end

View File

@ -28,99 +28,83 @@ class Redmine::ApiTest::IssueCategoriesTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "GET /projects/:project_id/issue_categories.xml" do test "GET /projects/:project_id/issue_categories.xml should return the issue categories" do
should "return issue categories" do get '/projects/1/issue_categories.xml', {}, credentials('jsmith')
get '/projects/1/issue_categories.xml', {}, credentials('jsmith') assert_response :success
assert_response :success assert_equal 'application/xml', @response.content_type
assert_equal 'application/xml', @response.content_type assert_tag :tag => 'issue_categories',
assert_tag :tag => 'issue_categories', :child => {:tag => 'issue_category', :child => {:tag => 'id', :content => '2'}}
:child => {:tag => 'issue_category', :child => {:tag => 'id', :content => '2'}}
end
end end
context "GET /issue_categories/2.xml" do test "GET /issue_categories/:id.xml should return the issue category" do
should "return requested issue category" do get '/issue_categories/2.xml', {}, credentials('jsmith')
get '/issue_categories/2.xml', {}, credentials('jsmith') assert_response :success
assert_response :success assert_equal 'application/xml', @response.content_type
assert_equal 'application/xml', @response.content_type assert_tag :tag => 'issue_category',
assert_tag :tag => 'issue_category', :child => {:tag => 'id', :content => '2'}
:child => {:tag => 'id', :content => '2'}
end
end end
context "POST /projects/:project_id/issue_categories.xml" do test "POST /projects/:project_id/issue_categories.xml should return create issue category" do
should "return create issue category" do assert_difference 'IssueCategory.count' do
assert_difference 'IssueCategory.count' do post '/projects/1/issue_categories.xml', {:issue_category => {:name => 'API'}}, credentials('jsmith')
post '/projects/1/issue_categories.xml', {:issue_category => {:name => 'API'}}, credentials('jsmith')
end
assert_response :created
assert_equal 'application/xml', @response.content_type
category = IssueCategory.first(:order => 'id DESC')
assert_equal 'API', category.name
assert_equal 1, category.project_id
end end
assert_response :created
assert_equal 'application/xml', @response.content_type
context "with invalid parameters" do category = IssueCategory.first(:order => 'id DESC')
should "return errors" do assert_equal 'API', category.name
assert_no_difference 'IssueCategory.count' do assert_equal 1, category.project_id
post '/projects/1/issue_categories.xml', {:issue_category => {:name => ''}}, credentials('jsmith')
end
assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type
assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
end
end
end end
context "PUT /issue_categories/2.xml" do test "POST /projects/:project_id/issue_categories.xml with invalid parameters should return errors" do
context "with valid parameters" do assert_no_difference 'IssueCategory.count' do
should "update issue category" do post '/projects/1/issue_categories.xml', {:issue_category => {:name => ''}}, credentials('jsmith')
assert_no_difference 'IssueCategory.count' do
put '/issue_categories/2.xml', {:issue_category => {:name => 'API Update'}}, credentials('jsmith')
end
assert_response :ok
assert_equal '', @response.body
assert_equal 'API Update', IssueCategory.find(2).name
end
end end
assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type
context "with invalid parameters" do assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
should "return errors" do
assert_no_difference 'IssueCategory.count' do
put '/issue_categories/2.xml', {:issue_category => {:name => ''}}, credentials('jsmith')
end
assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type
assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
end
end
end end
context "DELETE /issue_categories/1.xml" do test "PUT /issue_categories/:id.xml with valid parameters should update the issue category" do
should "destroy issue categories" do assert_no_difference 'IssueCategory.count' do
assert_difference 'IssueCategory.count', -1 do put '/issue_categories/2.xml', {:issue_category => {:name => 'API Update'}}, credentials('jsmith')
delete '/issue_categories/1.xml', {}, credentials('jsmith')
end
assert_response :ok
assert_equal '', @response.body
assert_nil IssueCategory.find_by_id(1)
end end
assert_response :ok
assert_equal '', @response.body
assert_equal 'API Update', IssueCategory.find(2).name
end
should "reassign issues with :reassign_to_id param" do test "PUT /issue_categories/:id.xml with invalid parameters should return errors" do
issue_count = Issue.count(:conditions => {:category_id => 1}) assert_no_difference 'IssueCategory.count' do
assert issue_count > 0 put '/issue_categories/2.xml', {:issue_category => {:name => ''}}, credentials('jsmith')
assert_difference 'IssueCategory.count', -1 do
assert_difference 'Issue.count(:conditions => {:category_id => 2})', 3 do
delete '/issue_categories/1.xml', {:reassign_to_id => 2}, credentials('jsmith')
end
end
assert_response :ok
assert_equal '', @response.body
assert_nil IssueCategory.find_by_id(1)
end end
assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type
assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
end
test "DELETE /issue_categories/:id.xml should destroy the issue category" do
assert_difference 'IssueCategory.count', -1 do
delete '/issue_categories/1.xml', {}, credentials('jsmith')
end
assert_response :ok
assert_equal '', @response.body
assert_nil IssueCategory.find_by_id(1)
end
test "DELETE /issue_categories/:id.xml should reassign issues with :reassign_to_id param" do
issue_count = Issue.count(:conditions => {:category_id => 1})
assert issue_count > 0
assert_difference 'IssueCategory.count', -1 do
assert_difference 'Issue.count(:conditions => {:category_id => 2})', 3 do
delete '/issue_categories/1.xml', {:reassign_to_id => 2}, credentials('jsmith')
end
end
assert_response :ok
assert_equal '', @response.body
assert_nil IssueCategory.find_by_id(1)
end end
end end

View File

@ -31,76 +31,62 @@ class Redmine::ApiTest::IssueRelationsTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "/issues/:issue_id/relations" do test "GET /issues/:issue_id/relations.xml should return issue relations" do
context "GET" do get '/issues/9/relations.xml', {}, credentials('jsmith')
should "return issue relations" do
get '/issues/9/relations.xml', {}, credentials('jsmith')
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'relations', assert_tag :tag => 'relations',
:attributes => { :type => 'array' }, :attributes => { :type => 'array' },
:child => { :child => {
:tag => 'relation', :tag => 'relation',
:child => { :child => {
:tag => 'id', :tag => 'id',
:content => '1' :content => '1'
} }
} }
end
end
context "POST" do
should "create a relation" do
assert_difference('IssueRelation.count') do
post '/issues/2/relations.xml', {:relation => {:issue_to_id => 7, :relation_type => 'relates'}}, credentials('jsmith')
end
relation = IssueRelation.first(:order => 'id DESC')
assert_equal 2, relation.issue_from_id
assert_equal 7, relation.issue_to_id
assert_equal 'relates', relation.relation_type
assert_response :created
assert_equal 'application/xml', @response.content_type
assert_tag 'relation', :child => {:tag => 'id', :content => relation.id.to_s}
end
context "with failure" do
should "return the errors" do
assert_no_difference('IssueRelation.count') do
post '/issues/2/relations.xml', {:relation => {:issue_to_id => 7, :relation_type => 'foo'}}, credentials('jsmith')
end
assert_response :unprocessable_entity
assert_tag :errors, :child => {:tag => 'error', :content => /relation_type is not included in the list/}
end
end
end
end end
context "/relations/:id" do test "POST /issues/:issue_id/relations.xml should create the relation" do
context "GET" do assert_difference('IssueRelation.count') do
should "return the relation" do post '/issues/2/relations.xml', {:relation => {:issue_to_id => 7, :relation_type => 'relates'}}, credentials('jsmith')
get '/relations/2.xml', {}, credentials('jsmith')
assert_response :success
assert_equal 'application/xml', @response.content_type
assert_tag 'relation', :child => {:tag => 'id', :content => '2'}
end
end end
context "DELETE" do relation = IssueRelation.first(:order => 'id DESC')
should "delete the relation" do assert_equal 2, relation.issue_from_id
assert_difference('IssueRelation.count', -1) do assert_equal 7, relation.issue_to_id
delete '/relations/2.xml', {}, credentials('jsmith') assert_equal 'relates', relation.relation_type
end
assert_response :ok assert_response :created
assert_equal '', @response.body assert_equal 'application/xml', @response.content_type
assert_nil IssueRelation.find_by_id(2) assert_tag 'relation', :child => {:tag => 'id', :content => relation.id.to_s}
end end
test "POST /issues/:issue_id/relations.xml with failure should return errors" do
assert_no_difference('IssueRelation.count') do
post '/issues/2/relations.xml', {:relation => {:issue_to_id => 7, :relation_type => 'foo'}}, credentials('jsmith')
end end
assert_response :unprocessable_entity
assert_tag :errors, :child => {:tag => 'error', :content => /relation_type is not included in the list/}
end
test "GET /relations/:id.xml should return the relation" do
get '/relations/2.xml', {}, credentials('jsmith')
assert_response :success
assert_equal 'application/xml', @response.content_type
assert_tag 'relation', :child => {:tag => 'id', :content => '2'}
end
test "DELETE /relations/:id.xml should delete the relation" do
assert_difference('IssueRelation.count', -1) do
delete '/relations/2.xml', {}, credentials('jsmith')
end
assert_response :ok
assert_equal '', @response.body
assert_nil IssueRelation.find_by_id(2)
end end
end end

View File

@ -24,28 +24,23 @@ class Redmine::ApiTest::IssueStatusesTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "/issue_statuses" do test "GET /issue_statuses.xml should return issue statuses" do
context "GET" do get '/issue_statuses.xml'
should "return issue statuses" do assert_response :success
get '/issue_statuses.xml' assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'issue_statuses',
assert_response :success :attributes => {:type => 'array'},
assert_equal 'application/xml', @response.content_type :child => {
assert_tag :tag => 'issue_statuses', :tag => 'issue_status',
:attributes => {:type => 'array'}, :child => {
:child => { :tag => 'id',
:tag => 'issue_status', :content => '2',
:child => { :sibling => {
:tag => 'id', :tag => 'name',
:content => '2', :content => 'Assigned'
:sibling => {
:tag => 'name',
:content => 'Assigned'
}
}
} }
end }
end }
end end
end end

View File

@ -24,177 +24,149 @@ class Redmine::ApiTest::MembershipsTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "/projects/:project_id/memberships" do test "GET /projects/:project_id/memberships.xml should return memberships" do
context "GET" do get '/projects/1/memberships.xml', {}, credentials('jsmith')
context "xml" do
should "return memberships" do
get '/projects/1/memberships.xml', {}, credentials('jsmith')
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'memberships', assert_tag :tag => 'memberships',
:attributes => {:type => 'array'}, :attributes => {:type => 'array'},
:child => { :child => {
:tag => 'membership', :tag => 'membership',
:child => {
:tag => 'id',
:content => '2',
:sibling => {
:tag => 'user',
:attributes => {:id => '3', :name => 'Dave Lopper'},
:sibling => {
:tag => 'roles',
:child => { :child => {
:tag => 'id', :tag => 'role',
:content => '2', :attributes => {:id => '2', :name => 'Developer'}
:sibling => {
:tag => 'user',
:attributes => {:id => '3', :name => 'Dave Lopper'},
:sibling => {
:tag => 'roles',
:child => {
:tag => 'role',
:attributes => {:id => '2', :name => 'Developer'}
}
}
}
} }
} }
end }
end }
}
end
context "json" do test "GET /projects/:project_id/memberships.json should return memberships" do
should "return memberships" do get '/projects/1/memberships.json', {}, credentials('jsmith')
get '/projects/1/memberships.json', {}, credentials('jsmith')
assert_response :success assert_response :success
assert_equal 'application/json', @response.content_type assert_equal 'application/json', @response.content_type
json = ActiveSupport::JSON.decode(response.body) json = ActiveSupport::JSON.decode(response.body)
assert_equal({ assert_equal({
"memberships" => "memberships" =>
[{"id"=>1, [{"id"=>1,
"project" => {"name"=>"eCookbook", "id"=>1}, "project" => {"name"=>"eCookbook", "id"=>1},
"roles" => [{"name"=>"Manager", "id"=>1}], "roles" => [{"name"=>"Manager", "id"=>1}],
"user" => {"name"=>"John Smith", "id"=>2}}, "user" => {"name"=>"John Smith", "id"=>2}},
{"id"=>2, {"id"=>2,
"project" => {"name"=>"eCookbook", "id"=>1}, "project" => {"name"=>"eCookbook", "id"=>1},
"roles" => [{"name"=>"Developer", "id"=>2}], "roles" => [{"name"=>"Developer", "id"=>2}],
"user" => {"name"=>"Dave Lopper", "id"=>3}}], "user" => {"name"=>"Dave Lopper", "id"=>3}}],
"limit" => 25, "limit" => 25,
"total_count" => 2, "total_count" => 2,
"offset" => 0}, "offset" => 0},
json) json)
end end
end
end
context "POST" do test "POST /projects/:project_id/memberships.xml should create the membership" do
context "xml" do assert_difference 'Member.count' do
should "create membership" do post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith')
assert_difference 'Member.count' do
post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith')
assert_response :created assert_response :created
end
end
should "return errors on failure" do
assert_no_difference 'Member.count' do
post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith')
assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type
assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"}
end
end
end
end end
end end
context "/memberships/:id" do test "POST /projects/:project_id/memberships.xml with invalid parameters should return errors" do
context "GET" do assert_no_difference 'Member.count' do
context "xml" do post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith')
should "return the membership" do
get '/memberships/2.xml', {}, credentials('jsmith')
assert_response :success assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'membership', assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"}
end
end
test "GET /memberships/:id.xml should return the membership" do
get '/memberships/2.xml', {}, credentials('jsmith')
assert_response :success
assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'membership',
:child => {
:tag => 'id',
:content => '2',
:sibling => {
:tag => 'user',
:attributes => {:id => '3', :name => 'Dave Lopper'},
:sibling => {
:tag => 'roles',
:child => { :child => {
:tag => 'id', :tag => 'role',
:content => '2', :attributes => {:id => '2', :name => 'Developer'}
:sibling => {
:tag => 'user',
:attributes => {:id => '3', :name => 'Dave Lopper'},
:sibling => {
:tag => 'roles',
:child => {
:tag => 'role',
:attributes => {:id => '2', :name => 'Developer'}
}
}
}
} }
end }
end }
}
end
context "json" do test "GET /memberships/:id.json should return the membership" do
should "return the membership" do get '/memberships/2.json', {}, credentials('jsmith')
get '/memberships/2.json', {}, credentials('jsmith')
assert_response :success assert_response :success
assert_equal 'application/json', @response.content_type assert_equal 'application/json', @response.content_type
json = ActiveSupport::JSON.decode(response.body) json = ActiveSupport::JSON.decode(response.body)
assert_equal( assert_equal(
{"membership" => { {"membership" => {
"id" => 2, "id" => 2,
"project" => {"name"=>"eCookbook", "id"=>1}, "project" => {"name"=>"eCookbook", "id"=>1},
"roles" => [{"name"=>"Developer", "id"=>2}], "roles" => [{"name"=>"Developer", "id"=>2}],
"user" => {"name"=>"Dave Lopper", "id"=>3}} "user" => {"name"=>"Dave Lopper", "id"=>3}}
}, },
json) json)
end end
end
test "PUT /memberships/:id.xml should update the membership" do
assert_not_equal [1,2], Member.find(2).role_ids.sort
assert_no_difference 'Member.count' do
put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith')
assert_response :ok
assert_equal '', @response.body
end end
member = Member.find(2)
assert_equal [1,2], member.role_ids.sort
end
context "PUT" do test "PUT /memberships/:id.xml with invalid parameters should return errors" do
context "xml" do put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [99]}}, credentials('jsmith')
should "update membership" do
assert_not_equal [1,2], Member.find(2).role_ids.sort
assert_no_difference 'Member.count' do
put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith')
assert_response :ok assert_response :unprocessable_entity
assert_equal '', @response.body assert_equal 'application/xml', @response.content_type
end assert_tag 'errors', :child => {:tag => 'error', :content => /member_roles is invalid/}
member = Member.find(2) end
assert_equal [1,2], member.role_ids.sort
end
should "return errors on failure" do test "DELETE /memberships/:id.xml should destroy the membership" do
put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [99]}}, credentials('jsmith') assert_difference 'Member.count', -1 do
delete '/memberships/2.xml', {}, credentials('jsmith')
assert_response :unprocessable_entity assert_response :ok
assert_equal 'application/xml', @response.content_type assert_equal '', @response.body
assert_tag 'errors', :child => {:tag => 'error', :content => /member_roles is invalid/}
end
end
end end
assert_nil Member.find_by_id(2)
end
context "DELETE" do test "DELETE /memberships/:id.xml should respond with 422 on failure" do
context "xml" do assert_no_difference 'Member.count' do
should "destroy membership" do # A membership with an inherited role can't be deleted
assert_difference 'Member.count', -1 do Member.find(2).member_roles.first.update_attribute :inherited_from, 99
delete '/memberships/2.xml', {}, credentials('jsmith') delete '/memberships/2.xml', {}, credentials('jsmith')
assert_response :ok assert_response :unprocessable_entity
assert_equal '', @response.body
end
assert_nil Member.find_by_id(2)
end
should "respond with 422 on failure" do
assert_no_difference 'Member.count' do
# A membership with an inherited role can't be deleted
Member.find(2).member_roles.first.update_attribute :inherited_from, 99
delete '/memberships/2.xml', {}, credentials('jsmith')
assert_response :unprocessable_entity
end
end
end
end end
end end
end end

View File

@ -31,67 +31,54 @@ class Redmine::ApiTest::NewsTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "GET /news" do should_allow_api_authentication(:get, "/projects/onlinestore/news.xml")
context ".xml" do should_allow_api_authentication(:get, "/projects/onlinestore/news.json")
should "return news" do
get '/news.xml'
assert_tag :tag => 'news', test "GET /news.xml should return news" do
:attributes => {:type => 'array'}, get '/news.xml'
:child => {
:tag => 'news',
:child => {
:tag => 'id',
:content => '2'
}
}
end
end
context ".json" do assert_tag :tag => 'news',
should "return news" do :attributes => {:type => 'array'},
get '/news.json' :child => {
:tag => 'news',
json = ActiveSupport::JSON.decode(response.body) :child => {
assert_kind_of Hash, json :tag => 'id',
assert_kind_of Array, json['news'] :content => '2'
assert_kind_of Hash, json['news'].first }
assert_equal 2, json['news'].first['id'] }
end
end
end end
context "GET /projects/:project_id/news" do test "GET /news.json should return news" do
context ".xml" do get '/news.json'
should_allow_api_authentication(:get, "/projects/onlinestore/news.xml")
should "return news" do json = ActiveSupport::JSON.decode(response.body)
get '/projects/ecookbook/news.xml' assert_kind_of Hash, json
assert_kind_of Array, json['news']
assert_kind_of Hash, json['news'].first
assert_equal 2, json['news'].first['id']
end
assert_tag :tag => 'news', test "GET /projects/:project_id/news.xml should return news" do
:attributes => {:type => 'array'}, get '/projects/ecookbook/news.xml'
:child => {
:tag => 'news',
:child => {
:tag => 'id',
:content => '2'
}
}
end
end
context ".json" do assert_tag :tag => 'news',
should_allow_api_authentication(:get, "/projects/onlinestore/news.json") :attributes => {:type => 'array'},
:child => {
:tag => 'news',
:child => {
:tag => 'id',
:content => '2'
}
}
end
should "return news" do test "GET /projects/:project_id/news.json should return news" do
get '/projects/ecookbook/news.json' get '/projects/ecookbook/news.json'
json = ActiveSupport::JSON.decode(response.body) json = ActiveSupport::JSON.decode(response.body)
assert_kind_of Hash, json assert_kind_of Hash, json
assert_kind_of Array, json['news'] assert_kind_of Array, json['news']
assert_kind_of Hash, json['news'].first assert_kind_of Hash, json['news'].first
assert_equal 2, json['news'].first['id'] assert_equal 2, json['news'].first['id']
end
end
end end
end end