This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
106 lines (90 loc) · 2.37 KB
/
test.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const fs = require("fs");
const { promisify } = require("util");
const request = promisify(require("request"));
// This file tests some basic image upload and tagging as CLI
// it gets the API URL from an api-url file
const baseUrl = fs.readFileSync(__dirname + "/api-url", 'utf8').trim();
console.log("Using API base URL: " + baseUrl);
const run = async () => {
let result;
console.log("Sign up as user 'test'");
result = await sendRequest({
method: "POST",
url: baseUrl + "signup",
body: {
username: "test",
password: "Abc123!"
}
});
console.log(result);
console.log("Sign in as user 'test'");
result = await sendRequest({
method: "POST",
url: baseUrl + "signin",
body: {
username: "test",
password: "Abc123!"
}
});
console.log(result);
const {token} = result;
console.log("Get signed S3 image URL");
result = await sendRequest({
method: "POST",
url: baseUrl + "images",
headers: {
Authorization: "Bearer " + token
}
});
console.log(result);
const imagePath = __dirname + '/test.jpg'
const knownLength = fs.statSync(imagePath).size;
console.log("Upload image to S3");
result = await sendRequest({
method: "POST",
url: result.formConfig.uploadUrl,
formData: {
...result.formConfig.formFields,
file: { // S3 expects file in the "file" field
value: fs.createReadStream(imagePath),
options: {
filename: result.formConfig.formFields.Key,
contentType: 'image/jpeg',
knownLength
}
}
}
});
console.log(result);
await sleep(1000);
console.log("Get all tags");
result = await sendRequest({
method: "GET",
url: baseUrl + "tags",
headers: {
Authorization: "Bearer " + token
}
});
console.log(result);
const tag = result.tags[0];
console.log("Get images by tag: " + tag);
result = await sendRequest({
method: "GET",
url: baseUrl + `tags/${tag}/images`,
headers: {
Authorization: "Bearer " + token
}
});
console.log(JSON.stringify(result, null, 2));
};
const sendRequest = async options => {
if (options.body) options.body = JSON.stringify(options.body);
const result = await request(options);
try {
return JSON.parse(result.body);
} catch(e) {
return result.body
}
};
const sleep = ms => new Promise(r => setTimeout(r, ms));
run();