Skip to content

Commit

Permalink
Merge pull request MONEI#66 from lpinca/add/asset
Browse files Browse the repository at this point in the history
Add Asset resource
  • Loading branch information
Alua-Kinzhebayeva committed Nov 28, 2015
2 parents eeee3a0 + a140e2a commit ac152db
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 5 deletions.
50 changes: 46 additions & 4 deletions lib/resources/asset.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 26 additions & 1 deletion src/resources/asset.coffee
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
128 changes: 128 additions & 0 deletions test/asset_test.js
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();
});
});
});
Loading

0 comments on commit ac152db

Please sign in to comment.