Skip to content

Commit 2739921

Browse files
author
Amir Tocker
committed
Add access_control parameter to upload and update
1 parent 2d7c2c0 commit 2739921

File tree

4 files changed

+136
-4
lines changed

4 files changed

+136
-4
lines changed

src/utils.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ exports.generate_transformation_string = (options) ->
452452
filter(transformations, utils.present).join "/"
453453

454454
exports.updateable_resource_params = (options, params = {}) ->
455+
params.access_control = jsonArrayParam(options.access_control) if options.access_control?
455456
params.auto_tagging = options.auto_tagging if options.auto_tagging?
456457
params.background_removal = options.background_removal if options.background_removal?
457458
params.categorization = options.categorization if options.categorization?
@@ -1045,7 +1046,7 @@ hashToQuery = (hash)->
10451046
# @param [Hash|String|Array<Hash>] data
10461047
# @return [String|nil] a JSON array string or `nil` if data is `nil`
10471048
###
1048-
jsonArrayParam = (data, callback)->
1049+
exports.jsonArrayParam = jsonArrayParam = (data, callback)->
10491050
return unless data
10501051

10511052
data = JSON.parse(data) if isString(data)

test/access_control_spec.coffee

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require('dotenv').load(silent: true)
2+
3+
expect = require("expect.js")
4+
cloudinary = require("../cloudinary")
5+
utils = require("../lib/utils")
6+
sinon = require('sinon')
7+
ClientRequest = require('_http_client').ClientRequest
8+
http = require('http')
9+
helper = require('./spechelper')
10+
escapeRegexp = helper.escapeRegexp
11+
12+
isString = require('lodash/isString')
13+
14+
xhr = request = requestStub = requestSpy = writeSpy = options = undefined
15+
describe "Access Control", ()->
16+
before "Verify Configuration", ->
17+
config = cloudinary.config(true)
18+
if(!(config.api_key && config.api_secret))
19+
expect().fail("Missing key and secret. Please set CLOUDINARY_URL.")
20+
21+
@timeout helper.TIMEOUT_LONG
22+
after ->
23+
config = cloudinary.config(true)
24+
if(!(config.api_key && config.api_secret))
25+
expect().fail("Missing key and secret. Please set CLOUDINARY_URL.")
26+
cloudinary.v2.api.delete_resources_by_tag(helper.TEST_TAG) unless cloudinary.config().keep_test_products
27+
28+
beforeEach ->
29+
options = {
30+
public_id: helper.TEST_TAG,
31+
tags: [helper.UPLOAD_TAGS..., 'access_control_test']
32+
}
33+
34+
acl = {
35+
access_type: 'anonymous',
36+
start: new Date(Date.UTC(2019,1,22, 16, 20, 57)),
37+
end: '2019-03-22 00:00 +0200'
38+
}
39+
acl_2 = {
40+
access_type: 'anonymous',
41+
start: '2019-02-22 16:20:57Z',
42+
end: '2019-03-22 00:00 +0200'
43+
}
44+
acl_string =
45+
'{"access_type":"anonymous","start":"2019-02-22 16:20:57 +0200","end":"2019-03-22 00:00 +0200"}'
46+
47+
describe "build_upload_params", ()->
48+
it "should accept a Hash value", ()->
49+
params = cloudinary.utils.build_upload_params( access_control: acl)
50+
expect(params).to.have.key('access_control')
51+
expect(isString(params.access_control)).to.be.ok()
52+
expect(params.access_control).to.match(/^\[.+\]$/)
53+
54+
it "should accept an array of Hash values", ()->
55+
params = cloudinary.utils.build_upload_params access_control: [acl, acl_2]
56+
expect(params).to.have.key('access_control')
57+
expect(isString(params.access_control)).to.be.ok()
58+
expect(params.access_control).to.match(/^\[.+\]$/)
59+
j = JSON.parse(params.access_control)
60+
expect(j.length).to.be(2)
61+
expect(j[0]["access_type"]).to.equal(acl.access_type)
62+
expect(Date.parse(j[0]["start"])).to.equal(Date.parse(acl.start))
63+
expect(Date.parse(j[0]["end"])).to.equal(Date.parse(acl.end))
64+
it "should accept a JSON string", ()->
65+
params = cloudinary.utils.build_upload_params access_control: acl_string
66+
expect(params).to.have.key('access_control')
67+
expect(isString(params.access_control)).to.be.ok()
68+
expect(params.access_control).to.equal("[#{acl_string}]")

test/api_spec.coffee

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ ClientRequest = require('_http_client').ClientRequest
1515
http = require('http')
1616
Q = require('q')
1717
fs = require('fs')
18-
1918
helper = require("./spechelper")
19+
mockTest = helper.mockTest
20+
2021
sharedExamples = helper.sharedExamples
2122
itBehavesLike = helper.itBehavesLike
2223
TEST_TAG = helper.TEST_TAG
@@ -56,6 +57,7 @@ sharedExamples "a list with a cursor", (testFunc, args...)->
5657
writeSpy.restore()
5758
requestSpy.restore()
5859
xhr.restore()
60+
5961
specify ":max_results", ()->
6062
testFunc args..., max_results: 10
6163
if writeSpy.called
@@ -690,6 +692,28 @@ describe "api", ->
690692
done()
691693
true
692694

695+
describe "access_control", ()->
696+
acl = {
697+
access_type: 'anonymous',
698+
start: new Date(Date.UTC(2019,1,22, 16, 20, 57)),
699+
end: '2019-03-22 00:00 +0200'
700+
}
701+
acl_string =
702+
'{"access_type":"anonymous","start":"2019-02-22T16:20:57.000Z","end":"2019-03-22 00:00 +0200"}'
703+
options = {
704+
public_id: helper.TEST_TAG,
705+
tags: [helper.UPLOAD_TAGS..., 'access_control_test']
706+
}
707+
708+
it "should allow the user to define ACL in the update parameters2", ()->
709+
helper.mockPromise((xhr, writeSpy, requestSpy)->
710+
options.access_control = [acl]
711+
cloudinary.v2.api.update("id", options)
712+
sinon.assert.calledWith(writeSpy, sinon.match((arg)->
713+
helper.apiParamMatcher('access_control', "[#{acl_string}]")(arg)
714+
))
715+
)
716+
693717
it "should support listing by moderation kind and value", (done) ->
694718
itBehavesLike "a list with a cursor", cloudinary.v2.api.resources_by_moderation, "manual", "approved"
695719
@timeout helper.TIMEOUT_MEDIUM

test/uploader_spec.coffee

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ describe "uploader", ->
3838
# Upload an image to be tested on.
3939
# @callback the callback receives the public_id of the uploaded image
4040
###
41-
upload_image = (callback)->
42-
cloudinary.v2.uploader.upload IMAGE_FILE, tags: UPLOAD_TAGS, (error, result) ->
41+
upload_image = (options, callback)->
42+
[options, callback] = if isFunction(options) then [{tags: UPLOAD_TAGS}, options] else [options, callback]
43+
cloudinary.v2.uploader.upload IMAGE_FILE, options, (error, result) ->
4344
expect(error).to.be undefined
4445
callback?(result)
4546

@@ -618,4 +619,42 @@ describe "uploader", ->
618619
expect(input_element.getAttribute("name")).to.be('file');
619620
expect(input_element.getAttribute("type")).to.be('file');
620621

622+
describe "access_control", ()->
623+
writeSpy = undefined
624+
requestSpy = undefined
625+
options = undefined
626+
627+
beforeEach ->
628+
writeSpy = sinon.spy(ClientRequest.prototype, 'write')
629+
requestSpy = sinon.spy(http, 'request')
630+
options = {
631+
public_id: helper.TEST_TAG,
632+
tags: [helper.UPLOAD_TAGS..., 'access_control_test']
633+
}
634+
635+
afterEach ->
636+
requestSpy.restore()
637+
writeSpy.restore()
638+
639+
acl = {
640+
access_type: 'anonymous',
641+
start: new Date(Date.UTC(2019,1,22, 16, 20, 57)),
642+
end: '2019-03-22 00:00 +0200'
643+
}
644+
acl_string =
645+
'{"access_type":"anonymous","start":"2019-02-22T16:20:57.000Z","end":"2019-03-22 00:00 +0200"}'
646+
647+
it "should allow the user to define ACL in the upload parameters", ()->
648+
options.access_control = [acl]
649+
upload_image(options).then (resource)=>
650+
sinon.assert.calledWith(writeSpy, sinon.match(
651+
helper.uploadParamMatcher('access_control', helper.escapeRegexp("[#{acl_string}]"))
652+
))
653+
expect(resource).to.have.key('access_control')
654+
response_acl = resource["access_control"]
655+
expect(response_acl.length).to.be(1)
656+
expect(response_acl[0]["access_type"]).to.be("anonymous")
657+
expect(Date.parse(response_acl[0]["start"])).to.be(Date.parse(acl.start))
658+
expect(Date.parse(response_acl[0]["end"])).to.be(Date.parse(acl.end))
659+
621660

0 commit comments

Comments
 (0)