Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# encoding: utf-8 | ||
module Api::V1 | ||
class ProductVariantsController < AppController | ||
doorkeeper_for :update, scopes: [:write_products], unless: lambda { @api_client } | ||
|
||
def update | ||
@variant = shop.variants.find(params[:id]) | ||
@variant.update_attributes params[:variant].except(:id) | ||
render :show | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
object @variant => :variant | ||
attributes :id, :product_id, :price, :compare_at_price, :weight, :sku, :position, :option1, :option2, :option3, :requires_shipping, :inventory_quantity, :inventory_management, :inventory_policy, :created_at, :updated_at |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
collection @products, root: :products, object_root: false | ||
attributes :id, :title, :body_html, :handle, :product_type, :vendor, :created_at, :updated_at | ||
extends "api/v1/products/show" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
object @product | ||
attributes :id, :title, :body_html, :handle, :product_type, :vendor, :created_at, :updated_at | ||
|
||
child :variants => :variants do | ||
extends "api/v1/product_variants/show" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
spec/controllers/api/v1/product_variants_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#encoding: utf-8 | ||
require 'spec_helper' | ||
|
||
describe Api::V1::ProductVariantsController do | ||
|
||
let(:shop) { Factory(:user).shop } | ||
|
||
let(:iphone4) { Factory :iphone4, shop: shop } | ||
|
||
let(:variant) { iphone4.variants.first } | ||
|
||
let(:body) { {id: variant.id, price: 45 }} # modify to | ||
|
||
before :each do | ||
request.host = "#{shop.primary_domain.host}" | ||
end | ||
|
||
context '#update' do | ||
|
||
before { [iphone4] } | ||
|
||
let(:application) { Factory :express_application } # OAuth application | ||
|
||
context 'with scopes' do # 资源访问范围匹配 | ||
|
||
let(:token) { Factory :access_token, application: application, resource_owner_id: shop.id, scopes: "write_products" } | ||
|
||
it 'should be success' do | ||
put :update, id: variant.id, variant: body, format: :json, access_token: token.token | ||
response.should be_success | ||
json = JSON(response.body)['variant'] | ||
pattern = { | ||
id: variant.id, | ||
product_id: iphone4.id, | ||
price: 45.0, | ||
compare_at_price: 3500.0, | ||
weight: 2.9, | ||
sku: "APPLE1000", | ||
position: 1, | ||
option1: "默认标题", | ||
option2: nil, | ||
option3: nil, | ||
requires_shipping: true, | ||
inventory_quantity: nil, | ||
inventory_management: nil, | ||
inventory_policy: "deny", | ||
created_at: WILDCARD_MATCHER, | ||
updated_at: WILDCARD_MATCHER | ||
} | ||
json.should match_json_expression(pattern) | ||
end | ||
|
||
end | ||
|
||
context 'without scopes' do # 资源访问范围不匹配 | ||
|
||
let(:token) { Factory :access_token, application: application, resource_owner_id: shop.id } | ||
|
||
it 'should be fail' do # 认证失败 | ||
put :update, id: variant.id, variant: body, format: :json, access_token: token.token | ||
response.should_not be_success | ||
response.status.should eql 401 | ||
end | ||
|
||
end | ||
|
||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters