File: oauth_applications_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 (164 lines) | stat: -rw-r--r-- 5,489 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require 'securerandom'

# Right now until we come up with a better way we have to make these tokens
# by hand because the Authorizations API is going away.
#
# See https://developer.github.com/changes/2019-11-05-deprecated-passwords-and-authorizations-api/
describe Octokit::Client::OauthApplications do
  before do
    Octokit.reset!

    @app_client = Octokit::Client.new(
      client_id: test_github_client_id,
      client_secret: test_github_client_secret
    )

    @access_token = test_github_oauth_token
  end

  after do
    Octokit.reset!
  end

  describe '.check_token' do
    it 'checks the token is valid', :vcr do
      @app_client.check_token(@access_token)
      path = "/applications/#{test_github_client_id}/token"

      expect(WebMock).to have_requested(:post, github_url(path)).with(
        basic_auth: [test_github_client_id, test_github_client_secret]
      )
    end

    it 'has a .check_application_authorization alias', :vcr do
      @app_client.check_application_authorization(@access_token)
      path = "/applications/#{test_github_client_id}/token"

      expect(WebMock).to have_requested(:post, github_url(path)).with(
        basic_auth: [test_github_client_id, test_github_client_secret]
      )
    end

    it 'works in Enterprise mode' do
      api_endpoint  = 'https://gh-enterprise.com/api/v3'
      client_id     = 'abcde12345fghij67890'
      client_secret = 'abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd'
      token         = '25f94a2a5c7fbaf499c665bc73d67c1c87e496da8985131633ee0a95819db2e8'

      path = File.join(api_endpoint, "/applications/#{client_id}/token")

      client = Octokit::Client.new(
        client_id: client_id,
        client_secret: client_secret,
        api_endpoint: api_endpoint
      )

      request = stub_request(:post, path).with(basic_auth: [client_id, client_secret])
      client.check_token(token)

      assert_requested request
    end
  end # .check_token

  describe '.reset_token' do
    it 'resets the token', :vcr do
      @app_client.reset_token(@access_token)
      path = "/applications/#{test_github_client_id}/token"

      expect(WebMock).to have_requested(:patch, github_url(path)).with(
        basic_auth: [test_github_client_id, test_github_client_secret]
      )
    end

    it 'has a .reset_application_authorization alias', :vcr do
      @app_client.reset_application_authorization(@access_token)
      path = "/applications/#{test_github_client_id}/token"

      expect(WebMock).to have_requested(:patch, github_url(path)).with(
        basic_auth: [test_github_client_id, test_github_client_secret]
      )
    end

    it 'works in Enterprise mode' do
      api_endpoint  = 'https://gh-enterprise.com/api/v3'
      client_id     = 'abcde12345fghij67890'
      client_secret = 'abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd'
      token         = '25f94a2a5c7fbaf499c665bc73d67c1c87e496da8985131633ee0a95819db2e8'

      path = File.join(api_endpoint, "/applications/#{client_id}/token")

      client = Octokit::Client.new(
        client_id: client_id,
        client_secret: client_secret,
        api_endpoint: api_endpoint
      )

      request = stub_request(:patch, path).with(basic_auth: [client_id, client_secret])
      client.reset_token(token)

      assert_requested request
    end
  end # .reset_token

  describe '.delete_app_token' do
    it 'deletes the token', :vcr do
      @app_client.delete_app_token(@access_token)
      path = "/applications/#{test_github_client_id}/token"

      expect(WebMock).to have_requested(:delete, github_url(path)).with(
        basic_auth: [test_github_client_id, test_github_client_secret]
      )
    end

    it 'has a .delete_application_authorization alias', :vcr do
      @app_client.delete_application_authorization(@access_token)
      path = "/applications/#{test_github_client_id}/token"

      expect(WebMock).to have_requested(:delete, github_url(path)).with(
        basic_auth: [test_github_client_id, test_github_client_secret]
      )
    end

    it 'has a .revoke_application_authorization alias', :vcr do
      @app_client.revoke_application_authorization(@access_token)
      path = "/applications/#{test_github_client_id}/token"

      expect(WebMock).to have_requested(:delete, github_url(path)).with(
        basic_auth: [test_github_client_id, test_github_client_secret]
      )
    end

    it 'works in Enterprise mode' do
      api_endpoint  = 'https://gh-enterprise.com/api/v3'
      client_id     = 'abcde12345fghij67890'
      client_secret = 'abcdabcdabcdabcdabcdabcdabcdabcdabcdabcd'
      token         = '25f94a2a5c7fbaf499c665bc73d67c1c87e496da8985131633ee0a95819db2e8'

      path = File.join(api_endpoint, "/applications/#{client_id}/token")

      client = Octokit::Client.new(
        client_id: client_id,
        client_secret: client_secret,
        api_endpoint: api_endpoint
      )

      request = stub_request(:delete, path).with(basic_auth: [client_id, client_secret])
      client.delete_app_token(token)

      assert_requested request
    end
  end # .delete_app_token

  describe '.delete_app_authorization', :vcr do
    it "revokes the app's access to the user" do
      @app_client.delete_app_authorization(@access_token)
      path = "/applications/#{test_github_client_id}/grant"

      expect(WebMock).to have_requested(:delete, github_url(path)).with(
        basic_auth: [test_github_client_id, test_github_client_secret]
      )
    end
  end
end