blob_show_spec.rb 15.9 KB
Newer Older
1 2
require 'spec_helper'

3
feature 'File blob', :js do
4 5
  include MobileHelpers

6
  let(:project) { create(:project, :public, :repository) }
Douwe Maan's avatar
Douwe Maan committed
7

8
  def visit_blob(path, anchor: nil, ref: 'master')
9
    visit project_blob_path(project, File.join(ref, path), anchor: anchor)
10

11
    wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
12 13
  end

Douwe Maan's avatar
Douwe Maan committed
14 15 16
  context 'Ruby file' do
    before do
      visit_blob('files/ruby/popen.rb')
Douwe Maan's avatar
Douwe Maan committed
17 18
    end

Douwe Maan's avatar
Douwe Maan committed
19 20 21
    it 'displays the blob' do
      aggregate_failures do
        # shows highlighted Ruby code
Douwe Maan's avatar
Douwe Maan committed
22
        expect(page).to have_css(".js-syntax-highlight")
Douwe Maan's avatar
Douwe Maan committed
23
        expect(page).to have_content("require 'fileutils'")
Douwe Maan's avatar
Douwe Maan committed
24

Douwe Maan's avatar
Douwe Maan committed
25 26
        # does not show a viewer switcher
        expect(page).not_to have_selector('.js-blob-viewer-switcher')
Douwe Maan's avatar
Douwe Maan committed
27

Douwe Maan's avatar
Douwe Maan committed
28 29
        # shows an enabled copy button
        expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')
30 31 32

        # shows a raw button
        expect(page).to have_link('Open raw')
Douwe Maan's avatar
Douwe Maan committed
33
      end
Douwe Maan's avatar
Douwe Maan committed
34
    end
35 36 37 38 39 40 41 42 43 44

    it 'displays file actions on all screen sizes' do
      file_actions_selector = '.file-actions'

      resize_screen_sm
      expect(page).to have_selector(file_actions_selector, visible: true)

      resize_screen_xs
      expect(page).to have_selector(file_actions_selector, visible: true)
    end
Douwe Maan's avatar
Douwe Maan committed
45 46 47 48 49 50 51 52
  end

  context 'Markdown file' do
    context 'visiting directly' do
      before do
        visit_blob('files/markdown/ruby-style-guide.md')
      end

Douwe Maan's avatar
Douwe Maan committed
53
      it 'displays the blob using the rich viewer' do
Douwe Maan's avatar
Douwe Maan committed
54 55 56 57 58 59 60 61 62 63 64 65 66
        aggregate_failures do
          # hides the simple viewer
          expect(page).to have_selector('.blob-viewer[data-type="simple"]', visible: false)
          expect(page).to have_selector('.blob-viewer[data-type="rich"]')

          # shows rendered Markdown
          expect(page).to have_link("PEP-8")

          # shows a viewer switcher
          expect(page).to have_selector('.js-blob-viewer-switcher')

          # shows a disabled copy button
          expect(page).to have_selector('.js-copy-blob-source-btn.disabled')
67 68 69

          # shows a raw button
          expect(page).to have_link('Open raw')
Douwe Maan's avatar
Douwe Maan committed
70 71 72 73 74 75 76
        end
      end

      context 'switching to the simple viewer' do
        before do
          find('.js-blob-viewer-switch-btn[data-viewer=simple]').click

77
          wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
78
        end
Douwe Maan's avatar
Douwe Maan committed
79

Douwe Maan's avatar
Douwe Maan committed
80
        it 'displays the blob using the simple viewer' do
Douwe Maan's avatar
Douwe Maan committed
81 82 83 84
          aggregate_failures do
            # hides the rich viewer
            expect(page).to have_selector('.blob-viewer[data-type="simple"]')
            expect(page).to have_selector('.blob-viewer[data-type="rich"]', visible: false)
Douwe Maan's avatar
Douwe Maan committed
85

Douwe Maan's avatar
Douwe Maan committed
86
            # shows highlighted Markdown code
Douwe Maan's avatar
Douwe Maan committed
87
            expect(page).to have_css(".js-syntax-highlight")
Douwe Maan's avatar
Douwe Maan committed
88 89 90 91 92 93 94 95 96 97 98
            expect(page).to have_content("[PEP-8](http://www.python.org/dev/peps/pep-0008/)")

            # shows an enabled copy button
            expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')
          end
        end

        context 'switching to the rich viewer again' do
          before do
            find('.js-blob-viewer-switch-btn[data-viewer=rich]').click

99
            wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
100 101
          end

Douwe Maan's avatar
Douwe Maan committed
102
          it 'displays the blob using the rich viewer' do
Douwe Maan's avatar
Douwe Maan committed
103 104 105 106 107 108 109 110 111 112 113
            aggregate_failures do
              # hides the simple viewer
              expect(page).to have_selector('.blob-viewer[data-type="simple"]', visible: false)
              expect(page).to have_selector('.blob-viewer[data-type="rich"]')

              # shows an enabled copy button
              expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')
            end
          end
        end
      end
Douwe Maan's avatar
Douwe Maan committed
114 115
    end

Douwe Maan's avatar
Douwe Maan committed
116 117
    context 'visiting with a line number anchor' do
      before do
118
        visit_blob('files/markdown/ruby-style-guide.md', anchor: 'L1')
Douwe Maan's avatar
Douwe Maan committed
119 120
      end

Douwe Maan's avatar
Douwe Maan committed
121
      it 'displays the blob using the simple viewer' do
Douwe Maan's avatar
Douwe Maan committed
122 123 124 125 126 127 128 129 130
        aggregate_failures do
          # hides the rich viewer
          expect(page).to have_selector('.blob-viewer[data-type="simple"]')
          expect(page).to have_selector('.blob-viewer[data-type="rich"]', visible: false)

          # highlights the line in question
          expect(page).to have_selector('#LC1.hll')

          # shows highlighted Markdown code
Douwe Maan's avatar
Douwe Maan committed
131
          expect(page).to have_css(".js-syntax-highlight")
Douwe Maan's avatar
Douwe Maan committed
132
          expect(page).to have_content("[PEP-8](http://www.python.org/dev/peps/pep-0008/)")
Douwe Maan's avatar
Douwe Maan committed
133

Douwe Maan's avatar
Douwe Maan committed
134 135 136 137
          # shows an enabled copy button
          expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')
        end
      end
Douwe Maan's avatar
Douwe Maan committed
138 139 140
    end
  end

Douwe Maan's avatar
Douwe Maan committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  context 'Markdown file (stored in LFS)' do
    before do
      project.add_master(project.creator)

      Files::CreateService.new(
        project,
        project.creator,
        start_branch: 'master',
        branch_name: 'master',
        commit_message: "Add Markdown in LFS",
        file_path: 'files/lfs/file.md',
        file_content: project.repository.blob_at('master', 'files/lfs/lfs_object.iso').data
      ).execute
    end

    context 'when LFS is enabled on the project' do
      before do
        allow(Gitlab.config.lfs).to receive(:enabled).and_return(true)
        project.update_attribute(:lfs_enabled, true)

        visit_blob('files/lfs/file.md')
      end

Douwe Maan's avatar
Douwe Maan committed
164
      it 'displays an error' do
Douwe Maan's avatar
Douwe Maan committed
165 166 167 168 169 170
        aggregate_failures do
          # hides the simple viewer
          expect(page).to have_selector('.blob-viewer[data-type="simple"]', visible: false)
          expect(page).to have_selector('.blob-viewer[data-type="rich"]')

          # shows an error message
171
          expect(page).to have_content('The rendered file could not be displayed because it is stored in LFS. You can download it instead.')
Douwe Maan's avatar
Douwe Maan committed
172 173 174 175 176 177

          # shows a viewer switcher
          expect(page).to have_selector('.js-blob-viewer-switcher')

          # does not show a copy button
          expect(page).not_to have_selector('.js-copy-blob-source-btn')
178

179 180
          # shows a download button
          expect(page).to have_link('Download')
Douwe Maan's avatar
Douwe Maan committed
181 182 183 184 185 186 187
        end
      end

      context 'switching to the simple viewer' do
        before do
          find('.js-blob-viewer-switcher .js-blob-viewer-switch-btn[data-viewer=simple]').click

188
          wait_for_requests
Douwe Maan's avatar
Douwe Maan committed
189 190
        end

Douwe Maan's avatar
Douwe Maan committed
191
        it 'displays an error' do
Douwe Maan's avatar
Douwe Maan committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
          aggregate_failures do
            # hides the rich viewer
            expect(page).to have_selector('.blob-viewer[data-type="simple"]')
            expect(page).to have_selector('.blob-viewer[data-type="rich"]', visible: false)

            # shows an error message
            expect(page).to have_content('The source could not be displayed because it is stored in LFS. You can download it instead.')

            # does not show a copy button
            expect(page).not_to have_selector('.js-copy-blob-source-btn')
          end
        end
      end
    end

    context 'when LFS is disabled on the project' do
      before do
        visit_blob('files/lfs/file.md')
      end

      it 'displays the blob' do
        aggregate_failures do
          # shows text
          expect(page).to have_content('size 1575078')

          # does not show a viewer switcher
          expect(page).not_to have_selector('.js-blob-viewer-switcher')

          # shows an enabled copy button
          expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')
222 223 224

          # shows a raw button
          expect(page).to have_link('Open raw')
Douwe Maan's avatar
Douwe Maan committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        end
      end
    end
  end

  context 'PDF file' do
    before do
      project.add_master(project.creator)

      Files::CreateService.new(
        project,
        project.creator,
        start_branch: 'master',
        branch_name: 'master',
        commit_message: "Add PDF",
        file_path: 'files/test.pdf',
Phil Hughes's avatar
Phil Hughes committed
241
        file_content: project.repository.blob_at('add-pdf-file', 'files/pdf/test.pdf').data
Douwe Maan's avatar
Douwe Maan committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
      ).execute

      visit_blob('files/test.pdf')
    end

    it 'displays the blob' do
      aggregate_failures do
        # shows rendered PDF
        expect(page).to have_selector('.js-pdf-viewer')

        # does not show a viewer switcher
        expect(page).not_to have_selector('.js-blob-viewer-switcher')

        # does not show a copy button
        expect(page).not_to have_selector('.js-copy-blob-source-btn')
257 258 259

        # shows a download button
        expect(page).to have_link('Download')
Douwe Maan's avatar
Douwe Maan committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
      end
    end
  end

  context 'ISO file (stored in LFS)' do
    context 'when LFS is enabled on the project' do
      before do
        allow(Gitlab.config.lfs).to receive(:enabled).and_return(true)
        project.update_attribute(:lfs_enabled, true)

        visit_blob('files/lfs/lfs_object.iso')
      end

      it 'displays the blob' do
        aggregate_failures do
          # shows a download link
          expect(page).to have_link('Download (1.5 MB)')

          # does not show a viewer switcher
          expect(page).not_to have_selector('.js-blob-viewer-switcher')

          # does not show a copy button
          expect(page).not_to have_selector('.js-copy-blob-source-btn')
283 284 285

          # shows a download button
          expect(page).to have_link('Download')
Douwe Maan's avatar
Douwe Maan committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        end
      end
    end

    context 'when LFS is disabled on the project' do
      before do
        visit_blob('files/lfs/lfs_object.iso')
      end

      it 'displays the blob' do
        aggregate_failures do
          # shows text
          expect(page).to have_content('size 1575078')

          # does not show a viewer switcher
          expect(page).not_to have_selector('.js-blob-viewer-switcher')

          # shows an enabled copy button
          expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')
305 306 307

          # shows a raw button
          expect(page).to have_link('Open raw')
Douwe Maan's avatar
Douwe Maan committed
308 309 310 311 312 313 314
        end
      end
    end
  end

  context 'ZIP file' do
    before do
Douwe Maan's avatar
Douwe Maan committed
315
      visit_blob('Gemfile.zip')
Douwe Maan's avatar
Douwe Maan committed
316 317 318 319 320 321 322 323 324 325 326 327
    end

    it 'displays the blob' do
      aggregate_failures do
        # shows a download link
        expect(page).to have_link('Download (2.11 KB)')

        # does not show a viewer switcher
        expect(page).not_to have_selector('.js-blob-viewer-switcher')

        # does not show a copy button
        expect(page).not_to have_selector('.js-copy-blob-source-btn')
328 329 330

        # shows a download button
        expect(page).to have_link('Download')
Douwe Maan's avatar
Douwe Maan committed
331
      end
332 333
    end
  end
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

  context 'empty file' do
    before do
      project.add_master(project.creator)

      Files::CreateService.new(
        project,
        project.creator,
        start_branch: 'master',
        branch_name: 'master',
        commit_message: "Add empty file",
        file_path: 'files/empty.md',
        file_content: ''
      ).execute

      visit_blob('files/empty.md')
    end

    it 'displays an error' do
      aggregate_failures do
        # shows an error message
        expect(page).to have_content('Empty file')

        # does not show a viewer switcher
        expect(page).not_to have_selector('.js-blob-viewer-switcher')

        # does not show a copy button
        expect(page).not_to have_selector('.js-copy-blob-source-btn')

        # does not show a download or raw button
        expect(page).not_to have_link('Download')
        expect(page).not_to have_link('Open raw')
      end
    end
  end
369

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  context 'binary file that appears to be text in the first 1024 bytes' do
    before do
      visit_blob('encoding/binary-1.bin', ref: 'binary-encoding')
    end

    it 'displays the blob' do
      aggregate_failures do
        # shows a download link
        expect(page).to have_link('Download (23.8 KB)')

        # does not show a viewer switcher
        expect(page).not_to have_selector('.js-blob-viewer-switcher')

        # The specs below verify an arguably incorrect result, but since we only
        # learn that the file is not actually text once the text viewer content
        # is loaded asynchronously, there is no straightforward way to get these
        # synchronously loaded elements to display correctly.
        #
        # Clicking the copy button will result in nothing being copied.
        # Clicking the raw button will result in the binary file being downloaded,
        # as expected.

        # shows an enabled copy button, incorrectly
        expect(page).to have_selector('.js-copy-blob-source-btn:not(.disabled)')

        # shows a raw button, incorrectly
        expect(page).to have_link('Open raw')
      end
    end
  end

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
  context '.gitlab-ci.yml' do
    before do
      project.add_master(project.creator)

      Files::CreateService.new(
        project,
        project.creator,
        start_branch: 'master',
        branch_name: 'master',
        commit_message: "Add .gitlab-ci.yml",
        file_path: '.gitlab-ci.yml',
        file_content: File.read(Rails.root.join('spec/support/gitlab_stubs/gitlab_ci.yml'))
      ).execute

      visit_blob('.gitlab-ci.yml')
    end

    it 'displays an auxiliary viewer' do
      aggregate_failures do
        # shows that configuration is valid
        expect(page).to have_content('This GitLab CI configuration is valid.')

        # shows a learn more link
        expect(page).to have_link('Learn more')
      end
    end
  end

  context '.gitlab/route-map.yml' do
    before do
      project.add_master(project.creator)

      Files::CreateService.new(
        project,
        project.creator,
        start_branch: 'master',
        branch_name: 'master',
        commit_message: "Add .gitlab/route-map.yml",
        file_path: '.gitlab/route-map.yml',
        file_content: <<-MAP.strip_heredoc
          # Team data
          - source: 'data/team.yml'
            public: 'team/'
        MAP
      ).execute

      visit_blob('.gitlab/route-map.yml')
    end

    it 'displays an auxiliary viewer' do
      aggregate_failures do
        # shows that map is valid
        expect(page).to have_content('This Route Map is valid.')

        # shows a learn more link
        expect(page).to have_link('Learn more')
      end
    end
  end

  context 'LICENSE' do
    before do
      visit_blob('LICENSE')
    end

    it 'displays an auxiliary viewer' do
      aggregate_failures do
        # shows license
        expect(page).to have_content('This project is licensed under the MIT License.')

        # shows a learn more link
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
        expect(page).to have_link('Learn more', 'http://choosealicense.com/licenses/mit/')
      end
    end
  end

  context '*.gemspec' do
    before do
      project.add_master(project.creator)

      Files::CreateService.new(
        project,
        project.creator,
        start_branch: 'master',
        branch_name: 'master',
        commit_message: "Add activerecord.gemspec",
        file_path: 'activerecord.gemspec',
        file_content: <<-SPEC.strip_heredoc
          Gem::Specification.new do |s|
            s.platform    = Gem::Platform::RUBY
            s.name        = "activerecord"
          end
        SPEC
      ).execute

      visit_blob('activerecord.gemspec')
    end

    it 'displays an auxiliary viewer' do
      aggregate_failures do
Douwe Maan's avatar
Douwe Maan committed
501
        # shows names of dependency manager and package
502 503 504 505 506 507 508
        expect(page).to have_content('This project manages its dependencies using RubyGems and defines a gem named activerecord.')

        # shows a link to the gem
        expect(page).to have_link('activerecord', 'https://rubygems.org/gems/activerecord')

        # shows a learn more link
        expect(page).to have_link('Learn more', 'http://choosealicense.com/licenses/mit/')
509 510 511
      end
    end
  end
512 513 514

  context 'realtime pipelines' do
    before do
515 516 517 518 519 520 521 522 523 524 525 526
      Files::CreateService.new(
        project,
        project.creator,
        start_branch: 'feature',
        branch_name: 'feature',
        commit_message: "Add ruby file",
        file_path: 'files/ruby/test.rb',
        file_content: "# Awesome content"
      ).execute

      create(:ci_pipeline, status: 'running', project: project, ref: 'feature', sha: project.commit('feature').sha)
      visit_blob('files/ruby/test.rb', ref: 'feature')
527
    end
528

529
    it 'should show the realtime pipeline status' do
530 531 532 533 534
      page.within('.commit-actions') do
        expect(page).to have_css('.ci-status-icon')
        expect(page).to have_css('.ci-status-icon-running')
        expect(page).to have_css('.js-ci-status-icon-running')
      end
535 536
    end
  end
537
end