forked from eschultink/S3-for-Google-Apps-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.gs
167 lines (146 loc) · 4.94 KB
/
tests.gs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Test and Demo functions for the library
*
* demo* functions run individual methods, but don't verify results (eg for debugging)
*
* runTests executes a serious of test of the main methods, and throws an exception if there's a failure
*
*/
/** @const {string} name of test bucket (remember all S3 buckets must be GLOBALLY unique) **/
var bucketName_ = "S3LibraryTestBucket";
/**
* DEMO FUNCTIONS - useful for debugging; commented out to not clutter tab-completion list
*/
//function demoCreateBucket() {
// var env = getTestEnv();
// var service = new S3(env.awsAccessKeyId, env.awsSecretKey);
// service.createBucket(bucketName_);
//}
//
//function demoDeleteBucket() {
// var env = getTestEnv();
// var service = new S3(env.awsAccessKeyId, env.awsSecretKey);
// service.deleteBucket(bucketName_);
//}
//
//function demoPutObject() {
// var env = getTestEnv();
// var service = new S3(env.awsAccessKeyId, env.awsSecretKey);
// service.putObject(bucketName_, "objectFoo", "blah ObjectContent blah");
//}
//
//function demoGetObject() {
// var env = getTestEnv();
// var service = new S3(env.awsAccessKeyId, env.awsSecretKey);
// var value = service.getObject(bucketName_, "objectFoo");
// Logger.log(value);
//}
//
//function demoDeleteObject() {
// var env = getTestEnv();
// var service = new S3(env.awsAccessKeyId, env.awsSecretKey);
// service.deleteObject(bucketName_, "objectFoo");
//}
/**
* executes tests of the library. throws exceptions if any fails
* (tests can't run locally in this file, bc require Amazon credentials to be defined - which they of course
* shouldn't be in a public script file)
*/
function runTests() {
Logger.log("START tests. should end with ALL TESTS PASSED.");
var env = getTestEnv();
var service = new S3(env.awsAccessKeyId, env.awsSecretKey);
var objectName = "objectFoo";
var blobObjectName = "blobtest";
var options = {
// logRequests:true
};
//clean up existing object, bucket if exist
try {
service.deleteObject(bucketName_, objectName, options);
} catch (e) {
Logger.log(e);
}
try {
service.deleteObject(bucketName_, blobObjectName, options);
} catch (e) {
Logger.log(e);
}
try {
service.deleteBucket(bucketName_, options);
} catch (e) {
Logger.log(e);
}
//get object from non-existent bucket (should fail)
var checkExpectedError = function (expectedErrorCode, e) {
Logger.log("caught exception: %s", e.toString());
if (e.name == 'AwsError' && e.code == expectedErrorCode) {
Logger.log("expected error occurred");
return true;
} else {
Logger.log("An error occurred, but not the expected one");
throw e;
}
};
var fail = false;
try {
var r = service.getObject(bucketName_, objectName, options);
} catch (e) {
fail = checkExpectedError("NoSuchBucket", e);
}
if (!fail) {
throw "request to get object from non-existent bucket succeeded; wtf";
}
Logger.log("PASSED get object from non-existent bucket");
//put object into non-existent bucket (should fail)
var fail = false;
try {
service.putObject(bucketName_, objectName, "blah", options);
} catch (e) {
fail = checkExpectedError("NoSuchBucket", e);
}
if (!fail) {
throw "request to put object into non-existing bucket succeeded; wtf";
}
Logger.log("PASSED put object into non-existent bucket");
//delete non-existent bucket
var fail = false;
try {
service.deleteBucket(bucketName_, options);
} catch (e) {
fail = checkExpectedError("NoSuchBucket", e);
}
if (!fail) {
throw "request to delete non-existent bucket succeeded; wtf";
}
Logger.log("PASSED delete non-existent bucket");
//create a bucket
service.createBucket(bucketName_, options);
Logger.log("PASSED create bucket");
//get non-existent object from existing bucket (should return null)
var r = service.getObject(bucketName_, objectName, options);
if (r !== null) {
throw "found object that should not exist";
}
Logger.log("PASSED get non-existent object from existing bucket");
//put object into bucket
var content = "blah";
service.putObject(bucketName_, objectName, content, options);
Logger.log("PASSED put object");
//get object
var r = service.getObject(bucketName_, objectName, options);
if (r !== content) {
throw "S3 object content does not match what was put in";
}
Logger.log("PASSED get object");
//put/get a Blob, to make sure deals w this OK
var blob = UrlFetchApp.fetch("http://www.google.com").getBlob();
service.putObject(bucketName_, blobObjectName, blob, options);
var retrievedBlob = service.getObject(bucketName_, blobObjectName, options);
if (!(retrievedBlob.getContentType() == blob.getContentType() &&
retrievedBlob.getDataAsString() == blob.getDataAsString())) {
throw "test to set html Blob into S3 and retrieve it failed";
}
Logger.log("PASSED put/get Blob object");
Logger.log("-------------\nALL TESTS PASSED");
}