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