summaryrefslogtreecommitdiff
path: root/spec/helpers/snippets_helper_spec.rb
blob: d88e151a11c65e217d656662da87100b804f097c (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
# frozen_string_literal: true

require 'spec_helper'

describe SnippetsHelper do
  include Gitlab::Routing
  include IconsHelper

  let_it_be(:public_personal_snippet) { create(:personal_snippet, :public) }
  let_it_be(:public_project_snippet) { create(:project_snippet, :public) }

  describe '#reliable_snippet_path' do
    subject { reliable_snippet_path(snippet) }

    context 'personal snippets' do
      let(:snippet) { public_personal_snippet }

      context 'public' do
        it 'returns a full path' do
          expect(subject).to eq("/snippets/#{snippet.id}")
        end
      end
    end

    context 'project snippets' do
      let(:snippet) { public_project_snippet }

      it 'returns a full path' do
        expect(subject).to eq("/#{snippet.project.full_path}/snippets/#{snippet.id}")
      end
    end
  end

  describe '#reliable_snippet_url' do
    subject { reliable_snippet_url(snippet) }

    context 'personal snippets' do
      let(:snippet) { public_personal_snippet }

      context 'public' do
        it 'returns a full url' do
          expect(subject).to eq("http://test.host/snippets/#{snippet.id}")
        end
      end
    end

    context 'project snippets' do
      let(:snippet) { public_project_snippet }

      it 'returns a full url' do
        expect(subject).to eq("http://test.host/#{snippet.project.full_path}/snippets/#{snippet.id}")
      end
    end
  end

  describe '#reliable_raw_snippet_path' do
    subject { reliable_raw_snippet_path(snippet) }

    context 'personal snippets' do
      let(:snippet) { public_personal_snippet }

      context 'public' do
        it 'returns a full path' do
          expect(subject).to eq("/snippets/#{snippet.id}/raw")
        end
      end
    end

    context 'project snippets' do
      let(:snippet) { public_project_snippet }

      it 'returns a full path' do
        expect(subject).to eq("/#{snippet.project.full_path}/snippets/#{snippet.id}/raw")
      end
    end
  end

  describe '#reliable_raw_snippet_url' do
    subject { reliable_raw_snippet_url(snippet) }

    context 'personal snippets' do
      let(:snippet) { public_personal_snippet }

      context 'public' do
        it 'returns a full url' do
          expect(subject).to eq("http://test.host/snippets/#{snippet.id}/raw")
        end
      end
    end

    context 'project snippets' do
      let(:snippet) { public_project_snippet }

      it 'returns a full url' do
        expect(subject).to eq("http://test.host/#{snippet.project.full_path}/snippets/#{snippet.id}/raw")
      end
    end
  end

  describe '#embedded_raw_snippet_button' do
    subject { embedded_raw_snippet_button.to_s }

    it 'returns view raw button of embedded snippets for personal snippets' do
      @snippet = create(:personal_snippet, :public)

      expect(subject).to eq(download_link("http://test.host/snippets/#{@snippet.id}/raw"))
    end

    it 'returns view raw button of embedded snippets for project snippets' do
      @snippet = create(:project_snippet, :public)

      expect(subject).to eq(download_link("http://test.host/#{@snippet.project.path_with_namespace}/snippets/#{@snippet.id}/raw"))
    end

    def download_link(url)
      "<a class=\"btn\" target=\"_blank\" rel=\"noopener noreferrer\" title=\"Open raw\" href=\"#{url}\">#{external_snippet_icon('doc-code')}</a>"
    end
  end

  describe '#embedded_snippet_download_button' do
    subject { embedded_snippet_download_button }

    it 'returns download button of embedded snippets for personal snippets' do
      @snippet = create(:personal_snippet, :public)

      expect(subject).to eq(download_link("http://test.host/snippets/#{@snippet.id}/raw"))
    end

    it 'returns download button of embedded snippets for project snippets' do
      @snippet = create(:project_snippet, :public)

      expect(subject).to eq(download_link("http://test.host/#{@snippet.project.path_with_namespace}/snippets/#{@snippet.id}/raw"))
    end

    def download_link(url)
      "<a class=\"btn\" target=\"_blank\" title=\"Download\" rel=\"noopener noreferrer\" href=\"#{url}?inline=false\">#{external_snippet_icon('download')}</a>"
    end
  end

  describe '#snippet_embed_tag' do
    subject { snippet_embed_tag(snippet) }

    context 'personal snippets' do
      let(:snippet) { public_personal_snippet }

      context 'public' do
        it 'returns a script tag with the snippet full url' do
          expect(subject).to eq(script_embed("http://test.host/snippets/#{snippet.id}"))
        end
      end
    end

    context 'project snippets' do
      let(:snippet) { public_project_snippet }

      it 'returns a script tag with the snippet full url' do
        expect(subject).to eq(script_embed("http://test.host/#{snippet.project.path_with_namespace}/snippets/#{snippet.id}"))
      end
    end

    def script_embed(url)
      "<script src=\"#{url}.js\"></script>"
    end
  end

  describe '#download_raw_snippet_button' do
    subject { download_raw_snippet_button(snippet) }

    context 'with personal snippet' do
      let(:snippet) { public_personal_snippet }

      it 'returns the download button' do
        expect(subject).to eq(download_link("/snippets/#{snippet.id}/raw"))
      end
    end

    context 'with project snippet' do
      let(:snippet) { public_project_snippet }

      it 'returns the download button' do
        expect(subject).to eq(download_link("/#{snippet.project.path_with_namespace}/snippets/#{snippet.id}/raw"))
      end
    end

    def download_link(url)
      "<a target=\"_blank\" rel=\"noopener noreferrer\" class=\"btn btn-sm has-tooltip\" title=\"Download\" data-container=\"body\" href=\"#{url}?inline=false\"><i aria-hidden=\"true\" data-hidden=\"true\" class=\"fa fa-download\"></i></a>"
    end
  end

  describe '#snippet_badge' do
    let(:snippet) { build(:personal_snippet, visibility) }

    subject { snippet_badge(snippet) }

    context 'when snippet is private' do
      let(:visibility) { :private }

      it 'returns the snippet badge' do
        expect(subject).to eq "<span class=\"badge badge-gray\"><i class=\"fa fa-lock\"></i> private</span>"
      end
    end

    context 'when snippet is public' do
      let(:visibility) { :public }

      it 'does not return anything' do
        expect(subject).to be_nil
      end
    end

    context 'when snippet is internal' do
      let(:visibility) { :internal }

      it 'does not return anything' do
        expect(subject).to be_nil
      end
    end
  end
end