##// END OF EJS Templates
Adds a simple API for listing roles (#9725)....
Jean-Philippe Lang -
r8679:be246de16121
parent child
Show More
@@ -0,0 +1,8
1 api.array :roles do
2 @roles.each do |role|
3 api.role do
4 api.id role.id
5 api.name role.name
6 end
7 end
8 end
@@ -0,0 +1,69
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 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.expand_path('../../../test_helper', __FILE__)
19
20 class ApiTest::RolesTest < ActionController::IntegrationTest
21 fixtures :roles
22
23 def setup
24 Setting.rest_api_enabled = '1'
25 end
26
27 context "/roles" do
28 context "GET" do
29 context "xml" do
30 should "return the roles" do
31 get '/roles.xml'
32
33 assert_response :success
34 assert_equal 'application/xml', @response.content_type
35 assert_equal 3, assigns(:roles).size
36
37 assert_tag :tag => 'roles',
38 :attributes => {:type => 'array'},
39 :child => {
40 :tag => 'role',
41 :child => {
42 :tag => 'id',
43 :content => '2',
44 :sibling => {
45 :tag => 'name',
46 :content => 'Developer'
47 }
48 }
49 }
50 end
51 end
52
53 context "json" do
54 should "return the roles" do
55 get '/roles.json'
56
57 assert_response :success
58 assert_equal 'application/json', @response.content_type
59 assert_equal 3, assigns(:roles).size
60
61 json = ActiveSupport::JSON.decode(response.body)
62 assert_kind_of Hash, json
63 assert_kind_of Array, json['roles']
64 assert_include({'id' => 2, 'name' => 'Developer'}, json['roles'])
65 end
66 end
67 end
68 end
69 end
@@ -18,13 +18,21
18 class RolesController < ApplicationController
18 class RolesController < ApplicationController
19 layout 'admin'
19 layout 'admin'
20
20
21 before_filter :require_admin
21 before_filter :require_admin, :except => :index
22 before_filter :require_admin_or_api_request, :only => :index
22 before_filter :find_role, :only => [:edit, :update, :destroy]
23 before_filter :find_role, :only => [:edit, :update, :destroy]
23
24 accept_api_auth :index
24
25
25 def index
26 def index
26 @role_pages, @roles = paginate :roles, :per_page => 25, :order => 'builtin, position'
27 respond_to do |format|
27 render :action => "index", :layout => false if request.xhr?
28 format.html {
29 @role_pages, @roles = paginate :roles, :per_page => 25, :order => 'builtin, position'
30 render :action => "index", :layout => false if request.xhr?
31 }
32 format.api {
33 @roles = Role.givable.all
34 }
35 end
28 end
36 end
29
37
30 def new
38 def new
@@ -24,6 +24,10 class RoutingRolesTest < ActionController::IntegrationTest
24 { :controller => 'roles', :action => 'index' }
24 { :controller => 'roles', :action => 'index' }
25 )
25 )
26 assert_routing(
26 assert_routing(
27 { :method => 'get', :path => "/roles.xml" },
28 { :controller => 'roles', :action => 'index', :format => 'xml' }
29 )
30 assert_routing(
27 { :method => 'get', :path => "/roles/new" },
31 { :method => 'get', :path => "/roles/new" },
28 { :controller => 'roles', :action => 'new' }
32 { :controller => 'roles', :action => 'new' }
29 )
33 )
General Comments 0
You need to be logged in to leave comments. Login now