summaryrefslogtreecommitdiff
path: root/spec/controllers/projects/badges_controller_spec.rb
blob: 242b2fd3ec6adb24590cbbc7d9af3b5c9b3b60e8 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::BadgesController do
  let_it_be(:project, reload: true) { create(:project, :repository) }
  let_it_be(:pipeline, reload: true) { create(:ci_empty_pipeline, project: project) }
  let_it_be(:user) { create(:user) }

  shared_examples 'a badge resource' do |badge_type|
    context 'when pipelines are public' do
      before do
        project.update!(public_builds: true)
      end

      context 'when project is public' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
        end

        it "returns the #{badge_type} badge to unauthenticated users" do
          get_badge(badge_type)

          expect(response).to have_gitlab_http_status(:ok)
        end
      end

      context 'when project is restricted' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
          project.add_guest(user)
          sign_in(user)
        end

        it "returns the #{badge_type} badge to guest users" do
          get_badge(badge_type)

          expect(response).to have_gitlab_http_status(:ok)
        end
      end
    end

    context 'format' do
      before do
        project.add_maintainer(user)
        sign_in(user)
      end

      it 'renders the `flat` badge layout by default' do
        get_badge(badge_type)

        expect(response).to render_template('projects/badges/badge')
      end

      context 'when style param is set to `flat`' do
        it 'renders the `flat` badge layout' do
          get_badge(badge_type, style: 'flat')

          expect(response).to render_template('projects/badges/badge')
        end
      end

      context 'when style param is set to an invalid type' do
        it 'renders the `flat` (default) badge layout' do
          get_badge(badge_type, style: 'xxx')

          expect(response).to render_template('projects/badges/badge')
        end
      end

      context 'when style param is set to `flat-square`' do
        it 'renders the `flat-square` badge layout' do
          get_badge(badge_type, style: 'flat-square')

          expect(response).to render_template('projects/badges/badge_flat-square')
        end
      end
    end

    context 'when pipelines are not public' do
      before do
        project.update!(public_builds: false)
      end

      context 'when project is public' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
        end

        it 'returns 404 to unauthenticated users' do
          get_badge(badge_type)

          expect(response).to have_gitlab_http_status(:not_found)
        end
      end

      context 'when project is restricted to the user' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::INTERNAL)
          project.add_guest(user)
          sign_in(user)
        end

        it 'defaults to project permissions' do
          get_badge(badge_type)

          expect(response).to have_gitlab_http_status(:not_found)
        end
      end
    end

    context 'customization' do
      render_views

      before do
        project.add_maintainer(user)
        sign_in(user)
      end

      context 'when key_text param is used' do
        it 'sets custom key text' do
          get_badge(badge_type, key_text: 'custom key text')

          expect(response.body).to include('custom key text')
        end
      end

      context 'when key_width param is used' do
        it 'sets custom key width' do
          get_badge(badge_type, key_width: '123')

          expect(response.body).to include('123')
        end
      end
    end
  end

  describe '#pipeline' do
    it_behaves_like 'a badge resource', :pipeline

    context 'with ignore_skipped param' do
      render_views

      before do
        pipeline.update!(status: :skipped)
        project.add_maintainer(user)
        sign_in(user)
      end

      it 'returns skipped badge if set to false' do
        get_badge(:pipeline, ignore_skipped: false)
        expect(response.body).to include('skipped')
      end

      it 'does not return skipped badge if set to true' do
        get_badge(:pipeline, ignore_skipped: true)
        expect(response.body).to include('unknown')
      end
    end
  end

  describe '#coverage' do
    it_behaves_like 'a badge resource', :coverage
  end

  def get_badge(badge, args = {})
    params = {
      namespace_id: project.namespace.to_param,
      project_id: project,
      ref: pipeline.ref
    }.merge(args.slice(:style, :key_text, :key_width, :ignore_skipped))

    get badge, params: params, format: :svg
  end
end