To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 3e / 3e2401296aa57bb537864ac16aaa5ce36ba06138.svn-base @ 1298:4f746d8966dd

History | View | Annotate | Download (6.67 KB)

1 1295:622f24f53b42 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2013  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 Redmine::ApiTest::MembershipsTest < Redmine::ApiTest::Base
21
  fixtures :projects, :users, :roles, :members, :member_roles
22
23
  def setup
24
    Setting.rest_api_enabled = '1'
25
  end
26
27
  context "/projects/:project_id/memberships" do
28
    context "GET" do
29
      context "xml" do
30
        should "return memberships" do
31
          get '/projects/1/memberships.xml', {}, credentials('jsmith')
32
33
          assert_response :success
34
          assert_equal 'application/xml', @response.content_type
35
          assert_tag :tag => 'memberships',
36
            :attributes => {:type => 'array'},
37
            :child => {
38
              :tag => 'membership',
39
              :child => {
40
                :tag => 'id',
41
                :content => '2',
42
                :sibling => {
43
                  :tag => 'user',
44
                  :attributes => {:id => '3', :name => 'Dave Lopper'},
45
                  :sibling => {
46
                    :tag => 'roles',
47
                    :child => {
48
                      :tag => 'role',
49
                      :attributes => {:id => '2', :name => 'Developer'}
50
                    }
51
                  }
52
                }
53
              }
54
            }
55
        end
56
      end
57
58
      context "json" do
59
        should "return memberships" do
60
          get '/projects/1/memberships.json', {}, credentials('jsmith')
61
62
          assert_response :success
63
          assert_equal 'application/json', @response.content_type
64
          json = ActiveSupport::JSON.decode(response.body)
65
          assert_equal({
66
            "memberships" =>
67
              [{"id"=>1,
68
                "project" => {"name"=>"eCookbook", "id"=>1},
69
                "roles" => [{"name"=>"Manager", "id"=>1}],
70
                "user" => {"name"=>"John Smith", "id"=>2}},
71
               {"id"=>2,
72
                "project" => {"name"=>"eCookbook", "id"=>1},
73
                "roles" => [{"name"=>"Developer", "id"=>2}],
74
                "user" => {"name"=>"Dave Lopper", "id"=>3}}],
75
           "limit" => 25,
76
           "total_count" => 2,
77
           "offset" => 0},
78
           json)
79
        end
80
      end
81
    end
82
83
    context "POST" do
84
      context "xml" do
85
        should "create membership" do
86
          assert_difference 'Member.count' do
87
            post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith')
88
89
            assert_response :created
90
          end
91
        end
92
93
        should "return errors on failure" do
94
          assert_no_difference 'Member.count' do
95
            post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith')
96
97
            assert_response :unprocessable_entity
98
            assert_equal 'application/xml', @response.content_type
99
            assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"}
100
          end
101
        end
102
      end
103
    end
104
  end
105
106
  context "/memberships/:id" do
107
    context "GET" do
108
      context "xml" do
109
        should "return the membership" do
110
          get '/memberships/2.xml', {}, credentials('jsmith')
111
112
          assert_response :success
113
          assert_equal 'application/xml', @response.content_type
114
          assert_tag :tag => 'membership',
115
            :child => {
116
              :tag => 'id',
117
              :content => '2',
118
              :sibling => {
119
                :tag => 'user',
120
                :attributes => {:id => '3', :name => 'Dave Lopper'},
121
                :sibling => {
122
                  :tag => 'roles',
123
                  :child => {
124
                    :tag => 'role',
125
                    :attributes => {:id => '2', :name => 'Developer'}
126
                  }
127
                }
128
              }
129
            }
130
        end
131
      end
132
133
      context "json" do
134
        should "return the membership" do
135
          get '/memberships/2.json', {}, credentials('jsmith')
136
137
          assert_response :success
138
          assert_equal 'application/json', @response.content_type
139
          json = ActiveSupport::JSON.decode(response.body)
140
          assert_equal(
141
            {"membership" => {
142
              "id" => 2,
143
              "project" => {"name"=>"eCookbook", "id"=>1},
144
              "roles" => [{"name"=>"Developer", "id"=>2}],
145
              "user" => {"name"=>"Dave Lopper", "id"=>3}}
146
            },
147
            json)
148
        end
149
      end
150
    end
151
152
    context "PUT" do
153
      context "xml" do
154
        should "update membership" do
155
          assert_not_equal [1,2], Member.find(2).role_ids.sort
156
          assert_no_difference 'Member.count' do
157
            put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith')
158
159
            assert_response :ok
160
            assert_equal '', @response.body
161
          end
162
          member = Member.find(2)
163
          assert_equal [1,2], member.role_ids.sort
164
        end
165
166
        should "return errors on failure" do
167
          put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [99]}}, credentials('jsmith')
168
169
          assert_response :unprocessable_entity
170
          assert_equal 'application/xml', @response.content_type
171
          assert_tag 'errors', :child => {:tag => 'error', :content => /member_roles is invalid/}
172
        end
173
      end
174
    end
175
176
    context "DELETE" do
177
      context "xml" do
178
        should "destroy membership" do
179
          assert_difference 'Member.count', -1 do
180
            delete '/memberships/2.xml', {}, credentials('jsmith')
181
182
            assert_response :ok
183
            assert_equal '', @response.body
184
          end
185
          assert_nil Member.find_by_id(2)
186
        end
187
188
        should "respond with 422 on failure" do
189
          assert_no_difference 'Member.count' do
190
            # A membership with an inherited role can't be deleted
191
            Member.find(2).member_roles.first.update_attribute :inherited_from, 99
192
            delete '/memberships/2.xml', {}, credentials('jsmith')
193
194
            assert_response :unprocessable_entity
195
          end
196
        end
197
      end
198
    end
199
  end
200
end