<commit_before>require 'spec_helper'

module Spree
  module Admin
    describe PaymentsController, type: :controller do
      before do
        allow(controller).to receive_messages spree_current_user: user
      end

      let(:user) { create(:admin_user) }
      let(:order) { create(:order) }

      describe '#create' do
        context "with a valid credit card" do
          let(:order) { create(:order_with_line_items, state: "payment") }
          let(:payment_method) { create(:credit_card_payment_method, available_to_admin: true) }
          let(:attributes) do
            {
              order_id: order.number,
              card: "new",
              payment: {
                amount: order.total,
                payment_method_id: payment_method.id.to_s,
                source_attributes: {
                  name: "<NAME>",
                  number: "4111 1111 1111 1111",
                  expiry: "09 / #{Time.current.year + 1}",
                verification_value: "123"
                }
              }
            }
          end

          before do
            post :create, params: attributes
          end

          it "should process payment correctly" do
            expect(order.payments.count).to eq(1)
            expect(order.payments.last.state).to eq 'checkout'
            expect(response).to redirect_to(spree.admin_order_payments_path(order))
            expect(order.reload.state).to eq('confirm')
          end

          context 'with credit card address fields' do
            let(:address) { build(:address) }

            let(:attributes) do
              attrs = super()
              attrs[:payment][:source_attributes][:address_attributes] = address_attributes
              attrs
            end

            let(:address_attributes) do
              {
                'firstname' => address.firstname,
                'address1' => address.address1,
                'city' => address.city,
                'country_id' => address.country_id,
                'state_id' => address.state_id,
                'zipcode' => address.zipcode,
                'phone' => address.phone
              }
            end

            it 'associates the address' do
              expect(order.payments.count).to eq(1)
              credit_card = order.payments.last.source
              expect(credit_card.address.attributes).to include(address_attributes)
            end
          end
        end
      end

      describe '#new' do
        # Regression test for https://github.com/spree/spree/issues/3233
        context "with a backend payment method" do
          before do
            @payment_method = create(:check_payment_method, available_to_admin: true)
          end

          it "loads backend payment methods" do
            get :new, params: { order_id: order.number }
            expect(response.status).to eq(200)
            expect(assigns[:payment_methods]).to include(@payment_method)
          end
        end
      end

      describe '#index' do
        context "order has billing address" do
          before do
            order.bill_address = create(:address)<commit_msg>Add failing spec for admin payment methods<commit_after>require 'spec_helper'

module Spree
  module Admin
    describe PaymentsController, type: :controller do
      before do
        allow(controller).to receive_messages spree_current_user: user
      end

      let(:user) { create(:admin_user) }
      let(:order) { create(:order) }

      describe '#create' do
        context "with a valid credit card" do
          let(:order) { create(:order_with_line_items, state: "payment") }
          let(:payment_method) { create(:credit_card_payment_method, available_to_admin: true) }
          let(:attributes) do
            {
              order_id: order.number,
              card: "new",
              payment: {
                amount: order.total,
                payment_method_id: payment_method.id.to_s,
                source_attributes: {
                  name: "Test User",
                  number: "4111 1111 1111 1111",
                  expiry: "09 / #{Time.current.year + 1}",
                verification_value: "123"
                }
              }
            }
          end

          before do
            post :create, params: attributes
          end

          it "should process payment correctly" do
            expect(order.payments.count).to eq(1)
            expect(order.payments.last.state).to eq 'checkout'
            expect(response).to redirect_to(spree.admin_order_payments_path(order))
            expect(order.reload.state).to eq('confirm')
          end

          context 'with credit card address fields' do
            let(:address) { build(:address) }

            let(:attributes) do
              attrs = super()
              attrs[:payment][:source_attributes][:address_attributes] = address_attributes
              attrs
            end

            let(:address_attributes) do
              {
                'firstname' => address.firstname,
                'address1' => address.address1,
                'city' => address.city,
                'country_id' => address.country_id,
                'state_id' => address.state_id,
                'zipcode' => address.zipcode,
                'phone' => address.phone
              }
            end

            it 'associates the address' do
              expect(order.payments.count).to eq(1)
              credit_card = order.payments.last.source
              expect(credit_card.address.attributes).to include(address_attributes)
            end
          end
        end
      end

      describe '#new' do
        # Regression test for https://github.com/spree/spree/issues/3233
        context "with a backend payment method" do
          context "and the payment method is active" do
            before do
              @payment_method = create(:check_payment_method, available_to_admin: true)
            end

            it "loads the payment method" do
              get :new, params: { order_id: order.number }
              expect(response.status).to eq(200)
              expect(assigns[:payment_methods]).to include(@payment_method)
            end
          end

          context "and the payment method is inactive" do
            before do
              @payment_method = create(:check_payment_method, available_to_admin: true, active: false)
            end

            it "does not load the payment method", :pending do
              get :new, params: { order_id: order.number }
              expect(response.status).to eq(200)
              expect(assigns[:payment_methods]).to be_empty
            end
          end
        end
      end

      describe '#index' do
        context "order has billing address" do
          before do
            order.bill_address = create(:address)
{'fraction_non_alphanumeric': 0.08903809662822945, 'fraction_numerical': 0.01007152240548825, 'mean_word_length': 1.6283084004602992, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 18, 'https://': 2, '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': '5106715', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1714, 'n_words': 432}