dup_spree_api 1.3.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +5 -0
  4. data/LICENSE +22 -0
  5. data/Rakefile +30 -0
  6. data/app/controllers/spree/api/addresses_controller.rb +20 -0
  7. data/app/controllers/spree/api/base_controller.rb +114 -0
  8. data/app/controllers/spree/api/countries_controller.rb +18 -0
  9. data/app/controllers/spree/api/images_controller.rb +32 -0
  10. data/app/controllers/spree/api/line_items_controller.rb +40 -0
  11. data/app/controllers/spree/api/orders_controller.rb +85 -0
  12. data/app/controllers/spree/api/payments_controller.rb +80 -0
  13. data/app/controllers/spree/api/product_properties_controller.rb +65 -0
  14. data/app/controllers/spree/api/products_controller.rb +49 -0
  15. data/app/controllers/spree/api/return_authorizations_controller.rb +54 -0
  16. data/app/controllers/spree/api/shipments_controller.rb +43 -0
  17. data/app/controllers/spree/api/taxonomies_controller.rb +50 -0
  18. data/app/controllers/spree/api/taxons_controller.rb +53 -0
  19. data/app/controllers/spree/api/variants_controller.rb +75 -0
  20. data/app/controllers/spree/api/zones_controller.rb +46 -0
  21. data/app/helpers/spree/api/api_helpers.rb +76 -0
  22. data/app/models/spree/api_configuration.rb +5 -0
  23. data/app/models/spree/line_item_decorator.rb +3 -0
  24. data/app/models/spree/option_value_decorator.rb +5 -0
  25. data/app/models/spree/order_decorator.rb +13 -0
  26. data/app/models/spree/user_decorator.rb +13 -0
  27. data/app/overrides/api_admin_user_edit_form.rb +6 -0
  28. data/app/views/spree/admin/users/_api_fields.html.erb +32 -0
  29. data/app/views/spree/api/addresses/show.v1.rabl +11 -0
  30. data/app/views/spree/api/countries/index.v1.rabl +7 -0
  31. data/app/views/spree/api/countries/show.v1.rabl +5 -0
  32. data/app/views/spree/api/errors/gateway_error.v1.rabl +2 -0
  33. data/app/views/spree/api/errors/invalid_api_key.v1.rabl +2 -0
  34. data/app/views/spree/api/errors/invalid_resource.v1.rabl +3 -0
  35. data/app/views/spree/api/errors/must_specify_api_key.v1.rabl +2 -0
  36. data/app/views/spree/api/errors/not_found.v1.rabl +2 -0
  37. data/app/views/spree/api/errors/unauthorized.v1.rabl +2 -0
  38. data/app/views/spree/api/images/show.v1.rabl +3 -0
  39. data/app/views/spree/api/line_items/new.v1.rabl +3 -0
  40. data/app/views/spree/api/line_items/show.v1.rabl +5 -0
  41. data/app/views/spree/api/orders/address.v1.rabl +0 -0
  42. data/app/views/spree/api/orders/canceled.v1.rabl +0 -0
  43. data/app/views/spree/api/orders/cart.v1.rabl +0 -0
  44. data/app/views/spree/api/orders/complete.v1.rabl +0 -0
  45. data/app/views/spree/api/orders/could_not_transition.v1.rabl +3 -0
  46. data/app/views/spree/api/orders/delivery.v1.rabl +3 -0
  47. data/app/views/spree/api/orders/index.v1.rabl +7 -0
  48. data/app/views/spree/api/orders/invalid_shipping_method.v1.rabl +2 -0
  49. data/app/views/spree/api/orders/payment.v1.rabl +4 -0
  50. data/app/views/spree/api/orders/show.v1.rabl +29 -0
  51. data/app/views/spree/api/payments/credit_over_limit.v1.rabl +2 -0
  52. data/app/views/spree/api/payments/index.v1.rabl +7 -0
  53. data/app/views/spree/api/payments/new.v1.rabl +6 -0
  54. data/app/views/spree/api/payments/show.v1.rabl +2 -0
  55. data/app/views/spree/api/product_properties/index.v1.rabl +7 -0
  56. data/app/views/spree/api/product_properties/new.v1.rabl +2 -0
  57. data/app/views/spree/api/product_properties/show.v1.rabl +2 -0
  58. data/app/views/spree/api/products/index.v1.rabl +8 -0
  59. data/app/views/spree/api/products/new.v1.rabl +3 -0
  60. data/app/views/spree/api/products/product.v1.rabl +1 -0
  61. data/app/views/spree/api/products/show.v1.rabl +25 -0
  62. data/app/views/spree/api/return_authorizations/index.v1.rabl +7 -0
  63. data/app/views/spree/api/return_authorizations/new.v1.rabl +3 -0
  64. data/app/views/spree/api/return_authorizations/show.v1.rabl +2 -0
  65. data/app/views/spree/api/shipments/cannot_ready_shipment.v1.rabl +2 -0
  66. data/app/views/spree/api/shipments/show.v1.rabl +7 -0
  67. data/app/views/spree/api/taxonomies/index.v1.rabl +7 -0
  68. data/app/views/spree/api/taxonomies/nested.v1.rabl +11 -0
  69. data/app/views/spree/api/taxonomies/new.v1.rabl +3 -0
  70. data/app/views/spree/api/taxonomies/show.v1.rabl +15 -0
  71. data/app/views/spree/api/taxons/index.v1.rabl +4 -0
  72. data/app/views/spree/api/taxons/new.v1.rabl +3 -0
  73. data/app/views/spree/api/taxons/show.v1.rabl +8 -0
  74. data/app/views/spree/api/taxons/taxons.v1.rabl +7 -0
  75. data/app/views/spree/api/variants/index.v1.rabl +10 -0
  76. data/app/views/spree/api/variants/new.v1.rabl +2 -0
  77. data/app/views/spree/api/variants/show.v1.rabl +3 -0
  78. data/app/views/spree/api/variants/variant.v1.rabl +1 -0
  79. data/app/views/spree/api/zones/index.v1.rabl +7 -0
  80. data/app/views/spree/api/zones/show.v1.rabl +6 -0
  81. data/config/initializers/metal_load_paths.rb +1 -0
  82. data/config/locales/en.yml +23 -0
  83. data/config/routes.rb +56 -0
  84. data/db/migrate/20100107141738_add_api_key_to_spree_users.rb +7 -0
  85. data/db/migrate/20120411123334_resize_api_key_field.rb +7 -0
  86. data/db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb +7 -0
  87. data/lib/spree/api.rb +12 -0
  88. data/lib/spree/api/controller_setup.rb +27 -0
  89. data/lib/spree/api/engine.rb +35 -0
  90. data/lib/spree/api/responders.rb +11 -0
  91. data/lib/spree/api/responders/rabl_template.rb +22 -0
  92. data/lib/spree/api/testing_support/helpers.rb +35 -0
  93. data/lib/spree/api/testing_support/setup.rb +28 -0
  94. data/lib/spree/api/version.rb +5 -0
  95. data/lib/spree_api.rb +3 -0
  96. data/script/rails +9 -0
  97. data/spec/controllers/spree/api/addresses_controller_spec.rb +45 -0
  98. data/spec/controllers/spree/api/base_controller_spec.rb +42 -0
  99. data/spec/controllers/spree/api/countries_controller_spec.rb +48 -0
  100. data/spec/controllers/spree/api/images_controller_spec.rb +66 -0
  101. data/spec/controllers/spree/api/line_items_controller_spec.rb +77 -0
  102. data/spec/controllers/spree/api/orders_controller_spec.rb +255 -0
  103. data/spec/controllers/spree/api/payments_controller_spec.rb +203 -0
  104. data/spec/controllers/spree/api/product_properties_controller_spec.rb +116 -0
  105. data/spec/controllers/spree/api/products_controller_spec.rb +211 -0
  106. data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +155 -0
  107. data/spec/controllers/spree/api/shipments_controller_spec.rb +59 -0
  108. data/spec/controllers/spree/api/taxonomies_controller_spec.rb +107 -0
  109. data/spec/controllers/spree/api/taxons_controller_spec.rb +87 -0
  110. data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +26 -0
  111. data/spec/controllers/spree/api/variants_controller_spec.rb +155 -0
  112. data/spec/controllers/spree/api/zones_controller_spec.rb +111 -0
  113. data/spec/fixtures/thinking-cat.jpg +0 -0
  114. data/spec/models/spree/legacy_user_spec.rb +19 -0
  115. data/spec/models/spree/order_spec.rb +18 -0
  116. data/spec/shared_examples/protect_product_actions.rb +17 -0
  117. data/spec/spec_helper.rb +27 -0
  118. data/spec/support/controller_hacks.rb +27 -0
  119. data/spec/support/database_cleaner.rb +14 -0
  120. data/spec/support/have_attributes_matcher.rb +13 -0
  121. data/spree_api.gemspec +23 -0
  122. metadata +254 -0
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Api::ShipmentsController do
4
+ render_views
5
+ let!(:shipment) { create(:shipment) }
6
+ let!(:attributes) { [:id, :tracking, :number, :cost, :shipped_at] }
7
+
8
+ before do
9
+ stub_authentication!
10
+ end
11
+
12
+ let!(:resource_scoping) { { :order_id => shipment.order.to_param, :id => shipment.to_param } }
13
+
14
+ context "as a non-admin" do
15
+ it "cannot make a shipment ready" do
16
+ api_put :ready
17
+ assert_unauthorized!
18
+ end
19
+
20
+ it "cannot make a shipment shipped" do
21
+ api_put :ship
22
+ assert_unauthorized!
23
+ end
24
+ end
25
+
26
+ context "as an admin" do
27
+ sign_in_as_admin!
28
+
29
+ it "can make a shipment ready" do
30
+ Spree::Order.any_instance.stub(:paid? => true, :complete? => true)
31
+ api_put :ready
32
+ json_response.should have_attributes(attributes)
33
+ json_response["state"].should == "ready"
34
+ shipment.reload.state.should == "ready"
35
+ end
36
+
37
+ it "cannot make a shipment ready if the order is unpaid" do
38
+ Spree::Order.any_instance.stub(:paid? => false)
39
+ api_put :ready
40
+ json_response["error"].should == "Cannot ready shipment."
41
+ response.status.should == 422
42
+ end
43
+
44
+ context "can transition a shipment from ready to ship" do
45
+ before do
46
+ Spree::Order.any_instance.stub(:paid? => true, :complete? => true)
47
+ shipment.update!(shipment.order)
48
+ shipment.state.should == "ready"
49
+ end
50
+
51
+ it "can transition a shipment from ready to ship" do
52
+ shipment.reload
53
+ api_put :ship, :order_id => shipment.order.to_param, :id => shipment.to_param, :shipment => { :tracking => "123123" }
54
+ json_response.should have_attributes(attributes)
55
+ json_response["state"].should == "shipped"
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::TaxonomiesController do
5
+ render_views
6
+
7
+ let(:taxonomy) { create(:taxonomy) }
8
+ let(:taxon) { create(:taxon, :name => "Ruby", :taxonomy => taxonomy) }
9
+ let(:taxon2) { create(:taxon, :name => "Rails", :taxonomy => taxonomy) }
10
+ let(:attributes) { [:id, :name] }
11
+
12
+ before do
13
+ stub_authentication!
14
+ taxon2.children << create(:taxon, :name => "3.2.2", :taxonomy => taxonomy)
15
+ taxon.children << taxon2
16
+ taxonomy.root.children << taxon
17
+ end
18
+
19
+ context "as a normal user" do
20
+ it "gets all taxonomies" do
21
+ api_get :index
22
+
23
+ json_response["taxonomies"].first['name'].should eq taxonomy.name
24
+ json_response["taxonomies"].first['root']['taxons'].count.should eq 1
25
+ end
26
+
27
+ it 'can control the page size through a parameter' do
28
+ create(:taxonomy)
29
+ api_get :index, :per_page => 1
30
+ json_response['count'].should == 1
31
+ json_response['current_page'].should == 1
32
+ json_response['pages'].should == 2
33
+ end
34
+
35
+ it 'can query the results through a paramter' do
36
+ expected_result = create(:taxonomy, :name => 'Style')
37
+ api_get :index, :q => { :name_cont => 'style' }
38
+ json_response['count'].should == 1
39
+ json_response['taxonomies'].first['name'].should eq expected_result.name
40
+ end
41
+
42
+ it "gets a single taxonomy" do
43
+ api_get :show, :id => taxonomy.id
44
+
45
+ json_response['name'].should eq taxonomy.name
46
+
47
+ children = json_response['root']['taxons']
48
+ children.count.should eq 1
49
+ children.first['name'].should eq taxon.name
50
+ children.first.key?('taxons').should be_false
51
+ end
52
+
53
+ it "gets a single taxonomy with set=nested" do
54
+ api_get :show, :id => taxonomy.id, :set => 'nested'
55
+
56
+ json_response['name'].should eq taxonomy.name
57
+
58
+ children = json_response['root']['taxons']
59
+ children.first.key?('taxons').should be_true
60
+ end
61
+
62
+ it "can learn how to create a new taxonomy" do
63
+ api_get :new
64
+ json_response["attributes"].should == attributes.map(&:to_s)
65
+ required_attributes = json_response["required_attributes"]
66
+ required_attributes.should include("name")
67
+ end
68
+
69
+ it "cannot create a new taxonomy if not an admin" do
70
+ api_post :create, :taxonomy => { :name => "Location" }
71
+ assert_unauthorized!
72
+ end
73
+
74
+ it "cannot update a taxonomy" do
75
+ api_put :update, :id => taxonomy.id, :taxonomy => { :name => "I hacked your store!" }
76
+ assert_unauthorized!
77
+ end
78
+
79
+ it "cannot delete a taxonomy" do
80
+ api_delete :destroy, :id => taxonomy.id
81
+ assert_unauthorized!
82
+ end
83
+ end
84
+
85
+ context "as an admin" do
86
+ sign_in_as_admin!
87
+
88
+ it "can create" do
89
+ api_post :create, :taxonomy => { :name => "Colors"}
90
+ json_response.should have_attributes(attributes)
91
+ response.status.should == 201
92
+ end
93
+
94
+ it "cannot create a new taxonomy with invalid attributes" do
95
+ api_post :create, :taxonomy => {}
96
+ response.status.should == 422
97
+ json_response["error"].should == "Invalid resource. Please fix errors and try again."
98
+ errors = json_response["errors"]
99
+ end
100
+
101
+ it "can destroy" do
102
+ api_delete :destroy, :id => taxonomy.id
103
+ response.status.should == 204
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::TaxonsController do
5
+ render_views
6
+
7
+ let(:taxonomy) { create(:taxonomy) }
8
+ let(:taxon) { create(:taxon, :name => "Ruby", :taxonomy => taxonomy) }
9
+ let(:taxon2) { create(:taxon, :name => "Rails", :taxonomy => taxonomy) }
10
+ let(:attributes) { ["id", "name", "permalink", "position", "parent_id", "taxonomy_id"] }
11
+
12
+ before do
13
+ stub_authentication!
14
+ taxon2.children << create(:taxon, :name => "3.2.2", :taxonomy => taxonomy)
15
+ taxon.children << taxon2
16
+ taxonomy.root.children << taxon
17
+ end
18
+
19
+ context "as a normal user" do
20
+ it "gets all taxons" do
21
+ api_get :index, :taxonomy_id => taxonomy.id
22
+
23
+ json_response.first['name'].should eq taxon.name
24
+ children = json_response.first['taxons']
25
+ children.count.should eq 1
26
+ children.first['name'].should eq taxon2.name
27
+ children.first['taxons'].count.should eq 1
28
+ end
29
+
30
+ it "gets a single taxon" do
31
+ api_get :show, :id => taxon.id, :taxonomy_id => taxonomy.id
32
+
33
+ json_response['name'].should eq taxon.name
34
+ json_response['taxons'].count.should eq 1
35
+ end
36
+
37
+ it "can learn how to create a new taxon" do
38
+ api_get :new, :taxonomy_id => taxonomy.id
39
+ json_response["attributes"].should == attributes.map(&:to_s)
40
+ required_attributes = json_response["required_attributes"]
41
+ required_attributes.should include("name")
42
+ end
43
+
44
+ it "cannot create a new taxon if not an admin" do
45
+ api_post :create, :taxonomy_id => taxonomy.id, :taxon => { :name => "Location" }
46
+ assert_unauthorized!
47
+ end
48
+
49
+ it "cannot update a taxon" do
50
+ api_put :update, :taxonomy_id => taxonomy.id, :id => taxon.id, :taxon => { :name => "I hacked your store!" }
51
+ assert_unauthorized!
52
+ end
53
+
54
+ it "cannot delete a taxon" do
55
+ api_delete :destroy, :taxonomy_id => taxonomy.id, :id => taxon.id
56
+ assert_unauthorized!
57
+ end
58
+ end
59
+
60
+ context "as an admin" do
61
+ sign_in_as_admin!
62
+
63
+ it "can create" do
64
+ api_post :create, :taxonomy_id => taxonomy.id, :taxon => { :name => "Colors", :parent_id => taxon.id}
65
+ json_response.should have_attributes(attributes)
66
+ response.status.should == 201
67
+
68
+ taxon.reload.children.count.should eq 2
69
+ end
70
+
71
+ it "cannot create a new taxon with invalid attributes" do
72
+ api_post :create, :taxonomy_id => taxonomy.id, :taxon => {}
73
+ response.status.should == 422
74
+ json_response["error"].should == "Invalid resource. Please fix errors and try again."
75
+ errors = json_response["errors"]
76
+
77
+ taxon.reload.children.count.should eq 1
78
+ end
79
+
80
+ it "can destroy" do
81
+ api_delete :destroy, :taxonomy_id => taxonomy.id, :id => taxon.id
82
+ response.status.should == 204
83
+ end
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,26 @@
1
+ require 'shared_examples/protect_product_actions'
2
+ require 'spec_helper'
3
+
4
+ module Spree
5
+ describe Spree::Api::ProductsController do
6
+ render_views
7
+
8
+ let!(:product) { create(:product) }
9
+ let(:attributes) { [:id, :name, :description, :price, :available_on, :permalink, :count_on_hand, :meta_description, :meta_keywords, :taxon_ids] }
10
+
11
+ context "without authentication" do
12
+ before { Spree::Api::Config[:requires_authentication] = false }
13
+
14
+ it "retreives a list of products" do
15
+ api_get :index
16
+ json_response["products"].first.should have_attributes(attributes)
17
+ json_response["count"].should == 1
18
+ json_response["current_page"].should == 1
19
+ json_response["pages"].should == 1
20
+ end
21
+
22
+ it_behaves_like "modifying product actions are restricted"
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,155 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::VariantsController do
5
+ render_views
6
+
7
+
8
+ let!(:product) { create(:product) }
9
+ let!(:variant) do
10
+ variant = product.master
11
+ variant.option_values << create(:option_value)
12
+ variant
13
+ end
14
+ let!(:attributes) { [:id, :name, :count_on_hand,
15
+ :sku, :price, :weight, :height,
16
+ :width, :depth, :is_master, :cost_price,
17
+ :permalink] }
18
+
19
+ before do
20
+ stub_authentication!
21
+ end
22
+
23
+ it "can see a paginated list of variants" do
24
+ api_get :index
25
+ json_response["variants"].first.should have_attributes(attributes)
26
+ json_response["count"].should == 1
27
+ json_response["current_page"].should == 1
28
+ json_response["pages"].should == 1
29
+ end
30
+
31
+ it 'can control the page size through a parameter' do
32
+ create(:variant)
33
+ api_get :index, :per_page => 1
34
+ json_response['count'].should == 1
35
+ json_response['current_page'].should == 1
36
+ json_response['pages'].should == 3
37
+ end
38
+
39
+ it 'can query the results through a paramter' do
40
+ expected_result = create(:variant, :sku => 'FOOBAR')
41
+ api_get :index, :q => { :sku_cont => 'FOO' }
42
+ json_response['count'].should == 1
43
+ json_response['variants'].first['sku'].should eq expected_result.sku
44
+ end
45
+
46
+ it "variants returned contain option values data" do
47
+ api_get :index
48
+ option_values = json_response["variants"].last["option_values"]
49
+ option_values.first.should have_attributes([:name,
50
+ :presentation,
51
+ :option_type_name,
52
+ :option_type_id])
53
+ end
54
+
55
+ # Regression test for #2141
56
+ context "a deleted variant" do
57
+ before do
58
+ variant.update_column(:deleted_at, Time.now)
59
+ end
60
+
61
+ it "is not returned in the results" do
62
+ api_get :index
63
+ json_response["variants"].count.should == 0
64
+ end
65
+
66
+ it "is not returned even when show_deleted is passed" do
67
+ api_get :index, :show_deleted => true
68
+ json_response["variants"].count.should == 0
69
+ end
70
+ end
71
+
72
+ context "pagination" do
73
+ default_per_page(1)
74
+
75
+ it "can select the next page of variants" do
76
+ second_variant = create(:variant)
77
+ api_get :index, :page => 2
78
+ json_response["variants"].first.should have_attributes(attributes)
79
+ json_response["total_count"].should == 3
80
+ json_response["current_page"].should == 2
81
+ json_response["pages"].should == 3
82
+ end
83
+ end
84
+
85
+ it "can see a single variant" do
86
+ api_get :show, :id => variant.to_param
87
+ json_response.should have_attributes(attributes)
88
+ option_values = json_response["option_values"]
89
+ option_values.first.should have_attributes([:name,
90
+ :presentation,
91
+ :option_type_name,
92
+ :option_type_id])
93
+ end
94
+
95
+ it "can learn how to create a new variant" do
96
+ api_get :new
97
+ json_response["attributes"].should == attributes.map(&:to_s)
98
+ json_response["required_attributes"].should be_empty
99
+ end
100
+
101
+ it "cannot create a new variant if not an admin" do
102
+ api_post :create, :variant => { :sku => "12345" }
103
+ assert_unauthorized!
104
+ end
105
+
106
+ it "cannot update a variant" do
107
+ api_put :update, :id => variant.to_param, :variant => { :sku => "12345" }
108
+ assert_unauthorized!
109
+ end
110
+
111
+ it "cannot delete a variant" do
112
+ api_delete :destroy, :id => variant.to_param
113
+ assert_unauthorized!
114
+ lambda { variant.reload }.should_not raise_error
115
+ end
116
+
117
+ context "as an admin" do
118
+ sign_in_as_admin!
119
+ let(:resource_scoping) { { :product_id => variant.product.to_param } }
120
+
121
+ # Test for #2141
122
+ context "deleted variants" do
123
+ before do
124
+ variant.update_column(:deleted_at, Time.now)
125
+ end
126
+
127
+ it "are visible by admin" do
128
+ api_get :index, :show_deleted => 1
129
+ json_response["variants"].count.should == 1
130
+ end
131
+ end
132
+
133
+ it "can create a new variant" do
134
+ api_post :create, :variant => { :sku => "12345" }
135
+ json_response.should have_attributes(attributes)
136
+ response.status.should == 201
137
+
138
+ variant.product.variants.count.should == 1
139
+ end
140
+
141
+ it "can update a variant" do
142
+ api_put :update, :id => variant.to_param, :variant => { :sku => "12345" }
143
+ response.status.should == 200
144
+ end
145
+
146
+ it "can delete a variant" do
147
+ api_delete :destroy, :id => variant.to_param
148
+ response.status.should == 204
149
+ lambda { variant.reload }.should raise_error(ActiveRecord::RecordNotFound)
150
+ end
151
+ end
152
+
153
+
154
+ end
155
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::ZonesController do
5
+ render_views
6
+
7
+ let!(:attributes) { [:id, :name, :zone_members] }
8
+
9
+ before do
10
+ stub_authentication!
11
+ @zone = create(:zone, :name => 'Europe')
12
+ end
13
+
14
+ it "gets list of zones" do
15
+ api_get :index
16
+ json_response['zones'].first.should have_attributes(attributes)
17
+ end
18
+
19
+ it 'can control the page size through a parameter' do
20
+ create(:zone)
21
+ api_get :index, :per_page => 1
22
+ json_response['count'].should == 1
23
+ json_response['current_page'].should == 1
24
+ json_response['pages'].should == 2
25
+ end
26
+
27
+ it 'can query the results through a paramter' do
28
+ expected_result = create(:zone, :name => 'South America')
29
+ api_get :index, :q => { :name_cont => 'south' }
30
+ json_response['count'].should == 1
31
+ json_response['zones'].first['name'].should eq expected_result.name
32
+ end
33
+
34
+ it "gets a zone" do
35
+ api_get :show, :id => @zone.id
36
+ json_response.should have_attributes(attributes)
37
+ json_response['name'].should eq @zone.name
38
+ json_response['zone_members'].size.should eq @zone.zone_members.count
39
+ end
40
+
41
+ context "specifying a rabl template to use" do
42
+ before do
43
+ Spree::Api::ZonesController.class_eval do
44
+ def custom_show
45
+ respond_with(zone)
46
+ end
47
+ end
48
+ end
49
+
50
+ it "uses the specified template" do
51
+ request.env['X-Spree-Template'] = 'show'
52
+ api_get :custom_show, :id => @zone.id
53
+ response.should render_template('spree/api/zones/show')
54
+ end
55
+
56
+ it "falls back to the default template if the specified template does not exist" do
57
+ request.env['X-Spree-Template'] = 'invoice'
58
+ api_get :show, :id => @zone.id
59
+ response.should render_template('spree/api/zones/show')
60
+ end
61
+ end
62
+
63
+ context "as an admin" do
64
+ sign_in_as_admin!
65
+
66
+ it "can create a new zone" do
67
+ params = {
68
+ :zone => {
69
+ :name => "North Pole",
70
+ :zone_members => [
71
+ {
72
+ :zoneable_type => "Spree::Country",
73
+ :zoneable_id => 1
74
+ }
75
+ ]
76
+ }
77
+ }
78
+
79
+ api_post :create, params
80
+ response.status.should == 201
81
+ json_response.should have_attributes(attributes)
82
+ json_response["zone_members"].should_not be_empty
83
+ end
84
+
85
+ it "updates a zone" do
86
+ params = { :id => @zone.id,
87
+ :zone => {
88
+ :name => "North Pole",
89
+ :zone_members => [
90
+ {
91
+ :zoneable_type => "Spree::Country",
92
+ :zoneable_id => 1
93
+ }
94
+ ]
95
+ }
96
+ }
97
+
98
+ api_put :update, params
99
+ response.status.should == 200
100
+ json_response['name'].should eq 'North Pole'
101
+ json_response['zone_members'].should_not be_blank
102
+ end
103
+
104
+ it "can delete a zone" do
105
+ api_delete :destroy, :id => @zone.id
106
+ response.status.should == 204
107
+ lambda { @zone.reload }.should raise_error(ActiveRecord::RecordNotFound)
108
+ end
109
+ end
110
+ end
111
+ end