1 /**
  2  * @fileOverview Live Connection tests.
  3  */
  4 
  5 /**
  6  * @name test.live.connection
  7  */
  8 (function () {
  9   var assert = require('assert'),
 10     sunny = require("../../lib"),
 11     CloudError = require("../../lib/errors").CloudError,
 12     Connection = require("../../lib/base/connection").Connection,
 13     Container = require("../../lib/base/blob/container").Container,
 14     utils = require("./utils"),
 15     Tests;
 16 
 17   /**
 18    * @exports Tests as test.live.connection.Tests
 19    * @namespace
 20    */
 21   Tests = {};
 22 
 23   Tests["Connection"] = {
 24     "Configuration object.": function (test, opts) {
 25       test.expect(1);
 26       test.ok(opts.config instanceof sunny.Configuration);
 27       test.done();
 28     },
 29     "Connection object.": function (test, opts) {
 30       test.expect(1);
 31       test.ok(opts.conn instanceof Connection);
 32       test.done();
 33     },
 34     "LIST containers (no parameters).": function (test, opts) {
 35       var request = opts.conn.getContainers();
 36       request.on('error', utils.errHandle(test));
 37       request.on('end', function (results) {
 38         test.ok(results);
 39         test.ok(results.hasOwnProperty('containers'));
 40         test.expect(2 + results.containers.length);
 41         results.containers.forEach(function (cont) {
 42           test.ok(cont instanceof Container);
 43         });
 44         test.done();
 45       });
 46       request.end();
 47     },
 48     "GET nonexistent container, no verification.": function (test, opts) {
 49       // Choose highly-unlikely-to-exist name.
 50       var contName = "sunny-nonexistent-container-name-test",
 51         request = opts.conn.getContainer(contName);
 52 
 53       test.expect(3);
 54       request.on('error', utils.errHandle(test));
 55       request.on('end', function (results) {
 56         test.ok(results);
 57         test.ok(results.hasOwnProperty('container'));
 58         test.ok(results.container instanceof Container);
 59         test.done();
 60       });
 61       request.end();
 62     },
 63     "GET nonexistent container, with verification.": function (test, opts) {
 64       // Choose highly-unlikely-to-exist name.
 65       var contName = "sunny_nonexistent_container_name_test",
 66         request = opts.conn.getContainer(contName, { validate: true });
 67 
 68       test.expect(1);
 69       request.on('error', function (err) {
 70         test.deepEqual(err.isNotFound(), true);
 71         test.done();
 72       });
 73       request.on('end', function (results) {
 74         test.ok(false, "Should not have completion. Got: " + results);
 75         test.done();
 76       });
 77       request.end();
 78     },
 79     "PUT invalid-named container.": function (test, opts) {
 80       var contName = "--SUNNYJS_BAD_NAME_FOR_PUT--",
 81         request;
 82 
 83       // Despite the name explicitly violating AWS S3 guidelines, this
 84       // will actually succeed, so let's add some slashes. (This incidentally
 85       // kills GSFD with an "unknown" 400 error).
 86       contName = opts.config.isAws() ? contName.replace("-", "/") : contName;
 87 
 88       test.expect(1);
 89       request = opts.conn.putContainer(contName);
 90       request.on('error', function (err) {
 91         test.deepEqual(err.isInvalidName(), true);
 92         test.done();
 93       });
 94       request.on('end', function (results) {
 95         test.ok(false, "Should not have completion. Got: " + results);
 96         test.done();
 97       });
 98       request.end();
 99     },
100     "GET invalid-named container, with verification.": function (test, opts) {
101       var contName = "SUNNYJS_BAD_NAME_FOR_GET-",
102         request;
103 
104       // Patch name for AWS.
105       contName = opts.config.isAws() ? contName.replace("-", "/") : contName;
106 
107       test.expect(2);
108       request = opts.conn.getContainer(contName, { validate: true });
109       request.on('error', function (err) {
110         test.deepEqual(err.isNotFound(), true);
111         test.deepEqual(err.isInvalidName(), true);
112         test.done();
113       });
114       request.on('end', function (results) {
115         test.ok(false, "Should not have completion. Got: " + results);
116         test.done();
117       });
118       request.end();
119     },
120     "GET weirder, invalid-named container.": function (test, opts) {
121       var contName = "--SUNNYJS_WEIRDER_BAD_NAME_FOR_GET--",
122         request;
123 
124       // Patch name for AWS.
125       contName = opts.config.isAws() ? contName.replace("-", "/") : contName;
126 
127       test.expect(2);
128       request = opts.conn.getContainer(contName, { validate: true });
129       request.on('error', function (err) {
130         test.deepEqual(err.isNotFound(), true);
131         // Google used to have a problem with this. Now ok. (?)
132         test.deepEqual(err.isInvalidName(), true);
133         test.done();
134       });
135       request.on('end', function (results) {
136         test.ok(false, "Should not have completion. Got: " + results);
137         test.done();
138       });
139       request.end();
140     },
141     "PUT container owned by another.": function (test, opts) {
142       // "test" bucket is already taken on AWS, GSFD.
143       // **Note**: If this bucket got delete in the future, this test could
144       // theoretically succeed for someone.
145       var request = opts.conn.putContainer("test");
146 
147       test.expect(1);
148       request.on('error', function (err) {
149         test.deepEqual(err.isNotOwner(), true);
150         test.done();
151       });
152       request.on('end', function (results) {
153         test.ok(false, "Should not have completion. Got: " + results);
154         test.done();
155       });
156       request.end();
157     },
158     "DELETE non-existent container.": function (test, opts) {
159       var contName = utils.testContainerName(),
160         request = opts.conn.delContainer(contName);
161 
162       test.expect(3);
163       request.on('error', utils.errHandle(test));
164       request.on('end', function (results) {
165         var container = results.container,
166           notFound = results.notFound;
167 
168         test.ok(container, "Container should not be empty.");
169         test.deepEqual(container.name, contName);
170         test.deepEqual(notFound, true);
171         test.done();
172       });
173       request.end();
174     },
175     "DELETE invalid-named container.": function (test, opts) {
176       var contName = "SUNNYJS_BAD_NAME_FOR_DELETE-",
177         request;
178 
179       // Patch name for AWS.
180       contName = opts.config.isAws() ? contName.replace("-", "/") : contName;
181 
182       test.expect(3);
183       request = opts.conn.delContainer(contName);
184       request.on('error', utils.errHandle(test));
185       request.on('end', function (results) {
186         var container = results.container,
187           notFound = results.notFound;
188 
189         test.ok(container, "Container should not be empty.");
190         test.deepEqual(container.name, contName);
191         test.deepEqual(notFound, true);
192         test.done();
193       });
194       request.end();
195     }
196     //,
197     //"TODO: DELETE container owned by another.": function (test, opts) {
198     //  test.done();
199     //}
200   };
201 
202   Tests["Connection (w/ Cont)"] = utils.createTestSetup(null, {
203     "PUT container that already exists.": function (test, opts) {
204       var self = this,
205         request = opts.conn.putContainer(self.containerName);
206 
207       test.expect(3);
208       request.on('error', utils.errHandle(test));
209       request.on('end', function (results) {
210         test.ok(results, "Should have valid result.");
211         test.deepEqual(results.container.name, self.containerName);
212         if (opts.config.isAws()) {
213           test.deepEqual(results.alreadyCreated, false);
214         } else if (opts.config.isGoogle()) {
215           test.deepEqual(results.alreadyCreated, true);
216         }
217         test.done();
218       });
219       request.end();
220     },
221     "GET and validate container.": function (test, opts) {
222       var self = this,
223         request = opts.conn.getContainer(self.containerName, {
224           validate: true
225         });
226 
227       test.expect(11);
228       request.on('error', utils.errHandle(test));
229       request.on('end', function (results, meta) {
230         test.ok(results, "Should have valid result.");
231         test.deepEqual(results.container.name, self.containerName);
232         test.deepEqual(results.alreadyCreated, true);
233 
234         // Test meta. (Make sure equal # of tests AWS/GSFD for easier expect).
235         test.ok(meta.headers);
236         test.ok(meta.headers['date']);
237         test.ok(meta.headers['server']);
238         test.ok(meta.headers['content-type']);
239         test.ok(meta.cloudHeaders);
240         test.ok(meta.metadata);
241         if (opts.config.isAws()) {
242           test.deepEqual(meta.headers['server'], "AmazonS3");
243           test.ok(meta.cloudHeaders['request-id']);
244         } else if (opts.config.isGoogle()) {
245           test.ok(meta.headers['server'].indexOf("HTTP Upload Server") > -1);
246           test.ok(meta.headers['expires']);
247         }
248 
249         test.done();
250       });
251       request.end();
252     }
253   });
254 
255   module.exports.Tests = Tests;
256 }());
257