repositories_filesystem_controller_test.rb 5.91 KB
Newer Older
1
# frozen_string_literal: true
2

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

require File.expand_path('../../test_helper', __FILE__)

22
class RepositoriesFilesystemControllerTest < Redmine::RepositoryControllerTest
23 24
  tests RepositoriesController

25
  fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
26
           :repositories, :enabled_modules
27

28
  REPOSITORY_PATH = Rails.root.join('tmp/test/filesystem_repository').to_s
29 30 31
  PRJ_ID = 3

  def setup
32
    super
33
    @ruby19_non_utf8_pass = Encoding.default_external.to_s != 'UTF-8'
34 35
    User.current = nil
    Setting.enabled_scm << 'Filesystem' unless Setting.enabled_scm.include?('Filesystem')
36
    @project = Project.find(PRJ_ID)
37
    @repository = Repository::Filesystem.create(
38
                      :project       => @project,
39
                      :url           => REPOSITORY_PATH,
40 41 42 43 44 45
                      :path_encoding => ''
                      )
    assert @repository
  end

  if File.directory?(REPOSITORY_PATH)
46
    def test_get_new
47 48
      @request.session[:user_id] = 1
      @project.repository.destroy
49 50 51 52
      get :new, :params => {
          :project_id => 'subproject1',
          :repository_scm => 'Filesystem'
        }
53
      assert_response :success
54 55 56
      assert_select 'select[name=?]', 'repository_scm' do
        assert_select 'option[value=?][selected=selected]', 'Filesystem'
      end
57 58
    end

59 60 61
    def test_browse_root
      @repository.fetch_changesets
      @repository.reload
62 63 64
      get :show, :params => {
          :id => PRJ_ID
        }
65
      assert_response :success
66 67 68 69 70 71 72 73 74

      assert_select 'table.entries tbody' do
        assert_select 'tr', 3
        assert_select 'tr.dir td.filename a', :text => 'dir'
        assert_select 'tr.dir td.filename a', :text => 'japanese'
        assert_select 'tr.file td.filename a', :text => 'test'
      end

      assert_select 'table.changesets tbody', 0
75

76 77 78
      assert_select 'input[name=rev]', 0
      assert_select 'a', :text => 'Statistics', :count => 0
      assert_select 'a', :text => 'Atom', :count => 0
79
    end
80 81

    def test_show_no_extension
82 83
      get :entry, :params => {
          :id => PRJ_ID,
84
          :repository_id => @repository.id,
85 86
          :path => repository_path_hash(['test'])[:param]
        }
87
      assert_response :success
88
      assert_select 'tr#L1 td.line-code', :text => /TEST CAT/
89
    end
90

91
    def test_entry_download_no_extension
92 93
      get :raw, :params => {
          :id => PRJ_ID,
94
          :repository_id => @repository.id,
95 96
          :path => repository_path_hash(['test'])[:param]
        }
97 98 99 100
      assert_response :success
      assert_equal 'application/octet-stream', @response.content_type
    end

101 102
    def test_show_non_ascii_contents
      with_settings :repositories_encodings => 'UTF-8,EUC-JP' do
103 104
        get :entry, :params => {
            :id => PRJ_ID,
105
            :repository_id => @repository.id,
106
            :path => repository_path_hash(['japanese', 'euc-jp.txt'])[:param]
107
          }
108
        assert_response :success
109
        assert_select 'tr#L2 td.line-code', :text => /japanese/
110
        if @ruby19_non_utf8_pass
111 112
          puts "TODO: show repository file contents test fails " +
               "when Encoding.default_external is not UTF-8. " +
113 114
               "Current value is '#{Encoding.default_external.to_s}'"
        else
115
          assert_select 'tr#L3 td.line-code', :text => /日本語/
116
        end
117 118
      end
    end
119 120

    def test_show_utf16
121
      enc = 'UTF-16'
122
      with_settings :repositories_encodings => enc do
123 124
        get :entry, :params => {
            :id => PRJ_ID,
125
            :repository_id => @repository.id,
126
            :path => repository_path_hash(['japanese', 'utf-16.txt'])[:param]
127
          }
128
        assert_response :success
129
        assert_select 'tr#L2 td.line-code', :text => /japanese/
130 131
      end
    end
132

133
    def test_show_text_file_should_show_other_if_too_big
134
      with_settings :file_max_size_displayed => 1 do
135 136
        get :entry, :params => {
            :id => PRJ_ID,
137
            :repository_id => @repository.id,
138
            :path => repository_path_hash(['japanese', 'big-file.txt'])[:param]
139
          }
140
        assert_response :success
141 142
        assert_equal 'text/html', @response.content_type
        assert_select 'p.nodata'
143 144
      end
    end
145 146 147 148

    def test_destroy_valid_repository
      @request.session[:user_id] = 1 # admin

149
      assert_difference 'Repository.count', -1 do
150 151 152
        delete :destroy, :params => {
            :id => @repository.id
          }
153
      end
154 155 156 157
      assert_response 302
      @project.reload
      assert_nil @project.repository
    end
158 159 160

    def test_destroy_invalid_repository
      @request.session[:user_id] = 1 # admin
161 162 163
      @project.repository.destroy
      @repository = Repository::Filesystem.create!(
                      :project       => @project,
164 165 166 167
                      :url           => "/invalid",
                      :path_encoding => ''
                      )

168
      assert_difference 'Repository.count', -1 do
169 170 171
        delete :destroy, :params => {
            :id => @repository.id
          }
172
      end
173 174 175 176
      assert_response 302
      @project.reload
      assert_nil @project.repository
    end
177 178 179 180 181
  else
    puts "Filesystem test repository NOT FOUND. Skipping functional tests !!!"
    def test_fake; assert true end
  end
end