Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixed customers orders tab in admin when using custom user class (#12750)

Damian Legawiecac1563b5f931
diff --git a/admin/app/views/spree/admin/orders/_filters.html.erb b/admin/app/views/spree/admin/orders/_filters.html.erb
index 8db8415e75..8613160cf5 100644
--- a/admin/app/views/spree/admin/orders/_filters.html.erb
+++ b/admin/app/views/spree/admin/orders/_filters.html.erb
@@ -1,7 +1,17 @@
<% frame_name ||= nil %>
+<% search_form_path = if @user.present?
+ spree.admin_user_orders_path(@user)
+ elsif @vendor.present? && spree.respond_to?(:admin_vendor_orders_path)
+ spree.admin_vendor_orders_path(@vendor)
+ elsif controller_name == 'checkouts'
+ spree.admin_checkouts_path
+ else
+ spree.admin_orders_path
+ end %>
+
<%= search_form_for [:admin, @vendor || @user, @search],
- url: controller_name == 'checkouts' ? spree.admin_checkouts_path : nil,
+ url: search_form_path,
class: "filter-wrap",
data: {
controller: "filters reveal",
diff --git a/admin/app/views/spree/admin/users/_form.html.erb b/admin/app/views/spree/admin/users/_form.html.erb
index ce2021c7d7..595a6fcd60 100644
--- a/admin/app/views/spree/admin/users/_form.html.erb
+++ b/admin/app/views/spree/admin/users/_form.html.erb
@@ -1,6 +1,6 @@
<div class="form-group">
<%= f.label :email, raw(Spree.t(:email) + required_span_tag) %>
- <%= f.email_field :email, class: 'form-control', required: true, autofocus: f.object.new_record?, required: true %>
+ <%= f.email_field :email, class: 'form-control', required: true, autofocus: f.object.new_record? %>
<%= f.error_message_on :email %>
</div>
<div class="form-group">
diff --git a/admin/spec/features/spree/admin/api_keys_spec.rb b/admin/spec/features/spree/admin/api_keys_spec.rb
new file mode 100644
index 0000000000..3032a0d314
--- /dev/null
+++ b/admin/spec/features/spree/admin/api_keys_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+
+describe 'API keys', type: :feature do
+ stub_authorization!
+
+ let!(:oauth_application) { create(:oauth_application, name: 'Test app') }
+
+ it 'renders a list of applications' do
+ visit '/admin/oauth_applications'
+
+ expect(page).to have_content('Test app')
+ end
+
+ it 'can create a new app', js: true do
+ visit '/admin/oauth_applications/new'
+
+ fill_in 'Name', with: 'My app'
+ click_button 'Create'
+
+ expect(page).to have_content('Client ID')
+ expect(page).to have_content('Client Secret')
+ expect(page).to have_content(Spree::OauthApplication.last.uid)
+ end
+
+ it 'can modify existing app' do
+ visit "/admin/oauth_applications/#{oauth_application.id}/edit"
+
+ fill_in 'Name', with: 'New name'
+ within('#page-header') { click_button 'Update' }
+
+ expect(page).to have_content('successfully updated!')
+ expect(page).to have_content('New name')
+ end
+end
diff --git a/admin/spec/features/spree/admin/customers_spec.rb b/admin/spec/features/spree/admin/customers_spec.rb
new file mode 100644
index 0000000000..64b1e34a28
--- /dev/null
+++ b/admin/spec/features/spree/admin/customers_spec.rb
@@ -0,0 +1,362 @@
+require 'spec_helper'
+
+RSpec.feature 'Customers', :js do
+ stub_authorization!
+
+ let!(:customer_user) { create(:user_with_addresses) }
+
+ context 'email, names and location' do
+ before { visit spree.admin_users_path }
+
+ it 'displays them' do
+ expect(page).to have_content(customer_user.first_name)
+ expect(page).to have_content(customer_user.last_name)
+ expect(page).to have_content(customer_user.billing_address.city)
+ expect(page).to have_content(customer_user.billing_address.state_name_text)
+ expect(page).to have_content(customer_user.email)
+ end
+ end
+
+ context 'no. of orders and amount spent' do
+ let!(:product) { create(:product_in_stock) }
+ let!(:order) { create(:order, total: 10, user: customer_user, completed_at: 5.minutes.ago) }
+ let!(:order2) { create(:order, total: 20, user: customer_user, completed_at: 5.minutes.ago) }
+ let!(:order3) { create(:order, total: 30, user: customer_user, completed_at: 5.minutes.ago) }
+
+ let!(:other_customer) { create(:user) }
+ let!(:other_customer_order) { create(:order, total: 100, user: other_customer, completed_at: 5.minutes.ago) }
+ let!(:other_customer_order2) { create(:order, total: 30, user: other_customer, completed_at: 5.minutes.ago) }
+
+ before do
+ visit spree.admin_users_path
+ end
+
+ it 'shows the correct number of orders and amount spend' do
+ expect(page).to have_content('60.0')
+ expect(page).to have_content('3')
+
+ expect(page).to have_content('130.0')
+ expect(page).to have_content('2')
+ end
+ end
+
+ context 'without any addresses' do
+ before do
+ customer_user.billing_address.destroy!
+ customer_user.shipping_address.destroy!
+
+ visit spree.admin_user_path(customer_user)
+ end
+
+ scenario 'Admin adds a shipping address' do
+ address_count = Spree::Address.count
+
+ within('#user-ship-address') do
+ click_on 'Add'
+
+ fill_in 'First Name', with: 'John'
+ fill_in 'Last Name', with: 'Doe'
+ fill_in 'Address', with: '4 Lovely Street'
+ fill_in 'City', with: 'Canton'
+ fill_in 'Zip Code', with: '06019'
+
+ click_button 'Create'
+ end
+
+ within('#user-ship-address') do
+ expect(page).to have_content('John Doe')
+ expect(page).to have_content('4 Lovely Street')
+ expect(page).to have_content('Canton')
+ expect(page).to have_content('06019')
+ end
+
+ expect(Spree::Address.count).to eq(address_count + 1)
+ address = Spree::Address.last
+
+ expect(customer_user.reload.ship_address_id).to eq(address.id)
+ expect(customer_user.ship_address.address1).to eq('4 Lovely Street')
+ expect(customer_user.ship_address.zipcode).to eq('06019')
+ expect(customer_user.ship_address.city).to eq('Canton')
+ end
+
+ scenario 'Admin adds a billing address' do
+ address_count = Spree::Address.count
+
+ within('#user-bill-address') do
+ click_on 'Add'
+
+ fill_in 'First Name', with: 'John'
+ fill_in 'Last Name', with: 'Doe'
+ fill_in 'Address', with: '4 Lovely Street'
+ fill_in 'City', with: 'Canton'
+ fill_in 'Zip Code', with: '06019'
+
+ click_button 'Create'
+ end
+
+ wait_for_turbo
+
+ expect(Spree::Address.count).to eq(address_count + 1)
+ address = Spree::Address.last
+
+ expect(customer_user.reload.bill_address_id).to eq(address.id)
+ expect(customer_user.bill_address.address1).to eq('4 Lovely Street')
+ expect(customer_user.bill_address.zipcode).to eq('06019')
+ expect(customer_user.bill_address.city).to eq('Canton')
+ end
+ end
+
+ describe 'update' do
+ let(:phone) { FFaker::PhoneNumber.phone_number }
+ let(:ship_address) { create(:address, user: customer_user) }
+ let(:bill_address) { create(:address, user: customer_user) }
+
+ before do
+ customer_user.update!(ship_address: ship_address, bill_address: bill_address)
+ visit spree.admin_user_path(customer_user)
+ end
+
+ it 'can update user' do
+ within '#user-details' do
+ click_on 'Edit'
+
+ fill_in 'Phone', with: phone
+
+ click_button 'Update'
+ end
+
+ expect(page).to have_content(phone)
+ expect(customer_user.reload.phone).to eq phone
+ end
+
+ it 'can update user ship address' do
+ within('#user-ship-address') do
+ click_on 'Edit'
+
+ fill_in 'First Name', with: 'John'
+ fill_in 'Last Name', with: 'Doe'
+
+ click_button 'Update'
+ end
+
+ expect(page).to have_content('John Doe')
+ expect(customer_user.reload.ship_address.first_name).to eq 'John'
+ expect(customer_user.reload.ship_address.last_name).to eq 'Doe'
+ end
+
+ it 'can update user bill address' do
+ within('#user-bill-address') do
+ click_on 'Edit'
+
+ scroll_to(find('#edit_address_billing'))
+
+ fill_in 'First Name', with: 'John'
+ fill_in 'Last Name', with: 'Doe'
+
+ click_button 'Update'
+ end
+
+ expect(page).to have_content('John Doe')
+ expect(customer_user.reload.bill_address.first_name).to eq 'John'
+ expect(customer_user.reload.bill_address.last_name).to eq 'Doe'
+ end
+
+ scenario 'Admin uses the shipping address' do
+ within('#user-bill-address') do
+ click_on 'Edit'
+ scroll_to(find('#edit_address_billing'))
+
+ find('label', text: 'Use Shipping Address').click
+ end
+
+ within('#user-bill-address') do
+ expect(page).to have_text('Same as shipping address')
+ end
+ end
+
+ context 'when bill address is the same' do
+ let(:bill_address) { ship_address }
+
+ scenario 'Admin adds a new billing address' do
+ within('#user-bill-address') do
+ expect(page).to have_text('Same as shipping address')
+
+ click_on 'Edit'
+ click_on 'Add new address'
+
+ fill_in 'First Name', with: 'John'
+ fill_in 'Last Name', with: 'Doe'
+ fill_in 'Address', with: '4 Lovely Street'
+ fill_in 'City', with: 'Canton'
+ fill_in 'Zip Code', with: '06019'
+
+ click_button 'Create'
+ end
+
+ within('#user-bill-address') do
+ expect(page).to have_content('John Doe')
+ expect(page).to have_content('4 Lovely Street')
+ expect(page).to have_content('Canton')
+ expect(page).to have_content('06019')
+ end
+
+ expect(customer_user.reload.bill_address_id).not_to eq(bill_address.id)
+ expect(customer_user.bill_address.address1).to eq('4 Lovely Street')
+ expect(customer_user.bill_address.zipcode).to eq('06019')
+ expect(customer_user.bill_address.city).to eq('Canton')
+ end
+
+ scenario 'Admin uses the shipping address' do
+ address_count = Spree::Address.count
+
+ within('#user-bill-address') do
+ expect(page).to have_text('Same as shipping address')
+
+ click_on 'Edit'
+ find('label', text: 'Use Shipping Address').click
+ end
+
+ within('#user-bill-address') do
+ expect(page).to have_text('Same as shipping address')
+ end
+
+ expect(Spree::Address.count).to eq(address_count)
+ expect(customer_user.reload.bill_address_id).to eq(bill_address.id)
+ end
+ end
+
+ context 'when user ship address is not editable' do
+ let!(:order) { create(:completed_order_with_totals, user: customer_user, ship_address: ship_address, bill_address: bill_address) }
+
+ before do
+ ship_address.update!(first_name: 'Jane', last_name: 'Poe')
+ order.reload
+ end
+
+ it 'creates a new one' do
+ within('#user-ship-address') do
+ click_on 'Edit'
+
+ fill_in 'First Name', with: 'Janette'
+ fill_in 'Last Name', with: 'Kovalsky'
+
+ click_button 'Update'
+ end
+
+ expect(page).to have_content('Janette Kovalsky')
+
+ wait_for_turbo
+
+ expect(customer_user.reload.ship_address_id).not_to eq(ship_address.id)
+ expect(customer_user.ship_address.first_name).to eq 'Janette'
+ expect(customer_user.ship_address.last_name).to eq 'Kovalsky'
+
+ expect(order.reload.ship_address_id).to eq(ship_address.id)
+ end
+
+ context 'when updating an existing ship address' do
+ let!(:existing_address) do
+ create(
+ :address,
+ user: customer_user,
+ first_name: 'Janette',
+ last_name: 'Kovalsky',
+ address1: '100 California Street',
+ address2: ship_address.address2,
+ city: ship_address.city,
+ zipcode: ship_address.zipcode,
+ state: ship_address.state,
+ country: ship_address.country,
+ phone: ship_address.phone
+ )
+ end
+
+ it 'switches the customer ship address to the existing address' do
+ within('#user-ship-address') do
+ click_on 'Edit'
+
+ fill_in 'First Name', with: 'Janette'
+ fill_in 'Last Name', with: 'Kovalsky'
+ fill_in 'Address', with: '100 California Street'
+
+ click_button 'Update'
+ end
+
+ expect(page).to have_content('100 California Street')
+
+ expect(customer_user.reload.ship_address_id).to eq(existing_address.id)
+ expect(order.reload.ship_address_id).to eq(ship_address.id)
+ end
+ end
+ end
+
+ context 'when user bill address is not editable' do
+ let!(:order) { create(:completed_order_with_totals, user: customer_user, ship_address: ship_address, bill_address: bill_address) }
+
+ before do
+ bill_address.update!(first_name: 'Jane', last_name: 'Poe')
+ order.reload
+ end
+
+ it 'creates a new one' do
+ within('#user-bill-address') do
+ click_on 'Edit'
+
+ scroll_to(find('#edit_address_billing'))
+
+ fill_in 'First Name', with: 'Gregory'
+ fill_in 'Last Name', with: 'Moe'
+
+ click_button 'Update'
+ end
+
+ expect(page).to have_content('Gregory Moe')
+
+ wait_for_turbo
+
+ expect(customer_user.reload.bill_address_id).not_to eq(bill_address.id)
+ expect(customer_user.bill_address.first_name).to eq('Gregory')
+ expect(customer_user.bill_address.last_name).to eq('Moe')
+
+ expect(order.reload.bill_address_id).to eq(bill_address.id)
+ end
+
+ context 'when updating an existing bill address' do
+ let!(:existing_address) do
+ create(
+ :address,
+ user: customer_user,
+ first_name: 'John',
+ last_name: 'Doe',
+ address1: '100 California Street',
+ address2: bill_address.address2,
+ city: bill_address.city,
+ zipcode: bill_address.zipcode,
+ state: bill_address.state,
+ country: bill_address.country,
+ phone: bill_address.phone
+ )
+ end
+
+ it 'switches the customer bill address to the existing address' do
+ within('#user-bill-address') do
+ click_on 'Edit'
+
+ scroll_to(find('#edit_address_billing'))
+
+ fill_in 'First Name', with: 'John'
+ fill_in 'Last Name', with: 'Doe'
+ fill_in 'Address', with: '100 California Street'
+
+ click_button 'Update'
+ end
+
+ expect(page).to have_content('100 California Street')
+
+ expect(customer_user.reload.bill_address_id).to eq(existing_address.id)
+ expect(order.reload.bill_address_id).to eq(bill_address.id)
+ end
+ end
+ end
+ end
+end
diff --git a/admin/spec/spec_helper.rb b/admin/spec/spec_helper.rb
index a85e078c54..4e1a2a76df 100644
--- a/admin/spec/spec_helper.rb
+++ b/admin/spec/spec_helper.rb
@@ -66,7 +66,7 @@ RSpec.configure do |config|
config.before(:suite) do
Capybara.match = :smart
- Capybara.javascript_driver = :selenium_chrome
+ Capybara.javascript_driver = :selenium_chrome_headless
Capybara.default_max_wait_time = 10
Capybara.raise_server_errors = false
# Clean out the database state before the tests run
diff --git a/core/lib/spree/testing_support/capybara_config.rb b/core/lib/spree/testing_support/capybara_config.rb
index 87cd073dcc..a43470768f 100644
--- a/core/lib/spree/testing_support/capybara_config.rb
+++ b/core/lib/spree/testing_support/capybara_config.rb
@@ -20,7 +20,7 @@ Capybara.register_driver :selenium_chrome_headless do |app|
options = ::Selenium::WebDriver::Chrome::Options.new
options.add_argument '--headless'
options.add_argument '--disable-gpu'
- options.add_argument '--window-size=1400,900'
+ options.add_argument '--window-size=1440,900'
options.add_argument '--disable-search-engine-choice-screen'
# Disable timers being throttled in background pages/tabs. Useful for parallel test runs.