Skip to content

A platform for community discussion. Free, open, simple.

5.5 KB
Raw Blame
# 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_client_id = client_id
    SiteSetting.patreon_client_secret = client_secret
    SiteSetting.patreon_login_enabled = true
  end

  shared_examples "patreon oauth" do
    [false, nil].each do |verified|
      it "does not sign in a synced patron when email verification is #{verified.inspect}" do
        group = Fabricate(:group)
        Patreon.set("filters", group.id.to_s => ["0"])
        Patreon::Pledge.save!(
          [
            {
              "data" => [
                {
                  "type" => "member",
                  "attributes" => {
                    "email" => user1.email,
                    "patron_status" => "active_patron",
                    "currently_entitled_amount_cents" => 100,
                  },
                  "relationships" => {
                    "user" => {
                      "data" => {
                        "id" => "493290423324",
                      },
                    },
                  },
                },
              ],
            },
          ],
          adapter: Patreon::ApiVersion::V2,
        )
        Patreon::Patron.sync_groups

        expect(group.users).to contain_exactly(user1)
        expect(user1.reload.custom_fields["patreon_id"]).to eq("493290423324")
        expect(UserAssociatedAccount.where(user_id: user1.id, provider_name: "patreon")).to be_empty

        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: verified,
                }.compact,
              },
            ),
          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
        expect(UserAssociatedAccount.where(user_id: user1.id, provider_name: "patreon")).to be_empty
      end
    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