diff --git a/api/lib/spree/api_configuration.rb b/api/lib/spree/api_configuration.rb index 87736c59c2..974a2c3a31 100644 --- a/api/lib/spree/api_configuration.rb +++ b/api/lib/spree/api_configuration.rb @@ -127,8 +127,8 @@ def promotion_attributes=(value) preference :store_attributes, :array, default: [ :id, :name, :legal_name, :url, :meta_description, :meta_keywords, :seo_title, :mail_from_address, :default_currency, :code, :default, - :bcc_email, :contact_phone, :contact_email, :tax_id, :vat_id, :description, - :address1, :address2, :city, :postal_code, :country_id, :state_id, :state_name, :available_locales + :bcc_email, :phone, :contact_email, :tax_id, :vat_id, :description, + :address1, :address2, :city, :zipcode, :country_id, :state_id, :state_name, :available_locales ] preference :store_credit_history_attributes, :array, default: [ diff --git a/api/spec/requests/spree/api/stores_spec.rb b/api/spec/requests/spree/api/stores_spec.rb index da2d2924e6..3d87a17f3e 100644 --- a/api/spec/requests/spree/api/stores_spec.rb +++ b/api/spec/requests/spree/api/stores_spec.rb @@ -4,6 +4,10 @@ module Spree::Api describe 'Stores', type: :request do + let(:country) { create :country, states_required: true } + let(:state) { create :state, name: 'maryland', abbr: 'md', country: } + let!(:base_attributes) { Spree::Api::Config.store_attributes } + let!(:store) do create(:store, name: "My Spree Store", url: "spreestore.example.com") end @@ -22,6 +26,60 @@ module Spree::Api default: false) end + let(:country_with_states) { create(:country, states: [create(:state)]) } + let(:country_without_states) { create(:country) } + + describe "Validations" do + context "when the country has states" do + it "is invalid without a state" do + store = Spree::Store.new(name: "Test Store", country: country_with_states, state: nil, url: "spreestore.example.com", + mail_from_address: "spreestore.example.com", code: "test-store",) + expect(store).not_to be_valid + expect(store.errors[:state]).to include("can't be blank") + end + + it "is valid with a state" do + store = Spree::Store.new(name: "Test Store", country: country_with_states, state: state, url: "spreestore.example.com", + mail_from_address: "spreestore.example.com", code: "test-store",) + expect(store).to be_valid + end + end + + context "when the country has no states" do + it "is valid without a state" do + store = Spree::Store.new(name: "Test Store", country: country_without_states, state: nil, url: "spreestore.example.com", + mail_from_address: "spreestore.example.com", code: "test-store",) + expect(store).to be_valid + end + end + + it "is valid without an address and without country/state" do + expect(store).to be_valid + end + + it "is valid with only correct country and state" do + store = Spree::Store.create!( + name: "Test Store", + url: "spreestore.example.com", + mail_from_address: "spreestore.example.com", + code: "test-store", + address1: "123 Main St", + city: "New York", + zipcode: "10001", + country: country, + ) + expect(store).to be_valid + end + end + + describe "#index" do + it "ensures the API store attributes match the expected attributes" do + get spree.api_stores_path + first_store = json_response["stores"].first + expect(first_store.keys).to include(*base_attributes.map(&:to_s)) + end + end + it "can list the available stores" do get spree.api_stores_path expect(json_response["stores"]).to match_array([ @@ -39,15 +97,15 @@ module Spree::Api "default" => true, "available_locales" => ["en"], "legal_name" => nil, - "contact_phone" => nil, "contact_email" => nil, "description" => nil, + "phone" => nil, "tax_id" => nil, "vat_id" => nil, "address1" => nil, "address2" => nil, "city" => nil, - "postal_code" => nil, + "zipcode" => nil, "country_id" => nil, "state_id" => nil, "state_name" => nil @@ -66,15 +124,15 @@ module Spree::Api "default" => false, "available_locales" => ["en"], "legal_name" => nil, - "contact_phone" => nil, "contact_email" => nil, "description" => nil, + "phone" => nil, "tax_id" => nil, "vat_id" => nil, "address1" => nil, "address2" => nil, "city" => nil, - "postal_code" => nil, + "zipcode" => nil, "country_id" => nil, "state_id" => nil, "state_name" => nil @@ -98,15 +156,15 @@ module Spree::Api "default" => true, "available_locales" => ["en"], "legal_name" => nil, - "contact_phone" => nil, "contact_email" => nil, "description" => nil, + "phone" => nil, "tax_id" => nil, "vat_id" => nil, "address1" => nil, "address2" => nil, "city" => nil, - "postal_code" => nil, + "zipcode" => nil, "country_id" => nil, "state_id" => nil, "state_name" => nil @@ -118,7 +176,14 @@ module Spree::Api code: "spree123", name: "Hack0rz", url: "spree123.example.com", - mail_from_address: "me@example.com" + mail_from_address: "me@example.com", + legal_name: 'ABC Corp', + address1: "123 Main St", + city: 'San Francisco', + country_id: country.id, + state_id: state.id, + phone: "123-456-7890", + zipcode: "12345" } post spree.api_stores_path, params: { store: store_hash } expect(response.status).to eq(201) @@ -128,13 +193,34 @@ module Spree::Api store_hash = { url: "spree123.example.com", mail_from_address: "me@example.com", - bcc_email: "bcc@example.net" + bcc_email: "bcc@example.net", + legal_name: 'XYZ Corp', + description: "Leading provider of high-quality tech accessories, offering the latest gadgets, peripherals, and electronics to enhance your digital lifestyle.", + tax_id: "TX-987654321", + vat_id: "VAT-123456789", + address1: "123 Innovation Drive", + address2: "Suite 456", + city: "New York", + country_id: country.id, + state_id: state.id, + phone: "123-456-7888", + zipcode: "10001" } put spree.api_store_path(store), params: { store: store_hash } expect(response.status).to eq(200) expect(store.reload.url).to eql "spree123.example.com" expect(store.reload.mail_from_address).to eql "me@example.com" expect(store.reload.bcc_email).to eql "bcc@example.net" + expect(store.reload.legal_name).to eql "XYZ Corp" + expect(store.reload.tax_id).to eql "TX-987654321" + expect(store.reload.vat_id).to eql "VAT-123456789" + expect(store.reload.address1).to eql "123 Innovation Drive" + expect(store.reload.address2).to eql "Suite 456" + expect(store.reload.city).to eql "New York" + expect(store.reload.country_id).to eql country.id + expect(store.reload.state_id).to eql state.id + expect(store.reload.phone).to eql "123-456-7888" + expect(store.reload.zipcode).to eql "10001" end context "deleting a store" do diff --git a/backend/app/views/spree/admin/stores/_address_form.html.erb b/backend/app/views/spree/admin/stores/_address_form.html.erb new file mode 100644 index 0000000000..99bff9af55 --- /dev/null +++ b/backend/app/views/spree/admin/stores/_address_form.html.erb @@ -0,0 +1,65 @@ + +<% s_or_b = type.chars.first %> +