44
55module Spree ::Api
66 describe 'Stores' , type : :request do
7+ let ( :country ) { create :country , states_required : true }
8+ let ( :country_without_states ) { create :country , states_required : false }
9+ let ( :state ) { create :state , name : 'maryland' , abbr : 'md' , country : }
10+ let! ( :base_attributes ) { Spree ::Api ::Config . store_attributes }
11+
712 let! ( :store ) do
813 create ( :store , name : "My Spree Store" , url : "spreestore.example.com" )
914 end
@@ -22,6 +27,58 @@ module Spree::Api
2227 default : false )
2328 end
2429
30+ describe "store state validation" do
31+ context "when store country has states_required" do
32+ it "is invalid without a state" do
33+ store = Spree ::Store . new ( name : "Test Store" , country : country , state : nil , url : "spreestore.example.com" ,
34+ mail_from_address : "spreestore@example.com" , code : "test-store" , )
35+ expect ( store ) . not_to be_valid
36+ expect ( store . errors [ :state ] ) . to include ( "can't be blank" )
37+ end
38+
39+ it "is valid with a state" do
40+ store = Spree ::Store . new ( name : "Test Store" , country : country , state : state , url : "spreestore.example.com" ,
41+ mail_from_address : "spreestore@example.com" , code : "test-store" , )
42+ expect ( store ) . to be_valid
43+ end
44+ end
45+
46+ context "when store country has no states" do
47+ it "is valid without a state" do
48+ store = Spree ::Store . new ( name : "Test Store" , country : country_without_states , state : nil , url : "spreestore.example.com" ,
49+ mail_from_address : "spreestore@example.com" , code : "test-store" , )
50+ expect ( store ) . to be_valid
51+ end
52+ end
53+
54+ it "is valid without an address and without country/state" do
55+ expect ( store ) . to be_valid
56+ end
57+
58+ it "is valid with only correct country and state" do
59+ store = Spree ::Store . create! (
60+ name : "Test Store" ,
61+ url : "spreestore.example.com" ,
62+ mail_from_address : "spreestore.example.com" ,
63+ code : "test-store" ,
64+ address1 : "123 Main St" ,
65+ city : "New York" ,
66+ zipcode : "10001" ,
67+ state : state ,
68+ country : country ,
69+ )
70+ expect ( store ) . to be_valid
71+ end
72+ end
73+
74+ describe "#index" do
75+ it "ensures the API store attributes match the expected attributes" do
76+ get spree . api_stores_path
77+ first_store = json_response [ "stores" ] . first
78+ expect ( first_store . keys ) . to include ( *base_attributes . map ( &:to_s ) )
79+ end
80+ end
81+
2582 it "can list the available stores" do
2683 get spree . api_stores_path
2784 expect ( json_response [ "stores" ] ) . to match_array ( [
@@ -37,7 +94,20 @@ module Spree::Api
3794 "default_currency" => nil ,
3895 "code" => store . code ,
3996 "default" => true ,
40- "available_locales" => [ "en" ]
97+ "available_locales" => [ "en" ] ,
98+ "legal_name" => nil ,
99+ "contact_email" => nil ,
100+ "contact_phone" => nil ,
101+ "description" => nil ,
102+ "tax_id" => nil ,
103+ "vat_id" => nil ,
104+ "address1" => nil ,
105+ "address2" => nil ,
106+ "city" => nil ,
107+ "zipcode" => nil ,
108+ "country_id" => nil ,
109+ "state_id" => nil ,
110+ "state_name" => nil
41111 } ,
42112 {
43113 "id" => non_default_store . id ,
@@ -51,7 +121,20 @@ module Spree::Api
51121 "default_currency" => nil ,
52122 "code" => non_default_store . code ,
53123 "default" => false ,
54- "available_locales" => [ "en" ]
124+ "available_locales" => [ "en" ] ,
125+ "legal_name" => nil ,
126+ "contact_email" => nil ,
127+ "contact_phone" => nil ,
128+ "description" => nil ,
129+ "tax_id" => nil ,
130+ "vat_id" => nil ,
131+ "address1" => nil ,
132+ "address2" => nil ,
133+ "city" => nil ,
134+ "zipcode" => nil ,
135+ "country_id" => nil ,
136+ "state_id" => nil ,
137+ "state_name" => nil
55138 }
56139 ] )
57140 end
@@ -70,7 +153,20 @@ module Spree::Api
70153 "default_currency" => nil ,
71154 "code" => store . code ,
72155 "default" => true ,
73- "available_locales" => [ "en" ]
156+ "available_locales" => [ "en" ] ,
157+ "legal_name" => nil ,
158+ "contact_email" => nil ,
159+ "contact_phone" => nil ,
160+ "description" => nil ,
161+ "tax_id" => nil ,
162+ "vat_id" => nil ,
163+ "address1" => nil ,
164+ "address2" => nil ,
165+ "city" => nil ,
166+ "zipcode" => nil ,
167+ "country_id" => nil ,
168+ "state_id" => nil ,
169+ "state_name" => nil
74170 )
75171 end
76172
@@ -79,7 +175,14 @@ module Spree::Api
79175 code : "spree123" ,
80176 name : "Hack0rz" ,
81177 url : "spree123.example.com" ,
82- mail_from_address : "me@example.com"
178+ mail_from_address : "me@example.com" ,
179+ legal_name : 'ABC Corp' ,
180+ address1 : "123 Main St" ,
181+ city : 'San Francisco' ,
182+ country_id : country . id ,
183+ state_id : state . id ,
184+ phone : "123-456-7890" ,
185+ zipcode : "12345"
83186 }
84187 post spree . api_stores_path , params : { store : store_hash }
85188 expect ( response . status ) . to eq ( 201 )
@@ -89,13 +192,34 @@ module Spree::Api
89192 store_hash = {
90193 url : "spree123.example.com" ,
91194 mail_from_address : "me@example.com" ,
92- bcc_email : "bcc@example.net"
195+ bcc_email : "bcc@example.net" ,
196+ legal_name : 'XYZ Corp' ,
197+ description : "Leading provider of high-quality tech accessories, offering the latest gadgets, peripherals, and electronics to enhance your digital lifestyle." ,
198+ tax_id : "TX-987654321" ,
199+ vat_id : "VAT-123456789" ,
200+ address1 : "123 Innovation Drive" ,
201+ address2 : "Suite 456" ,
202+ city : "New York" ,
203+ country_id : country . id ,
204+ state_id : state . id ,
205+ contact_phone : "123-456-7888" ,
206+ zipcode : "10001"
93207 }
94208 put spree . api_store_path ( store ) , params : { store : store_hash }
95209 expect ( response . status ) . to eq ( 200 )
96210 expect ( store . reload . url ) . to eql "spree123.example.com"
97211 expect ( store . reload . mail_from_address ) . to eql "me@example.com"
98212 expect ( store . reload . bcc_email ) . to eql "bcc@example.net"
213+ expect ( store . reload . legal_name ) . to eql "XYZ Corp"
214+ expect ( store . reload . tax_id ) . to eql "TX-987654321"
215+ expect ( store . reload . vat_id ) . to eql "VAT-123456789"
216+ expect ( store . reload . address1 ) . to eql "123 Innovation Drive"
217+ expect ( store . reload . address2 ) . to eql "Suite 456"
218+ expect ( store . reload . city ) . to eql "New York"
219+ expect ( store . reload . country_id ) . to eql country . id
220+ expect ( store . reload . state_id ) . to eql state . id
221+ expect ( store . reload . contact_phone ) . to eql "123-456-7888"
222+ expect ( store . reload . zipcode ) . to eql "10001"
99223 end
100224
101225 context "deleting a store" do
0 commit comments