0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-11 02:59:07 +08:00
discourse/plugins/discourse-patreon/spec/integration/patreon_auth_spec.rb
Rafael dos Santos Silva 8c0a4bb8f9
DEV: Upgrade Patreon plugin to API v2 with v1 backward compatibility (#38871)
## Summary

Patreon API v1 has been deprecated for years and the Patreon team has
requested we migrate to v2 (see
https://meta.discourse.org/t/upgrade-patreon-discourse-plugin-to-api-v2/386701).

Since hundreds of existing customers have v1 OAuth clients, this PR
supports **both versions simultaneously** via an adapter pattern,
controlled by a new `patreon_api_version` site setting (defaults to
`"1"` so no one breaks on upgrade).

### What changed

- **Adapter pattern**: `Patreon::ApiVersion::V1` and `ApiVersion::V2`
modules with identical interfaces for endpoints, response parsing, and
OAuth config. `ApiVersion.current` routes based on the site setting.
- **v2 API support**: campaigns endpoint returns tiers (not rewards),
members fetched separately with cursor-based pagination, v2 identity
endpoint with explicit field selection, v2 OAuth scopes
- **Webhooks accept both formats**: payload version detected from
`data.type` (`"pledge"` → v1, `"member"` → v2), regardless of the site
setting
- **Admin deprecation notice**: `ProblemCheck::PatreonApiV1Deprecated`
warns on the dashboard when still using v1
- **Shared logic unchanged**: rate limiting, PluginStore, group sync,
admin UI all version-agnostic
- Also fixes pre-existing flaky seed/campaign spec failures

### Migration path for existing users

1. Admin sees deprecation warning on dashboard
2. Create a new v2 OAuth client at
https://www.patreon.com/portal/registration/register-clients
3. Update credentials in site settings
4. Switch `patreon_api_version` to `"2"`
5. Creator re-authenticates via Patreon login to get v2-scoped tokens
6. Trigger a manual data sync from the Patreon admin panel

### Future v1 removal

When ready to drop v1: delete `lib/api_version/v1.rb`, the problem
check, the site setting, v1 fixtures, and v1 test contexts — one clean
cut.

## Test plan

- [x] All 41 plugin specs pass (both v1 and v2 contexts)
- [x] Lint passes on all changed files
- [x] Zeitwerk reload passes (`bin/rails runner
"Rails.application.reloader.reload!"`)
- [x] Manual testing with a real Patreon v2 OAuth client
2026-03-30 14:43:34 -03:00

143 lines
4.3 KiB
Ruby
Vendored

# frozen_string_literal: true
describe "Patreon Oauth2" do
let(:access_token) { "patreon_access_token_448" }
let(:client_id) { "abcdef11223344" }
let(:client_secret) { "adddcccdddd99922" }
let(:temp_code) { "patreon_temp_code_544254" }
fab!(:user1, :user)
fab!(:user2, :user)
before do
SiteSetting.patreon_creator_discourse_username = user2.username
SiteSetting.patreon_login_enabled = true
SiteSetting.patreon_client_id = client_id
SiteSetting.patreon_client_secret = client_secret
end
shared_examples "patreon oauth" do
it "doesn't sign in the user if the email from patreon is not verified" do
post "/auth/patreon"
expect(response.status).to eq(302)
expect(response.location).to start_with("https://www.patreon.com/oauth2/authorize")
setup_patreon_stubs(email: user1.email, verified: false)
post "/auth/patreon/callback", params: { state: session["omniauth.state"], code: temp_code }
expect(response.status).to eq(302)
expect(response.location).to eq("http://test.localhost/")
expect(session[:current_user_id]).to be_blank
end
it "signs in the user if the email from patreon is verified" do
post "/auth/patreon"
expect(response.status).to eq(302)
expect(response.location).to start_with("https://www.patreon.com/oauth2/authorize")
setup_patreon_stubs(email: user1.email, verified: true)
post "/auth/patreon/callback", params: { state: session["omniauth.state"], code: temp_code }
expect(response.status).to eq(302)
expect(response.location).to eq("http://test.localhost/")
expect(session[:current_user_id]).to eq(user1.id)
end
end
context "with API v1" do
before do
SiteSetting.patreon_api_version = "1"
stub_request(:post, "https://api.patreon.com/oauth2/token").with(
body:
hash_including(
"client_id" => client_id,
"client_secret" => client_secret,
"code" => temp_code,
"grant_type" => "authorization_code",
"redirect_uri" => "http://test.localhost/auth/patreon/callback",
),
).to_return(
status: 200,
body: Rack::Utils.build_query(access_token: access_token),
headers: {
"Content-Type" => "application/x-www-form-urlencoded",
},
)
end
def setup_patreon_stubs(email:, verified:)
stub_request(:get, "https://api.patreon.com/oauth2/api/current_user").with(
headers: {
"Authorization" => "Bearer #{access_token}",
},
).to_return(
status: 200,
body:
JSON.dump(
data: {
id: "493290423324",
attributes: {
email: email,
full_name: "Patron",
is_email_verified: verified,
},
},
),
headers: {
"Content-Type" => "application/json",
},
)
end
include_examples "patreon oauth"
end
context "with API v2" do
before do
SiteSetting.patreon_api_version = "2"
stub_request(:post, "https://www.patreon.com/api/oauth2/token").with(
body:
hash_including(
"client_id" => client_id,
"client_secret" => client_secret,
"code" => temp_code,
"grant_type" => "authorization_code",
"redirect_uri" => "http://test.localhost/auth/patreon/callback",
),
).to_return(
status: 200,
body: Rack::Utils.build_query(access_token: access_token),
headers: {
"Content-Type" => "application/x-www-form-urlencoded",
},
)
end
def setup_patreon_stubs(email:, verified:)
stub_request(
:get,
"https://www.patreon.com/api/oauth2/v2/identity?fields%5Buser%5D=email,full_name,is_email_verified",
).with(headers: { "Authorization" => "Bearer #{access_token}" }).to_return(
status: 200,
body:
JSON.dump(
data: {
id: "493290423324",
attributes: {
email: email,
full_name: "Patron",
is_email_verified: verified,
},
},
),
headers: {
"Content-Type" => "application/json",
},
)
end
include_examples "patreon oauth"
end
end