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

client_spec.rb « zentao « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b17ad867f0dcafd1f32237ce68149ef2d502a914 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Zentao::Client, :clean_gitlab_redis_cache do
  subject(:client) { described_class.new(zentao_integration) }

  let(:zentao_integration) { create(:zentao_integration) }

  def mock_get_products_url
    client.send(:url, "products/#{zentao_integration.zentao_product_xid}")
  end

  def mock_fetch_issues_url
    client.send(:url, "products/#{zentao_integration.zentao_product_xid}/issues")
  end

  def mock_fetch_issue_url(issue_id)
    client.send(:url, "issues/#{issue_id}")
  end

  let(:mock_headers) do
    {
      headers: {
        'Content-Type' => 'application/json',
        'Token' => zentao_integration.api_token
      }
    }
  end

  describe '#new' do
    context 'if integration is nil' do
      let(:zentao_integration) { nil }

      it 'raises ConfigError' do
        expect { client }.to raise_error(described_class::ConfigError)
      end
    end

    context 'integration is provided' do
      it 'is initialized successfully' do
        expect { client }.not_to raise_error
      end
    end
  end

  describe '#fetch_product' do
    context 'with valid product' do
      let(:mock_response) { { 'id' => zentao_integration.zentao_product_xid } }

      before do
        WebMock.stub_request(:get, mock_get_products_url)
               .with(mock_headers).to_return(status: 200, body: mock_response.to_json)
      end

      it 'fetches the product' do
        expect(client.fetch_product(zentao_integration.zentao_product_xid)).to eq mock_response
      end
    end

    context 'with invalid product' do
      before do
        WebMock.stub_request(:get, mock_get_products_url)
               .with(mock_headers).to_return(status: 404, body: {}.to_json)
      end

      it 'fetches the empty product' do
        expect do
          client.fetch_product(zentao_integration.zentao_product_xid)
        end.to raise_error(Gitlab::Zentao::Client::RequestError)
      end
    end

    context 'with invalid response' do
      before do
        WebMock.stub_request(:get, mock_get_products_url)
               .with(mock_headers).to_return(status: 200, body: '[invalid json}')
      end

      it 'fetches the empty product' do
        expect do
          client.fetch_product(zentao_integration.zentao_product_xid)
        end.to raise_error(Gitlab::Zentao::Client::Error, 'invalid response format')
      end
    end
  end

  describe '#ping' do
    context 'with valid resource' do
      before do
        WebMock.stub_request(:get, mock_get_products_url)
               .with(mock_headers).to_return(status: 200, body: { 'deleted' => '0' }.to_json)
      end

      it 'responds with success' do
        expect(client.ping[:success]).to eq true
      end
    end

    context 'with deleted resource' do
      before do
        WebMock.stub_request(:get, mock_get_products_url)
               .with(mock_headers).to_return(status: 200, body: { 'deleted' => '1' }.to_json)
      end

      it 'responds with unsuccess' do
        expect(client.ping[:success]).to eq false
      end
    end
  end

  describe '#fetch_issues' do
    let(:mock_response) { { 'issues' => [{ 'id' => 'story-1' }, { 'id' => 'bug-11' }] } }

    before do
      WebMock.stub_request(:get, mock_fetch_issues_url)
             .with(mock_headers).to_return(status: 200, body: mock_response.to_json)
    end

    it 'returns the response' do
      expect(client.fetch_issues).to eq(mock_response)
    end

    describe 'marking the issues as seen in the product' do
      let(:cache) { ::Gitlab::SetCache.new }
      let(:cache_key) do
        [
          :zentao_product_issues,
          OpenSSL::Digest::SHA256.hexdigest(zentao_integration.client_url),
          zentao_integration.zentao_product_xid
        ].join(':')
      end

      it 'adds issue ids to the cache' do
        expect { client.fetch_issues }.to change { cache.read(cache_key) }
          .from(be_empty)
          .to match_array(%w[bug-11 story-1])
      end

      it 'does not add issue ids to the cache if max set size has been reached' do
        cache.write(cache_key, %w[foo bar])
        stub_const("#{described_class}::CACHE_MAX_SET_SIZE", 1)

        client.fetch_issues

        expect(cache.read(cache_key)).to match_array(%w[foo bar])
      end

      it 'does not duplicate issue ids in the cache' do
        client.fetch_issues
        client.fetch_issues

        expect(cache.read(cache_key)).to match_array(%w[bug-11 story-1])
      end

      it 'touches the cache ttl every time issues are fetched' do
        fresh_ttl = 1.month.to_i

        freeze_time do
          client.fetch_issues

          expect(cache.ttl(cache_key)).to eq(fresh_ttl)
        end

        travel_to(1.minute.from_now) do
          client.fetch_issues

          expect(cache.ttl(cache_key)).to eq(fresh_ttl)
        end
      end
    end
  end

  describe '#fetch_issue' do
    context 'with invalid id' do
      let(:invalid_ids) { ['story', 'story-', '-', '123', ''] }

      it 'raises Error' do
        invalid_ids.each do |id|
          expect { client.fetch_issue(id) }
            .to raise_error(Gitlab::Zentao::Client::Error, 'invalid issue id')
        end
      end
    end

    context 'with valid id' do
      let(:valid_ids) { %w[story-1 bug-23] }

      context 'when issue has been seen on the index' do
        before do
          issues_body = { issues: valid_ids.map { { id: _1 } } }.to_json

          WebMock.stub_request(:get, mock_fetch_issues_url)
                 .with(mock_headers).to_return(status: 200, body: issues_body)

          client.fetch_issues
        end

        it 'fetches the issue' do
          valid_ids.each do |id|
            WebMock.stub_request(:get, mock_fetch_issue_url(id))
                  .with(mock_headers).to_return(status: 200, body: { issue: { id: id } }.to_json)

            expect(client.fetch_issue(id).dig('issue', 'id')).to eq id
          end
        end
      end

      context 'when issue has not been seen on the index' do
        it 'raises RequestError' do
          valid_ids.each do |id|
            expect { client.fetch_issue(id) }.to raise_error(Gitlab::Zentao::Client::RequestError)
          end
        end
      end
    end
  end

  describe '#url' do
    context 'api url' do
      shared_examples 'joins api_url correctly' do
        it 'verify url' do
          expect(client.send(:url, "products/1").to_s)
            .to eq("https://jihudemo.zentao.net/zentao/api.php/v1/products/1")
        end
      end

      context 'no ends slash' do
        let(:zentao_integration) { create(:zentao_integration, api_url: 'https://jihudemo.zentao.net/zentao') }

        include_examples 'joins api_url correctly'
      end

      context 'ends slash' do
        let(:zentao_integration) { create(:zentao_integration, api_url: 'https://jihudemo.zentao.net/zentao/') }

        include_examples 'joins api_url correctly'
      end
    end

    context 'no api url' do
      let(:zentao_integration) { create(:zentao_integration, url: 'https://jihudemo.zentao.net') }

      it 'joins url correctly' do
        expect(client.send(:url, "products/1").to_s)
          .to eq("https://jihudemo.zentao.net/api.php/v1/products/1")
      end
    end
  end
end