|
| 1 | +const sinon = require('sinon'); |
| 2 | +const cloudinary = require("../../../../cloudinary"); |
| 3 | +const helper = require("../../../spechelper"); |
| 4 | +const describe = require('../../../testUtils/suite'); |
| 5 | +const testConstants = require('../../../testUtils/testConstants'); |
| 6 | +const { PUBLIC_IDS } = testConstants; |
| 7 | +const { PUBLIC_ID } = PUBLIC_IDS; |
| 8 | + |
| 9 | +describe("oauth_token", function(){ |
| 10 | + it("should send the oauth_token option to the server (admin_api)", function() { |
| 11 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 12 | + cloudinary.v2.api.resource(PUBLIC_ID, { oauth_token: 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4' }); |
| 13 | + return sinon.assert.calledWith(requestSpy, |
| 14 | + sinon.match.has("headers", |
| 15 | + sinon.match.has("Authorization", "Bearer MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4") |
| 16 | + )); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should send the oauth_token config to the server (admin_api)", function() { |
| 21 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 22 | + cloudinary.config({ |
| 23 | + api_key: undefined, |
| 24 | + api_secret: undefined, |
| 25 | + oauth_token: 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4' |
| 26 | + }); |
| 27 | + cloudinary.v2.api.resource(PUBLIC_ID); |
| 28 | + return sinon.assert.calledWith(requestSpy, |
| 29 | + sinon.match.has("headers", |
| 30 | + sinon.match.has("Authorization", "Bearer MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4") |
| 31 | + )); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should not fail when only providing api_key and secret (admin_api)", function() { |
| 36 | + cloudinary.config({ |
| 37 | + api_key: "1234", |
| 38 | + api_secret: "1234", |
| 39 | + oauth_token: undefined |
| 40 | + }); |
| 41 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 42 | + cloudinary.v2.api.resource(PUBLIC_ID); |
| 43 | + return sinon.assert.calledWith(requestSpy, sinon.match({ auth: "1234:1234" })); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should fail when missing all credentials (admin_api)", function() { |
| 48 | + cloudinary.config({ |
| 49 | + api_key: undefined, |
| 50 | + api_secret: undefined, |
| 51 | + oauth_token: undefined |
| 52 | + }); |
| 53 | + expect(() => { |
| 54 | + cloudinary.v2.api.resource(PUBLIC_ID) |
| 55 | + }).to.throwError(/Must supply api_key/); |
| 56 | + }); |
| 57 | + |
| 58 | + it("oauth_token as option should take priority with secret and key (admin_api)", function() { |
| 59 | + cloudinary.config({ |
| 60 | + api_key: '1234', |
| 61 | + api_secret: '1234' |
| 62 | + }); |
| 63 | + return cloudinary.v2.api.resource(PUBLIC_ID, {oauth_token: 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4'}) |
| 64 | + .then( |
| 65 | + () => expect().fail() |
| 66 | + ).catch(({ error }) => expect(error.message).to.contain("Invalid token")); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should send the oauth_token option to the server (upload_api)", function() { |
| 70 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 71 | + cloudinary.v2.uploader.upload(PUBLIC_ID, { oauth_token: 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4' }); |
| 72 | + return sinon.assert.calledWith(requestSpy, |
| 73 | + sinon.match.has("headers", |
| 74 | + sinon.match.has("Authorization", "Bearer MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4") |
| 75 | + )); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should send the oauth_token config to the server (upload_api)", function() { |
| 80 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 81 | + cloudinary.config({ |
| 82 | + api_key: undefined, |
| 83 | + api_secret: undefined, |
| 84 | + oauth_token: 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4' |
| 85 | + }); |
| 86 | + cloudinary.v2.uploader.upload(PUBLIC_ID); |
| 87 | + return sinon.assert.calledWith(requestSpy, |
| 88 | + sinon.match.has("headers", |
| 89 | + sinon.match.has("Authorization", "Bearer MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4") |
| 90 | + )); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should not fail when only providing api_key and secret (upload_api)", function() { |
| 95 | + cloudinary.config({ |
| 96 | + api_key: "1234", |
| 97 | + api_secret: "1234", |
| 98 | + oauth_token: undefined |
| 99 | + }); |
| 100 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 101 | + cloudinary.v2.uploader.upload(PUBLIC_ID) |
| 102 | + return sinon.assert.calledWith(requestSpy, sinon.match({ auth: null })); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should fail when missing all credentials (upload_api)", function() { |
| 107 | + cloudinary.config({ |
| 108 | + api_key: undefined, |
| 109 | + api_secret: undefined, |
| 110 | + oauth_token: undefined |
| 111 | + }); |
| 112 | + expect(() => { |
| 113 | + cloudinary.v2.uploader.upload(PUBLIC_ID) |
| 114 | + }).to.throwError(/Must supply api_key/); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should not need credentials for unsigned upload", function() { |
| 118 | + cloudinary.config({ |
| 119 | + api_key: undefined, |
| 120 | + api_secret: undefined, |
| 121 | + oauth_token: undefined |
| 122 | + }); |
| 123 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 124 | + cloudinary.v2.uploader.unsigned_upload(PUBLIC_ID, 'preset') |
| 125 | + return sinon.assert.calledWith(requestSpy, sinon.match({ auth: null })); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments