1 /**
  2  * @fileOverview Runs core tests against module.
  3  */
  4 
  5 /**
  6  * @name test
  7  */
  8 (function () {
  9   var sunny = require("../lib"),
 10     errors = require("../lib/errors"),
 11     CloudError = errors.CloudError,
 12     Tests;
 13 
 14   /**
 15    * Core API unit tests.
 16    *
 17    * **Note**: These tests should run without cloud credentials or even a
 18    * network connection.
 19    *
 20    * @exports Tests as test.core
 21    * @namespace
 22    */
 23   Tests = {
 24     "Empty CloudError.": function (test) {
 25       var err = new CloudError();
 26 
 27       test.deepEqual(err['message'], "");
 28       test.deepEqual(err['statusCode'], null);
 29       test.notEqual(err['stack'], "");
 30       test.ok(!err.isNotFound());
 31       test.ok(!err.isInvalidName());
 32       test.ok(!err.isNotOwner());
 33       test.done();
 34     },
 35     "Simple CloudError.": function (test) {
 36       var err = new CloudError("Hi.");
 37 
 38       test.deepEqual(err['message'], "Hi.");
 39       test.deepEqual(err['statusCode'], null);
 40       test.notEqual(err['stack'], "");
 41       test.done();
 42     },
 43     "CloudError wraps Error.": function (test) {
 44       var realErr = new Error("Hello"),
 45         err = new CloudError(null, { error: realErr });
 46 
 47       test.deepEqual(err['message'], "Hello");
 48       test.deepEqual(err['statusCode'], null);
 49       test.deepEqual(err['stack'], realErr.stack);
 50       test.done();
 51     },
 52     "CloudError wraps Response.": function (test) {
 53       var err = new CloudError("Yo.", { response: { statusCode: 404 } });
 54 
 55       test.deepEqual(err['message'], "Yo.");
 56       test.deepEqual(err['statusCode'], 404);
 57       test.notEqual(err['stack'], "");
 58       test.ok(!err.isNotFound());
 59       test.ok(!err.isInvalidName());
 60       test.ok(!err.isNotOwner());
 61       test.done();
 62     },
 63     "CloudError not found.": function (test) {
 64       var errs = [
 65         new CloudError("Yippee.", {
 66           response: { statusCode: 404 },
 67           types: [CloudError.TYPES.NOT_FOUND]
 68         }),
 69         new CloudError("Yippee.", {
 70           response: { statusCode: 404 },
 71           types: CloudError.TYPES.NOT_FOUND
 72         }),
 73         new CloudError("Yippee.", {
 74           response: { statusCode: 404 },
 75           types: (function () {
 76             var types = {};
 77             // Set some of the attributes (only need NOT_FOUND).
 78             types[CloudError.TYPES.NOT_FOUND] = true;
 79             types[CloudError.TYPES.INVALID_NAME] = false;
 80             return types;
 81           }())
 82         })
 83       ];
 84 
 85       errs.forEach(function (err) {
 86         test.deepEqual(err['message'], "Yippee.");
 87         test.deepEqual(err['statusCode'], 404);
 88         test.notEqual(err['stack'], "");
 89         test.ok(err.isNotFound());
 90         test.ok(!err.isInvalidName());
 91         test.ok(!err.isNotOwner());
 92       });
 93       test.done();
 94     },
 95     "CloudError not found and invalid.": function (test) {
 96       var errs = [
 97         new CloudError("Wow.", {
 98           response: { statusCode: 409 },
 99           types: [
100             CloudError.TYPES.NOT_FOUND,
101             CloudError.TYPES.INVALID_NAME
102           ]
103         }),
104         new CloudError("Wow.", {
105           response: { statusCode: 409 },
106           types: (function () {
107             var types = {};
108             types[CloudError.TYPES.NOT_FOUND] = true;
109             types[CloudError.TYPES.INVALID_NAME] = true;
110             return types;
111           }())
112         })
113       ];
114 
115       errs.forEach(function (err) {
116         test.deepEqual(err['message'], "Wow.");
117         test.deepEqual(err['statusCode'], 409);
118         test.notEqual(err['stack'], "");
119         test.ok(err.isNotFound());
120         test.ok(err.isInvalidName());
121         test.ok(!err.isNotOwner());
122       });
123       test.done();
124     }
125   };
126 
127   module.exports = Tests;
128 }());
129