|
| 1 | +expect = require("expect.js") |
| 2 | +cloudinary = require("../cloudinary.js") |
| 3 | +utils = cloudinary.utils |
| 4 | + |
| 5 | +KEY = "00112233FF99" |
| 6 | +ALT_KEY = "CCBB2233FF00" |
| 7 | +describe "authToken", -> |
| 8 | + urlBackup = process.env.CLOUDINARY_URL |
| 9 | + |
| 10 | + beforeEach -> |
| 11 | + process.env.CLOUDINARY_URL = "cloudinary://a:b@test123?load_strategies=false" |
| 12 | + cloudinary.config true |
| 13 | + cloudinary.config().auth_token = {key: KEY, duration: 300, start_time: 11111111} |
| 14 | + after -> |
| 15 | + process.env.CLOUDINARY_URL = urlBackup |
| 16 | + cloudinary.config true |
| 17 | + |
| 18 | + it "should generate with start and window", -> |
| 19 | + token = utils.generate_auth_token start_time: 1111111111, acl: "/image/*", duration: 300 |
| 20 | + expect(token).to.eql '__cld_token__=st=1111111111~exp=1111111411~acl=/image/*~hmac=0854e8b6b6a46471a80b2dc28c69bd352d977a67d031755cc6f3486c121b43af' |
| 21 | + |
| 22 | + describe "authenticated url", -> |
| 23 | + beforeEach -> |
| 24 | + cloudinary.config private_cdn: true |
| 25 | + |
| 26 | + it "should add token if authToken is globally set and signed = true", -> |
| 27 | + url = cloudinary.url "sample.jpg", sign_url: true, resource_type: "image", type: "authenticated", version: "1486020273" |
| 28 | + expect(url).to.eql("http://test123-res.cloudinary.com/image/authenticated/v1486020273/sample.jpg?__cld_token__=st=11111111~exp=11111411~hmac=8db0d753ee7bbb9e2eaf8698ca3797436ba4c20e31f44527e43b6a6e995cfdb3") |
| 29 | + |
| 30 | + it "should add token for 'public' resource", -> |
| 31 | + url = cloudinary.url "sample.jpg", sign_url: true, resource_type: "image", type: "public", version: "1486020273" |
| 32 | + expect(url).to.eql("http://test123-res.cloudinary.com/image/public/v1486020273/sample.jpg?__cld_token__=st=11111111~exp=11111411~hmac=c2b77d9f81be6d89b5d0ebc67b671557e88a40bcf03dd4a6997ff4b994ceb80e") |
| 33 | + |
| 34 | + it "should not add token if signed is false", -> |
| 35 | + url = cloudinary.url "sample.jpg", type: "authenticated", version: "1486020273" |
| 36 | + expect(url).to.eql("http://test123-res.cloudinary.com/image/authenticated/v1486020273/sample.jpg") |
| 37 | + |
| 38 | + it "should not add token if authToken is globally set but null auth token is explicitly set and signed = true", -> |
| 39 | + url = cloudinary.url "sample.jpg", auth_token: false, sign_url: true, type: "authenticated", version: "1486020273" |
| 40 | + expect(url).to.eql("http://test123-res.cloudinary.com/image/authenticated/s--v2fTPYTu--/v1486020273/sample.jpg") |
| 41 | + |
| 42 | + it "explicit authToken should override global setting", -> |
| 43 | + url = cloudinary.url "sample.jpg", sign_url: true, auth_token: {key: ALT_KEY, start_time: 222222222, duration: 100}, type: "authenticated", transformation: {crop: "scale", width: 300} |
| 44 | + expect(url).to.eql("http://test123-res.cloudinary.com/image/authenticated/c_scale,w_300/sample.jpg?__cld_token__=st=222222222~exp=222222322~hmac=7d276841d70c4ecbd0708275cd6a82e1f08e47838fbb0bceb2538e06ddfa3029") |
| 45 | + |
| 46 | + it "should compute expiration as start time + duration", -> |
| 47 | + token = {key: KEY, start_time: 11111111, duration: 300} |
| 48 | + url = cloudinary.url "sample.jpg", sign_url: true, auth_token: token, resource_type: "image", type: "authenticated", version: "1486020273" |
| 49 | + expect(url).to.eql("http://test123-res.cloudinary.com/image/authenticated/v1486020273/sample.jpg?__cld_token__=st=11111111~exp=11111411~hmac=8db0d753ee7bbb9e2eaf8698ca3797436ba4c20e31f44527e43b6a6e995cfdb3") |
| 50 | + |
| 51 | + describe "authentication token", -> |
| 52 | + it "should generate token string", -> |
| 53 | + user = "foobar" # we can't rely on the default "now" value in tests |
| 54 | + tokenOptions = {key: KEY, duration: 300, acl: "/*/t_#{user}"} |
| 55 | + tokenOptions.start_time = 222222222 # we can't rely on the default "now" value in tests |
| 56 | + cookieToken = utils.generate_auth_token tokenOptions |
| 57 | + expect(cookieToken).to.eql("__cld_token__=st=222222222~exp=222222522~acl=/*/t_foobar~hmac=eb5e2266c8ec9573f696025f075b92998080347e1c12ac39a26c94d7d712704a") |
| 58 | + |
| 59 | + it "should add token to an image tag url", -> |
| 60 | + tag = cloudinary.image "sample.jpg", sign_url: true, type: "authenticated", version: "1486020273" |
| 61 | + expect(tag).to.match /<img.*src='http:\/\/res.cloudinary.com\/test123\/image\/authenticated\/v1486020273\/sample.jpg\?__cld_token__=st=11111111~exp=11111411~hmac=9bd6f41e2a5893da8343dc8eb648de8bf73771993a6d1457d49851250caf3b80.*>/ |
0 commit comments