Welcome to mirror list, hosted at ThFree Co, Russian Federation.

repositories_controller_shared_examples.rb « harbor « shared_examples « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a0d47d1a2d112bd4ab4c6ab1c878c079208c06c1 (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
# frozen_string_literal: true

RSpec.shared_examples 'a harbor repositories controller' do |args|
  include HarborHelper
  let_it_be(:user) { create(:user) }
  let_it_be(:unauthorized_user) { create(:user) }
  let_it_be(:json_header) { { accept: 'application/json' } }

  let(:mock_repositories) do
    [
      {
        "artifact_count": 6,
        "creation_time": "2022-04-24T10:59:02.719Z",
        "id": 33,
        "name": "test/photon",
        "project_id": 3,
        "pull_count": 12,
        "update_time": "2022-04-24T11:06:27.678Z"
      },
      {
        "artifact_count": 1,
        "creation_time": "2022-04-23T08:04:08.880Z",
        "id": 1,
        "name": "test/gemnasium",
        "project_id": 3,
        "pull_count": 0,
        "update_time": "2022-04-23T08:04:08.880Z"
      }
    ]
  end

  shared_examples 'responds with 404 status' do
    it 'returns 404' do
      subject

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

  shared_examples 'responds with 200 status with html' do
    it 'renders the index template' do
      subject

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

  shared_examples 'responds with 302 status' do
    it 'returns 302' do
      subject

      expect(response).to redirect_to(new_user_session_path)
    end
  end

  shared_examples 'responds with 200 status with json' do
    it 'renders the index template' do
      subject

      expect(response).to have_gitlab_http_status(:ok)
      expect(response).not_to render_template(:index)
    end
  end

  shared_examples 'responds with 422 status with json' do
    it 'returns 422' do
      subject

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

  before do
    stub_request(:get, "https://demo.goharbor.io/api/v2.0/projects/testproject/repositories?page=1&page_size=10")
      .with(
        headers: {
      'Authorization': 'Basic aGFyYm9ydXNlcm5hbWU6aGFyYm9ycGFzc3dvcmQ=',
      'Content-Type': 'application/json'
      }).to_return(status: 200, body: mock_repositories.to_json, headers: { "x-total-count": 2 })
    container.add_reporter(user)
    sign_in(user)
  end

  describe 'GET #index.html' do
    subject do
      get harbor_repository_url(container)
    end

    it_behaves_like 'responds with 200 status with html'

    context 'with anonymous user' do
      before do
        sign_out(user)
      end

      it_behaves_like "responds with #{args[:anonymous_status_code]} status"
    end

    context 'with unauthorized user' do
      before do
        sign_in(unauthorized_user)
      end

      it_behaves_like 'responds with 404 status'
    end
  end

  describe 'GET #index.json' do
    subject do
      get harbor_repository_url(container), headers: json_header
    end

    it_behaves_like 'responds with 200 status with json'

    context 'with valid params' do
      context 'with valid page params' do
        subject do
          get harbor_repository_url(container, page: '1'), headers: json_header
        end

        it_behaves_like 'responds with 200 status with json'
      end

      context 'with valid limit params' do
        subject do
          get harbor_repository_url(container, limit: '10'), headers: json_header
        end

        it_behaves_like 'responds with 200 status with json'
      end
    end

    context 'with invalid params' do
      context 'with invalid page params' do
        subject do
          get harbor_repository_url(container, page: 'aaa'), headers: json_header
        end

        it_behaves_like 'responds with 422 status with json'
      end

      context 'with invalid limit params' do
        subject do
          get harbor_repository_url(container, limit: 'aaa'), headers: json_header
        end

        it_behaves_like 'responds with 422 status with json'
      end
    end
  end
end