# frozen_string_literal: true

require "rails_helper"

describe Doorkeeper::OpenidConnect::OAuth::Authorization::Code do
  subject { Doorkeeper::OAuth::Authorization::Code.new pre_auth, resource_owner }

  let(:resource_owner) { create :user }
  let(:access_grant) { create :access_grant }
  let(:pre_auth) { double }
  let(:client) { double }
  let(:openid_request_class) { Doorkeeper::OpenidConnect.configuration.open_id_request_model }

  describe "#issue_token" do
    before do
      allow(pre_auth).to receive(:client) { client }
      allow(pre_auth).to receive_messages(redirect_uri: "redirect_uri", scopes: "scopes", nonce: "123456", code_challenge: "987654", code_challenge_method: "plain", custom_access_token_attributes: {})
      allow(client).to receive(:id).and_return("client_id")

      allow(Doorkeeper::AccessGrant).to receive(:create!) { access_grant }
      allow(openid_request_class).to receive(:create!)
    end

    it "stores the nonce" do
      subject.issue_token

      expect(openid_request_class).to have_received(:create!).with({
        access_grant: access_grant,
        nonce: "123456",
      })
    end

    it "does not store the nonce if not present" do
      allow(pre_auth).to receive(:nonce).and_return(nil)
      subject.issue_token

      expect(openid_request_class).not_to have_received(:create!)
    end

    it "does not store the nonce if blank" do
      allow(pre_auth).to receive(:nonce).and_return(" ")
      subject.issue_token

      expect(openid_request_class).not_to have_received(:create!)
    end

    it "returns the created grant" do
      expect(subject.issue_token).to be_a Doorkeeper::AccessGrant
    end
  end
end
