Project

General

Profile

Patch #4905 » repository_git_test.rb

Lluís Vilanova, 2012-06-12 22:38

 
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_case', __FILE__)
19
require 'tmpdir'
20

    
21
class RedminePmTest::RepositoryGitTest < RedminePmTest::TestCase
22
  fixtures :projects, :users, :members, :roles, :member_roles
23

    
24
  GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
25

    
26
  # TODO: On a successful "clone", the resulting directory should be removed
27
  # TODO: Calls to "clone" could use a last argument to set the destination directory
28
  # TODO: Calls to "push" need something to push ("git init <dir>" + "git commit <something>" before pushing?)
29
  # All these TODOs should already be handled by 'test_write_commands'
30

    
31
  def test_anonymous_read_on_public_repo_with_permission_should_succeed
32
    assert_success "clone", git_url
33
  end
34

    
35
  def test_anonymous_read_on_public_repo_without_permission_should_fail
36
    Role.anonymous.remove_permission! :browse_repository
37
    assert_failure "clone", git_url
38
  end
39

    
40
  def test_anonymous_read_on_private_repo_should_fail
41
    Project.find(1).update_attribute :is_public, false
42
    assert_failure "clone", git_url
43
  end
44

    
45
  def test_anonymous_commit_on_public_repo_should_fail
46
    Role.anonymous.add_permission! :commit_access
47
    assert_failure "push", "--force", git_url
48
  end
49

    
50
  def test_anonymous_commit_on_private_repo_should_fail
51
    Role.anonymous.add_permission! :commit_access
52
    Project.find(1).update_attribute :is_public, false
53
    assert_failure "push", "--force", git_url
54
  end
55

    
56
  def test_non_member_read_on_public_repo_with_permission_should_succeed
57
    Role.anonymous.remove_permission! :browse_repository
58
    with_credentials "miscuser8", "foo" do
59
      assert_success "clone", git_url
60
    end
61
  end
62

    
63
  def test_non_member_read_on_public_repo_without_permission_should_fail
64
    Role.anonymous.remove_permission! :browse_repository
65
    Role.non_member.remove_permission! :browse_repository
66
    with_credentials "miscuser8", "foo" do
67
      assert_failure "clone", git_url
68
    end
69
  end
70

    
71
  def test_non_member_read_on_private_repo_should_fail
72
    Project.find(1).update_attribute :is_public, false
73
    with_credentials "miscuser8", "foo" do
74
      assert_failure "clone", git_url
75
    end
76
  end
77

    
78
  def test_non_member_commit_on_public_repo_should_fail
79
    Role.non_member.add_permission! :commit_access
80
    assert_failure "push", "--force", git_url(random_filename)
81
  end
82

    
83
  def test_non_member_commit_on_private_repo_should_fail
84
    Role.non_member.add_permission! :commit_access
85
    Project.find(1).update_attribute :is_public, false
86
    assert_failure "push", "--force", git_url
87
  end
88

    
89
  def test_member_read_on_public_repo_with_permission_should_succeed
90
    Role.anonymous.remove_permission! :browse_repository
91
    Role.non_member.remove_permission! :browse_repository
92
    with_credentials "dlopper", "foo" do
93
      assert_success "clone", git_url
94
    end
95
  end
96

    
97
  def test_member_read_on_public_repo_without_permission_should_fail
98
    Role.anonymous.remove_permission! :browse_repository
99
    Role.non_member.remove_permission! :browse_repository
100
    Role.find(2).remove_permission! :browse_repository
101
    with_credentials "dlopper", "foo" do
102
      assert_failure "clone", git_url
103
    end
104
  end
105

    
106
  def test_member_read_on_private_repo_with_permission_should_succeed
107
    Project.find(1).update_attribute :is_public, false
108
    with_credentials "dlopper", "foo" do
109
      assert_success "clone", git_url
110
    end
111
  end
112

    
113
  def test_member_read_on_private_repo_without_permission_should_fail
114
    Role.find(2).remove_permission! :browse_repository
115
    Project.find(1).update_attribute :is_public, false
116
    with_credentials "dlopper", "foo" do
117
      assert_failure "clone", git_url
118
    end
119
  end
120

    
121
  def test_member_commit_on_public_repo_with_permission_should_succeed
122
    Role.find(2).add_permission! :commit_access
123
    with_credentials "dlopper", "foo" do
124
      assert_success "push", "--force", git_url
125
    end
126
  end
127

    
128
  def test_member_commit_on_public_repo_without_permission_should_fail
129
    Role.find(2).remove_permission! :commit_access
130
    with_credentials "dlopper", "foo" do
131
      assert_failure "push", "--force", git_url
132
    end
133
  end
134

    
135
  def test_member_commit_on_private_repo_with_permission_should_succeed
136
    Role.find(2).add_permission! :commit_access
137
    Project.find(1).update_attribute :is_public, false
138
    with_credentials "dlopper", "foo" do
139
      assert_success "push", "--force", git_url
140
    end
141
  end
142

    
143
  def test_member_commit_on_private_repo_without_permission_should_fail
144
    Role.find(2).remove_permission! :commit_access
145
    Project.find(1).update_attribute :is_public, false
146
    with_credentials "dlopper", "foo" do
147
      assert_failure "push", "--force", git_url
148
    end
149
  end
150

    
151
  def test_invalid_credentials_should_fail
152
    Project.find(1).update_attribute :is_public, false
153
    with_credentials "dlopper", "foo" do
154
      assert_success "clone", git_url
155
    end
156
    with_credentials "dlopper", "wrong" do
157
      assert_failure "clone", git_url
158
    end
159
  end
160

    
161
  def test_checkout
162
    Dir.mktmpdir do |dir|
163
      assert_success "clone", git_url, dir
164
    end
165
  end
166

    
167
  def test_read_commands
168
    # TODO: other tests already handled this
169
    assert_success "clone", git_url
170
  end
171

    
172
  def test_write_commands
173
    Role.find(2).add_permission! :commit_access
174
    filename = DateTime.now.strftime("%y%m%d%H%M%S")
175

    
176
    Dir.mktmpdir do |dir|
177
      assert_success "clone", git_url, dir
178
      Dir.chdir(dir) do
179
        # creates a file in the working copy
180
        f = File.new(File.join(dir, filename), "w")
181
        f.write "test file content"
182
        f.close
183

    
184
        assert_success "add", filename
185
        with_credentials "dlopper", "foo" do
186
          assert_success "commit --message Committing_a_file"
187
          assert_success "mv   --message Moving_a_file", git_url(filename), git_url("#{filename}_moved")
188
          assert_success "rm --message Deleting_a_file", git_url(filename)
189
        end
190
        assert_success "push", "--force", git_url
191

    
192
        # checks that the working copy was updated
193
        assert !File.exists?(File.join(dir, "#{filename}"))
194
        assert File.exists?(File.join(dir, "#{filename}_moved"))
195
      end
196
    end
197
  end
198

    
199
  def test_read_invalid_repo_should_fail
200
    assert_failure "clone", git_url("invalid")
201
  end
202

    
203
  protected
204

    
205
  def execute(*args)
206
    a = [GIT_BIN]
207
    super a, *args
208
  end
209

    
210
  def git_url(path=nil)
211
    host = ENV['REDMINE_TEST_DAV_SERVER'] || '127.0.0.1'
212
    username = "#{username}" if username
213
    # TODO: not sure if the password bit will work
214
    password = "#{password}" if password
215
    url = "http://#{username}:#{password}@#{host}/git/ecookbook"
216
    url << "/#{path}" if path
217
    url
218
  end
219
end
(20-20/24)