File: apps_spec.rb

package info (click to toggle)
ruby-octokit 10.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,092 kB
  • sloc: ruby: 13,339; sh: 99; makefile: 7; javascript: 3
file content (392 lines) | stat: -rw-r--r-- 13,575 bytes parent folder | download
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# frozen_string_literal: true

describe Octokit::Client::Apps do
  before(:each) do
    Octokit.reset!
    @client     = oauth_client
    @jwt_client = Octokit::Client.new(bearer_token: new_jwt_token)
    use_vcr_placeholder_for(@jwt_client.bearer_token, '<JWT_BEARER_TOKEN>')
  end

  after(:each) do
    Octokit.reset!
  end

  describe '.app', :vcr do
    it 'returns current App' do
      response = @jwt_client.app

      expect(response.id).not_to be_nil
      assert_requested :get, github_url('/app')
    end

    it 'works for GitHub Enterprise installs' do
      client = Octokit::Client.new \
        bearer_token: new_jwt_token,
        api_endpoint: 'https://ghe.local/api/v3'

      request = stub_get('https://ghe.local/api/v3/app')
      client.app

      assert_requested request
    end
  end

  describe '.list_app_installations', :vcr do
    it 'returns installations for an app' do
      installations = @jwt_client.list_app_installations
      expect(installations).to be_kind_of Array
      assert_requested :get, github_url('/app/installations')
    end

    it 'works for GitHub Enterprise installs' do
      client = Octokit::Client.new \
        bearer_token: new_jwt_token,
        api_endpoint: 'https://ghe.local/api/v3'

      request = stub_get('https://ghe.local/api/v3/app/installations')
      client.list_app_installations

      assert_requested request
    end

    it 'allows auto_pagination', :vcr do
      @client.auto_paginate = true
      installations = @client.list_app_installations(per_page: 1)

      expect(installations.count).to eq 2
      expect(installations).to be_kind_of(Array)
    end
  end # .list_app_installations

  describe '.list_user_installations', :vcr do
    it 'returns installations for a user' do
      response = @client.list_user_installations

      expect(response.total_count).not_to be_nil
      expect(response.installations).to be_kind_of(Array)
      assert_requested :get, github_url('/user/installations')
    end

    it 'works for GitHub Enterprise installs' do
      client = Octokit::Client.new \
        bearer_token: new_jwt_token,
        api_endpoint: 'https://ghe.local/api/v3'

      request = stub_get('https://ghe.local/api/v3/user/installations')
      client.list_user_installations

      assert_requested request
    end

    it 'allows auto_pagination', :vcr do
      @client.auto_paginate = true
      response = @client.list_user_installations(per_page: 1)

      expect(response.total_count).to eq 2
      expect(response.installations.count).to eq 2
      expect(response.installations).to be_kind_of(Array)
    end
  end # .list_user_installations

  describe '.find_organization_installation', :vcr do
    let(:organization) { test_github_org }

    it 'returns installation for an organization' do
      response = @jwt_client.find_organization_installation(organization)

      expect(response.id).not_to be_nil
      expect(response.target_type).to eq('Organization')
      assert_requested :get, github_url("/orgs/#{organization}/installation")
    end

    it 'works for GitHub Enterprise installs' do
      client = Octokit::Client.new \
        bearer_token: new_jwt_token,
        api_endpoint: 'https://ghe.local/api/v3'

      request = stub_get('https://ghe.local/api/v3/organizations/1234/installation')
      client.find_organization_installation(1234)

      assert_requested request
    end

    it 'allows auto_pagination' do
      @jwt_client.auto_paginate = true
      response = @jwt_client.find_organization_installation(organization, per_page: 1)

      expect(response.id).not_to be_nil
      expect(response.target_type).to eq('Organization')
    end
  end # .find_organization_installation

  describe '.find_repository_installation', :vcr do
    it 'returns installation for an repository' do
      response = @jwt_client.find_repository_installation(@test_org_repo)

      expect(response.id).not_to be_nil
      expect(response.target_type).to eq('Organization')
      assert_requested :get, github_url("/repos/#{@test_org_repo}/installation")
    end

    it 'works for GitHub Enterprise installs' do
      client = Octokit::Client.new \
        bearer_token: new_jwt_token,
        api_endpoint: 'https://ghe.local/api/v3'

      request = stub_get('https://ghe.local/api/v3/repos/testing/1234/installation')
      client.find_repository_installation('testing/1234')

      assert_requested request
    end

    it 'allows auto_pagination' do
      @jwt_client.auto_paginate = true
      response = @jwt_client.find_repository_installation(@test_org_repo, per_page: 1)

      expect(response.id).not_to be_nil
      expect(response.target_type).to eq('Organization')
    end
  end # .find_repository_installation

  describe '.find_user_installation', :vcr do
    let(:user) { test_github_login }

    it 'returns installation for a user' do
      response = @jwt_client.find_user_installation(user)

      expect(response.id).not_to be_nil
      expect(response.account.login).to eq(user)
      assert_requested :get, github_url("/users/#{user}/installation")
    end

    it 'works for GitHub Enterprise installs' do
      client = Octokit::Client.new \
        bearer_token: new_jwt_token,
        api_endpoint: 'https://ghe.local/api/v3'

      request = stub_get('https://ghe.local/api/v3/users/1234/installation')
      client.find_user_installation('1234')

      assert_requested request
    end

    it 'allows auto_pagination' do
      @jwt_client.auto_paginate = true
      response = @jwt_client.find_user_installation(user, per_page: 1)

      expect(response.id).not_to be_nil
      expect(response.account.login).to eq(user)
    end
  end # .find_user_installation

  describe '.list_app_hook_deliveries', :vcr do
    it 'lists hook deliveries for an app' do
      response = @jwt_client.list_app_hook_deliveries
      expect(response).to be_kind_of(Array)
      expect(response.count).to eq 2
    end

    it 'allows auto_pagination' do
      @jwt_client.auto_paginate = true
      response = @jwt_client.list_app_hook_deliveries(per_page: 1)

      expect(response).to be_kind_of(Array)
      expect(response.count).to eq 3
    end
  end # .list_app_hook_deliveries

  describe '.app_hook_delivery', :vcr do
    let(:delivery_id) { 73_209_766_136 }
    it 'returns a webhook delivery' do
      response = @jwt_client.app_hook_delivery(delivery_id)
      expect(response.id).to eq(73_209_766_136)
    end
  end # .app_hook_delivery

  describe '.deliver_app_hook', :vcr do
    let(:delivery_id) { 55_148_726_666 }
    it 'schedules a webhook for redelivery' do
      response = @jwt_client.deliver_app_hook(delivery_id)
      expect(response).to be_truthy
    end
  end # .deliver_app_hook

  context 'with app installation', :vcr do
    let(:installation) { test_github_integration_installation }

    describe '.installation' do
      it 'returns the installation' do
        response = @jwt_client.installation(installation)
        expect(response).to be_kind_of Sawyer::Resource
        assert_requested :get, github_url("/app/installations/#{installation}")
      end

      it 'works for GitHub Enterprise installs' do
        client = Octokit::Client.new \
          bearer_token: new_jwt_token,
          api_endpoint: 'https://ghe.local/api/v3'

        request = stub_get('https://ghe.local/api/v3/app/installations/1234')
        client.installation(1234)

        assert_requested request
      end
    end # .installation

    describe '.find_installation_repositories_for_user' do
      it 'returns repositories for a user' do
        response = @client.find_installation_repositories_for_user(installation)
        expect(response.total_count).not_to be_nil
        expect(response.repositories).to be_kind_of(Array)
        assert_requested :get, github_url("/user/installations/#{installation}/repositories")
      end

      it 'works for GitHub Enterprise installs' do
        client = Octokit::Client.new \
          bearer_token: new_jwt_token,
          api_endpoint: 'https://ghe.local/api/v3'

        request = stub_get('https://ghe.local/api/v3/user/installations/1234/repositories')
        client.find_installation_repositories_for_user(1234)

        assert_requested request
      end

      it 'allows auto_pagination', :vcr do
        @client.auto_paginate = true
        response = @client.find_installation_repositories_for_user(installation, per_page: 1)

        expect(response.total_count).to eq 2
        expect(response.repositories.count).to eq 2
        expect(response.repositories).to be_kind_of(Array)
      end
    end # .find_installation_repositories_for_user

    describe '.create_app_installation_access_token' do
      it 'creates an access token for the installation' do
        response = @jwt_client.create_app_installation_access_token(installation)

        expect(response).to be_kind_of(Sawyer::Resource)
        expect(response.token).not_to be_nil
        expect(response.expires_at).not_to be_nil

        assert_requested :post, github_url("/app/installations/#{installation}/access_tokens")
      end

      it 'works for GitHub Enterprise installs' do
        client = Octokit::Client.new \
          bearer_token: new_jwt_token,
          api_endpoint: 'https://ghe.local/api/v3'

        path = 'app/installations/1234/access_tokens'
        request = stub_post("https://ghe.local/api/v3/#{path}")
        client.create_app_installation_access_token(1234)

        assert_requested request
      end
    end # .create_app_installation_access_token

    describe '.delete_installation' do
      it 'deletes an installation' do
        response = @jwt_client.delete_installation(installation)
        expect(response).to be_truthy
      end
    end # .delete_installation

    context 'with app installation access token' do
      let(:installation_client) do
        token = @jwt_client.create_app_installation_access_token(installation).token
        use_vcr_placeholder_for(token, '<INTEGRATION_INSTALLATION_TOKEN>')
        Octokit::Client.new(access_token: token)
      end

      let(:ghe_installation_client) do
        Octokit::Client.new \
          access_token: 'v1.1f699f1069f60xxx',
          api_endpoint: 'https://ghe.local/api/v3'
      end

      describe '.list_app_installation_repositories' do
        it 'lists the installations repositories' do
          response = installation_client.list_app_installation_repositories
          expect(response.total_count).not_to be_nil
          expect(response.repositories).to be_kind_of(Array)
        end

        it 'works for GitHub Enterprise installs' do
          request = stub_get('https://ghe.local/api/v3/installation/repositories')
          ghe_installation_client.list_app_installation_repositories
          assert_requested request
        end
        it 'allows auto_pagination', :vcr do
          installation_client.auto_paginate = true
          response = installation_client.list_app_installation_repositories({ per_page: 1 })

          expect(response.total_count).to eq 2
          expect(response.repositories.count).to eq 2
          expect(response.repositories).to be_kind_of(Array)
        end
      end # .list_app_installation_repositories
    end # with app installation access token

    context 'with repository' do
      let(:repository) { test_org_repo }

      before(:each) do
        @repo = @client.create_repository(
          "#{test_github_repository}_#{Time.now.to_f}",
          organization: test_github_org
        )
      end

      after(:each) do
        @client.delete_repository(@repo.full_name)
      end

      describe '.add_repository_to_app_installation' do
        it 'adds the repository to the installation' do
          response = @client.add_repository_to_app_installation(installation, @repo.id)
          expect(response).to be_truthy
        end
      end # .add_repository_to_app_installation

      context 'with installed repository on installation' do
        before(:each) do
          @client.add_repository_to_app_installation(installation, @repo.id)
        end

        describe '.remove_repository_from_app_installation' do
          it 'removes the repository from the installation' do
            response = @client.remove_repository_from_app_installation(installation, @repo.id)
            expect(response).to be_truthy
          end
        end # .remove_repository_from_app_installation
      end # with installed repository on installation
    end # with repository

    context 'with repository on GitHub Enterprise' do
      let(:ghe_client) do
        Octokit::Client.new \
          access_token: 'x' * 40,
          api_endpoint: 'https://ghe.local/api/v3'
      end

      describe '.add_repository_to_app_installation' do
        it 'works for GitHub Enterprise installs' do
          request = stub_put('https://ghe.local/api/v3/user/installations/1234/repositories/1234')
          ghe_client.add_repository_to_app_installation(1234, 1234)
          assert_requested request
        end
      end # .add_repository_to_app_installation

      describe '.remove_repository_from_app_installation' do
        it 'works for GitHub Enterprise installs' do
          request = stub_delete('https://ghe.local/api/v3/user/installations/1234/repositories/1234')
          ghe_client.remove_repository_from_app_installation(1234, 1234)
          assert_requested request
        end
      end # .remove_repository_from_app_installation
    end # with repository on GitHub Enterprise
  end # with app installation
end