Skip to content

Commit

Permalink
Organize authToken tests into distinct areas (#409)
Browse files Browse the repository at this point in the history
- One describe per file
- Tests for Image Tag
- Tests for URL generation
- Tests for genearting tokens using utils
  • Loading branch information
patrick-tolosa committed Jun 18, 2020
1 parent 102274a commit 36cffc7
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 131 deletions.
131 changes: 0 additions & 131 deletions test/authtoken_spec.js

This file was deleted.

22 changes: 22 additions & 0 deletions test/unit/authToken/authTokenImageTag_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require('dotenv').load({
silent: true,
});

const expect = require("expect.js");
const cloudinary = require("../../../cloudinary.js");
const commonAuthSetupAndTeardown = require('./utils/commonAuthSetupAndTeardown');

describe("AuthToken tests using image tag", function () {
// Sets auth token globally in config, sets before and after hooks
commonAuthSetupAndTeardown({});

it("should add token to an image tag url", function () {
let tag = cloudinary.image("sample.jpg", {
sign_url: true,
type: "authenticated",
version: "1486020273",
});

expect(tag).to.match(/<img.*src='http:\/\/res.cloudinary.com\/test123\/image\/authenticated\/v1486020273\/sample.jpg\?__cld_token__=st=11111111~exp=11111411~hmac=9bd6f41e2a5893da8343dc8eb648de8bf73771993a6d1457d49851250caf3b80.*>/);
});
});
84 changes: 84 additions & 0 deletions test/unit/authToken/authTokenURL_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require('dotenv').load({
silent: true,
});

const expect = require("expect.js");
const cloudinary = require("../../../cloudinary.js");
const commonAuthSetupAndTeardown = require('./utils/commonAuthSetupAndTeardown');

describe("AuthToken tests using cloudinary.url and a private CDN", function () {
// Sets auth token globally in config, sets before and after hooks
commonAuthSetupAndTeardown({
private_cdn: true
});

it("Should add a token if signed_url equals true", function () {
let url = cloudinary.url("sample.jpg", {
sign_url: true,
resource_type: "image",
type: "authenticated",
version: "1486020273",
});
expect(url).to.eql("http://test123-res.cloudinary.com/image/authenticated/v1486020273/sample.jpg?__cld_token__=st=11111111~exp=11111411~hmac=8db0d753ee7bbb9e2eaf8698ca3797436ba4c20e31f44527e43b6a6e995cfdb3");
});

it("Should add token for a public resource type", function () {
let url = cloudinary.url("sample.jpg", {
sign_url: true,
resource_type: "image",
type: "public",
version: "1486020273",
});
expect(url).to.eql("http://test123-res.cloudinary.com/image/public/v1486020273/sample.jpg?__cld_token__=st=11111111~exp=11111411~hmac=c2b77d9f81be6d89b5d0ebc67b671557e88a40bcf03dd4a6997ff4b994ceb80e");
});

it("Should not add token if signed is false", function () {
let url = cloudinary.url("sample.jpg", {
type: "authenticated",
version: "1486020273",
});
expect(url).to.eql("http://test123-res.cloudinary.com/image/authenticated/v1486020273/sample.jpg");
});

it("should not add token if authToken is globally set but null auth token is explicitly set and signed = true", function () {
let url = cloudinary.url("sample.jpg", {
auth_token: false,
sign_url: true,
type: "authenticated",
version: "1486020273",
});
expect(url).to.eql("http://test123-res.cloudinary.com/image/authenticated/s--v2fTPYTu--/v1486020273/sample.jpg");
});

it("explicit authToken should override global setting", function () {
let url = cloudinary.url("sample.jpg", {
sign_url: true,
auth_token: {
key: "CCBB2233FF00",
start_time: 222222222,
duration: 100,
},
type: "authenticated",
transformation: {
crop: "scale",
width: 300,
},
});
expect(url).to.eql("http://test123-res.cloudinary.com/image/authenticated/c_scale,w_300/sample.jpg?__cld_token__=st=222222222~exp=222222322~hmac=55cfe516530461213fe3b3606014533b1eca8ff60aeab79d1bb84c9322eebc1f");
});

it("should compute expiration as start time + duration", function () {
let token = {
start_time: 11111111,
duration: 300,
};
let url = cloudinary.url("sample.jpg", {
sign_url: true,
auth_token: token,
resource_type: "image",
type: "authenticated",
version: "1486020273",
});
expect(url).to.eql("http://test123-res.cloudinary.com/image/authenticated/v1486020273/sample.jpg?__cld_token__=st=11111111~exp=11111411~hmac=8db0d753ee7bbb9e2eaf8698ca3797436ba4c20e31f44527e43b6a6e995cfdb3");
});
});
35 changes: 35 additions & 0 deletions test/unit/authToken/authTokenUtils_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require('dotenv').load({
silent: true,
});

const expect = require("expect.js");
const cloudinary = require("../../../cloudinary.js");
const commonAuthSetupAndTeardown = require('./utils/commonAuthSetupAndTeardown');

const utils = cloudinary.utils;

describe("AuthToken tests using utils.generate_auth_token", function () {
// Sets auth token globally in config, sets before and after hooks
commonAuthSetupAndTeardown({});

it("should generate with start and window", function () {
let token = utils.generate_auth_token({
start_time: 1111111111,
acl: "/image/*",
duration: 300,
});

expect(token).to.eql("__cld_token__=st=1111111111~exp=1111111411~acl=%2fimage%2f*~hmac=1751370bcc6cfe9e03f30dd1a9722ba0f2cdca283fa3e6df3342a00a7528cc51");
});

it("should generate token string", function () {
let token = utils.generate_auth_token({
start_time: 222222222,
key: "00112233FF99",
duration: 300,
acl: `/*/t_foobar`,
});

expect(token).to.eql("__cld_token__=st=222222222~exp=222222522~acl=%2f*%2ft_foobar~hmac=8e39600cc18cec339b21fe2b05fcb64b98de373355f8ce732c35710d8b10259f");
});
});
36 changes: 36 additions & 0 deletions test/unit/authToken/utils/commonAuthSetupAndTeardown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const cloudinary = require("../../../../cloudinary.js");
const createTestConfig = require('../../../testUtils/createTestConfig');

const AUTH_KEY_1 = "00112233FF99";

/**
*
*/
function commonAuthSetupAndTeardown(config = {}) {
let urlBackup = '';

before(() => {
urlBackup = process.env.CLOUDINARY_URL;
});

after(function () {
process.env.CLOUDINARY_URL = urlBackup;
cloudinary.config(true);
});

beforeEach(function () {
// Reset the configuration to a known state before each test
process.env.CLOUDINARY_URL = "cloudinary://a:b@test123";

cloudinary.config(true); // reset
cloudinary.config(createTestConfig(Object.assign({}, {
auth_token: {
key: AUTH_KEY_1,
duration: 300,
start_time: 11111111,
}
}, config)));
});
}

module.exports = commonAuthSetupAndTeardown;

0 comments on commit 36cffc7

Please sign in to comment.