|
| 1 | +import path from 'path' |
| 2 | +import { expect } from 'chai' |
| 3 | +import { describe, it, setup } from 'mocha' |
| 4 | +import { jsonReader, writeDownloadedFile } from '../utility/fileOperations/readwrite' |
| 5 | +import { contentstackClient } from '../utility/ContentstackClient.js' |
| 6 | + |
| 7 | +var client = {} |
| 8 | + |
| 9 | +var folderUID = '' |
| 10 | +var assetUID = '' |
| 11 | +var publishAssetUID = '' |
| 12 | +var assetURL = '' |
| 13 | +describe('Assets api Test', () => { |
| 14 | + setup(() => { |
| 15 | + const user = jsonReader('loggedinuser.json') |
| 16 | + client = contentstackClient(user.authtoken) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should asset Upload ', done => { |
| 20 | + const asset = { |
| 21 | + upload: path.join(__dirname, '../mock/customUpload.html'), |
| 22 | + title: 'customasset', |
| 23 | + description: 'Custom Asset Desc', |
| 24 | + tags: ['Custom'] |
| 25 | + } |
| 26 | + makeAsset().create(asset) |
| 27 | + .then((asset) => { |
| 28 | + assetUID = asset.uid |
| 29 | + assetURL = asset.url |
| 30 | + expect(asset.uid).to.be.not.equal(null) |
| 31 | + expect(asset.url).to.be.not.equal(null) |
| 32 | + expect(asset.filename).to.be.equal('customUpload.html') |
| 33 | + expect(asset.title).to.be.equal('customasset') |
| 34 | + expect(asset.description).to.be.equal('Custom Asset Desc') |
| 35 | + expect(asset.content_type).to.be.equal('text/html') |
| 36 | + done() |
| 37 | + }) |
| 38 | + .catch(done) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should download asset from URL.', done => { |
| 42 | + makeAsset().download({ url: assetURL, responseType: 'stream' }) |
| 43 | + .then((response) => { |
| 44 | + writeDownloadedFile(response, 'asset1') |
| 45 | + done() |
| 46 | + }).catch(done) |
| 47 | + }) |
| 48 | + it('should download asset from fetch details ', done => { |
| 49 | + makeAsset(assetUID).fetch() |
| 50 | + .then((asset) => asset.download({ responseType: 'stream' })) |
| 51 | + .then((response) => { |
| 52 | + writeDownloadedFile(response, 'asset2') |
| 53 | + done() |
| 54 | + }).catch(done) |
| 55 | + }) |
| 56 | + |
| 57 | + it('should create folder ', done => { |
| 58 | + makeAsset().folder().create({ asset: { name: 'Sample Folder' } }) |
| 59 | + .then((asset) => { |
| 60 | + folderUID = asset.uid |
| 61 | + expect(asset.uid).to.be.not.equal(null) |
| 62 | + expect(asset.name).to.be.equal('Sample Folder') |
| 63 | + expect(asset.is_dir).to.be.equal(true) |
| 64 | + done() |
| 65 | + }) |
| 66 | + .catch(done) |
| 67 | + }) |
| 68 | + |
| 69 | + it('should asset Upload in folder', done => { |
| 70 | + const asset = { |
| 71 | + upload: path.join(__dirname, '../mock/customUpload.html'), |
| 72 | + title: 'customasset in Folder', |
| 73 | + description: 'Custom Asset Desc in Folder', |
| 74 | + parent_uid: folderUID, |
| 75 | + tags: 'folder' |
| 76 | + } |
| 77 | + makeAsset().create(asset) |
| 78 | + .then((asset) => { |
| 79 | + publishAssetUID = asset.uid |
| 80 | + expect(asset.uid).to.be.not.equal(null) |
| 81 | + expect(asset.url).to.be.not.equal(null) |
| 82 | + expect(asset.filename).to.be.equal('customUpload.html') |
| 83 | + expect(asset.title).to.be.equal('customasset in Folder') |
| 84 | + expect(asset.description).to.be.equal('Custom Asset Desc in Folder') |
| 85 | + expect(asset.content_type).to.be.equal('text/html') |
| 86 | + expect(asset.parent_uid).to.be.equal(folderUID) |
| 87 | + done() |
| 88 | + }) |
| 89 | + .catch(done) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should asset Upload in folder with contenttype', done => { |
| 93 | + const asset = { |
| 94 | + upload: path.join(__dirname, '../mock/berries.jfif'), |
| 95 | + title: 'customasset2 in Folder', |
| 96 | + description: 'Custom Asset Desc in Folder', |
| 97 | + parent_uid: folderUID, |
| 98 | + tags: 'folder', |
| 99 | + content_type: 'image/jpeg' |
| 100 | + } |
| 101 | + makeAsset().create(asset) |
| 102 | + .then((asset) => { |
| 103 | + publishAssetUID = asset.uid |
| 104 | + expect(asset.uid).to.be.not.equal(null) |
| 105 | + expect(asset.url).to.be.not.equal(null) |
| 106 | + expect(asset.filename).to.be.equal('berries.jfif') |
| 107 | + expect(asset.title).to.be.equal('customasset2 in Folder') |
| 108 | + expect(asset.description).to.be.equal('Custom Asset Desc in Folder') |
| 109 | + expect(asset.content_type).to.be.equal('image/jpeg') |
| 110 | + expect(asset.parent_uid).to.be.equal(folderUID) |
| 111 | + done() |
| 112 | + }) |
| 113 | + .catch(done) |
| 114 | + }) |
| 115 | + it('should replace asset ', done => { |
| 116 | + const asset = { |
| 117 | + upload: path.join(__dirname, '../mock/upload.html') |
| 118 | + } |
| 119 | + makeAsset(assetUID) |
| 120 | + .replace(asset) |
| 121 | + .then((asset) => { |
| 122 | + expect(asset.uid).to.be.equal(assetUID) |
| 123 | + expect(asset.filename).to.be.equal('upload.html') |
| 124 | + expect(asset.content_type).to.be.equal('text/html') |
| 125 | + done() |
| 126 | + }) |
| 127 | + .catch(done) |
| 128 | + }) |
| 129 | + |
| 130 | + it('should fetch and Update asset details', done => { |
| 131 | + makeAsset(assetUID) |
| 132 | + .fetch() |
| 133 | + .then((asset) => { |
| 134 | + asset.title = 'Update title' |
| 135 | + asset.description = 'Update description' |
| 136 | + delete asset.ACL |
| 137 | + return asset.update() |
| 138 | + }) |
| 139 | + .then((asset) => { |
| 140 | + expect(asset.uid).to.be.equal(assetUID) |
| 141 | + expect(asset.title).to.be.equal('Update title') |
| 142 | + expect(asset.description).to.be.equal('Update description') |
| 143 | + done() |
| 144 | + }) |
| 145 | + .catch(done) |
| 146 | + }) |
| 147 | + |
| 148 | + it('should publish Asset', done => { |
| 149 | + makeAsset(publishAssetUID) |
| 150 | + .publish({ publishDetails: { |
| 151 | + locales: ['hi-in', 'en-us'], |
| 152 | + environments: ['development'] |
| 153 | + } }) |
| 154 | + .then((data) => { |
| 155 | + expect(data.notice).to.be.equal('Asset sent for publishing.') |
| 156 | + done() |
| 157 | + }) |
| 158 | + .catch(done) |
| 159 | + }) |
| 160 | + |
| 161 | + it('should unpublish Asset', done => { |
| 162 | + makeAsset(publishAssetUID) |
| 163 | + .unpublish({ publishDetails: { |
| 164 | + locales: ['hi-in', 'en-us'], |
| 165 | + environments: ['development'] |
| 166 | + } }) |
| 167 | + .then((data) => { |
| 168 | + expect(data.notice).to.be.equal('Asset sent for unpublishing.') |
| 169 | + done() |
| 170 | + }) |
| 171 | + .catch(done) |
| 172 | + }) |
| 173 | + |
| 174 | + it('should delete asset', done => { |
| 175 | + makeAsset(assetUID) |
| 176 | + .delete() |
| 177 | + .then((data) => { |
| 178 | + expect(data.notice).to.be.equal('Asset deleted successfully.') |
| 179 | + done() |
| 180 | + }) |
| 181 | + .catch(done) |
| 182 | + }) |
| 183 | + |
| 184 | + it('should query to fetch all asset', done => { |
| 185 | + makeAsset() |
| 186 | + .query() |
| 187 | + .find() |
| 188 | + .then((collection) => { |
| 189 | + collection.items.forEach((asset) => { |
| 190 | + expect(asset.uid).to.be.not.equal(null) |
| 191 | + expect(asset.title).to.be.not.equal(null) |
| 192 | + expect(asset.description).to.be.not.equal(null) |
| 193 | + }) |
| 194 | + done() |
| 195 | + }) |
| 196 | + .catch(done) |
| 197 | + }) |
| 198 | + |
| 199 | + it('should query to fetch title match asset', done => { |
| 200 | + makeAsset() |
| 201 | + .query({ query: { title: 'Update title' } }) |
| 202 | + .find() |
| 203 | + .then((collection) => { |
| 204 | + collection.items.forEach((asset) => { |
| 205 | + expect(asset.uid).to.be.not.equal(null) |
| 206 | + expect(asset.title).to.be.equal('Update title') |
| 207 | + expect(asset.description).to.be.equal('Update description') |
| 208 | + }) |
| 209 | + done() |
| 210 | + }) |
| 211 | + .catch(done) |
| 212 | + }) |
| 213 | +}) |
| 214 | + |
| 215 | +function makeAsset (uid = null) { |
| 216 | + return client.stack({ api_key: process.env.API_KEY }).asset(uid) |
| 217 | +} |
0 commit comments