<commit_before># frozen_string_literal: true

require "spec_helper"

describe "Resource Owner Password Credentials Flow not set up" do
  before do
    client_exists
    create_resource_owner
  end

  context "with valid user credentials" do
    it "does not issue new token" do
      expect do
        post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
      end.to_not(change { Doorkeeper::AccessToken.count })
    end
  end
end

describe "Resource Owner Password Credentials Flow" do
  let(:client_attributes) { { redirect_uri: nil } }

  before do
    config_is_set(:grant_flows, ["password"])
    config_is_set(:resource_owner_from_credentials) { User.authenticate! params[:username], params[:password] }
    client_exists(client_attributes)
    create_resource_owner
  end

  context "with valid user credentials" do
    context "with confidential client authorized using Basic auth" do
      it "should issue a new token" do
        expect do
          post password_token_endpoint_url(
            resource_owner: @resource_owner,
          ), headers: { "HTTP_AUTHORIZATION" => basic_auth_header_for_client(@client) }
        end.to(change { Doorkeeper::AccessToken.count })

        token = Doorkeeper::AccessToken.first

        expect(token.application_id).to eq @client.id
        should_have_json "access_token", token.token
      end
    end

    context "with non-confidential/public client" do
      let(:client_attributes) { { confidential: false } }

      context "when configured to check application supported grant flow" do
        before do
          Doorkeeper.configuration.instance_variable_set(
            :@allow_grant_flow_for_client,
            ->(_grant_flow, client) { client.name == "admin" },
          )
        end

        scenario "forbids the request when doesn't satisfy condition" do
          @client.update(name: "sample app")

          expect do
            post password_token_endpoint_url(
              client_id: @client.uid,
              client_secret: "foobar",
              resource_owner: @resource_owner,
            )
          end.not_to(change { Doorkeeper::AccessToken.count })

          expect(response.status).to eq(401)
          should_have_json "error", "invalid_client"
        end

        scenario "allows the request when satisfies condition" do<commit_msg>Add spec for password grant flow to ensure returns expires_in<commit_after># frozen_string_literal: true

require "spec_helper"

describe "Resource Owner Password Credentials Flow not set up" do
  before do
    client_exists
    create_resource_owner
  end

  context "with valid user credentials" do
    it "does not issue new token" do
      expect do
        post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
      end.to_not(change { Doorkeeper::AccessToken.count })
    end
  end
end

describe "Resource Owner Password Credentials Flow" do
  let(:client_attributes) { { redirect_uri: nil } }

  before do
    config_is_set(:grant_flows, ["password"])
    config_is_set(:resource_owner_from_credentials) { User.authenticate! params[:username], params[:password] }
    client_exists(client_attributes)
    create_resource_owner
  end

  context "with valid user credentials" do
    context "with confidential client authorized using Basic auth" do
      it "should issue a new token" do
        expect do
          post password_token_endpoint_url(
            resource_owner: @resource_owner,
          ), headers: { "HTTP_AUTHORIZATION" => basic_auth_header_for_client(@client) }
        end.to(change { Doorkeeper::AccessToken.count })

        token = Doorkeeper::AccessToken.first

        expect(token.application_id).to eq @client.id
        should_have_json "access_token", token.token
        expect(json_response).to include(
          "access_token", "expires_in", "token_type", "created_at",
        )
      end
    end

    context "with non-confidential/public client" do
      let(:client_attributes) { { confidential: false } }

      context "when configured to check application supported grant flow" do
        before do
          Doorkeeper.configuration.instance_variable_set(
            :@allow_grant_flow_for_client,
            ->(_grant_flow, client) { client.name == "admin" },
          )
        end

        scenario "forbids the request when doesn't satisfy condition" do
          @client.update(name: "sample app")

          expect do
            post password_token_endpoint_url(
              client_id: @client.uid,
              client_secret: "foobar",
              resource_owner: @resource_owner,
            )
          end.not_to(change { Doorkeeper::AccessToken.count })

          expect(response.status).to eq(401)
          should_have_json "error", "invalid_client"
        end

        scenario "allows the request when satisfies condition" do
{'fraction_non_alphanumeric': 0.0787321063394683, 'fraction_numerical': 0.001226993865030675, 'mean_word_length': 2.863349131121643, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2960544', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1306, 'n_words': 362}