dup_spree_api 1.3.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +5 -0
- data/LICENSE +22 -0
- data/Rakefile +30 -0
- data/app/controllers/spree/api/addresses_controller.rb +20 -0
- data/app/controllers/spree/api/base_controller.rb +114 -0
- data/app/controllers/spree/api/countries_controller.rb +18 -0
- data/app/controllers/spree/api/images_controller.rb +32 -0
- data/app/controllers/spree/api/line_items_controller.rb +40 -0
- data/app/controllers/spree/api/orders_controller.rb +85 -0
- data/app/controllers/spree/api/payments_controller.rb +80 -0
- data/app/controllers/spree/api/product_properties_controller.rb +65 -0
- data/app/controllers/spree/api/products_controller.rb +49 -0
- data/app/controllers/spree/api/return_authorizations_controller.rb +54 -0
- data/app/controllers/spree/api/shipments_controller.rb +43 -0
- data/app/controllers/spree/api/taxonomies_controller.rb +50 -0
- data/app/controllers/spree/api/taxons_controller.rb +53 -0
- data/app/controllers/spree/api/variants_controller.rb +75 -0
- data/app/controllers/spree/api/zones_controller.rb +46 -0
- data/app/helpers/spree/api/api_helpers.rb +76 -0
- data/app/models/spree/api_configuration.rb +5 -0
- data/app/models/spree/line_item_decorator.rb +3 -0
- data/app/models/spree/option_value_decorator.rb +5 -0
- data/app/models/spree/order_decorator.rb +13 -0
- data/app/models/spree/user_decorator.rb +13 -0
- data/app/overrides/api_admin_user_edit_form.rb +6 -0
- data/app/views/spree/admin/users/_api_fields.html.erb +32 -0
- data/app/views/spree/api/addresses/show.v1.rabl +11 -0
- data/app/views/spree/api/countries/index.v1.rabl +7 -0
- data/app/views/spree/api/countries/show.v1.rabl +5 -0
- data/app/views/spree/api/errors/gateway_error.v1.rabl +2 -0
- data/app/views/spree/api/errors/invalid_api_key.v1.rabl +2 -0
- data/app/views/spree/api/errors/invalid_resource.v1.rabl +3 -0
- data/app/views/spree/api/errors/must_specify_api_key.v1.rabl +2 -0
- data/app/views/spree/api/errors/not_found.v1.rabl +2 -0
- data/app/views/spree/api/errors/unauthorized.v1.rabl +2 -0
- data/app/views/spree/api/images/show.v1.rabl +3 -0
- data/app/views/spree/api/line_items/new.v1.rabl +3 -0
- data/app/views/spree/api/line_items/show.v1.rabl +5 -0
- data/app/views/spree/api/orders/address.v1.rabl +0 -0
- data/app/views/spree/api/orders/canceled.v1.rabl +0 -0
- data/app/views/spree/api/orders/cart.v1.rabl +0 -0
- data/app/views/spree/api/orders/complete.v1.rabl +0 -0
- data/app/views/spree/api/orders/could_not_transition.v1.rabl +3 -0
- data/app/views/spree/api/orders/delivery.v1.rabl +3 -0
- data/app/views/spree/api/orders/index.v1.rabl +7 -0
- data/app/views/spree/api/orders/invalid_shipping_method.v1.rabl +2 -0
- data/app/views/spree/api/orders/payment.v1.rabl +4 -0
- data/app/views/spree/api/orders/show.v1.rabl +29 -0
- data/app/views/spree/api/payments/credit_over_limit.v1.rabl +2 -0
- data/app/views/spree/api/payments/index.v1.rabl +7 -0
- data/app/views/spree/api/payments/new.v1.rabl +6 -0
- data/app/views/spree/api/payments/show.v1.rabl +2 -0
- data/app/views/spree/api/product_properties/index.v1.rabl +7 -0
- data/app/views/spree/api/product_properties/new.v1.rabl +2 -0
- data/app/views/spree/api/product_properties/show.v1.rabl +2 -0
- data/app/views/spree/api/products/index.v1.rabl +8 -0
- data/app/views/spree/api/products/new.v1.rabl +3 -0
- data/app/views/spree/api/products/product.v1.rabl +1 -0
- data/app/views/spree/api/products/show.v1.rabl +25 -0
- data/app/views/spree/api/return_authorizations/index.v1.rabl +7 -0
- data/app/views/spree/api/return_authorizations/new.v1.rabl +3 -0
- data/app/views/spree/api/return_authorizations/show.v1.rabl +2 -0
- data/app/views/spree/api/shipments/cannot_ready_shipment.v1.rabl +2 -0
- data/app/views/spree/api/shipments/show.v1.rabl +7 -0
- data/app/views/spree/api/taxonomies/index.v1.rabl +7 -0
- data/app/views/spree/api/taxonomies/nested.v1.rabl +11 -0
- data/app/views/spree/api/taxonomies/new.v1.rabl +3 -0
- data/app/views/spree/api/taxonomies/show.v1.rabl +15 -0
- data/app/views/spree/api/taxons/index.v1.rabl +4 -0
- data/app/views/spree/api/taxons/new.v1.rabl +3 -0
- data/app/views/spree/api/taxons/show.v1.rabl +8 -0
- data/app/views/spree/api/taxons/taxons.v1.rabl +7 -0
- data/app/views/spree/api/variants/index.v1.rabl +10 -0
- data/app/views/spree/api/variants/new.v1.rabl +2 -0
- data/app/views/spree/api/variants/show.v1.rabl +3 -0
- data/app/views/spree/api/variants/variant.v1.rabl +1 -0
- data/app/views/spree/api/zones/index.v1.rabl +7 -0
- data/app/views/spree/api/zones/show.v1.rabl +6 -0
- data/config/initializers/metal_load_paths.rb +1 -0
- data/config/locales/en.yml +23 -0
- data/config/routes.rb +56 -0
- data/db/migrate/20100107141738_add_api_key_to_spree_users.rb +7 -0
- data/db/migrate/20120411123334_resize_api_key_field.rb +7 -0
- data/db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb +7 -0
- data/lib/spree/api.rb +12 -0
- data/lib/spree/api/controller_setup.rb +27 -0
- data/lib/spree/api/engine.rb +35 -0
- data/lib/spree/api/responders.rb +11 -0
- data/lib/spree/api/responders/rabl_template.rb +22 -0
- data/lib/spree/api/testing_support/helpers.rb +35 -0
- data/lib/spree/api/testing_support/setup.rb +28 -0
- data/lib/spree/api/version.rb +5 -0
- data/lib/spree_api.rb +3 -0
- data/script/rails +9 -0
- data/spec/controllers/spree/api/addresses_controller_spec.rb +45 -0
- data/spec/controllers/spree/api/base_controller_spec.rb +42 -0
- data/spec/controllers/spree/api/countries_controller_spec.rb +48 -0
- data/spec/controllers/spree/api/images_controller_spec.rb +66 -0
- data/spec/controllers/spree/api/line_items_controller_spec.rb +77 -0
- data/spec/controllers/spree/api/orders_controller_spec.rb +255 -0
- data/spec/controllers/spree/api/payments_controller_spec.rb +203 -0
- data/spec/controllers/spree/api/product_properties_controller_spec.rb +116 -0
- data/spec/controllers/spree/api/products_controller_spec.rb +211 -0
- data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +155 -0
- data/spec/controllers/spree/api/shipments_controller_spec.rb +59 -0
- data/spec/controllers/spree/api/taxonomies_controller_spec.rb +107 -0
- data/spec/controllers/spree/api/taxons_controller_spec.rb +87 -0
- data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +26 -0
- data/spec/controllers/spree/api/variants_controller_spec.rb +155 -0
- data/spec/controllers/spree/api/zones_controller_spec.rb +111 -0
- data/spec/fixtures/thinking-cat.jpg +0 -0
- data/spec/models/spree/legacy_user_spec.rb +19 -0
- data/spec/models/spree/order_spec.rb +18 -0
- data/spec/shared_examples/protect_product_actions.rb +17 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/support/controller_hacks.rb +27 -0
- data/spec/support/database_cleaner.rb +14 -0
- data/spec/support/have_attributes_matcher.rb +13 -0
- data/spree_api.gemspec +23 -0
- metadata +254 -0
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'shared_examples/protect_product_actions'
|
3
|
+
|
4
|
+
module Spree
|
5
|
+
describe Spree::Api::ProductPropertiesController do
|
6
|
+
render_views
|
7
|
+
|
8
|
+
let!(:product) { create(:product) }
|
9
|
+
let!(:property_1) {product.product_properties.create(:property_name => "My Property 1", :value => "my value 1")}
|
10
|
+
let!(:property_2) {product.product_properties.create(:property_name => "My Property 2", :value => "my value 2")}
|
11
|
+
|
12
|
+
let(:attributes) { [:id, :product_id, :property_id, :value, :property_name] }
|
13
|
+
let(:resource_scoping) { { :product_id => product.to_param } }
|
14
|
+
|
15
|
+
before do
|
16
|
+
stub_authentication!
|
17
|
+
end
|
18
|
+
|
19
|
+
context "if product is deleted" do
|
20
|
+
before do
|
21
|
+
product.update_column(:deleted_at, Time.now)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "can not see a list of product properties" do
|
25
|
+
api_get :index
|
26
|
+
response.status.should == 404
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "can see a list of all product properties" do
|
31
|
+
api_get :index
|
32
|
+
json_response["product_properties"].count.should eq 2
|
33
|
+
json_response["product_properties"].first.should have_attributes(attributes)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "can control the page size through a parameter" do
|
37
|
+
api_get :index, :per_page => 1
|
38
|
+
json_response['product_properties'].count.should == 1
|
39
|
+
json_response['current_page'].should == 1
|
40
|
+
json_response['pages'].should == 2
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'can query the results through a paramter' do
|
44
|
+
Spree::ProductProperty.last.update_attribute(:value, 'loose')
|
45
|
+
property = Spree::ProductProperty.last
|
46
|
+
api_get :index, :q => { :value_cont => 'loose' }
|
47
|
+
json_response['count'].should == 1
|
48
|
+
json_response['product_properties'].first['value'].should eq property.value
|
49
|
+
end
|
50
|
+
|
51
|
+
it "can see a single product_property" do
|
52
|
+
api_get :show, :id => property_1.property_name
|
53
|
+
json_response.should have_attributes(attributes)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "can learn how to create a new product property" do
|
57
|
+
api_get :new
|
58
|
+
json_response["attributes"].should == attributes.map(&:to_s)
|
59
|
+
json_response["required_attributes"].should be_empty
|
60
|
+
end
|
61
|
+
|
62
|
+
it "cannot create a new product property if not an admin" do
|
63
|
+
api_post :create, :product_property => { :property_name => "My Property 3" }
|
64
|
+
assert_unauthorized!
|
65
|
+
end
|
66
|
+
|
67
|
+
it "cannot update a product property" do
|
68
|
+
api_put :update, :id => property_1.property_name, :product_property => { :value => "my value 456" }
|
69
|
+
assert_unauthorized!
|
70
|
+
end
|
71
|
+
|
72
|
+
it "cannot delete a product property" do
|
73
|
+
api_delete :destroy, :property_name => property_1.property_name
|
74
|
+
assert_unauthorized!
|
75
|
+
lambda { property_1.reload }.should_not raise_error
|
76
|
+
end
|
77
|
+
|
78
|
+
context "as an admin" do
|
79
|
+
sign_in_as_admin!
|
80
|
+
|
81
|
+
it "can create a new product property" do
|
82
|
+
expect do
|
83
|
+
api_post :create, :product_property => { :property_name => "My Property 3", :value => "my value 3" }
|
84
|
+
end.to change(product.product_properties, :count).by(1)
|
85
|
+
json_response.should have_attributes(attributes)
|
86
|
+
response.status.should == 201
|
87
|
+
end
|
88
|
+
|
89
|
+
it "can update a product property" do
|
90
|
+
api_put :update, :id => property_1.property_name, :product_property => { :value => "my value 456" }
|
91
|
+
response.status.should == 200
|
92
|
+
end
|
93
|
+
|
94
|
+
it "can delete a variant" do
|
95
|
+
api_delete :destroy, :id => property_1.property_name
|
96
|
+
response.status.should == 204
|
97
|
+
lambda { property_1.reload }.should raise_error(ActiveRecord::RecordNotFound)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context "with product identified by id" do
|
102
|
+
let(:resource_scoping) { { :product_id => product.id } }
|
103
|
+
it "can see a list of all product properties" do
|
104
|
+
api_get :index
|
105
|
+
json_response["product_properties"].count.should eq 2
|
106
|
+
json_response["product_properties"].first.should have_attributes(attributes)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "can see a single product_property by id" do
|
110
|
+
api_get :show, :id => property_1.id
|
111
|
+
json_response.should have_attributes(attributes)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,211 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'shared_examples/protect_product_actions'
|
3
|
+
|
4
|
+
module Spree
|
5
|
+
describe Spree::Api::ProductsController do
|
6
|
+
render_views
|
7
|
+
|
8
|
+
let!(:product) { create(:product) }
|
9
|
+
let!(:inactive_product) { create(:product, :available_on => Time.now.tomorrow, :name => "inactive") }
|
10
|
+
let(:attributes) { [:id, :name, :description, :price, :available_on, :permalink, :count_on_hand, :meta_description, :meta_keywords, :taxon_ids] }
|
11
|
+
|
12
|
+
before do
|
13
|
+
stub_authentication!
|
14
|
+
end
|
15
|
+
|
16
|
+
context "as a normal user" do
|
17
|
+
it "retrieves a list of products" do
|
18
|
+
api_get :index
|
19
|
+
json_response["products"].first.should have_attributes(attributes)
|
20
|
+
json_response["count"].should == 1
|
21
|
+
json_response["current_page"].should == 1
|
22
|
+
json_response["pages"].should == 1
|
23
|
+
end
|
24
|
+
|
25
|
+
it "does not list unavailable products" do
|
26
|
+
api_get :index
|
27
|
+
json_response["products"].first["name"].should_not eq("inactive")
|
28
|
+
end
|
29
|
+
|
30
|
+
context "pagination" do
|
31
|
+
default_per_page(1)
|
32
|
+
|
33
|
+
it "can select the next page of products" do
|
34
|
+
second_product = create(:product)
|
35
|
+
api_get :index, :page => 2
|
36
|
+
json_response["products"].first.should have_attributes(attributes)
|
37
|
+
json_response["total_count"].should == 2
|
38
|
+
json_response["current_page"].should == 2
|
39
|
+
json_response["pages"].should == 2
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'can control the page size through a parameter' do
|
43
|
+
create(:product)
|
44
|
+
api_get :index, :per_page => 1
|
45
|
+
json_response['count'].should == 1
|
46
|
+
json_response['total_count'].should == 2
|
47
|
+
json_response['current_page'].should == 1
|
48
|
+
json_response['pages'].should == 2
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "jsonp" do
|
53
|
+
it "retrieves a list of products of jsonp" do
|
54
|
+
api_get :index, {:callback => 'callback'}
|
55
|
+
response.body.should =~ /^callback\(.*\)$/
|
56
|
+
response.header['Content-Type'].should include('application/javascript')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it "can search for products" do
|
61
|
+
create(:product, :name => "The best product in the world")
|
62
|
+
api_get :index, :q => { :name_cont => "best" }
|
63
|
+
json_response["products"].first.should have_attributes(attributes)
|
64
|
+
json_response["count"].should == 1
|
65
|
+
end
|
66
|
+
|
67
|
+
it "gets a single product" do
|
68
|
+
product.master.images.create!(:attachment => image("thinking-cat.jpg"))
|
69
|
+
product.set_property("spree", "rocks")
|
70
|
+
api_get :show, :id => product.to_param
|
71
|
+
json_response.should have_attributes(attributes)
|
72
|
+
json_response['variants'].first.should have_attributes([:name,
|
73
|
+
:is_master,
|
74
|
+
:count_on_hand,
|
75
|
+
:price])
|
76
|
+
|
77
|
+
json_response["images"].first.should have_attributes([:attachment_file_name,
|
78
|
+
:attachment_width,
|
79
|
+
:attachment_height,
|
80
|
+
:attachment_content_type])
|
81
|
+
|
82
|
+
json_response["product_properties"].first.should have_attributes([:value,
|
83
|
+
:product_id,
|
84
|
+
:property_name])
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
context "finds a product by permalink first then by id" do
|
89
|
+
let!(:other_product) { create(:product, :permalink => "these-are-not-the-droids-you-are-looking-for") }
|
90
|
+
|
91
|
+
before do
|
92
|
+
product.update_attribute(:permalink, "#{other_product.id}-and-1-ways")
|
93
|
+
end
|
94
|
+
|
95
|
+
specify do
|
96
|
+
api_get :show, :id => product.to_param
|
97
|
+
json_response["permalink"].should =~ /and-1-ways/
|
98
|
+
product.destroy
|
99
|
+
|
100
|
+
api_get :show, :id => other_product.id
|
101
|
+
json_response["permalink"].should =~ /droids/
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
it "cannot see inactive products" do
|
106
|
+
api_get :show, :id => inactive_product.to_param
|
107
|
+
json_response["error"].should == "The resource you were looking for could not be found."
|
108
|
+
response.status.should == 404
|
109
|
+
end
|
110
|
+
|
111
|
+
it "returns a 404 error when it cannot find a product" do
|
112
|
+
api_get :show, :id => "non-existant"
|
113
|
+
json_response["error"].should == "The resource you were looking for could not be found."
|
114
|
+
response.status.should == 404
|
115
|
+
end
|
116
|
+
|
117
|
+
it "can learn how to create a new product" do
|
118
|
+
api_get :new
|
119
|
+
json_response["attributes"].should == attributes.map(&:to_s)
|
120
|
+
required_attributes = json_response["required_attributes"]
|
121
|
+
required_attributes.should include("name")
|
122
|
+
required_attributes.should include("price")
|
123
|
+
end
|
124
|
+
|
125
|
+
it_behaves_like "modifying product actions are restricted"
|
126
|
+
end
|
127
|
+
|
128
|
+
context "as an admin" do
|
129
|
+
sign_in_as_admin!
|
130
|
+
|
131
|
+
it "can see all products" do
|
132
|
+
api_get :index
|
133
|
+
json_response["products"].count.should == 2
|
134
|
+
json_response["count"].should == 2
|
135
|
+
json_response["current_page"].should == 1
|
136
|
+
json_response["pages"].should == 1
|
137
|
+
end
|
138
|
+
|
139
|
+
# Regression test for #1626
|
140
|
+
context "deleted products" do
|
141
|
+
before do
|
142
|
+
create(:product, :deleted_at => Time.now)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "does not include deleted products" do
|
146
|
+
api_get :index
|
147
|
+
json_response["products"].count.should == 2
|
148
|
+
end
|
149
|
+
|
150
|
+
it "can include deleted products" do
|
151
|
+
api_get :index, :show_deleted => 1
|
152
|
+
json_response["products"].count.should == 3
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
it "can create a new product" do
|
157
|
+
api_post :create, :product => { :name => "The Other Product",
|
158
|
+
:price => 19.99 }
|
159
|
+
json_response.should have_attributes(attributes)
|
160
|
+
response.status.should == 201
|
161
|
+
end
|
162
|
+
|
163
|
+
# Regression test for #2140
|
164
|
+
context "with authentication_required set to false" do
|
165
|
+
before do
|
166
|
+
Spree::Api::Config.requires_authentication = false
|
167
|
+
end
|
168
|
+
|
169
|
+
after do
|
170
|
+
Spree::Api::Config.requires_authentication = true
|
171
|
+
end
|
172
|
+
|
173
|
+
it "can still create a product" do
|
174
|
+
api_post :create, :product => { :name => "The Other Product",
|
175
|
+
:price => 19.99 },
|
176
|
+
:token => "fake"
|
177
|
+
json_response.should have_attributes(attributes)
|
178
|
+
response.status.should == 201
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
it "cannot create a new product with invalid attributes" do
|
183
|
+
api_post :create, :product => {}
|
184
|
+
response.status.should == 422
|
185
|
+
json_response["error"].should == "Invalid resource. Please fix errors and try again."
|
186
|
+
errors = json_response["errors"]
|
187
|
+
errors.delete("permalink") # Don't care about this one.
|
188
|
+
errors.keys.should =~ ["name", "price"]
|
189
|
+
end
|
190
|
+
|
191
|
+
it "can update a product" do
|
192
|
+
api_put :update, :id => product.to_param, :product => { :name => "New and Improved Product!" }
|
193
|
+
response.status.should == 200
|
194
|
+
end
|
195
|
+
|
196
|
+
it "cannot update a product with an invalid attribute" do
|
197
|
+
api_put :update, :id => product.to_param, :product => { :name => "" }
|
198
|
+
response.status.should == 422
|
199
|
+
json_response["error"].should == "Invalid resource. Please fix errors and try again."
|
200
|
+
json_response["errors"]["name"].should == ["can't be blank"]
|
201
|
+
end
|
202
|
+
|
203
|
+
it "can delete a product" do
|
204
|
+
product.deleted_at.should be_nil
|
205
|
+
api_delete :destroy, :id => product.to_param
|
206
|
+
response.status.should == 204
|
207
|
+
product.reload.deleted_at.should_not be_nil
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
describe Api::ReturnAuthorizationsController do
|
5
|
+
render_views
|
6
|
+
|
7
|
+
let!(:order) do
|
8
|
+
order = create(:order)
|
9
|
+
order.line_items << create(:line_item)
|
10
|
+
order.shipments << create(:shipment, :state => 'shipped')
|
11
|
+
order.finalize!
|
12
|
+
order.shipments.update_all(:state => 'shipped')
|
13
|
+
order.inventory_units.update_all(:state => 'shipped')
|
14
|
+
order
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:product) { create(:product) }
|
18
|
+
let(:attributes) { [:id, :reason, :amount, :state] }
|
19
|
+
let(:resource_scoping) { { :order_id => order.to_param } }
|
20
|
+
|
21
|
+
before do
|
22
|
+
stub_authentication!
|
23
|
+
end
|
24
|
+
|
25
|
+
context "as the order owner" do
|
26
|
+
before do
|
27
|
+
Order.any_instance.stub :user => current_api_user
|
28
|
+
end
|
29
|
+
|
30
|
+
it "cannot see any return authorizations" do
|
31
|
+
api_get :index
|
32
|
+
assert_unauthorized!
|
33
|
+
end
|
34
|
+
|
35
|
+
it "cannot see a single return authorization" do
|
36
|
+
api_get :show, :id => 1
|
37
|
+
assert_unauthorized!
|
38
|
+
end
|
39
|
+
|
40
|
+
it "cannot learn how to create a new return authorization" do
|
41
|
+
api_get :new
|
42
|
+
assert_unauthorized!
|
43
|
+
end
|
44
|
+
|
45
|
+
it "cannot create a new return authorization" do
|
46
|
+
api_post :create
|
47
|
+
assert_unauthorized!
|
48
|
+
end
|
49
|
+
|
50
|
+
it "cannot update a return authorization" do
|
51
|
+
api_put :update
|
52
|
+
assert_unauthorized!
|
53
|
+
end
|
54
|
+
|
55
|
+
it "cannot delete a return authorization" do
|
56
|
+
api_delete :destroy
|
57
|
+
assert_unauthorized!
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "as an admin" do
|
62
|
+
sign_in_as_admin!
|
63
|
+
|
64
|
+
it "can show return authorization" do
|
65
|
+
FactoryGirl.create(:return_authorization, :order => order)
|
66
|
+
return_authorization = order.return_authorizations.first
|
67
|
+
api_get :show, :order_id => order.number, :id => return_authorization.id
|
68
|
+
response.status.should == 200
|
69
|
+
json_response.should have_attributes(attributes)
|
70
|
+
json_response["state"].should_not be_blank
|
71
|
+
end
|
72
|
+
|
73
|
+
it "can get a list of return authorizations" do
|
74
|
+
FactoryGirl.create(:return_authorization, :order => order)
|
75
|
+
FactoryGirl.create(:return_authorization, :order => order)
|
76
|
+
api_get :index, { :order_id => order.number }
|
77
|
+
response.status.should == 200
|
78
|
+
return_authorizations = json_response["return_authorizations"]
|
79
|
+
return_authorizations.first.should have_attributes(attributes)
|
80
|
+
return_authorizations.first.should_not == return_authorizations.last
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'can control the page size through a parameter' do
|
84
|
+
FactoryGirl.create(:return_authorization, :order => order)
|
85
|
+
FactoryGirl.create(:return_authorization, :order => order)
|
86
|
+
api_get :index, :order_id => order.number, :per_page => 1
|
87
|
+
json_response['count'].should == 1
|
88
|
+
json_response['current_page'].should == 1
|
89
|
+
json_response['pages'].should == 2
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'can query the results through a paramter' do
|
93
|
+
FactoryGirl.create(:return_authorization, :order => order)
|
94
|
+
expected_result = create(:return_authorization, :reason => 'damaged')
|
95
|
+
order.return_authorizations << expected_result
|
96
|
+
api_get :index, :q => { :reason_cont => 'damage' }
|
97
|
+
json_response['count'].should == 1
|
98
|
+
json_response['return_authorizations'].first['reason'].should eq expected_result.reason
|
99
|
+
end
|
100
|
+
|
101
|
+
it "can learn how to create a new return authorization" do
|
102
|
+
api_get :new
|
103
|
+
json_response["attributes"].should == ["id", "number", "state", "amount", "order_id", "reason", "created_at", "updated_at"]
|
104
|
+
required_attributes = json_response["required_attributes"]
|
105
|
+
required_attributes.should include("order")
|
106
|
+
end
|
107
|
+
|
108
|
+
it "can update a return authorization on the order" do
|
109
|
+
FactoryGirl.create(:return_authorization, :order => order)
|
110
|
+
return_authorization = order.return_authorizations.first
|
111
|
+
api_put :update, :id => return_authorization.id, :return_authorization => { :amount => 19.99 }
|
112
|
+
response.status.should == 200
|
113
|
+
json_response.should have_attributes(attributes)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "can delete a return authorization on the order" do
|
117
|
+
FactoryGirl.create(:return_authorization, :order => order)
|
118
|
+
return_authorization = order.return_authorizations.first
|
119
|
+
api_delete :destroy, :id => return_authorization.id
|
120
|
+
response.status.should == 204
|
121
|
+
lambda { return_authorization.reload }.should raise_error(ActiveRecord::RecordNotFound)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "can add a new return authorization to an existing order" do
|
125
|
+
api_post :create, :return_autorization => { :order_id => order.number, :amount => 14.22, :reason => "Defective" }
|
126
|
+
response.status.should == 201
|
127
|
+
json_response.should have_attributes(attributes)
|
128
|
+
json_response["state"].should_not be_blank
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context "as just another user" do
|
133
|
+
it "cannot add a return authorization to the order" do
|
134
|
+
api_post :create, :return_autorization => { :order_id => order.number, :amount => 14.22, :reason => "Defective" }
|
135
|
+
assert_unauthorized!
|
136
|
+
end
|
137
|
+
|
138
|
+
it "cannot update a return authorization on the order" do
|
139
|
+
FactoryGirl.create(:return_authorization, :order => order)
|
140
|
+
return_authorization = order.return_authorizations.first
|
141
|
+
api_put :update, :id => return_authorization.id, :return_authorization => { :amount => 19.99 }
|
142
|
+
assert_unauthorized!
|
143
|
+
return_authorization.reload.amount.should_not == 19.99
|
144
|
+
end
|
145
|
+
|
146
|
+
it "cannot delete a return authorization on the order" do
|
147
|
+
FactoryGirl.create(:return_authorization, :order => order)
|
148
|
+
return_authorization = order.return_authorizations.first
|
149
|
+
api_delete :destroy, :id => return_authorization.id
|
150
|
+
assert_unauthorized!
|
151
|
+
lambda { return_authorization.reload }.should_not raise_error(ActiveRecord::RecordNotFound)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|