|
| 1 | +const helper = require('../../../spechelper'); |
| 2 | +const cloudinary = require('../../../../cloudinary'); |
| 3 | +const { |
| 4 | + strictEqual, |
| 5 | + deepStrictEqual |
| 6 | +} = require('assert'); |
| 7 | +const {TEST_CLOUD_NAME} = require('../../../testUtils/testConstants'); |
| 8 | + |
| 9 | +describe('Asset relations API', () => { |
| 10 | + const testPublicId = 'test-public-id'; |
| 11 | + const testAssetId = 'test-asset-id'; |
| 12 | + const singleRelatedPublicId = 'related-public-id'; |
| 13 | + const multipleRelatedPublicId = ['related-public-id-1', 'related-public-id-2']; |
| 14 | + |
| 15 | + describe('when creating new relation', () => { |
| 16 | + describe('using public id', () => { |
| 17 | + it('should allow passing a single public id to create a relation', () => { |
| 18 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 19 | + cloudinary.v2.api.add_related_assets(testPublicId, singleRelatedPublicId, { |
| 20 | + resource_type: 'image', |
| 21 | + type: 'upload' |
| 22 | + }); |
| 23 | + |
| 24 | + const [calledWithUrl] = requestSpy.firstCall.args; |
| 25 | + strictEqual(calledWithUrl.method, 'POST'); |
| 26 | + strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/image/upload/test-public-id`); |
| 27 | + const callApiArguments = writeSpy.firstCall.args; |
| 28 | + deepStrictEqual(callApiArguments, ['assets_to_relate=related-public-id']); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should allow passing multiple public ids to create a relation', () => { |
| 33 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 34 | + cloudinary.v2.api.add_related_assets(testPublicId, multipleRelatedPublicId, { |
| 35 | + resource_type: 'image', |
| 36 | + type: 'upload' |
| 37 | + }); |
| 38 | + |
| 39 | + const [calledWithUrl] = requestSpy.firstCall.args; |
| 40 | + strictEqual(calledWithUrl.method, 'POST'); |
| 41 | + strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/image/upload/${testPublicId}`); |
| 42 | + const callApiArguments = writeSpy.firstCall.args; |
| 43 | + deepStrictEqual(callApiArguments, ['assets_to_relate=related-public-id-1&assets_to_relate=related-public-id-2']); |
| 44 | + }); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('using asset id', () => { |
| 49 | + it('should allow passing a single public id to create a relation', () => { |
| 50 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 51 | + cloudinary.v2.api.add_related_assets_by_asset_id(testAssetId, singleRelatedPublicId); |
| 52 | + |
| 53 | + const [calledWithUrl] = requestSpy.firstCall.args; |
| 54 | + strictEqual(calledWithUrl.method, 'POST'); |
| 55 | + strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/test-asset-id`); |
| 56 | + const callApiArguments = writeSpy.firstCall.args; |
| 57 | + deepStrictEqual(callApiArguments, ['assets_to_relate=related-public-id']); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should allow passing multiple public ids to create a relation', () => { |
| 62 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 63 | + cloudinary.v2.api.add_related_assets_by_asset_id(testAssetId, multipleRelatedPublicId); |
| 64 | + |
| 65 | + const [calledWithUrl] = requestSpy.firstCall.args; |
| 66 | + strictEqual(calledWithUrl.method, 'POST'); |
| 67 | + strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/test-asset-id`); |
| 68 | + const callApiArguments = writeSpy.firstCall.args; |
| 69 | + deepStrictEqual(callApiArguments, ['assets_to_relate=related-public-id-1&assets_to_relate=related-public-id-2']); |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('when deleting existing relation', () => { |
| 76 | + describe('using public id', () => { |
| 77 | + it('should allow passing a single public id to delete a relation', () => { |
| 78 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 79 | + cloudinary.v2.api.delete_related_assets(testPublicId, singleRelatedPublicId, { |
| 80 | + resource_type: 'image', |
| 81 | + type: 'upload' |
| 82 | + }); |
| 83 | + |
| 84 | + const [calledWithUrl] = requestSpy.firstCall.args; |
| 85 | + strictEqual(calledWithUrl.method, 'DELETE'); |
| 86 | + strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/image/upload/test-public-id`); |
| 87 | + const callApiArguments = writeSpy.firstCall.args; |
| 88 | + deepStrictEqual(callApiArguments, ['assets_to_unrelate=related-public-id']); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should allow passing multiple public ids to delete a relation', () => { |
| 93 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 94 | + cloudinary.v2.api.delete_related_assets(testPublicId, multipleRelatedPublicId, { |
| 95 | + resource_type: 'image', |
| 96 | + type: 'upload' |
| 97 | + }); |
| 98 | + |
| 99 | + const [calledWithUrl] = requestSpy.firstCall.args; |
| 100 | + strictEqual(calledWithUrl.method, 'DELETE'); |
| 101 | + strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/image/upload/test-public-id`); |
| 102 | + const callApiArguments = writeSpy.firstCall.args; |
| 103 | + deepStrictEqual(callApiArguments, ['assets_to_unrelate=related-public-id-1&assets_to_unrelate=related-public-id-2']); |
| 104 | + }); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('and using asset id', () => { |
| 109 | + it('should allow passing a single public id to delete a relation', () => { |
| 110 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 111 | + cloudinary.v2.api.delete_related_assets_by_asset_id(testAssetId, singleRelatedPublicId); |
| 112 | + |
| 113 | + const [calledWithUrl] = requestSpy.firstCall.args; |
| 114 | + strictEqual(calledWithUrl.method, 'DELETE'); |
| 115 | + strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/test-asset-id`); |
| 116 | + const callApiArguments = writeSpy.firstCall.args; |
| 117 | + deepStrictEqual(callApiArguments, ['assets_to_unrelate=related-public-id']); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should allow passing multiple public ids to delete a relation', () => { |
| 122 | + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { |
| 123 | + cloudinary.v2.api.delete_related_assets_by_asset_id(testAssetId, multipleRelatedPublicId); |
| 124 | + |
| 125 | + const [calledWithUrl] = requestSpy.firstCall.args; |
| 126 | + strictEqual(calledWithUrl.method, 'DELETE'); |
| 127 | + strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/test-asset-id`); |
| 128 | + const callApiArguments = writeSpy.firstCall.args; |
| 129 | + deepStrictEqual(callApiArguments, ['assets_to_unrelate=related-public-id-1&assets_to_unrelate=related-public-id-2']); |
| 130 | + }); |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments