File: ide_helper_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (175 lines) | stat: -rw-r--r-- 5,881 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe IdeHelper, feature_category: :web_ide do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { project.creator }

  before do
    allow(helper).to receive(:current_user).and_return(user)
    allow(helper).to receive(:content_security_policy_nonce).and_return('test-csp-nonce')
    allow(helper).to receive(:new_session_path).and_return('test-sign-in-path')
  end

  describe '#ide_data' do
    let_it_be(:fork_info) { { ide_path: '/test/ide/path' } }
    let_it_be(:params) do
      {
        branch: 'master',
        path: 'foo/bar',
        merge_request_id: '1'
      }
    end

    let(:base_data) do
      {
        'use-new-web-ide' => 'false',
        'user-preferences-path' => profile_preferences_path,
        'sign-in-path' => 'test-sign-in-path',
        'project' => nil,
        'preview-markdown-path' => nil
      }
    end

    it 'returns hash' do
      expect(helper.ide_data(project: nil, fork_info: fork_info, params: params))
        .to include(base_data)
    end

    context 'with project' do
      it 'returns hash with parameters' do
        serialized_project = API::Entities::Project.represent(project, current_user: user).to_json

        expect(
          helper.ide_data(project: project, fork_info: nil, params: params)
        ).to include(base_data.merge(
          'fork-info' => nil,
          'branch-name' => params[:branch],
          'file-path' => params[:path],
          'merge-request' => params[:merge_request_id],
          'project' => serialized_project,
          'preview-markdown-path' => Gitlab::Routing.url_helpers.project_preview_markdown_path(project)
        ))
      end

      context 'with fork info' do
        it 'returns hash with fork info' do
          expect(helper.ide_data(project: project, fork_info: fork_info, params: params))
            .to include('fork-info' => fork_info.to_json)
        end
      end
    end

    context 'with vscode_web_ide=true' do
      let(:base_data) do
        {
          'use-new-web-ide' => 'true',
          'user-preferences-path' => profile_preferences_path,
          'sign-in-path' => 'test-sign-in-path',
          'new-web-ide-help-page-path' =>
            help_page_path('user/project/web_ide/index.md'),
          'csp-nonce' => 'test-csp-nonce'
        }
      end

      before do
        stub_feature_flags(vscode_web_ide: true)
      end

      it 'returns hash' do
        expect(helper.ide_data(project: nil, fork_info: fork_info, params: params))
          .to include(base_data)
      end

      it 'includes extensions gallery settings' do
        expect(WebIde::ExtensionsMarketplace).to receive(:webide_extensions_gallery_settings)
          .with(user: user).and_return({ enabled: false })

        actual = helper.ide_data(project: nil, fork_info: fork_info, params: params)

        expect(actual).to include({ 'extensions-gallery-settings' => { enabled: false }.to_json })
      end

      it 'includes editor font configuration' do
        ide_data = helper.ide_data(project: nil, fork_info: fork_info, params: params)
        editor_font = ::Gitlab::Json.parse(ide_data.fetch('editor-font'), symbolize_names: true)

        expect(editor_font).to include({
          fallback_font_family: 'monospace',
          font_faces: [
            {
              family: 'GitLab Mono',
              display: 'block',
              src: [{
                url: a_string_matching(%r{gitlab-mono/GitLabMono-[^I]}),
                format: 'woff2'
              }]
            },
            {
              family: 'GitLab Mono',
              display: 'block',
              style: 'italic',
              src: [{
                url: a_string_matching(%r{gitlab-mono/GitLabMono-Italic}),
                format: 'woff2'
              }]
            }
          ]
        })
      end

      it 'does not use new web ide if feature flag is disabled' do
        stub_feature_flags(vscode_web_ide: false)

        expect(helper.ide_data(project: nil, fork_info: fork_info, params: params))
          .to include('use-new-web-ide' => 'false')
      end

      context 'with project' do
        it 'returns hash with parameters' do
          expect(
            helper.ide_data(project: project, fork_info: nil, params: params)
          ).to include(base_data.merge(
            'branch-name' => params[:branch],
            'file-path' => params[:path],
            'merge-request' => params[:merge_request_id],
            'fork-info' => nil
          ))
        end
      end
    end
  end

  describe '#show_web_ide_oauth_callback_mismatch_callout?' do
    let_it_be(:oauth_application) { create(:oauth_application, owner: nil) }

    before do
      stub_feature_flags(vscode_web_ide: true)
    end

    it 'returns false if no Web IDE OAuth application found' do
      expect(helper.show_web_ide_oauth_callback_mismatch_callout?).to be false
    end

    it "returns true if domain does not match OAuth application callback URLs" do
      stub_application_setting({ web_ide_oauth_application: oauth_application })
      expect(helper.show_web_ide_oauth_callback_mismatch_callout?).to be true
    end

    it "returns false if domain matches OAuth application callback URL" do
      oauth_application.redirect_uri = "#{request.base_url}/oauth-redirect"
      stub_application_setting({ web_ide_oauth_application: oauth_application })
      expect(helper.show_web_ide_oauth_callback_mismatch_callout?).to be false
    end
  end

  describe '#web_ide_oauth_application_id' do
    let_it_be(:oauth_application) { create(:oauth_application, owner: nil) }

    it 'returns Web IDE OAuth application ID' do
      stub_application_setting({ web_ide_oauth_application: oauth_application })
      expect(helper.web_ide_oauth_application_id).to eq(oauth_application.id)
    end
  end
end