url_builder_spec.rb 4.16 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::UrlBuilder do
4 5 6 7
  describe '.build' do
    context 'when passing a Commit' do
      it 'returns a proper URL' do
        commit = build_stubbed(:commit)
8

9 10
        url = described_class.build(commit)

11
        expect(url).to eq "#{Settings.gitlab['url']}/#{commit.project.full_path}/commit/#{commit.id}"
12
      end
13
    end
14

15 16 17
    context 'when passing an Issue' do
      it 'returns a proper URL' do
        issue = build_stubbed(:issue, iid: 42)
18

19 20
        url = described_class.build(issue)

21
        expect(url).to eq "#{Settings.gitlab['url']}/#{issue.project.full_path}/issues/#{issue.iid}"
22
      end
23 24
    end

25 26 27 28 29
    context 'when passing a MergeRequest' do
      it 'returns a proper URL' do
        merge_request = build_stubbed(:merge_request, iid: 42)

        url = described_class.build(merge_request)
30

31
        expect(url).to eq "#{Settings.gitlab['url']}/#{merge_request.project.full_path}/merge_requests/#{merge_request.iid}"
32
      end
33 34
    end

35 36 37 38
    context 'when passing a Note' do
      context 'on a Commit' do
        it 'returns a proper URL' do
          note = build_stubbed(:note_on_commit)
39

40
          url = described_class.build(note)
41

42
          expect(url).to eq "#{Settings.gitlab['url']}/#{note.project.full_path}/commit/#{note.commit_id}#note_#{note.id}"
43 44
        end
      end
45

Douwe Maan's avatar
Douwe Maan committed
46
      context 'on a Commit Diff' do
47
        it 'returns a proper URL' do
Douwe Maan's avatar
Douwe Maan committed
48
          note = build_stubbed(:diff_note_on_commit)
49

50
          url = described_class.build(note)
51

52
          expect(url).to eq "#{Settings.gitlab['url']}/#{note.project.full_path}/commit/#{note.commit_id}#note_#{note.id}"
53 54 55 56 57 58 59 60 61 62
        end
      end

      context 'on an Issue' do
        it 'returns a proper URL' do
          issue = create(:issue, iid: 42)
          note = build_stubbed(:note_on_issue, noteable: issue)

          url = described_class.build(note)

63
          expect(url).to eq "#{Settings.gitlab['url']}/#{issue.project.full_path}/issues/#{issue.iid}#note_#{note.id}"
64 65 66 67 68 69 70 71 72 73
        end
      end

      context 'on a MergeRequest' do
        it 'returns a proper URL' do
          merge_request = create(:merge_request, iid: 42)
          note = build_stubbed(:note_on_merge_request, noteable: merge_request)

          url = described_class.build(note)

74
          expect(url).to eq "#{Settings.gitlab['url']}/#{merge_request.project.full_path}/merge_requests/#{merge_request.iid}#note_#{note.id}"
75 76 77
        end
      end

Douwe Maan's avatar
Douwe Maan committed
78
      context 'on a MergeRequest Diff' do
79 80
        it 'returns a proper URL' do
          merge_request = create(:merge_request, iid: 42)
Douwe Maan's avatar
Douwe Maan committed
81
          note = build_stubbed(:diff_note_on_merge_request, noteable: merge_request)
82 83 84

          url = described_class.build(note)

85
          expect(url).to eq "#{Settings.gitlab['url']}/#{merge_request.project.full_path}/merge_requests/#{merge_request.iid}#note_#{note.id}"
86 87 88 89 90 91 92 93 94 95
        end
      end

      context 'on a ProjectSnippet' do
        it 'returns a proper URL' do
          project_snippet = create(:project_snippet)
          note = build_stubbed(:note_on_project_snippet, noteable: project_snippet)

          url = described_class.build(note)

96
          expect(url).to eq "#{Settings.gitlab['url']}/#{project_snippet.project.full_path}/snippets/#{note.noteable_id}#note_#{note.id}"
97 98
        end
      end
99

100 101 102 103 104 105 106 107 108 109 110
      context 'on a PersonalSnippet' do
        it 'returns a proper URL' do
          personal_snippet = create(:personal_snippet)
          note = build_stubbed(:note_on_personal_snippet, noteable: personal_snippet)

          url = described_class.build(note)

          expect(url).to eq "#{Settings.gitlab['url']}/snippets/#{note.noteable_id}#note_#{note.id}"
        end
      end

111 112
      context 'on another object' do
        it 'returns a proper URL' do
113
          project = build_stubbed(:project)
114

115 116
          expect { described_class.build(project) }
            .to raise_error(NotImplementedError, 'No URL builder defined for Project')
117 118
        end
      end
119
    end
Sebastian Klier's avatar
Sebastian Klier committed
120 121 122 123 124 125

    context 'when passing a WikiPage' do
      it 'returns a proper URL' do
        wiki_page = build(:wiki_page)
        url = described_class.build(wiki_page)

126
        expect(url).to eq "#{Gitlab.config.gitlab.url}#{wiki_page.wiki.wiki_base_path}/#{wiki_page.slug}"
Sebastian Klier's avatar
Sebastian Klier committed
127 128
      end
    end
129
  end
130
end