summaryrefslogtreecommitdiff
path: root/spec/presenters/snippet_presenter_spec.rb
blob: 591d86652b64706e0de0ad4370b911f4ea1be25d (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
# frozen_string_literal: true

require 'spec_helper'

describe SnippetPresenter do
  include Gitlab::Routing.url_helpers

  let_it_be(:user) { create(:user) }
  let_it_be(:personal_snippet) { create(:personal_snippet, author: user) }
  let_it_be(:project_snippet) { create(:project_snippet, author: user) }

  let(:project) { project_snippet.project }
  let(:presenter) { described_class.new(snippet, current_user: user) }

  before do
    project.add_developer(user)
  end

  describe '#web_url' do
    subject { presenter.web_url }

    context 'with PersonalSnippet' do
      let(:snippet) { personal_snippet }

      it 'returns snippet web url' do
        expect(subject).to match "/snippets/#{snippet.id}"
      end
    end

    context 'with ProjectSnippet' do
      let(:snippet) { project_snippet }

      it 'returns snippet web url' do
        expect(subject).to match "/#{project.full_path}/snippets/#{snippet.id}"
      end
    end
  end

  describe '#raw_url' do
    subject { presenter.raw_url }

    context 'with PersonalSnippet' do
      let(:snippet) { personal_snippet }

      it 'returns snippet web url' do
        expect(subject).to match "/snippets/#{snippet.id}/raw"
      end
    end

    context 'with ProjectSnippet' do
      let(:snippet) { project_snippet }

      it 'returns snippet web url' do
        expect(subject).to match "/#{project.full_path}/snippets/#{snippet.id}/raw"
      end
    end
  end

  describe '#can_read_snippet?' do
    subject { presenter.can_read_snippet? }

    context 'with PersonalSnippet' do
      let(:snippet) { personal_snippet }

      it 'checks read_snippet' do
        expect(presenter).to receive(:can?).with(user, :read_snippet, snippet)

        subject
      end
    end

    context 'with ProjectSnippet' do
      let(:snippet) { project_snippet }

      it 'checks read_snippet ' do
        expect(presenter).to receive(:can?).with(user, :read_snippet, snippet)

        subject
      end
    end
  end

  describe '#can_update_snippet?' do
    subject { presenter.can_update_snippet? }

    context 'with PersonalSnippet' do
      let(:snippet) { personal_snippet }

      it 'checks update_snippet' do
        expect(presenter).to receive(:can?).with(user, :update_snippet, snippet)

        subject
      end
    end

    context 'with ProjectSnippet' do
      let(:snippet) { project_snippet }

      it 'checks update_snippet ' do
        expect(presenter).to receive(:can?).with(user, :update_snippet, snippet)

        subject
      end
    end
  end

  describe '#can_admin_snippet?' do
    subject { presenter.can_admin_snippet? }

    context 'with PersonalSnippet' do
      let(:snippet) { personal_snippet }

      it 'checks admin_snippet' do
        expect(presenter).to receive(:can?).with(user, :admin_snippet, snippet)

        subject
      end
    end

    context 'with ProjectSnippet' do
      let(:snippet) { project_snippet }

      it 'checks admin_snippet ' do
        expect(presenter).to receive(:can?).with(user, :admin_snippet, snippet)

        subject
      end
    end
  end

  describe '#can_report_as_spam' do
    let(:snippet) { personal_snippet }

    subject { presenter.can_report_as_spam? }

    it 'returns false if the user cannot submit the snippet as spam' do
      expect(subject).to be_falsey
    end

    it 'returns true if the user can submit the snippet as spam' do
      allow(snippet).to receive(:submittable_as_spam_by?).and_return(true)

      expect(subject).to be_truthy
    end
  end

  describe '#blob' do
    let(:snippet) { personal_snippet }

    subject { presenter.blob }

    context 'when snippet does not have a repository' do
      it 'returns SnippetBlob' do
        expect(subject).to eq snippet.blob
      end
    end

    context 'when snippet has a repository' do
      let(:snippet) { create(:snippet, :repository, author: user) }

      it 'returns repository first blob' do
        expect(subject).to eq snippet.blobs.first
      end
    end
  end
end