summaryrefslogtreecommitdiff
path: root/spec/lib/api/entities/snippet_spec.rb
blob: dada0942e49cef892d72d8d7c9b4e8e2db3f4fe0 (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
# frozen_string_literal: true

require 'spec_helper'

describe ::API::Entities::Snippet do
  let_it_be(:user) { create(:user) }
  let_it_be(:personal_snippet) { create(:personal_snippet, :repository, author: user ) }
  let_it_be(:project_snippet) { create(:project_snippet, :repository, author: user) }

  let(:entity) { described_class.new(snippet) }

  subject { entity.as_json }

  shared_examples 'common attributes' do
    it { expect(subject[:id]).to eq snippet.id }
    it { expect(subject[:title]).to eq snippet.title }
    it { expect(subject[:description]).to eq snippet.description }
    it { expect(subject[:updated_at]).to eq snippet.updated_at }
    it { expect(subject[:created_at]).to eq snippet.created_at }
    it { expect(subject[:project_id]).to eq snippet.project_id }
    it { expect(subject[:visibility]).to eq snippet.visibility }
    it { expect(subject).to include(:author) }

    describe 'file_name' do
      it 'returns attribute from repository' do
        expect(subject[:file_name]).to eq snippet.blobs.first.path
      end

      context 'when repository is empty' do
        it 'returns attribute from db' do
          allow(snippet.repository).to receive(:empty?).and_return(true)

          expect(subject[:file_name]).to eq snippet.file_name
        end
      end
    end

    describe 'ssh_url_to_repo' do
      it 'returns attribute' do
        expect(subject[:ssh_url_to_repo]).to eq snippet.ssh_url_to_repo
      end

      context 'when repository does not exist' do
        it 'does not include attribute' do
          allow(snippet).to receive(:repository_exists?).and_return(false)

          expect(subject).not_to include(:ssh_url_to_repo)
        end
      end
    end

    describe 'http_url_to_repo' do
      it 'returns attribute' do
        expect(subject[:http_url_to_repo]).to eq snippet.http_url_to_repo
      end

      context 'when repository does not exist' do
        it 'does not include attribute' do
          allow(snippet).to receive(:repository_exists?).and_return(false)

          expect(subject).not_to include(:http_url_to_repo)
        end
      end
    end
  end

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

    it_behaves_like 'common attributes'

    it 'returns snippet web_url attribute' do
      expect(subject[:web_url]).to match("/snippets/#{snippet.id}")
    end

    it 'returns snippet raw_url attribute' do
      expect(subject[:raw_url]).to match("/snippets/#{snippet.id}/raw")
    end
  end

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

    it_behaves_like 'common attributes'

    it 'returns snippet web_url attribute' do
      expect(subject[:web_url]).to match("#{snippet.project.full_path}/snippets/#{snippet.id}")
    end

    it 'returns snippet raw_url attribute' do
      expect(subject[:raw_url]).to match("#{snippet.project.full_path}/snippets/#{snippet.id}/raw")
    end
  end
end