forked from MONEI/Shopify-api-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request MONEI#66 from lpinca/add/asset
Add Asset resource
- Loading branch information
Showing
9 changed files
with
457 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 +1,28 @@ | ||
class Asset | ||
BaseChild = require './base_child' | ||
|
||
class Asset extends BaseChild | ||
parent: '/themes' | ||
child: '/assets' | ||
slug: 'asset' | ||
|
||
constructor: (site) -> | ||
super(site) | ||
|
||
create: (parentId, fields, callback) -> | ||
url = @resource.queryString "#{@prefix}/#{parentId}#{@child}" | ||
@resource.put url, @slug, fields, callback | ||
|
||
update: @::create | ||
|
||
get: (parentId, params, callback) -> | ||
params.asset || (params.asset = { key: params.key }) | ||
delete params.key | ||
|
||
url = @resource.queryString "#{@prefix}/#{parentId}#{@child}", params | ||
@resource.get url, @slug, callback | ||
|
||
delete: (parentId, key, callback) -> | ||
url = @resource.queryString "#{@prefix}/#{parentId}#{@child}", { asset: { key: key } } | ||
@resource.delete url, key, callback | ||
|
||
module.exports = Asset |
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,128 @@ | ||
var common = require('./common.js') | ||
, scope = common.nock(common.test_shop) | ||
, fixtures = {} | ||
, resource; | ||
|
||
common.setObject('asset'); | ||
|
||
[ | ||
'allResponseBody', | ||
'singleResponseBody', | ||
'createRequestBody', | ||
'createResponseBody', | ||
'updateRequestBody', | ||
'updateResponseBody' | ||
].forEach(function (fixture) { | ||
fixtures[fixture] = common.load(fixture); | ||
}); | ||
|
||
resource = new (common.resource())(common.endpoint); | ||
|
||
describe('Asset', function () { | ||
it('should get a list of all assets', function (next) { | ||
var resBody = fixtures.allResponseBody; | ||
|
||
scope.get('/admin/themes/828155753/assets.json') | ||
.reply(200, resBody); | ||
|
||
resource.all(828155753, function (err, res) { | ||
if (err) return next(err); | ||
|
||
res.should.be.eql(resBody.assets); | ||
next(); | ||
}); | ||
}); | ||
|
||
it('should get a single asset by its key (1/2)', function (next) { | ||
var resBody = fixtures.singleResponseBody; | ||
var path = '/admin/themes/828155753/assets.json?' + | ||
'theme_id=828155753&asset%5Bkey%5D=templates%2Findex.liquid'; | ||
|
||
scope.get(path).reply(200, resBody); | ||
|
||
resource.get(828155753, { | ||
key: 'templates/index.liquid', | ||
theme_id: 828155753 | ||
}, function (err, res) { | ||
if (err) return next(err); | ||
|
||
res.should.be.eql(resBody.asset); | ||
next(); | ||
}); | ||
}); | ||
|
||
it('should get a single asset by its key (2/2)', function (next) { | ||
var resBody = fixtures.singleResponseBody; | ||
var path = '/admin/themes/828155753/assets.json?' + | ||
'asset%5Bkey%5D=templates%2Findex.liquid&theme_id=828155753'; | ||
|
||
scope.get(path).reply(200, resBody); | ||
|
||
resource.get(828155753, { | ||
asset: { key: 'templates/index.liquid' }, | ||
theme_id: 828155753 | ||
}, function (err, res) { | ||
if (err) return next(err); | ||
|
||
res.should.be.eql(resBody.asset); | ||
next(); | ||
}); | ||
}); | ||
|
||
it('should create an asset', function (next) { | ||
var resBody = fixtures.createResponseBody | ||
, reqBody = fixtures.createRequestBody; | ||
|
||
scope.put('/admin/themes/828155753/assets.json', reqBody) | ||
.reply(200, resBody); | ||
|
||
resource.create(828155753, reqBody.asset, function (err, res) { | ||
if (err) return next(err); | ||
|
||
res.should.be.eql(resBody.asset); | ||
next(); | ||
}); | ||
}); | ||
|
||
it('should update an asset', function (next) { | ||
var resBody = fixtures.updateResponseBody | ||
, reqBody = fixtures.updateRequestBody; | ||
|
||
scope.put('/admin/themes/828155753/assets.json', reqBody) | ||
.reply(200, resBody); | ||
|
||
resource.update(828155753, reqBody.asset, function (err, res) { | ||
if (err) return next(err); | ||
|
||
res.should.be.eql(resBody.asset); | ||
next(); | ||
}); | ||
}); | ||
|
||
it('should remove an asset', function (next) { | ||
var path = '/admin/themes/828155753/assets.json?' + | ||
'asset%5Bkey%5D=assets%2Fbg-body.gif'; | ||
|
||
scope.delete(path).reply(200, {}); | ||
|
||
resource.delete(828155753, 'assets/bg-body.gif', function (err, res) { | ||
if (err) return next(err); | ||
|
||
res.should.be.exactly('assets/bg-body.gif'); | ||
next(); | ||
}); | ||
}); | ||
|
||
it('should error when deleting an assest that cannot be deleted', function (next) { | ||
var path = '/admin/themes/828155753/assets.json?' + | ||
'asset%5Bkey%5D=layout%2Ftheme.liquid'; | ||
|
||
scope.delete(path).reply(403, {}); | ||
|
||
resource.delete(828155753, 'layout/theme.liquid', function (err) { | ||
err.should.be.an.Error(); | ||
err.message.should.be.exactly('Status code 403'); | ||
next(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.