44
55module Spree ::Api
66 describe 'Stores' , type : :request do
7+ let ( :country ) { create :country , states_required : true }
8+ let ( :state ) { create :state , name : 'maryland' , abbr : 'md' , country : }
9+ let! ( :base_attributes ) { Spree ::Api ::Config . store_attributes }
10+
711 let! ( :store ) do
812 create ( :store , name : "My Spree Store" , url : "spreestore.example.com" )
913 end
@@ -22,6 +26,60 @@ module Spree::Api
2226 default : false )
2327 end
2428
29+ let ( :country_with_states ) { create ( :country , states : [ create ( :state ) ] ) }
30+ let ( :country_without_states ) { create ( :country ) }
31+
32+ describe "Validations" do
33+ context "when the country has states" do
34+ it "is invalid without a state" do
35+ store = Spree ::Store . new ( name : "Test Store" , country : country_with_states , state : nil , url : "spreestore.example.com" ,
36+ mail_from_address : "spreestore.example.com" , code : "test-store" , )
37+ expect ( store ) . not_to be_valid
38+ expect ( store . errors [ :state ] ) . to include ( "can't be blank" )
39+ end
40+
41+ it "is valid with a state" do
42+ store = Spree ::Store . new ( name : "Test Store" , country : country_with_states , state : state , url : "spreestore.example.com" ,
43+ mail_from_address : "spreestore.example.com" , code : "test-store" , )
44+ expect ( store ) . to be_valid
45+ end
46+ end
47+
48+ context "when the country has no states" do
49+ it "is valid without a state" do
50+ store = Spree ::Store . new ( name : "Test Store" , country : country_without_states , state : nil , url : "spreestore.example.com" ,
51+ mail_from_address : "spreestore.example.com" , code : "test-store" , )
52+ expect ( store ) . to be_valid
53+ end
54+ end
55+
56+ it "is valid without an address and without country/state" do
57+ expect ( store ) . to be_valid
58+ end
59+
60+ it "is valid with only correct country and state" do
61+ store = Spree ::Store . create! (
62+ name : "Test Store" ,
63+ url : "spreestore.example.com" ,
64+ mail_from_address : "spreestore.example.com" ,
65+ code : "test-store" ,
66+ address1 : "123 Main St" ,
67+ city : "New York" ,
68+ zipcode : "10001" ,
69+ country : country ,
70+ )
71+ expect ( store ) . to be_valid
72+ end
73+ end
74+
75+ describe "#index" do
76+ it "ensures the API store attributes match the expected attributes" do
77+ get spree . api_stores_path
78+ first_store = json_response [ "stores" ] . first
79+ expect ( first_store . keys ) . to include ( *base_attributes . map ( &:to_s ) )
80+ end
81+ end
82+
2583 it "can list the available stores" do
2684 get spree . api_stores_path
2785 expect ( json_response [ "stores" ] ) . to match_array ( [
@@ -39,15 +97,15 @@ module Spree::Api
3997 "default" => true ,
4098 "available_locales" => [ "en" ] ,
4199 "legal_name" => nil ,
42- "contact_phone" => nil ,
43100 "contact_email" => nil ,
44101 "description" => nil ,
102+ "phone" => nil ,
45103 "tax_id" => nil ,
46104 "vat_id" => nil ,
47105 "address1" => nil ,
48106 "address2" => nil ,
49107 "city" => nil ,
50- "postal_code " => nil ,
108+ "zipcode " => nil ,
51109 "country_id" => nil ,
52110 "state_id" => nil ,
53111 "state_name" => nil
@@ -66,15 +124,15 @@ module Spree::Api
66124 "default" => false ,
67125 "available_locales" => [ "en" ] ,
68126 "legal_name" => nil ,
69- "contact_phone" => nil ,
70127 "contact_email" => nil ,
71128 "description" => nil ,
129+ "phone" => nil ,
72130 "tax_id" => nil ,
73131 "vat_id" => nil ,
74132 "address1" => nil ,
75133 "address2" => nil ,
76134 "city" => nil ,
77- "postal_code " => nil ,
135+ "zipcode " => nil ,
78136 "country_id" => nil ,
79137 "state_id" => nil ,
80138 "state_name" => nil
@@ -98,15 +156,15 @@ module Spree::Api
98156 "default" => true ,
99157 "available_locales" => [ "en" ] ,
100158 "legal_name" => nil ,
101- "contact_phone" => nil ,
102159 "contact_email" => nil ,
103160 "description" => nil ,
161+ "phone" => nil ,
104162 "tax_id" => nil ,
105163 "vat_id" => nil ,
106164 "address1" => nil ,
107165 "address2" => nil ,
108166 "city" => nil ,
109- "postal_code " => nil ,
167+ "zipcode " => nil ,
110168 "country_id" => nil ,
111169 "state_id" => nil ,
112170 "state_name" => nil
@@ -118,7 +176,14 @@ module Spree::Api
118176 code : "spree123" ,
119177 name : "Hack0rz" ,
120178 url : "spree123.example.com" ,
121- mail_from_address : "me@example.com"
179+ mail_from_address : "me@example.com" ,
180+ legal_name : 'ABC Corp' ,
181+ address1 : "123 Main St" ,
182+ city : 'San Francisco' ,
183+ country_id : country . id ,
184+ state_id : state . id ,
185+ phone : "123-456-7890" ,
186+ zipcode : "12345"
122187 }
123188 post spree . api_stores_path , params : { store : store_hash }
124189 expect ( response . status ) . to eq ( 201 )
@@ -128,13 +193,34 @@ module Spree::Api
128193 store_hash = {
129194 url : "spree123.example.com" ,
130195 mail_from_address : "me@example.com" ,
131- bcc_email : "bcc@example.net"
196+ bcc_email : "bcc@example.net" ,
197+ legal_name : 'XYZ Corp' ,
198+ description : "Leading provider of high-quality tech accessories, offering the latest gadgets, peripherals, and electronics to enhance your digital lifestyle." ,
199+ tax_id : "TX-987654321" ,
200+ vat_id : "VAT-123456789" ,
201+ address1 : "123 Innovation Drive" ,
202+ address2 : "Suite 456" ,
203+ city : "New York" ,
204+ country_id : country . id ,
205+ state_id : state . id ,
206+ phone : "123-456-7888" ,
207+ zipcode : "10001"
132208 }
133209 put spree . api_store_path ( store ) , params : { store : store_hash }
134210 expect ( response . status ) . to eq ( 200 )
135211 expect ( store . reload . url ) . to eql "spree123.example.com"
136212 expect ( store . reload . mail_from_address ) . to eql "me@example.com"
137213 expect ( store . reload . bcc_email ) . to eql "bcc@example.net"
214+ expect ( store . reload . legal_name ) . to eql "XYZ Corp"
215+ expect ( store . reload . tax_id ) . to eql "TX-987654321"
216+ expect ( store . reload . vat_id ) . to eql "VAT-123456789"
217+ expect ( store . reload . address1 ) . to eql "123 Innovation Drive"
218+ expect ( store . reload . address2 ) . to eql "Suite 456"
219+ expect ( store . reload . city ) . to eql "New York"
220+ expect ( store . reload . country_id ) . to eql country . id
221+ expect ( store . reload . state_id ) . to eql state . id
222+ expect ( store . reload . phone ) . to eql "123-456-7888"
223+ expect ( store . reload . zipcode ) . to eql "10001"
138224 end
139225
140226 context "deleting a store" do
0 commit comments