Chris@1115: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1115: # Chris@1115: # This program is free software; you can redistribute it and/or Chris@1115: # modify it under the terms of the GNU General Public License Chris@1115: # as published by the Free Software Foundation; either version 2 Chris@1115: # of the License, or (at your option) any later version. Chris@1115: # Chris@1115: # This program is distributed in the hope that it will be useful, Chris@1115: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1115: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1115: # GNU General Public License for more details. Chris@1115: # Chris@1115: # You should have received a copy of the GNU General Public License Chris@1115: # along with this program; if not, write to the Free Software Chris@1115: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1115: Chris@1115: require File.expand_path('../../../test_helper', __FILE__) Chris@1115: Chris@1464: class Redmine::ApiTest::GroupsTest < Redmine::ApiTest::Base Chris@1115: fixtures :users, :groups_users Chris@1115: Chris@1115: def setup Chris@1115: Setting.rest_api_enabled = '1' Chris@1115: end Chris@1115: Chris@1464: test "GET /groups.xml should require authentication" do Chris@1464: get '/groups.xml' Chris@1464: assert_response 401 Chris@1464: end Chris@1115: Chris@1464: test "GET /groups.xml should return groups" do Chris@1464: get '/groups.xml', {}, credentials('admin') Chris@1464: assert_response :success Chris@1464: assert_equal 'application/xml', response.content_type Chris@1115: Chris@1464: assert_select 'groups' do Chris@1464: assert_select 'group' do Chris@1464: assert_select 'name', :text => 'A Team' Chris@1464: assert_select 'id', :text => '10' Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: Chris@1464: test "GET /groups.json should require authentication" do Chris@1464: get '/groups.json' Chris@1464: assert_response 401 Chris@1464: end Chris@1115: Chris@1464: test "GET /groups.json should return groups" do Chris@1464: get '/groups.json', {}, credentials('admin') Chris@1464: assert_response :success Chris@1464: assert_equal 'application/json', response.content_type Chris@1115: Chris@1464: json = MultiJson.load(response.body) Chris@1464: groups = json['groups'] Chris@1464: assert_kind_of Array, groups Chris@1464: group = groups.detect {|g| g['name'] == 'A Team'} Chris@1464: assert_not_nil group Chris@1464: assert_equal({'id' => 10, 'name' => 'A Team'}, group) Chris@1464: end Chris@1115: Chris@1464: test "GET /groups/:id.xml should return the group with its users" do Chris@1464: get '/groups/10.xml', {}, credentials('admin') Chris@1464: assert_response :success Chris@1464: assert_equal 'application/xml', response.content_type Chris@1115: Chris@1464: assert_select 'group' do Chris@1464: assert_select 'name', :text => 'A Team' Chris@1464: assert_select 'id', :text => '10' Chris@1464: end Chris@1464: end Chris@1115: Chris@1464: test "GET /groups/:id.xml should include users if requested" do Chris@1464: get '/groups/10.xml?include=users', {}, credentials('admin') Chris@1464: assert_response :success Chris@1464: assert_equal 'application/xml', response.content_type Chris@1464: Chris@1464: assert_select 'group' do Chris@1464: assert_select 'users' do Chris@1464: assert_select 'user', Group.find(10).users.count Chris@1464: assert_select 'user[id=8]' Chris@1115: end Chris@1115: end Chris@1115: end Chris@1115: Chris@1464: test "GET /groups/:id.xml include memberships if requested" do Chris@1464: get '/groups/10.xml?include=memberships', {}, credentials('admin') Chris@1464: assert_response :success Chris@1464: assert_equal 'application/xml', response.content_type Chris@1115: Chris@1464: assert_select 'group' do Chris@1464: assert_select 'memberships' Chris@1115: end Chris@1115: end Chris@1115: Chris@1464: test "POST /groups.xml with valid parameters should create the group" do Chris@1464: assert_difference('Group.count') do Chris@1464: post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin') Chris@1464: assert_response :created Chris@1464: assert_equal 'application/xml', response.content_type Chris@1115: end Chris@1115: Chris@1464: group = Group.order('id DESC').first Chris@1464: assert_equal 'Test', group.name Chris@1464: assert_equal [2, 3], group.users.map(&:id).sort Chris@1115: Chris@1464: assert_select 'group' do Chris@1464: assert_select 'name', :text => 'Test' Chris@1115: end Chris@1115: end Chris@1115: Chris@1464: test "POST /groups.xml with invalid parameters should return errors" do Chris@1464: assert_no_difference('Group.count') do Chris@1464: post '/groups.xml', {:group => {:name => ''}}, credentials('admin') Chris@1464: end Chris@1464: assert_response :unprocessable_entity Chris@1464: assert_equal 'application/xml', response.content_type Chris@1464: Chris@1464: assert_select 'errors' do Chris@1464: assert_select 'error', :text => /Name can't be blank/ Chris@1115: end Chris@1115: end Chris@1115: Chris@1464: test "PUT /groups/:id.xml with valid parameters should update the group" do Chris@1464: put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin') Chris@1464: assert_response :ok Chris@1464: assert_equal '', @response.body Chris@1464: Chris@1464: group = Group.find(10) Chris@1464: assert_equal 'New name', group.name Chris@1464: assert_equal [2, 3], group.users.map(&:id).sort Chris@1464: end Chris@1464: Chris@1464: test "PUT /groups/:id.xml with invalid parameters should return errors" do Chris@1464: put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin') Chris@1464: assert_response :unprocessable_entity Chris@1464: assert_equal 'application/xml', response.content_type Chris@1464: Chris@1464: assert_select 'errors' do Chris@1464: assert_select 'error', :text => /Name can't be blank/ Chris@1115: end Chris@1115: end Chris@1115: Chris@1464: test "DELETE /groups/:id.xml should delete the group" do Chris@1464: assert_difference 'Group.count', -1 do Chris@1464: delete '/groups/10.xml', {}, credentials('admin') Chris@1464: assert_response :ok Chris@1464: assert_equal '', @response.body Chris@1115: end Chris@1115: end Chris@1464: Chris@1464: test "POST /groups/:id/users.xml should add user to the group" do Chris@1464: assert_difference 'Group.find(10).users.count' do Chris@1464: post '/groups/10/users.xml', {:user_id => 5}, credentials('admin') Chris@1464: assert_response :ok Chris@1464: assert_equal '', @response.body Chris@1464: end Chris@1464: assert_include User.find(5), Group.find(10).users Chris@1464: end Chris@1464: Chris@1464: test "DELETE /groups/:id/users/:user_id.xml should remove user from the group" do Chris@1464: assert_difference 'Group.find(10).users.count', -1 do Chris@1464: delete '/groups/10/users/8.xml', {}, credentials('admin') Chris@1464: assert_response :ok Chris@1464: assert_equal '', @response.body Chris@1464: end Chris@1464: assert_not_include User.find(8), Group.find(10).users Chris@1464: end Chris@1115: end