Project

General

Profile

Patch #5895 » user_query_api-3.diff

added integration tests - trunk r3859 - Adam Kaplan, 2010-07-23 21:53

View differences:

test/integration/users_api_test.rb (revision 0)
1
# Redmine - project management software
2
# Copyright (C) 2006-2010  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

  
18
require "#{File.dirname(__FILE__)}/../test_helper"
19

  
20
class UsersApiTest < ActionController::IntegrationTest
21
    fixtures :users, :roles
22

  
23
  def setup
24
    Setting.rest_api_enabled = '1'
25
  end
26
  
27
  def teardown
28
    Setting.rest_api_enabled = '0'
29
  end
30
    
31
  context "get /users.xml with authorized user" do
32
    setup do
33
		get '/users.xml', nil, :authorization => credentials('admin')
34
    end
35
    
36
    should_respond_with :success
37
    should_respond_with_content_type 'application/xml'
38

  
39
  end
40
   
41
  context "get /users.xml with unauthorized user" do
42
    setup do
43
		get '/users.xml', nil, :authorization => credentials('jsmith')
44
    end
45
    
46
    should_respond_with :forbidden
47

  
48
  end
49

  
50
  context "get /users.xml with name " do
51
    setup do
52
		get '/users.xml?name=jsmith', nil, :authorization => credentials('admin')
53
    end
54
    
55

  
56

  
57
    should_respond_with_content_type 'application/xml'
58
    
59
    should "show only users with the name jsmith " do
60

  
61
    c = ARCondition.new(["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", "jsmith","jsmith","jsmith","jsmith"])
62
    @user_count = User.count(:conditions => c.conditions)
63
	    assert_tag :tag => 'users', :children => { :count => @user_count, :only => {:tag => "user"} }
64
    end
65

  
66
  end    
67
    
68

  
69
  context "get /users.xml with status 1 " do
70
    setup do
71
		get '/users.xml?status=1', nil, :authorization => credentials('admin')
72
    end
73
    
74

  
75

  
76
    should_respond_with_content_type 'application/xml'
77
    
78
    should "show only users with the status 1 " do
79

  
80
    c = ARCondition.new(["status = 1"])
81
    @user_count = User.count(:conditions => c.conditions)
82
	    assert_tag :tag => 'users', :children => { :count => @user_count, :only => {:tag => "user"} }
83
    end
84

  
85
  end    
86

  
87

  
88
  def credentials(user, password=nil)
89
    ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
90
  end
91
end
app/controllers/users_controller.rb (working copy)
46 46
						:limit  =>  @user_pages.items_per_page,
47 47
						:offset =>  @user_pages.current.offset
48 48

  
49
    render :layout => !request.xhr?	
49
    render :layout => !request.xhr?
50 50
  end
51 51
  
52 52
  def show
......
67 67
        return
68 68
      end
69 69
    end
70
    render :layout => 'base'
71 70

  
71
    respond_to do |format|
72
      format.html {
73
        render :layout => 'base'
74
      }
75
      format.xml
76
    end
77

  
72 78
  rescue ActiveRecord::RecordNotFound
73 79
    render_404
74 80
  end
app/views/users/index.xml.builder (revision 0)
1
xml.instruct!
2
xml.users :type => 'array' do
3
  @users.each do |user|
4
    xml.user do
5
      xml.id     user.id
6
      xml.first  user.firstname
7
      xml.last   user.lastname
8
      xml.login  user.login
9
      xml.mail   user.mail
10
    end
11
  end
12
end
config/routes.rb (working copy)
171 171
  map.with_options :controller => 'users' do |users|
172 172
    users.with_options :conditions => {:method => :get} do |user_views|
173 173
      user_views.connect 'users', :action => 'index'
174
      user_views.connect 'users.:format', :action => 'index'
174 175
      user_views.connect 'users/:id', :action => 'show', :id => /\d+/
175 176
      user_views.connect 'users/new', :action => 'add'
176 177
      user_views.connect 'users/:id/edit/:tab', :action => 'edit', :tab => nil
(3-3/3)