-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathcreateTestCloud.js
More file actions
62 lines (52 loc) · 1.96 KB
/
createTestCloud.js
File metadata and controls
62 lines (52 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* This file when used as a script (node tmp.js) will create a new cloud within Cloudinary
* The CLOUDINARY_URL environment variable string is created, and stored in ./cloudinary_url.sh
* To use a fresh cloud in your tests, source ./cloudinary_url.sh before running the tests
* Example: node tools/createTestCloud && source tools/cloudinary_url.sh && npm run test
*/
const fs = require('fs');
const path = require('path');
const https = require('https');
const ENV_FILE_PATH = path.resolve(__dirname, '../.env');
const cloudinary = require('../cloudinary');
function setup() {
let req = https.request({
method: 'POST',
hostname: 'sub-account-testing.cloudinary.com',
path: '/create_sub_account',
port: 443
}, (res) => {
let data = '';
res.on('data', (d) => {
data += d;
});
res.on('end', () => {
let cloudData = JSON.parse(data);
let { payload: { cloudApiKey, cloudApiSecret, cloudName, id } } = cloudData;
let URL = `CLOUDINARY_URL=cloudinary://${cloudApiKey}:${cloudApiSecret}@${cloudName}`;
fs.writeFileSync(`tools/cloudinary_url.sh`, URL); // This is needed for Travis
fs.writeFileSync(ENV_FILE_PATH, URL); // This is needed for local develoepr tests
const dotenv = require('dotenv');
const dotenvInit = typeof dotenv.config === 'function' ? dotenv.config : dotenv.load;
dotenvInit();
cloudinary.config(true);
cloudinary.v2.uploader.upload('./test/.resources/sample.jpg', {
cloud_name: cloudName,
api_key: cloudApiKey,
api_secret: cloudApiSecret,
public_id: 'sample'
}).then((result) => {
console.log('Successfully created a temporary cloud');
console.log('Cloudname: ', cloudName);
console.log('Sample image uploaded to: ', result.url);
}).catch(() => {
throw 'FATAL - Could not create sample asset';
});
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
}
setup();