spree_api 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. data/app/controllers/spree/admin/users_controller_decorator.rb +17 -0
  2. data/app/controllers/spree/api/v1/addresses_controller.rb +17 -0
  3. data/app/controllers/spree/api/v1/images_controller.rb +9 -26
  4. data/app/controllers/spree/api/v1/orders_controller.rb +5 -0
  5. data/app/controllers/spree/api/v1/taxonomies_controller.rb +47 -0
  6. data/app/controllers/spree/api/v1/taxons_controller.rb +51 -0
  7. data/app/helpers/spree/api/api_helpers.rb +10 -2
  8. data/app/overrides/api_admin_user_edit_form.rb +6 -0
  9. data/app/views/spree/admin/users/_api_fields.html.erb +20 -0
  10. data/app/views/spree/api/v1/addresses/show.rabl +11 -0
  11. data/app/views/spree/api/v1/images/show.rabl +1 -0
  12. data/app/views/spree/api/v1/orders/address.rabl +0 -3
  13. data/app/views/spree/api/v1/orders/show.rabl +3 -3
  14. data/app/views/spree/api/v1/products/show.rabl +1 -1
  15. data/app/views/spree/api/v1/taxonomies/index.rabl +2 -0
  16. data/app/views/spree/api/v1/taxonomies/nested.rabl +11 -0
  17. data/app/views/spree/api/v1/taxonomies/new.rabl +3 -0
  18. data/app/views/spree/api/v1/taxonomies/show.rabl +15 -0
  19. data/app/views/spree/api/v1/taxons/index.rabl +4 -0
  20. data/app/views/spree/api/v1/taxons/new.rabl +3 -0
  21. data/app/views/spree/api/v1/taxons/show.rabl +8 -0
  22. data/app/views/spree/api/v1/taxons/taxons.rabl +7 -0
  23. data/config/initializers/metal_load_paths.rb +1 -0
  24. data/config/locales/en.yml +8 -1
  25. data/config/routes.rb +18 -2
  26. data/lib/spree/api/controller_setup.rb +0 -1
  27. data/spec/controllers/spree/api/v1/addresses_controller_spec.rb +23 -0
  28. data/spec/controllers/spree/api/v1/countries_controller_spec.rb +1 -1
  29. data/spec/controllers/spree/api/v1/images_controller_spec.rb +21 -15
  30. data/spec/controllers/spree/api/v1/line_items_controller_spec.rb +3 -3
  31. data/spec/controllers/spree/api/v1/orders_controller_spec.rb +8 -8
  32. data/spec/controllers/spree/api/v1/payments_controller_spec.rb +2 -2
  33. data/spec/controllers/spree/api/v1/products_controller_spec.rb +6 -6
  34. data/spec/controllers/spree/api/v1/shipments_controller_spec.rb +1 -1
  35. data/spec/controllers/spree/api/v1/taxonomies_controller_spec.rb +88 -0
  36. data/spec/controllers/spree/api/v1/taxons_controller_spec.rb +82 -0
  37. data/spec/controllers/spree/api/v1/variants_controller_spec.rb +2 -2
  38. data/spec/spec_helper.rb +1 -0
  39. metadata +38 -17
  40. data/README.md +0 -29
@@ -0,0 +1,17 @@
1
+ Spree::Admin::UsersController.class_eval do
2
+ before_filter :load_roles, :only => [:edit, :new, :update, :create, :generate_api_key, :clear_api_key]
3
+
4
+ def generate_api_key
5
+ if @user.generate_api_key!
6
+ flash.notice = t('key_generated', :scope => 'spree.api')
7
+ end
8
+ redirect_to edit_admin_user_path(@user)
9
+ end
10
+
11
+ def clear_api_key
12
+ if @user.clear_api_key!
13
+ flash.notice = t('key_cleared', :scope => 'spree.api')
14
+ end
15
+ redirect_to edit_admin_user_path(@user)
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class AddressesController < Spree::Api::V1::BaseController
5
+ def show
6
+ @address = Address.find(params[:id])
7
+ end
8
+
9
+ def update
10
+ @address = Address.find(params[:id])
11
+ @address.update_attributes(params[:address])
12
+ render :show, :status => 200
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -2,44 +2,27 @@ module Spree
2
2
  module Api
3
3
  module V1
4
4
  class ImagesController < Spree::Api::V1::BaseController
5
+ def show
6
+ @image = Image.find(params[:id])
7
+ end
8
+
5
9
  def create
6
- @image = product_or_variant.images.create!(params[:image])
10
+ @image = Image.create(params[:image])
7
11
  render :show, :status => 201
8
12
  end
9
13
 
10
14
  def update
11
- image.update_attributes(params[:image])
15
+ @image = Image.find(params[:id])
16
+ @image.update_attributes(params[:image])
12
17
  render :show, :status => 200
13
18
  end
14
19
 
15
20
  def destroy
16
- image.destroy
21
+ @image = Image.find(params[:id])
22
+ @image.destroy
17
23
  render :text => nil
18
24
  end
19
25
 
20
- private
21
-
22
- def image
23
- @image = product_or_variant.images.find(params[:id])
24
- end
25
-
26
- def product_or_variant
27
- return @product_or_variant if @product_or_variant
28
- if params[:product_id]
29
- @product_or_variant = product
30
- else
31
- @product_or_variant = variant
32
- end
33
- end
34
-
35
- def variant
36
- Variant.find(params[:variant_id])
37
- end
38
-
39
- def product
40
- find_product(params[:product_id]).master
41
- end
42
-
43
26
  end
44
27
  end
45
28
  end
@@ -14,6 +14,11 @@ module Spree
14
14
  authorize! :read, order
15
15
  end
16
16
 
17
+ def search
18
+ @orders = Order.search(params[:q]).result.page(params[:page])
19
+ render :index
20
+ end
21
+
17
22
  def create
18
23
  @order = Order.build_from_api(current_api_user, @nested_params)
19
24
  next!
@@ -0,0 +1,47 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class TaxonomiesController < Spree::Api::V1::BaseController
5
+ def index
6
+ @taxonomies = Taxonomy.order('name').includes(:root => :children)
7
+ end
8
+
9
+ def show
10
+ @taxonomy = Taxonomy.find(params[:id])
11
+ end
12
+
13
+ def create
14
+ authorize! :create, Taxonomy
15
+ @taxonomy = Taxonomy.new(params[:taxonomy])
16
+ if @taxonomy.save
17
+ render :show, :status => 201
18
+ else
19
+ invalid_resource!(@taxonomy)
20
+ end
21
+ end
22
+
23
+ def update
24
+ authorize! :update, Taxonomy
25
+ if taxonomy.update_attributes(params[:taxonomy])
26
+ render :show, :status => 200
27
+ else
28
+ invalid_resource!(taxonomy)
29
+ end
30
+ end
31
+
32
+ def destroy
33
+ authorize! :delete, Taxonomy
34
+ taxonomy.destroy
35
+ render :text => nil, :status => 200
36
+ end
37
+
38
+ private
39
+
40
+ def taxonomy
41
+ @taxonomy ||= Taxonomy.find(params[:id])
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,51 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class TaxonsController < Spree::Api::V1::BaseController
5
+ def index
6
+ @taxons = taxonomy.root.children
7
+ end
8
+
9
+ def show
10
+ @taxon = taxon
11
+ end
12
+
13
+ def create
14
+ authorize! :create, Taxon
15
+ @taxon = Taxon.new(params[:taxon])
16
+ if @taxon.save
17
+ render :show, :status => 201
18
+ else
19
+ invalid_resource!(@taxon)
20
+ end
21
+ end
22
+
23
+ def update
24
+ authorize! :update, Taxon
25
+ if taxon.update_attributes(params[:taxon])
26
+ render :show, :status => 200
27
+ else
28
+ invalid_resource!(taxon)
29
+ end
30
+ end
31
+
32
+ def destroy
33
+ authorize! :delete, Taxon
34
+ taxon.destroy
35
+ render :text => nil, :status => 200
36
+ end
37
+
38
+ private
39
+
40
+ def taxonomy
41
+ @taxonomy ||= Taxonomy.find(params[:taxonomy_id])
42
+ end
43
+
44
+ def taxon
45
+ @taxon ||= taxonomy.taxons.find(params[:id])
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+ end
@@ -12,8 +12,8 @@ module Spree
12
12
  end
13
13
 
14
14
  def product_attributes
15
- [:id, :name, :description, :price,
16
- :available_on, :permalink, :count_on_hand, :meta_description, :meta_keywords]
15
+ [:id, :name, :description, :price, :available_on, :permalink,
16
+ :count_on_hand, :meta_description, :meta_keywords, :taxon_ids]
17
17
  end
18
18
 
19
19
  def variant_attributes
@@ -51,6 +51,14 @@ module Spree
51
51
  def shipment_attributes
52
52
  [:id, :tracking, :number, :cost, :shipped_at, :state]
53
53
  end
54
+
55
+ def taxonomy_attributes
56
+ [:id, :name]
57
+ end
58
+
59
+ def taxon_attributes
60
+ [:id, :name, :permalink, :position, :parent_id, :taxonomy_id]
61
+ end
54
62
  end
55
63
  end
56
64
  end
@@ -0,0 +1,6 @@
1
+ Deface::Override.new(:virtual_path => "spree/admin/users/edit",
2
+ :name => "api_admin_user_edit_form",
3
+ :insert_after => "[data-hook='admin_user_edit_form'], #admin_user_edit_form[data-hook]",
4
+ :partial => "spree/admin/users/api_fields",
5
+ :disabled => false)
6
+
@@ -0,0 +1,20 @@
1
+ <h2><%= t('access', :scope => 'spree.api') %></h2>
2
+
3
+ <% if @user.api_key.present? %>
4
+ <p><strong><%= t('key', :scope => 'spree.api') %></strong> <%= @user.api_key %></p>
5
+
6
+ <%= form_tag spree.clear_api_key_admin_user_path(@user), :method => :put do %>
7
+ <%= button t('clear_key', :scope => 'spree.api') %>
8
+ <% end %>
9
+
10
+ <%= form_tag spree.generate_api_key_admin_user_path(@user), :method => :put do %>
11
+ <%= button t('regenerate_key', :scope => 'spree.api') %>
12
+ <% end %>
13
+
14
+ <% else %>
15
+
16
+ <p><%= t('no_key', :scope => 'spree.api') %></p>
17
+ <%= form_tag spree.generate_api_key_admin_user_path(@user), :method => :put do %>
18
+ <%= button t('generate_key', :scope => 'spree.api') %>
19
+ <% end %>
20
+ <% end %>
@@ -0,0 +1,11 @@
1
+ object @address
2
+ attributes :id, :firstname, :lastname, :address1, :address2,
3
+ :city, :zipcode, :phone,
4
+ :company, :alternative_phone, :country_id, :state_id,
5
+ :state_name
6
+ child(:country) do |address|
7
+ attributes :id, :iso_name, :iso, :iso3, :name, :numcode
8
+ end
9
+ child(:state) do |address|
10
+ attributes :abbr, :country_id, :id, :name
11
+ end
@@ -1,2 +1,3 @@
1
1
  object @image
2
2
  attributes *image_attributes
3
+ attributes :viewable_type, :viewable_id
@@ -1,3 +0,0 @@
1
- attributes :id, :firstname, :lastname, :address1, :address2,
2
- :city, :zipcode, :country, :state, :phone, :state_name,
3
- :company, :alternative_phone, :country_id, :state_id
@@ -3,11 +3,11 @@ attributes *order_attributes
3
3
  extends "spree/api/v1/orders/#{@order.state}"
4
4
 
5
5
  child :billing_address => :bill_address do
6
- extends "spree/api/v1/orders/address"
6
+ extends "spree/api/v1/addresses/show"
7
7
  end
8
8
 
9
9
  child :shipping_address => :ship_address do
10
- extends "spree/api/v1/orders/address"
10
+ extends "spree/api/v1/addresses/show"
11
11
  end
12
12
 
13
13
  child :line_items => :line_items do
@@ -23,4 +23,4 @@ end
23
23
 
24
24
  child :shipments => :shipments do
25
25
  extends "spree/api/v1/shipments/show"
26
- end
26
+ end
@@ -9,7 +9,7 @@ child :variants_including_master => :variants do
9
9
  end
10
10
 
11
11
  child :images => :images do
12
- attributes *image_attributes
12
+ extends "spree/api/v1/images/show"
13
13
  end
14
14
 
15
15
  child :option_types => :option_types do
@@ -0,0 +1,2 @@
1
+ collection @taxonomies
2
+ extends "spree/api/v1/taxonomies/show"
@@ -0,0 +1,11 @@
1
+ attributes *taxonomy_attributes
2
+
3
+ child :root => :root do
4
+ attributes *taxon_attributes
5
+
6
+ child :children => :taxons do
7
+ attributes *taxon_attributes
8
+
9
+ extends "spree/api/v1/taxons/taxons"
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ object false
2
+ node(:attributes) { [*taxonomy_attributes] }
3
+ node(:required_attributes) { required_fields_for(Spree::Taxonomy) }
@@ -0,0 +1,15 @@
1
+ object @taxonomy
2
+
3
+ if set = params[:set]
4
+ extends "spree/api/v1/taxonomies/#{set}"
5
+ else
6
+ attributes *taxonomy_attributes
7
+
8
+ child :root => :root do
9
+ attributes *taxon_attributes
10
+
11
+ child :children => :taxons do
12
+ attributes *taxon_attributes
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ collection @taxons
2
+ attributes *taxon_attributes
3
+
4
+ extends "spree/api/v1/taxons/taxons"
@@ -0,0 +1,3 @@
1
+ object false
2
+ node(:attributes) { [*taxon_attributes] }
3
+ node(:required_attributes) { required_fields_for(Spree::Taxon) }
@@ -0,0 +1,8 @@
1
+ object @taxon
2
+ attributes *taxon_attributes
3
+
4
+ node do |t|
5
+ child t.children => :taxons do
6
+ attributes *taxon_attributes
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ node do |t|
2
+ child t.children => :taxons do
3
+ attributes *taxon_attributes
4
+
5
+ extends "spree/api/v1/taxons/taxons"
6
+ end
7
+ end
@@ -0,0 +1 @@
1
+ Spree::Api::V1::BaseController.append_view_path(ApplicationController.view_paths)
@@ -8,7 +8,14 @@ en:
8
8
  resource_not_found: "The resource you were looking for could not be found."
9
9
  gateway_error: "There was a problem with the payment gateway: %{text}"
10
10
  credit_over_limit: "This payment can only be credited up to %{limit}. Please specify an amount less than or equal to this number."
11
-
11
+ access: "API Access"
12
+ key: "Key"
13
+ clear_key: "Clear key"
14
+ regenerate_key: "Regenerate Key"
15
+ no_key: "No key"
16
+ generate_key: "Generate API key"
17
+ key_generated: "Key generated"
18
+ key_cleared: "Key cleared"
12
19
  order:
13
20
  could_not_transition: "The order could not be transitioned. Please fix the errors and try again."
14
21
  invalid_shipping_method: "Invalid shipping method specified."
data/config/routes.rb CHANGED
@@ -1,4 +1,13 @@
1
1
  Spree::Core::Engine.routes.prepend do
2
+ namespace :admin do
3
+ resources :users do
4
+ member do
5
+ put :generate_api_key
6
+ put :clear_api_key
7
+ end
8
+ end
9
+ end
10
+
2
11
  namespace :api do
3
12
  scope :module => :v1 do
4
13
  resources :products do
@@ -7,14 +16,17 @@ Spree::Core::Engine.routes.prepend do
7
16
  end
8
17
 
9
18
  resources :variants
10
- resources :images
11
19
  end
12
20
 
21
+ resources :images
22
+
13
23
  resources :variants, :only => [:index] do
14
- resources :images
15
24
  end
16
25
 
17
26
  resources :orders do
27
+ collection do
28
+ get :search
29
+ end
18
30
  member do
19
31
  put :address
20
32
  put :delivery
@@ -40,6 +52,10 @@ Spree::Core::Engine.routes.prepend do
40
52
  end
41
53
 
42
54
  resources :countries, :only => [:index, :show]
55
+ resources :addresses, :only => [:show, :update]
56
+ resources :taxonomies do
57
+ resources :taxons
58
+ end
43
59
  end
44
60
  end
45
61
  end
@@ -17,7 +17,6 @@ module Spree
17
17
 
18
18
  include CanCan::ControllerAdditions
19
19
  append_view_path File.expand_path("../../../app/views", File.dirname(__FILE__))
20
- append_view_path Rails.root + "app/views"
21
20
 
22
21
  respond_to :json
23
22
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::V1::AddressesController do
5
+ render_views
6
+
7
+ before do
8
+ stub_authentication!
9
+ @address = create(:address)
10
+ end
11
+
12
+ it "gets an address" do
13
+ api_get :show, :id => @address.id
14
+ json_response['address']['address1'].should eq @address.address1
15
+ end
16
+
17
+ it "updates an address" do
18
+ api_put :update, :id => @address.id,
19
+ :address => { :address1 => "123 Test Lane" }
20
+ json_response['address']['address1'].should eq '123 Test Lane'
21
+ end
22
+ end
23
+ end
@@ -6,7 +6,7 @@ module Spree
6
6
 
7
7
  before do
8
8
  stub_authentication!
9
- @state = Factory(:state)
9
+ @state = create(:state)
10
10
  @country = @state.country
11
11
  end
12
12
 
@@ -4,9 +4,9 @@ module Spree
4
4
  describe Spree::Api::V1::ImagesController do
5
5
  render_views
6
6
 
7
- let!(:product) { Factory(:product) }
8
- let!(:attributes) { [:id, :position, :attachment_content_type,
9
- :attachment_file_name, :type, :attachment_updated_at, :attachment_width,
7
+ let!(:product) { create(:product) }
8
+ let!(:attributes) { [:id, :position, :attachment_content_type,
9
+ :attachment_file_name, :type, :attachment_updated_at, :attachment_width,
10
10
  :attachment_height, :alt] }
11
11
 
12
12
  before do
@@ -14,19 +14,25 @@ module Spree
14
14
  end
15
15
 
16
16
  it "can upload a new image for a product" do
17
- product.images.count.should == 0
18
- api_post :create, :product_id => product.to_param, :image => { :attachment => upload_image("thinking-cat.jpg") }
19
- response.status.should == 201
20
- json_response.should have_attributes(attributes)
21
- product.images.count.should == 1
17
+ lambda do
18
+ api_post :create,
19
+ :image => { :attachment => upload_image("thinking-cat.jpg"),
20
+ :viewable_type => 'Spree::Product',
21
+ :viewable_id => product.id }
22
+ response.status.should == 201
23
+ json_response.should have_attributes(attributes)
24
+ end.should change(Image, :count).by(1)
22
25
  end
23
26
 
24
27
  it "can upload a new image for a variant" do
25
- product.master.images.count.should == 0
26
- api_post :create, :variant_id => product.master.to_param, :image => { :attachment => upload_image("thinking-cat.jpg") }
27
- response.status.should == 201
28
- json_response.should have_attributes(attributes)
29
- product.images.count.should == 1
28
+ lambda do
29
+ api_post :create,
30
+ :image => { :attachment => upload_image("thinking-cat.jpg"),
31
+ :viewable_type => 'Spree::Variant',
32
+ :viewable_id => product.master.to_param }
33
+ response.status.should == 201
34
+ json_response.should have_attributes(attributes)
35
+ end.should change(Image, :count).by(1)
30
36
  end
31
37
 
32
38
  context "working with an existing image" do
@@ -34,14 +40,14 @@ module Spree
34
40
 
35
41
  it "can update image data" do
36
42
  product_image.position.should == 1
37
- api_post :update, :variant_id => product.master.to_param, :image => { :position => 2 }, :id => product_image.id
43
+ api_post :update, :image => { :position => 2 }, :id => product_image.id
38
44
  response.status.should == 200
39
45
  json_response.should have_attributes(attributes)
40
46
  product_image.reload.position.should == 2
41
47
  end
42
48
 
43
49
  it "can delete an image" do
44
- api_delete :destroy, :variant_id => product.master.to_param, :id => product_image.id
50
+ api_delete :destroy, :id => product_image.id
45
51
  response.status.should == 200
46
52
  lambda { product_image.reload }.should raise_error(ActiveRecord::RecordNotFound)
47
53
  end
@@ -5,12 +5,12 @@ module Spree
5
5
  render_views
6
6
 
7
7
  let!(:order) do
8
- order = Factory(:order)
9
- order.line_items << Factory(:line_item)
8
+ order = create(:order)
9
+ order.line_items << create(:line_item)
10
10
  order
11
11
  end
12
12
 
13
- let(:product) { Factory(:product) }
13
+ let(:product) { create(:product) }
14
14
  let(:attributes) { [:id, :quantity, :price, :variant] }
15
15
  let(:resource_scoping) { { :order_id => order.to_param } }
16
16
 
@@ -4,7 +4,7 @@ module Spree
4
4
  describe Api::V1::OrdersController do
5
5
  render_views
6
6
 
7
- let!(:order) { Factory(:order) }
7
+ let!(:order) { create(:order) }
8
8
  let(:attributes) { [:number, :item_total, :total,
9
9
  :state, :adjustment_total, :credit_total,
10
10
  :user_id, :created_at, :updated_at,
@@ -35,7 +35,7 @@ module Spree
35
35
  end
36
36
 
37
37
  it "can create an order" do
38
- variant = Factory(:variant)
38
+ variant = create(:variant)
39
39
  api_post :create, :order => { :line_items => [{ :variant_id => variant.to_param, :quantity => 5 }] }
40
40
  response.status.should == 200
41
41
  order = Order.last
@@ -47,7 +47,7 @@ module Spree
47
47
 
48
48
  context "working with an order" do
49
49
  before do
50
- Factory(:payment_method)
50
+ create(:payment_method)
51
51
  order.next # Switch from cart to address
52
52
  order.ship_address.should be_nil
53
53
  order.state.should == "address"
@@ -60,10 +60,10 @@ module Spree
60
60
  end
61
61
 
62
62
  let(:address_params) { { :country_id => Country.first.id, :state_id => State.first.id } }
63
- let(:shipping_address) { clean_address(Factory.attributes_for(:address).merge!(address_params)) }
64
- let(:billing_address) { clean_address(Factory.attributes_for(:address).merge!(address_params)) }
65
- let!(:shipping_method) { Factory(:shipping_method) }
66
- let!(:payment_method) { Factory(:payment_method) }
63
+ let(:shipping_address) { clean_address(attributes_for(:address).merge!(address_params)) }
64
+ let(:billing_address) { clean_address(attributes_for(:address).merge!(address_params)) }
65
+ let!(:shipping_method) { create(:shipping_method) }
66
+ let!(:payment_method) { create(:payment_method) }
67
67
 
68
68
  it "can add address information to an order" do
69
69
  api_put :address, :id => order.to_param, :shipping_address => shipping_address, :billing_address => billing_address
@@ -104,7 +104,7 @@ module Spree
104
104
 
105
105
  context "with a line item" do
106
106
  before do
107
- order.line_items << Factory(:line_item)
107
+ order.line_items << create(:line_item)
108
108
  end
109
109
 
110
110
  context "for delivery" do
@@ -2,8 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  module Spree
4
4
  describe Spree::Api::V1::PaymentsController do
5
- let!(:order) { Factory(:order) }
6
- let!(:payment) { Factory(:payment, :order => order) }
5
+ let!(:order) { create(:order) }
6
+ let!(:payment) { create(:payment, :order => order) }
7
7
  let!(:attributes) { [:id, :source_type, :source_id, :amount,
8
8
  :payment_method_id, :response_code, :state, :avs_response,
9
9
  :created_at, :updated_at] }
@@ -4,9 +4,9 @@ module Spree
4
4
  describe Spree::Api::V1::ProductsController do
5
5
  render_views
6
6
 
7
- let!(:product) { Factory(:product) }
8
- let!(:inactive_product) { Factory(:product, :available_on => Time.now.tomorrow, :name => "inactive") }
9
- let(:attributes) { [:id, :name, :description, :price, :available_on, :permalink, :count_on_hand, :meta_description, :meta_keywords] }
7
+ let!(:product) { create(:product) }
8
+ let!(:inactive_product) { create(:product, :available_on => Time.now.tomorrow, :name => "inactive") }
9
+ let(:attributes) { [:id, :name, :description, :price, :available_on, :permalink, :count_on_hand, :meta_description, :meta_keywords, :taxon_ids] }
10
10
 
11
11
  before do
12
12
  stub_authentication!
@@ -30,7 +30,7 @@ module Spree
30
30
  default_per_page(1)
31
31
 
32
32
  it "can select the next page of products" do
33
- second_product = Factory(:product)
33
+ second_product = create(:product)
34
34
  api_get :index, :page => 2
35
35
  json_response["products"].first.should have_attributes(attributes)
36
36
  json_response["count"].should == 2
@@ -40,7 +40,7 @@ module Spree
40
40
  end
41
41
 
42
42
  it "can search for products" do
43
- Factory(:product, :name => "The best product in the world")
43
+ create(:product, :name => "The best product in the world")
44
44
  api_get :search, :q => { :name_cont => "best" }
45
45
  json_response["products"].first.should have_attributes(attributes)
46
46
  json_response["count"].should == 1
@@ -64,7 +64,7 @@ module Spree
64
64
 
65
65
 
66
66
  context "finds a product by permalink first then by id" do
67
- let!(:other_product) { Factory(:product, :permalink => "these-are-not-the-droids-you-are-looking-for") }
67
+ let!(:other_product) { create(:product, :permalink => "these-are-not-the-droids-you-are-looking-for") }
68
68
 
69
69
  before do
70
70
  product.update_attribute(:permalink, "#{other_product.id}-and-1-ways")
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Spree::Api::V1::ShipmentsController do
4
- let!(:shipment) { Factory(:shipment) }
4
+ let!(:shipment) { create(:shipment) }
5
5
  let!(:attributes) { [:id, :tracking, :number, :cost, :shipped_at] }
6
6
 
7
7
  before do
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::V1::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.first['taxonomy']['name'].should eq taxonomy.name
24
+ json_response.first['taxonomy']['root']['taxons'].count.should eq 1
25
+ end
26
+
27
+ it "gets a single taxonomy" do
28
+ api_get :show, :id => taxonomy.id
29
+
30
+ json_response['taxonomy']['name'].should eq taxonomy.name
31
+
32
+ children = json_response['taxonomy']['root']['taxons']
33
+ children.count.should eq 1
34
+ children.first['taxon']['name'].should eq taxon.name
35
+ children.first['taxon'].key?('taxons').should be_false
36
+ end
37
+
38
+ it "gets a single taxonomy with set=nested" do
39
+ api_get :show, :id => taxonomy.id, :set => 'nested'
40
+
41
+ json_response['taxonomy']['name'].should eq taxonomy.name
42
+
43
+ children = json_response['taxonomy']['root']['taxons']
44
+ children.first['taxon'].key?('taxons').should be_true
45
+ end
46
+
47
+ it "can learn how to create a new taxonomy" do
48
+ api_get :new
49
+ json_response["attributes"].should == attributes.map(&:to_s)
50
+ required_attributes = json_response["required_attributes"]
51
+ required_attributes.should include("name")
52
+ end
53
+
54
+ it "cannot create a new taxonomy if not an admin" do
55
+ api_post :create, :taxonomy => { :name => "Location" }
56
+ assert_unauthorized!
57
+ end
58
+
59
+ it "cannot update a taxonomy" do
60
+ api_put :update, :id => taxonomy.id, :taxonomy => { :name => "I hacked your store!" }
61
+ assert_unauthorized!
62
+ end
63
+
64
+ it "cannot delete a taxonomy" do
65
+ api_delete :destroy, :id => taxonomy.id
66
+ assert_unauthorized!
67
+ end
68
+ end
69
+
70
+ context "as an admin" do
71
+ sign_in_as_admin!
72
+
73
+ it "can create" do
74
+ api_post :create, :taxonomy => { :name => "Colors"}
75
+ json_response.should have_attributes(attributes)
76
+ response.status.should == 201
77
+ end
78
+
79
+ it "cannot create a new taxonomy with invalid attributes" do
80
+ api_post :create, :taxonomy => {}
81
+ response.status.should == 422
82
+ json_response["error"].should == "Invalid resource. Please fix errors and try again."
83
+ errors = json_response["errors"]
84
+ end
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::V1::TaxonsController do
5
+ render_views
6
+
7
+ let(:taxonomy) { Factory(:taxonomy) }
8
+ let(:taxon) { Factory(:taxon, :name => "Ruby", :taxonomy => taxonomy) }
9
+ let(:taxon2) { Factory(: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['taxon']['name'].should eq taxon.name
24
+ children = json_response.first['taxon']['taxons']
25
+ children.count.should eq 1
26
+ children.first['taxon']['name'].should eq taxon2.name
27
+ children.first['taxon']['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['taxon']['name'].should eq taxon.name
34
+ json_response['taxon']['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
+ end
80
+
81
+ end
82
+ end
@@ -5,10 +5,10 @@ module Spree
5
5
  render_views
6
6
 
7
7
 
8
- let!(:product) { Factory(:product) }
8
+ let!(:product) { create(:product) }
9
9
  let!(:variant) do
10
10
  variant = product.master
11
- variant.option_values << Factory(:option_value)
11
+ variant.option_values << create(:option_value)
12
12
  variant
13
13
  end
14
14
  let!(:attributes) { [:id, :name, :count_on_hand,
data/spec/spec_helper.rb CHANGED
@@ -16,6 +16,7 @@ require 'spree/api/testing_support/setup'
16
16
  RSpec.configure do |config|
17
17
  config.backtrace_clean_patterns = [/gems\/activesupport/, /gems\/actionpack/, /gems\/rspec/]
18
18
 
19
+ config.include FactoryGirl::Syntax::Methods
19
20
  config.include Spree::Api::TestingSupport::Helpers, :type => :controller
20
21
  config.extend Spree::Api::TestingSupport::Setup, :type => :controller
21
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,33 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-28 00:00:00.000000000 Z
12
+ date: 2012-05-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spree_core
16
- requirement: &70116387070120 !ruby/object:Gem::Requirement
16
+ requirement: &70243020240540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
20
20
  - !ruby/object:Gem::Version
21
- version: 1.1.0
21
+ version: 1.1.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70116387070120
24
+ version_requirements: *70243020240540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: spree_auth
27
- requirement: &70116387068680 !ruby/object:Gem::Requirement
27
+ requirement: &70243020240060 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
31
31
  - !ruby/object:Gem::Version
32
- version: 1.1.0
32
+ version: 1.1.1
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70116387068680
35
+ version_requirements: *70243020240060
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rabl
38
- requirement: &70116387015640 !ruby/object:Gem::Requirement
38
+ requirement: &70243020239600 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - =
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.6.5
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70116387015640
46
+ version_requirements: *70243020239600
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec-rails
49
- requirement: &70116387011680 !ruby/object:Gem::Requirement
49
+ requirement: &70243020238960 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - =
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.9.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70116387011680
57
+ version_requirements: *70243020238960
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: database_cleaner
60
- requirement: &70116387010380 !ruby/object:Gem::Requirement
60
+ requirement: &70243020238580 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70116387010380
68
+ version_requirements: *70243020238580
69
69
  description: Spree's API
70
70
  email:
71
71
  - ryan@spreecommerce.com
@@ -76,8 +76,9 @@ files:
76
76
  - .gitignore
77
77
  - Gemfile
78
78
  - LICENSE
79
- - README.md
80
79
  - Rakefile
80
+ - app/controllers/spree/admin/users_controller_decorator.rb
81
+ - app/controllers/spree/api/v1/addresses_controller.rb
81
82
  - app/controllers/spree/api/v1/base_controller.rb
82
83
  - app/controllers/spree/api/v1/countries_controller.rb
83
84
  - app/controllers/spree/api/v1/images_controller.rb
@@ -86,6 +87,8 @@ files:
86
87
  - app/controllers/spree/api/v1/payments_controller.rb
87
88
  - app/controllers/spree/api/v1/products_controller.rb
88
89
  - app/controllers/spree/api/v1/shipments_controller.rb
90
+ - app/controllers/spree/api/v1/taxonomies_controller.rb
91
+ - app/controllers/spree/api/v1/taxons_controller.rb
89
92
  - app/controllers/spree/api/v1/variants_controller.rb
90
93
  - app/helpers/spree/api/api_helpers.rb
91
94
  - app/models/spree/line_item_decorator.rb
@@ -93,6 +96,9 @@ files:
93
96
  - app/models/spree/order_decorator.rb
94
97
  - app/models/spree/payment_decorator.rb
95
98
  - app/models/spree/user_decorator.rb
99
+ - app/overrides/api_admin_user_edit_form.rb
100
+ - app/views/spree/admin/users/_api_fields.html.erb
101
+ - app/views/spree/api/v1/addresses/show.rabl
96
102
  - app/views/spree/api/v1/countries/index.rabl
97
103
  - app/views/spree/api/v1/countries/show.rabl
98
104
  - app/views/spree/api/v1/errors/gateway_error.rabl
@@ -123,10 +129,19 @@ files:
123
129
  - app/views/spree/api/v1/products/product.rabl
124
130
  - app/views/spree/api/v1/products/show.rabl
125
131
  - app/views/spree/api/v1/shipments/show.rabl
132
+ - app/views/spree/api/v1/taxonomies/index.rabl
133
+ - app/views/spree/api/v1/taxonomies/nested.rabl
134
+ - app/views/spree/api/v1/taxonomies/new.rabl
135
+ - app/views/spree/api/v1/taxonomies/show.rabl
136
+ - app/views/spree/api/v1/taxons/index.rabl
137
+ - app/views/spree/api/v1/taxons/new.rabl
138
+ - app/views/spree/api/v1/taxons/show.rabl
139
+ - app/views/spree/api/v1/taxons/taxons.rabl
126
140
  - app/views/spree/api/v1/variants/index.rabl
127
141
  - app/views/spree/api/v1/variants/new.rabl
128
142
  - app/views/spree/api/v1/variants/show.rabl
129
143
  - app/views/spree/api/v1/variants/variant.rabl
144
+ - config/initializers/metal_load_paths.rb
130
145
  - config/locales/en.yml
131
146
  - config/routes.rb
132
147
  - db/migrate/20100107141738_add_api_key_to_spree_users.rb
@@ -139,6 +154,7 @@ files:
139
154
  - lib/spree/api/version.rb
140
155
  - lib/spree_api.rb
141
156
  - script/rails
157
+ - spec/controllers/spree/api/v1/addresses_controller_spec.rb
142
158
  - spec/controllers/spree/api/v1/base_controller_spec.rb
143
159
  - spec/controllers/spree/api/v1/countries_controller_spec.rb
144
160
  - spec/controllers/spree/api/v1/images_controller_spec.rb
@@ -147,6 +163,8 @@ files:
147
163
  - spec/controllers/spree/api/v1/payments_controller_spec.rb
148
164
  - spec/controllers/spree/api/v1/products_controller_spec.rb
149
165
  - spec/controllers/spree/api/v1/shipments_controller_spec.rb
166
+ - spec/controllers/spree/api/v1/taxonomies_controller_spec.rb
167
+ - spec/controllers/spree/api/v1/taxons_controller_spec.rb
150
168
  - spec/controllers/spree/api/v1/variants_controller_spec.rb
151
169
  - spec/fixtures/thinking-cat.jpg
152
170
  - spec/models/spree/order_spec.rb
@@ -170,7 +188,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
170
188
  version: '0'
171
189
  segments:
172
190
  - 0
173
- hash: 618732053223266577
191
+ hash: -3593481641416743509
174
192
  required_rubygems_version: !ruby/object:Gem::Requirement
175
193
  none: false
176
194
  requirements:
@@ -179,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
197
  version: '0'
180
198
  segments:
181
199
  - 0
182
- hash: 618732053223266577
200
+ hash: -3593481641416743509
183
201
  requirements: []
184
202
  rubyforge_project:
185
203
  rubygems_version: 1.8.10
@@ -187,6 +205,7 @@ signing_key:
187
205
  specification_version: 3
188
206
  summary: Spree's API
189
207
  test_files:
208
+ - spec/controllers/spree/api/v1/addresses_controller_spec.rb
190
209
  - spec/controllers/spree/api/v1/base_controller_spec.rb
191
210
  - spec/controllers/spree/api/v1/countries_controller_spec.rb
192
211
  - spec/controllers/spree/api/v1/images_controller_spec.rb
@@ -195,6 +214,8 @@ test_files:
195
214
  - spec/controllers/spree/api/v1/payments_controller_spec.rb
196
215
  - spec/controllers/spree/api/v1/products_controller_spec.rb
197
216
  - spec/controllers/spree/api/v1/shipments_controller_spec.rb
217
+ - spec/controllers/spree/api/v1/taxonomies_controller_spec.rb
218
+ - spec/controllers/spree/api/v1/taxons_controller_spec.rb
198
219
  - spec/controllers/spree/api/v1/variants_controller_spec.rb
199
220
  - spec/fixtures/thinking-cat.jpg
200
221
  - spec/models/spree/order_spec.rb
data/README.md DELETED
@@ -1,29 +0,0 @@
1
- # Api
2
-
3
- TODO: Write a gem description
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'api'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install api
18
-
19
- ## Usage
20
-
21
- TODO: Write usage instructions here
22
-
23
- ## Contributing
24
-
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Added some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request