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