Chris@119: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@119: # Chris@119: # This program is free software; you can redistribute it and/or Chris@119: # modify it under the terms of the GNU General Public License Chris@119: # as published by the Free Software Foundation; either version 2 Chris@119: # of the License, or (at your option) any later version. Chris@909: # Chris@119: # This program is distributed in the hope that it will be useful, Chris@119: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@119: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@119: # GNU General Public License for more details. Chris@909: # Chris@119: # You should have received a copy of the GNU General Public License Chris@119: # along with this program; if not, write to the Free Software Chris@119: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@119: Chris@119: require File.expand_path('../../../test_helper', __FILE__) Chris@1295: Chris@1295: class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base Chris@1115: fixtures :users, :members, :member_roles, :roles, :projects Chris@119: Chris@119: def setup Chris@119: Setting.rest_api_enabled = '1' Chris@119: end Chris@119: Chris@119: context "GET /users" do Chris@119: should_allow_api_authentication(:get, "/users.xml") Chris@119: should_allow_api_authentication(:get, "/users.json") Chris@119: end Chris@119: Chris@119: context "GET /users/2" do Chris@119: context ".xml" do Chris@119: should "return requested user" do Chris@119: get '/users/2.xml' Chris@909: Chris@1115: assert_response :success Chris@119: assert_tag :tag => 'user', Chris@119: :child => {:tag => 'id', :content => '2'} Chris@119: end Chris@1115: Chris@1115: context "with include=memberships" do Chris@1115: should "include memberships" do Chris@1115: get '/users/2.xml?include=memberships' Chris@1115: Chris@1115: assert_response :success Chris@1115: assert_tag :tag => 'memberships', Chris@1115: :parent => {:tag => 'user'}, Chris@1115: :children => {:count => 1} Chris@1115: end Chris@1115: end Chris@119: end Chris@119: Chris@119: context ".json" do Chris@119: should "return requested user" do Chris@119: get '/users/2.json' Chris@909: Chris@1115: assert_response :success Chris@119: json = ActiveSupport::JSON.decode(response.body) Chris@119: assert_kind_of Hash, json Chris@119: assert_kind_of Hash, json['user'] Chris@119: assert_equal 2, json['user']['id'] Chris@119: end Chris@1115: Chris@1115: context "with include=memberships" do Chris@1115: should "include memberships" do Chris@1115: get '/users/2.json?include=memberships' Chris@1115: Chris@1115: assert_response :success Chris@1115: json = ActiveSupport::JSON.decode(response.body) Chris@1115: assert_kind_of Array, json['user']['memberships'] Chris@1115: assert_equal [{ Chris@1115: "id"=>1, Chris@1115: "project"=>{"name"=>"eCookbook", "id"=>1}, Chris@1115: "roles"=>[{"name"=>"Manager", "id"=>1}] Chris@1115: }], json['user']['memberships'] Chris@1115: end Chris@1115: end Chris@119: end Chris@119: end Chris@909: Chris@119: context "GET /users/current" do Chris@119: context ".xml" do Chris@119: should "require authentication" do Chris@119: get '/users/current.xml' Chris@909: Chris@119: assert_response 401 Chris@119: end Chris@909: Chris@119: should "return current user" do Chris@1115: get '/users/current.xml', {}, credentials('jsmith') Chris@909: Chris@119: assert_tag :tag => 'user', Chris@119: :child => {:tag => 'id', :content => '2'} Chris@119: end Chris@119: end Chris@119: end Chris@119: Chris@1295: test "GET /users/:id should not return login for other user" do Chris@1295: get '/users/3.xml', {}, credentials('jsmith') Chris@1295: assert_response :success Chris@1295: assert_no_tag 'user', :child => {:tag => 'login'} Chris@1295: end Chris@1295: Chris@1295: test "GET /users/:id should return login for current user" do Chris@1295: get '/users/2.xml', {}, credentials('jsmith') Chris@1295: assert_response :success Chris@1295: assert_tag 'user', :child => {:tag => 'login', :content => 'jsmith'} Chris@1295: end Chris@1295: Chris@1295: test "GET /users/:id should not return api_key for other user" do Chris@1295: get '/users/3.xml', {}, credentials('jsmith') Chris@1295: assert_response :success Chris@1295: assert_no_tag 'user', :child => {:tag => 'api_key'} Chris@1295: end Chris@1295: Chris@1295: test "GET /users/:id should return api_key for current user" do Chris@1295: get '/users/2.xml', {}, credentials('jsmith') Chris@1295: assert_response :success Chris@1295: assert_tag 'user', :child => {:tag => 'api_key', :content => User.find(2).api_key} Chris@1295: end Chris@1295: Chris@119: context "POST /users" do Chris@119: context "with valid parameters" do Chris@119: setup do Chris@1115: @parameters = { Chris@1115: :user => { Chris@1115: :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', Chris@1115: :mail => 'foo@example.net', :password => 'secret123', Chris@1115: :mail_notification => 'only_assigned' Chris@1115: } Chris@1115: } Chris@119: end Chris@909: Chris@119: context ".xml" do Chris@119: should_allow_api_authentication(:post, Chris@119: '/users.xml', Chris@1115: {:user => { Chris@1115: :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', Chris@1115: :mail => 'foo@example.net', :password => 'secret123' Chris@1115: }}, Chris@119: {:success_code => :created}) Chris@909: Chris@119: should "create a user with the attributes" do Chris@119: assert_difference('User.count') do Chris@1115: post '/users.xml', @parameters, credentials('admin') Chris@119: end Chris@909: Chris@119: user = User.first(:order => 'id DESC') Chris@119: assert_equal 'foo', user.login Chris@119: assert_equal 'Firstname', user.firstname Chris@119: assert_equal 'Lastname', user.lastname Chris@119: assert_equal 'foo@example.net', user.mail Chris@119: assert_equal 'only_assigned', user.mail_notification Chris@119: assert !user.admin? Chris@1115: assert user.check_password?('secret123') Chris@909: Chris@119: assert_response :created Chris@119: assert_equal 'application/xml', @response.content_type Chris@119: assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} Chris@119: end Chris@119: end Chris@909: Chris@119: context ".json" do Chris@119: should_allow_api_authentication(:post, Chris@119: '/users.json', Chris@1115: {:user => { Chris@1115: :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', Chris@1115: :mail => 'foo@example.net' Chris@1115: }}, Chris@119: {:success_code => :created}) Chris@909: Chris@119: should "create a user with the attributes" do Chris@119: assert_difference('User.count') do Chris@1115: post '/users.json', @parameters, credentials('admin') Chris@119: end Chris@909: Chris@119: user = User.first(:order => 'id DESC') Chris@119: assert_equal 'foo', user.login Chris@119: assert_equal 'Firstname', user.firstname Chris@119: assert_equal 'Lastname', user.lastname Chris@119: assert_equal 'foo@example.net', user.mail Chris@119: assert !user.admin? Chris@909: Chris@119: assert_response :created Chris@119: assert_equal 'application/json', @response.content_type Chris@119: json = ActiveSupport::JSON.decode(response.body) Chris@119: assert_kind_of Hash, json Chris@119: assert_kind_of Hash, json['user'] Chris@119: assert_equal user.id, json['user']['id'] Chris@119: end Chris@119: end Chris@119: end Chris@909: Chris@119: context "with invalid parameters" do Chris@119: setup do Chris@119: @parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}} Chris@119: end Chris@909: Chris@119: context ".xml" do Chris@119: should "return errors" do Chris@119: assert_no_difference('User.count') do Chris@1115: post '/users.xml', @parameters, credentials('admin') Chris@119: end Chris@909: Chris@119: assert_response :unprocessable_entity Chris@119: assert_equal 'application/xml', @response.content_type Chris@1115: assert_tag 'errors', :child => { Chris@1115: :tag => 'error', Chris@1115: :content => "First name can't be blank" Chris@1115: } Chris@119: end Chris@119: end Chris@909: Chris@119: context ".json" do Chris@119: should "return errors" do Chris@119: assert_no_difference('User.count') do Chris@1115: post '/users.json', @parameters, credentials('admin') Chris@119: end Chris@909: Chris@119: assert_response :unprocessable_entity Chris@119: assert_equal 'application/json', @response.content_type Chris@119: json = ActiveSupport::JSON.decode(response.body) Chris@119: assert_kind_of Hash, json Chris@119: assert json.has_key?('errors') Chris@119: assert_kind_of Array, json['errors'] Chris@119: end Chris@119: end Chris@119: end Chris@119: end Chris@119: Chris@119: context "PUT /users/2" do Chris@119: context "with valid parameters" do Chris@119: setup do Chris@1115: @parameters = { Chris@1115: :user => { Chris@1115: :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', Chris@1115: :mail => 'jsmith@somenet.foo' Chris@1115: } Chris@1115: } Chris@119: end Chris@909: Chris@119: context ".xml" do Chris@119: should_allow_api_authentication(:put, Chris@119: '/users/2.xml', Chris@1115: {:user => { Chris@1115: :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', Chris@1115: :mail => 'jsmith@somenet.foo' Chris@1115: }}, Chris@119: {:success_code => :ok}) Chris@909: Chris@119: should "update user with the attributes" do Chris@119: assert_no_difference('User.count') do Chris@1115: put '/users/2.xml', @parameters, credentials('admin') Chris@119: end Chris@909: Chris@119: user = User.find(2) Chris@119: assert_equal 'jsmith', user.login Chris@119: assert_equal 'John', user.firstname Chris@119: assert_equal 'Renamed', user.lastname Chris@119: assert_equal 'jsmith@somenet.foo', user.mail Chris@119: assert !user.admin? Chris@909: Chris@119: assert_response :ok Chris@1115: assert_equal '', @response.body Chris@119: end Chris@119: end Chris@909: Chris@119: context ".json" do Chris@119: should_allow_api_authentication(:put, Chris@119: '/users/2.json', Chris@1115: {:user => { Chris@1115: :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', Chris@1115: :mail => 'jsmith@somenet.foo' Chris@1115: }}, Chris@119: {:success_code => :ok}) Chris@909: Chris@119: should "update user with the attributes" do Chris@119: assert_no_difference('User.count') do Chris@1115: put '/users/2.json', @parameters, credentials('admin') Chris@119: end Chris@909: Chris@119: user = User.find(2) Chris@119: assert_equal 'jsmith', user.login Chris@119: assert_equal 'John', user.firstname Chris@119: assert_equal 'Renamed', user.lastname Chris@119: assert_equal 'jsmith@somenet.foo', user.mail Chris@119: assert !user.admin? Chris@909: Chris@119: assert_response :ok Chris@1115: assert_equal '', @response.body Chris@119: end Chris@119: end Chris@119: end Chris@909: Chris@119: context "with invalid parameters" do Chris@119: setup do Chris@1115: @parameters = { Chris@1115: :user => { Chris@1115: :login => 'jsmith', :firstname => '', :lastname => 'Lastname', Chris@1115: :mail => 'foo' Chris@1115: } Chris@1115: } Chris@119: end Chris@909: Chris@119: context ".xml" do Chris@119: should "return errors" do Chris@119: assert_no_difference('User.count') do Chris@1115: put '/users/2.xml', @parameters, credentials('admin') Chris@119: end Chris@909: Chris@119: assert_response :unprocessable_entity Chris@119: assert_equal 'application/xml', @response.content_type Chris@1115: assert_tag 'errors', :child => { Chris@1115: :tag => 'error', Chris@1115: :content => "First name can't be blank" Chris@1115: } Chris@119: end Chris@119: end Chris@909: Chris@119: context ".json" do Chris@119: should "return errors" do Chris@119: assert_no_difference('User.count') do Chris@1115: put '/users/2.json', @parameters, credentials('admin') Chris@119: end Chris@909: Chris@119: assert_response :unprocessable_entity Chris@119: assert_equal 'application/json', @response.content_type Chris@119: json = ActiveSupport::JSON.decode(response.body) Chris@119: assert_kind_of Hash, json Chris@119: assert json.has_key?('errors') Chris@119: assert_kind_of Array, json['errors'] Chris@119: end Chris@119: end Chris@119: end Chris@128: end Chris@909: Chris@128: context "DELETE /users/2" do Chris@128: context ".xml" do Chris@128: should_allow_api_authentication(:delete, Chris@128: '/users/2.xml', Chris@128: {}, Chris@128: {:success_code => :ok}) Chris@909: Chris@128: should "delete user" do Chris@128: assert_difference('User.count', -1) do Chris@1115: delete '/users/2.xml', {}, credentials('admin') Chris@128: end Chris@909: Chris@128: assert_response :ok Chris@1115: assert_equal '', @response.body Chris@128: end Chris@128: end Chris@909: Chris@128: context ".json" do Chris@128: should_allow_api_authentication(:delete, Chris@128: '/users/2.xml', Chris@128: {}, Chris@128: {:success_code => :ok}) Chris@909: Chris@128: should "delete user" do Chris@128: assert_difference('User.count', -1) do Chris@1115: delete '/users/2.json', {}, credentials('admin') Chris@119: end Chris@909: Chris@128: assert_response :ok Chris@1115: assert_equal '', @response.body Chris@119: end Chris@119: end Chris@119: end Chris@119: end