4
4
5
5
module Spree ::Api
6
6
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
+
7
11
let! ( :store ) do
8
12
create ( :store , name : "My Spree Store" , url : "spreestore.example.com" )
9
13
end
@@ -22,6 +26,60 @@ module Spree::Api
22
26
default : false )
23
27
end
24
28
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
+
25
83
it "can list the available stores" do
26
84
get spree . api_stores_path
27
85
expect ( json_response [ "stores" ] ) . to match_array ( [
@@ -39,15 +97,15 @@ module Spree::Api
39
97
"default" => true ,
40
98
"available_locales" => [ "en" ] ,
41
99
"legal_name" => nil ,
42
- "contact_phone" => nil ,
43
100
"contact_email" => nil ,
44
101
"description" => nil ,
102
+ "phone" => nil ,
45
103
"tax_id" => nil ,
46
104
"vat_id" => nil ,
47
105
"address1" => nil ,
48
106
"address2" => nil ,
49
107
"city" => nil ,
50
- "postal_code " => nil ,
108
+ "zipcode " => nil ,
51
109
"country_id" => nil ,
52
110
"state_id" => nil ,
53
111
"state_name" => nil
@@ -66,15 +124,15 @@ module Spree::Api
66
124
"default" => false ,
67
125
"available_locales" => [ "en" ] ,
68
126
"legal_name" => nil ,
69
- "contact_phone" => nil ,
70
127
"contact_email" => nil ,
71
128
"description" => nil ,
129
+ "phone" => nil ,
72
130
"tax_id" => nil ,
73
131
"vat_id" => nil ,
74
132
"address1" => nil ,
75
133
"address2" => nil ,
76
134
"city" => nil ,
77
- "postal_code " => nil ,
135
+ "zipcode " => nil ,
78
136
"country_id" => nil ,
79
137
"state_id" => nil ,
80
138
"state_name" => nil
@@ -98,15 +156,15 @@ module Spree::Api
98
156
"default" => true ,
99
157
"available_locales" => [ "en" ] ,
100
158
"legal_name" => nil ,
101
- "contact_phone" => nil ,
102
159
"contact_email" => nil ,
103
160
"description" => nil ,
161
+ "phone" => nil ,
104
162
"tax_id" => nil ,
105
163
"vat_id" => nil ,
106
164
"address1" => nil ,
107
165
"address2" => nil ,
108
166
"city" => nil ,
109
- "postal_code " => nil ,
167
+ "zipcode " => nil ,
110
168
"country_id" => nil ,
111
169
"state_id" => nil ,
112
170
"state_name" => nil
@@ -118,7 +176,14 @@ module Spree::Api
118
176
code : "spree123" ,
119
177
name : "Hack0rz" ,
120
178
url : "spree123.example.com" ,
121
- mail_from_address :
"[email protected] "
179
+ mail_from_address :
"[email protected] " ,
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"
122
187
}
123
188
post spree . api_stores_path , params : { store : store_hash }
124
189
expect ( response . status ) . to eq ( 201 )
@@ -128,13 +193,34 @@ module Spree::Api
128
193
store_hash = {
129
194
url : "spree123.example.com" ,
130
195
mail_from_address :
"[email protected] " ,
131
-
196
+
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"
132
208
}
133
209
put spree . api_store_path ( store ) , params : { store : store_hash }
134
210
expect ( response . status ) . to eq ( 200 )
135
211
expect ( store . reload . url ) . to eql "spree123.example.com"
136
212
expect ( store . reload . mail_from_address ) . to eql "[email protected] "
137
213
expect ( store . reload . bcc_email ) . to eql "[email protected] "
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"
138
224
end
139
225
140
226
context "deleting a store" do
0 commit comments