mirror of
https://github.com/discourse/discourse.git
synced 2026-08-06 13:08:40 +08:00
RSpec setup becomes harder to follow at either extreme: trivial fixture wrappers hide lifecycle and intent, while forcing every named operation inline repeats low-level protocol and configuration details. This change documents and applies a test-setup hierarchy: - use `fab!`, `let`, `let!`, `subject`, and inline `Fabricate` according to lifecycle and role; - use a small example-group method when parameterized behavior gives one spec useful vocabulary; - move helpers into auto-loaded `spec/support` only when they are shared across spec files; - use fabricators and page objects for the data shapes and system-test interfaces they own. Core and plugin support files are loaded centrally by `rails_helper`, so plugin-specific support loaders are unnecessary. The migration specs encountered during the sweep are removed according to repository policy; production migrations are unchanged.
142 lines
4.2 KiB
Ruby
Vendored
142 lines
4.2 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")
|
|
|
|
stub_request(:get, identity_url).with(
|
|
headers: {
|
|
"Authorization" => "Bearer #{access_token}",
|
|
},
|
|
).to_return(
|
|
status: 200,
|
|
body:
|
|
JSON.dump(
|
|
data: {
|
|
id: "493290423324",
|
|
attributes: {
|
|
email: user1.email,
|
|
full_name: "Patron",
|
|
is_email_verified: false,
|
|
},
|
|
},
|
|
),
|
|
headers: {
|
|
"Content-Type" => "application/json",
|
|
},
|
|
)
|
|
|
|
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")
|
|
|
|
stub_request(:get, identity_url).with(
|
|
headers: {
|
|
"Authorization" => "Bearer #{access_token}",
|
|
},
|
|
).to_return(
|
|
status: 200,
|
|
body:
|
|
JSON.dump(
|
|
data: {
|
|
id: "493290423324",
|
|
attributes: {
|
|
email: user1.email,
|
|
full_name: "Patron",
|
|
is_email_verified: true,
|
|
},
|
|
},
|
|
),
|
|
headers: {
|
|
"Content-Type" => "application/json",
|
|
},
|
|
)
|
|
|
|
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
|
|
let(:identity_url) { "https://api.patreon.com/oauth2/api/current_user" }
|
|
|
|
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
|
|
|
|
include_examples "patreon oauth"
|
|
end
|
|
|
|
context "with API v2" do
|
|
let(:identity_url) do
|
|
"https://www.patreon.com/api/oauth2/v2/identity?fields%5Buser%5D=email,full_name,is_email_verified"
|
|
end
|
|
|
|
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
|
|
|
|
include_examples "patreon oauth"
|
|
end
|
|
end
|