Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: require File.expand_path('../../../test_helper', __FILE__) Chris@1494: Chris@1494: class Redmine::ApiTest::MembershipsTest < Redmine::ApiTest::Base Chris@1494: fixtures :projects, :users, :roles, :members, :member_roles Chris@1494: Chris@1494: def setup Chris@1494: Setting.rest_api_enabled = '1' Chris@1494: end Chris@1494: Chris@1494: test "GET /projects/:project_id/memberships.xml should return memberships" do Chris@1494: get '/projects/1/memberships.xml', {}, credentials('jsmith') Chris@1494: Chris@1494: assert_response :success Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: assert_tag :tag => 'memberships', Chris@1494: :attributes => {:type => 'array'}, Chris@1494: :child => { Chris@1494: :tag => 'membership', Chris@1494: :child => { Chris@1494: :tag => 'id', Chris@1494: :content => '2', Chris@1494: :sibling => { Chris@1494: :tag => 'user', Chris@1494: :attributes => {:id => '3', :name => 'Dave Lopper'}, Chris@1494: :sibling => { Chris@1494: :tag => 'roles', Chris@1494: :child => { Chris@1494: :tag => 'role', Chris@1494: :attributes => {:id => '2', :name => 'Developer'} Chris@1494: } Chris@1494: } Chris@1494: } Chris@1494: } Chris@1494: } Chris@1494: end Chris@1494: Chris@1494: test "GET /projects/:project_id/memberships.json should return memberships" do Chris@1494: get '/projects/1/memberships.json', {}, credentials('jsmith') Chris@1494: Chris@1494: assert_response :success Chris@1494: assert_equal 'application/json', @response.content_type Chris@1494: json = ActiveSupport::JSON.decode(response.body) Chris@1494: assert_equal({ Chris@1494: "memberships" => Chris@1494: [{"id"=>1, Chris@1494: "project" => {"name"=>"eCookbook", "id"=>1}, Chris@1494: "roles" => [{"name"=>"Manager", "id"=>1}], Chris@1494: "user" => {"name"=>"John Smith", "id"=>2}}, Chris@1494: {"id"=>2, Chris@1494: "project" => {"name"=>"eCookbook", "id"=>1}, Chris@1494: "roles" => [{"name"=>"Developer", "id"=>2}], Chris@1494: "user" => {"name"=>"Dave Lopper", "id"=>3}}], Chris@1494: "limit" => 25, Chris@1494: "total_count" => 2, Chris@1494: "offset" => 0}, Chris@1494: json) Chris@1494: end Chris@1494: Chris@1494: test "POST /projects/:project_id/memberships.xml should create the membership" do Chris@1494: assert_difference 'Member.count' do Chris@1494: post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith') Chris@1494: Chris@1494: assert_response :created Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: test "POST /projects/:project_id/memberships.xml with invalid parameters should return errors" do Chris@1494: assert_no_difference 'Member.count' do Chris@1494: post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith') Chris@1494: Chris@1494: assert_response :unprocessable_entity Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"} Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: test "GET /memberships/:id.xml should return the membership" do Chris@1494: get '/memberships/2.xml', {}, credentials('jsmith') Chris@1494: Chris@1494: assert_response :success Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: assert_tag :tag => 'membership', Chris@1494: :child => { Chris@1494: :tag => 'id', Chris@1494: :content => '2', Chris@1494: :sibling => { Chris@1494: :tag => 'user', Chris@1494: :attributes => {:id => '3', :name => 'Dave Lopper'}, Chris@1494: :sibling => { Chris@1494: :tag => 'roles', Chris@1494: :child => { Chris@1494: :tag => 'role', Chris@1494: :attributes => {:id => '2', :name => 'Developer'} Chris@1494: } Chris@1494: } Chris@1494: } Chris@1494: } Chris@1494: end Chris@1494: Chris@1494: test "GET /memberships/:id.json should return the membership" do Chris@1494: get '/memberships/2.json', {}, credentials('jsmith') Chris@1494: Chris@1494: assert_response :success Chris@1494: assert_equal 'application/json', @response.content_type Chris@1494: json = ActiveSupport::JSON.decode(response.body) Chris@1494: assert_equal( Chris@1494: {"membership" => { Chris@1494: "id" => 2, Chris@1494: "project" => {"name"=>"eCookbook", "id"=>1}, Chris@1494: "roles" => [{"name"=>"Developer", "id"=>2}], Chris@1494: "user" => {"name"=>"Dave Lopper", "id"=>3}} Chris@1494: }, Chris@1494: json) Chris@1494: end Chris@1494: Chris@1494: test "PUT /memberships/:id.xml should update the membership" do Chris@1494: assert_not_equal [1,2], Member.find(2).role_ids.sort Chris@1494: assert_no_difference 'Member.count' do Chris@1494: put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith') Chris@1494: Chris@1494: assert_response :ok Chris@1494: assert_equal '', @response.body Chris@1494: end Chris@1494: member = Member.find(2) Chris@1494: assert_equal [1,2], member.role_ids.sort Chris@1494: end Chris@1494: Chris@1494: test "PUT /memberships/:id.xml with invalid parameters should return errors" do Chris@1494: put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [99]}}, credentials('jsmith') Chris@1494: Chris@1494: assert_response :unprocessable_entity Chris@1494: assert_equal 'application/xml', @response.content_type Chris@1494: assert_tag 'errors', :child => {:tag => 'error', :content => /member_roles is invalid/} Chris@1494: end Chris@1494: Chris@1494: test "DELETE /memberships/:id.xml should destroy the membership" do Chris@1494: assert_difference 'Member.count', -1 do Chris@1494: delete '/memberships/2.xml', {}, credentials('jsmith') Chris@1494: Chris@1494: assert_response :ok Chris@1494: assert_equal '', @response.body Chris@1494: end Chris@1494: assert_nil Member.find_by_id(2) Chris@1494: end Chris@1494: Chris@1494: test "DELETE /memberships/:id.xml should respond with 422 on failure" do Chris@1494: assert_no_difference 'Member.count' do Chris@1494: # A membership with an inherited role can't be deleted Chris@1494: Member.find(2).member_roles.first.update_attribute :inherited_from, 99 Chris@1494: delete '/memberships/2.xml', {}, credentials('jsmith') Chris@1494: Chris@1494: assert_response :unprocessable_entity Chris@1494: end Chris@1494: end Chris@1494: end