├── .gitignore ├── contributing.md ├── index.js ├── lib └── OptimizelyClient.js ├── license ├── package.json ├── readme.md └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_STORE 2 | node_modules/ 3 | npm-debug.log -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We love pull requests. Here's a quick guide. 4 | 5 | Fork, then clone the repo: 6 | 7 | git clone git@github.com:your-username/optimizely-node-client.git 8 | 9 | Make sure the tests pass: 10 | 11 | npm test 12 | 13 | Make your change. Add tests for your change. Make the tests pass: 14 | 15 | npm test 16 | 17 | Push to your fork and [submit a pull request][pr]. 18 | 19 | [pr]: https://github.com/funnelenvy/optimizely-node-client/compare/ 20 | 21 | At this point you're waiting on us. We like to at least comment on pull requests 22 | within three business days (and, typically, one business day). We may suggest 23 | some changes or improvements or alternatives. 24 | 25 | Some things that will increase the chance that your pull request is accepted: 26 | 27 | * Write tests. 28 | * Follow our [style guide][style]. 29 | * Write a [good commit message][commit]. 30 | 31 | [style]: https://github.com/RisingStack/node-style-guide 32 | [commit]: https://atom.io/docs/latest/contributing#git-commit-messages 33 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/OptimizelyClient'); 2 | -------------------------------------------------------------------------------- /lib/OptimizelyClient.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @fileOverview Optimizely Client for Node 3 | * @name Optimizely Client 4 | * @author Arun 5 | * @version 0.6.2 6 | */ 7 | 8 | /** @access private */ 9 | var Promise = require("bluebird"); 10 | var rest = require('restler'); 11 | 12 | /** @const*/ 13 | var methodNamesToPromisify = 14 | "get post put del head patch json postJson putJson".split(" "); 15 | 16 | var EventEmitterPromisifier = function(originalMethod) { 17 | // return a function 18 | return function promisified() { 19 | var args = [].slice.call(arguments); 20 | // Needed so that the original method can be called with the correct receiver 21 | var self = this; 22 | // which returns a promise 23 | return new Promise(function(resolve, reject) { 24 | // We call the originalMethod here because if it throws, 25 | // it will reject the returned promise with the thrown error 26 | var emitter = originalMethod.apply(self, args); 27 | 28 | emitter 29 | .on("success", function(data) { 30 | resolve(data); 31 | }) 32 | .on("fail", function(data) { 33 | //Failed Responses including 400 status codes 34 | reject(data); 35 | }) 36 | .on("error", function(err) { 37 | //Internal Error 38 | reject(err); 39 | }) 40 | .on("abort", function() { 41 | reject(new Promise.CancellationError()); 42 | }) 43 | .on("timeout", function() { 44 | reject(new Promise.TimeoutError()); 45 | }); 46 | }); 47 | }; 48 | }; 49 | 50 | Promise.promisifyAll(rest, { 51 | filter: function(name) { 52 | return methodNamesToPromisify.indexOf(name) > -1; 53 | }, 54 | promisifier: EventEmitterPromisifier 55 | }); 56 | //////////////// 57 | //0. Constructor 58 | //////////////// 59 | /** 60 | * @public 61 | * @Constructor 62 | * @name OptimizelyClient 63 | * @since 0.0.1 64 | * @description Optimizely Client Constructor 65 | * @param {string} apiToken The Optimizely API Token 66 | * @param {object} options to define custom {string} 'url' or {boolean} OAuth2 67 | * @return {OptimizelyClient} The newly created optimizely client. 68 | * @throws {error} Throws an error if apiToken is not provided 69 | * @example 70 | * var apiToken = "*";//Get token from www.optimizely.com/tokens 71 | * var oc = new OptimizelyClient(API_TOKEN); 72 | */ 73 | var OptimizelyClient = function(apiToken, options) { 74 | //initialize 75 | if (!apiToken) throw new Error("Required: apiToken"); 76 | this.apiToken = String(apiToken); 77 | this.baseUrl = (options && options.url) ? options.url : 'https://www.optimizelyapis.com/experiment/v1/'; 78 | if(options && options.OAuth2){ 79 | this.baseHeaders = { 80 | 'Authorization': 'Bearer ' + this.apiToken, 81 | 'Content-Type': 'application/json' 82 | } 83 | 84 | } else { 85 | this.baseHeaders = { 86 | 'Token': this.apiToken, 87 | 'Content-Type': 'application/json' 88 | } 89 | } 90 | 91 | } 92 | //////////////// 93 | //1. Projects 94 | //////////////// 95 | 96 | /** 97 | * @pubilc 98 | * @name OptimizelyClient#createProject 99 | * @since 0.0.1 100 | * @description Create a project in Optimizely 101 | * @param {object} options An object with the following properties: 102 | * { 103 | * @param project_name 104 | * @param {string} [status = "Archived|Active"] 105 | * @param {boolean} [include_jquery = false] 106 | * @param {string} [ip_filter=""] 107 | * } 108 | * @returns {promise} A promise fulfilled with the created project 109 | * @example 110 | * oc.createProject({ 111 | * project_name:"sample project name", 112 | * project_status:"Active", 113 | * include_jquery:false, 114 | * ip_filter:"" 115 | * }) 116 | * .then(function(createdProject){ 117 | * //...do something with created project 118 | * }) 119 | * .then(null,function(error){ 120 | * //handle error 121 | * }) 122 | */ 123 | OptimizelyClient.prototype.createProject = function(options) { 124 | options = options || {}; 125 | options.project_name = options.project_name || ""; 126 | options.project_status = options.project_status || "Active"; 127 | options.include_jquery = !!options.include_jquery; 128 | //TODO:Check for truthiness 129 | options.ip_filter = options.ip_filter || ""; 130 | var postUrl = this.baseUrl + 'projects/'; 131 | return rest.postAsync(postUrl, { 132 | method: 'post', 133 | headers: this.baseHeaders, 134 | data: JSON.stringify(options) 135 | }) 136 | } 137 | /** 138 | * @pubilc 139 | * @name OptimizelyClient#getProject 140 | * @since 0.0.1 141 | * @description Retrieve a project from Optimizely 142 | * @param {object} options An object with the following properties: 143 | * { 144 | * @param {string} id 145 | * } 146 | * @note the id may be passed as a string instead of a member of an object 147 | */ 148 | OptimizelyClient.prototype.getProject = function(options) { 149 | if (typeof options === "string" || typeof options === "number") options = { 150 | id: options 151 | }; 152 | options = options || {}; 153 | options.id = String(options.id || ""); 154 | if (!options.id) throw new Error("required: options.id"); 155 | var theUrl = this.baseUrl + 'projects/' + options.id; 156 | return rest.getAsync(theUrl, { 157 | method: 'get', 158 | headers: this.baseHeaders 159 | }); 160 | } 161 | /** 162 | * @public 163 | * @name OptimizelyClient#updateProject 164 | * @since 0.2.0 165 | * @description Update an Existing Project in Optimizely 166 | * @param {object} options object with the following properties: 167 | * { 168 | * @param {String} id 169 | * @param {String} [project_name] 170 | * @param {String} [project_status = "Active|Archived"] 171 | * @param {Boolean} [include_jquery] 172 | * @param {String} [project_javascript] 173 | * @param {Boolean} [enable_force_variation] 174 | * @param {Boolean} [exclude_disabled_experiments] 175 | * @param {Boolean} [exclude_names] 176 | * @param {Boolean} [ip_anonymization] 177 | * @param {String} [ip_filtering] 178 | * } 179 | * @return {promise} A promise fulfilled with the updated project 180 | */ 181 | OptimizelyClient.prototype.updateProject = function(options) { 182 | options = options || {}; 183 | options.id = options.id || false; 184 | if(!options.id) throw new Error('required: options.id'); 185 | var putUrl = this.baseUrl + 'projects/' + options.id; 186 | return rest.putAsync(putUrl, { 187 | method: 'put', 188 | headers: this.baseHeaders, 189 | data: JSON.stringify(options) 190 | }); 191 | } 192 | /** 193 | * @public 194 | * @name OptimizelyClient#getProjectList 195 | * @since 0.1.0 196 | * @description Retrieves a list of projects from Optimizely 197 | * @return {promise} A promise fulfilled with an array of all projects 198 | * 199 | */ 200 | OptimizelyClient.prototype.getProjects = function(){ 201 | var theUrl = this.baseUrl + 'projects/'; 202 | return rest.getAsync(theUrl, { 203 | method: 'get', 204 | headers: this.baseHeaders 205 | }); 206 | } 207 | 208 | //////////////// 209 | //2. Experiments 210 | //////////////// 211 | 212 | /** 213 | *@pubilc 214 | *@name OptimizelyClient#createExperiment 215 | *@since 0.0.1 216 | *@description create an experiment in Optimizely 217 | */ 218 | OptimizelyClient.prototype.createExperiment = function(options) { 219 | options = options || {}; 220 | options.description = options.description || ""; 221 | options.project_id = options.project_id || ""; 222 | options.edit_url = options.edit_url || ""; 223 | options.custom_css = options.custom_css || ""; 224 | options.custom_js = options.custom_js || ""; 225 | if (!options.edit_url) throw new Error("Required: options.edit_url"); 226 | if (!options.project_id) throw new Error("Required: options.project_id"); 227 | var postUrl = this.baseUrl + 'projects/' + options.project_id + 228 | '/experiments/'; 229 | delete options.project_id; 230 | return rest.postAsync(postUrl, { 231 | method: 'post', 232 | headers: this.baseHeaders, 233 | data: JSON.stringify(options) 234 | }) 235 | } 236 | /** 237 | *@pubilc 238 | *@name OptimizelyClient#getExperiment 239 | *@since 0.0.1 240 | *@description Retrieve an experiment by id/object.id 241 | *@param {object} options An object with the following properties: 242 | *{ 243 | * @param id 244 | *} 245 | *@note the id may be passed as a string instead of a member of an object 246 | */ 247 | OptimizelyClient.prototype.getExperiment = function(options) { 248 | if (typeof options === "string" || typeof options === "number") options = { 249 | id: options 250 | }; 251 | options = options || {}; 252 | options.id = String(options.id || ""); 253 | if (!options.id) throw new Error("required: options.id"); 254 | var theUrl = this.baseUrl + 'experiments/' + options.id; 255 | return rest.getAsync(theUrl, { 256 | method: 'get', 257 | headers: this.baseHeaders 258 | }); 259 | } 260 | /** 261 | *@pubilc 262 | *@name OptimizelyClient#updateExperiment 263 | *@since 0.0.1 264 | *@description Update an experiment 265 | *@param {object} options An object with the following properties: 266 | *{ 267 | * @param id 268 | * @param {string} [status = "Draft|Active"] 269 | * @param {boolean} [include_jquery = false] 270 | * @param {string} [ip_filter=""] 271 | *} 272 | */ 273 | OptimizelyClient.prototype.updateExperiment = function(options) { 274 | options = options || {}; 275 | options.id = String(options.id || ""); 276 | options.description = options.description || ""; 277 | options.edit_url = options.edit_url || ""; 278 | options.custom_css = options.custom_css || ""; 279 | options.custom_js = options.custom_js || ""; 280 | if (!options.id) throw new Error("required: options.id"); 281 | var theUrl = this.baseUrl + 'experiments/' + options.id; 282 | delete options.id 283 | return rest.putAsync(theUrl, { 284 | method: 'put', 285 | headers: this.baseHeaders, 286 | data: JSON.stringify(options) 287 | }) 288 | } 289 | /** 290 | *@pubilc 291 | *@name OptimizelyClient#pushExperiment 292 | *@since 0.2.0 293 | *@description Create or update an experiment based on presence of an id 294 | *@param {object} options An object with the following properties: 295 | *{ 296 | * @param See createExperiment and updateExperiment 297 | 298 | *} 299 | */ 300 | OptimizelyClient.prototype.pushExperiment = function(options) { 301 | options = options || {}; 302 | options.id = options.id || ""; 303 | return options.id ? 304 | this.updateExperiment(options): 305 | this.createExperiment(options); 306 | } 307 | /** 308 | *@pubilc 309 | *@name OptimizelyClient#getExperiments 310 | *@since 0.0.1 311 | *@description Retrieve all experiments associatd with a given project 312 | *@param {object} options An object with the following properties: 313 | *{ 314 | * @param {string} project_id 315 | *} 316 | *@note the id may be passed as a string instead of a member of an object 317 | */ 318 | OptimizelyClient.prototype.getExperiments = function(options) { 319 | if (typeof options === "string" || typeof options === "number") options = { 320 | project_id: options 321 | }; 322 | options = options || {}; 323 | options.project_id = String(options.project_id || ""); 324 | if (!options.project_id) throw new Error("required: options.project_id"); 325 | var theUrl = this.baseUrl + 'projects/' + options.project_id + 326 | '/experiments/'; 327 | return rest.getAsync(theUrl, { 328 | method: 'get', 329 | headers: this.baseHeaders 330 | }) 331 | } 332 | 333 | /** 334 | *@pubilc 335 | *@name OptimizelyClient#updateExperimentByDescription 336 | *@since 0.0.1 337 | *@description Get an experiment description 338 | *@param {object} options An object with the following properties: 339 | *{ 340 | * @param project_name 341 | * @param {string} [status = "Draft|Active"] 342 | * @param {boolean} [include_jquery = false] 343 | * @param {string} [ip_filter=""] 344 | *} 345 | */ 346 | OptimizelyClient.prototype.getExperimentByDescription = function(options) { 347 | if (typeof options === "string") options = { 348 | project_id: options 349 | }; 350 | options = options || {}; 351 | options.project_id = String(options.project_id || ""); 352 | options.description = options.description || arguments[1]; 353 | if (!options.project_id) throw new Error("Required: options.project_id"); 354 | if (!options.description) throw new Error("Required: options.description"); 355 | return this.getExperiments(options.project_id).then(function(data) { 356 | if (typeof data === "string") data = JSON.parse(data); 357 | for (var i in data) { 358 | if (data[i]['description'] === 359 | options.description) { 360 | return data[i]; 361 | }; 362 | } 363 | return null; 364 | }) 365 | } 366 | /** 367 | *@pubilc 368 | *@name OptimizelyClient#deleteExperiment 369 | *@since 0.2.0 370 | *@description Delete an experiment by id/object.id 371 | *@param {object} options An object with the following properties: 372 | *{ 373 | * @param id 374 | *} 375 | *@note the id may be passed as a string/number instead of a member of an object 376 | */ 377 | OptimizelyClient.prototype.deleteExperiment = function(options) { 378 | if (typeof options === "string" || typeof options === "number") options = { 379 | id: options 380 | }; 381 | options = options || {}; 382 | options.id = String(options.id || ""); 383 | if (!options.id) throw new Error("required: options.id"); 384 | var theUrl = this.baseUrl + 'experiments/' + options.id; 385 | return rest.delAsync(theUrl, { 386 | method: 'delete', 387 | headers: this.baseHeaders 388 | }); 389 | } 390 | /** 391 | * @public 392 | * @name OptimizelyClient#getResults 393 | * @since 0.4.0 394 | * @description get non-stats engine results 395 | * @param {object} options An object with the following properties: 396 | * { 397 | * @param {String} id Experiment ID 398 | * @param {object} [dimension = {}] An object with the following properties: 399 | * { 400 | * @param {String} id Dimension ID 401 | * @param {String} value Dimension Value 402 | * } 403 | * } 404 | * @note the id may be passed as a string/number instead of a member of an object 405 | */ 406 | OptimizelyClient.prototype.getResults = function(options) { 407 | if (typeof options === "string" || typeof options === "number") options = { 408 | id: options 409 | }; 410 | options = options || {}; 411 | options.id = String(options.id || ""); 412 | if (!options.id) throw new Error("required: options.id"); 413 | var theUrl = this.baseUrl + 'experiments/' + options.id + '/results'; 414 | if (options.dimension) { 415 | var urlParameters = "?"; 416 | if (!options.dimension.id) throw new Error("required: options.dimension.id"); 417 | if (!options.dimension.value) throw new Error("required: options.dimension.value"); 418 | urlParameters += "dimension_id=" + encodeURIComponent(options.dimension.id); 419 | urlParameters += "&dimension_value=" + encodeURIComponent(options.dimension.value); 420 | theUrl += urlParameters; 421 | } 422 | return rest.getAsync(theUrl, { 423 | method: 'GET', 424 | headers: this.baseHeaders 425 | }); 426 | } 427 | /** 428 | * @public 429 | * @name OptimizelyClient#getStats 430 | * @since 0.4.0 431 | * @description get stats engine results 432 | * @param {object} options An object with the following properties: 433 | * { 434 | * @param {String} id Experiment ID 435 | * @param {object} [dimension = {}] An object with the following properties: 436 | * { 437 | * @param {String} id Dimension ID 438 | * @param {String} value Dimension Value 439 | * } 440 | * } 441 | * @note the id may be passed as a string/number instead of a member of an object 442 | */ 443 | OptimizelyClient.prototype.getStats = function(options) { 444 | if (typeof options === "string" || typeof options === "number") options = { 445 | id: options 446 | }; 447 | options = options || {}; 448 | options.id = String(options.id || ""); 449 | if (!options.id) throw new Error("required: options.id"); 450 | var theUrl = this.baseUrl + 'experiments/' + options.id + '/stats'; 451 | if (options.dimension) { 452 | var urlParameters = "?"; 453 | if (!options.dimension.id) throw new Error("required: options.dimension.id"); 454 | if (!options.dimension.value) throw new Error("required: options.dimension.value"); 455 | urlParameters += "dimension_id=" + encodeURIComponent(options.dimension.id); 456 | urlParameters += "&dimension_value=" + encodeURIComponent(options.dimension.value); 457 | theUrl += urlParameters; 458 | } 459 | return rest.getAsync(theUrl, { 460 | method: 'GET', 461 | headers: this.baseHeaders 462 | }); 463 | } 464 | //////////////// 465 | //3. Variations 466 | //////////////// 467 | /** 468 | *@pubilc 469 | *@name OptimizelyClient#createVariation 470 | *@since 0.0.1 471 | *@description create an experiment in Optimizely 472 | *@param {object} options An object with the following properties: 473 | *{ 474 | * @param {string|number} experiment_id 475 | * @param {string} [descriptions = ""] 476 | *} 477 | */ 478 | OptimizelyClient.prototype.createVariation = function(options) { 479 | options = options || {}; 480 | options.experiment_id = String(options.experiment_id || ""); 481 | options.description = options.description || ""; 482 | if (!options.experiment_id) throw new Error( 483 | "Required: options.experiment_id"); 484 | var postUrl = this.baseUrl + 'experiments/' + options.experiment_id + 485 | '/variations/'; 486 | delete options.experiment_id; 487 | return rest.postAsync(postUrl, { 488 | method: 'post', 489 | headers: this.baseHeaders, 490 | data: JSON.stringify(options) 491 | }) 492 | } 493 | /** 494 | *@pubilc 495 | *@name OptimizelyClient#getVariation 496 | *@since 0.0.1 497 | *@description read a variation in Optimizely 498 | *@param {object} options An object with the following properties: 499 | *{ 500 | * @param {string|number} id 501 | *} 502 | */ 503 | OptimizelyClient.prototype.getVariation = function(options) { 504 | if (typeof options === "string") options = { 505 | id: options 506 | }; 507 | if (!options.id) throw new Error("Required: options.id"); 508 | var theUrl = this.baseUrl + 'variations/' + options.id; 509 | return rest.getAsync(theUrl, { 510 | method: 'get', 511 | headers: this.baseHeaders 512 | }) 513 | } 514 | 515 | /** 516 | * @pubilc 517 | * @name OptimizelyClient#updateVariation 518 | * @since 0.0.1 519 | * @description Update a variation in Optimizely 520 | * @param {object} options An object with the following properties: 521 | * { 522 | * @param {string|number} id 523 | * @param {string} [description] 524 | * } 525 | */ 526 | OptimizelyClient.prototype.updateVariation = function(options) { 527 | var optionsToUpdate = {}; 528 | options = options || {}; 529 | if (!options.id) throw new Error( 530 | "Required: options.id"); 531 | optionsToUpdate.id = options.id || ""; 532 | optionsToUpdate.description = options.description || ""; 533 | optionsToUpdate.js_component = options.js_component || ""; 534 | var theUrl = this.baseUrl + 'variations/' + options.id; 535 | return rest.putAsync(theUrl, { 536 | method: 'put', 537 | headers: this.baseHeaders, 538 | data: JSON.stringify(optionsToUpdate) 539 | }) 540 | } 541 | /** 542 | * @pubilc 543 | * @name OptimizelyClient#pushVariation 544 | * @since 0.2.0 545 | * @description Create or update a variation based on the presence of an id 546 | * @param {object} options An object with the following properties: 547 | * { 548 | * @param See createVariation and updateVariaion 549 | * } 550 | */ 551 | OptimizelyClient.prototype.pushVariation = function(options) { 552 | options = options || {}; 553 | options.id = options.id || ""; 554 | return options.id ? 555 | this.updateVariation(options): 556 | this.createVariation(options); 557 | } 558 | /** 559 | * @pubilc 560 | * @name OptimizelyClient#deleteVariation 561 | * @since 0.1.0 562 | * @description delete a variation in Optimizely 563 | * @param {object} options An object with the following properties: 564 | * { 565 | * @param {string|number} id 566 | * } 567 | */ 568 | OptimizelyClient.prototype.deleteVariation = function(options) { 569 | if (typeof options === "string") options = { 570 | id: options 571 | }; 572 | if (!options.id) throw new Error("Required: options.id"); 573 | var theUrl = this.baseUrl + 'variations/' + options.id; 574 | return rest.delAsync(theUrl, { 575 | method: 'delete', 576 | headers: this.baseHeaders 577 | }) 578 | } 579 | 580 | //////////////// 581 | //4. Audiences 582 | //////////////// 583 | /** 584 | * @pubilc 585 | * @name OptimizelyClient#getAudience 586 | * @since 0.4.0 587 | * @description Read an audience in Optimizely 588 | * @param {object} options An object with the following properties: 589 | * { 590 | * @param {string|number} id The Audience ID 591 | * } 592 | * @returns {promise} A promise fulfilled with the Audience 593 | * @note the id may be passed as a string instead of a member of an object 594 | */ 595 | OptimizelyClient.prototype.getAudience = function(options) { 596 | if (typeof options === "string") options = { 597 | id: options 598 | }; 599 | if (!options.id) throw new Error("Required: options.id"); 600 | var theUrl = this.baseUrl + 'audiences/' + options.id; 601 | return rest.getAsync(theUrl, { 602 | method: 'get', 603 | headers: this.baseHeaders 604 | }); 605 | } 606 | /** 607 | * @pubilc 608 | * @name OptimizelyClient#createAudience 609 | * @since 0.4.0 610 | * @description Create an Audience in Optimizely 611 | * @param {object} options An object with the following properties: 612 | * { 613 | * @param {String} id Project ID 614 | * @param {String} name 615 | * @param {String} [description] 616 | * @param {Boolean} [segmentation] Only available for platinum 617 | * @param {Array} [conditions] See http://developers.optimizely.com/rest/conditions/ 618 | * } 619 | * @returns {promise} A promise fulfilled with the created project 620 | */ 621 | OptimizelyClient.prototype.createAudience = function(options) { 622 | var optionsToSend = {}; 623 | options = options || {}; 624 | if (!options.name) throw new Error("Required: options.name"); 625 | if (!options.id) throw new Error("Required: options.id"); 626 | 627 | optionsToSend.name = options.name; 628 | optionsToSend.id = options.id; 629 | optionsToSend.description = options.description || ""; 630 | optionsToSend.segmentation = options.segmentation || false; 631 | optionsToSend.conditions = options.conditions || []; 632 | 633 | var postUrl = this.baseUrl + 'projects/' + options.id + '/audiences/'; 634 | return rest.postAsync(postUrl, { 635 | method: 'post', 636 | headers: this.baseHeaders, 637 | data: JSON.stringify(optionsToSend) 638 | }) 639 | } 640 | /** 641 | * @public 642 | * @name OptimizelyClient#updateAudience 643 | * @since 0.4.0 644 | * @description Update an Existing Project in Optimizely 645 | * @param {object} options object with the following properties: 646 | * { 647 | * @param {String} id 648 | * @param {String} [name] 649 | * @param {Array} [conditions] See http://developers.optimizely.com/rest/conditions/ 650 | * @param {Boolean} [segmentation] Platinum Customers only 651 | * } 652 | * @return {promise} A promise fulfilled with the updated audience 653 | */ 654 | OptimizelyClient.prototype.updateAudience = function(options) { 655 | options = options || {}; 656 | options.id = options.id || false; 657 | if(!options.id) throw new Error('required: options.id'); 658 | var putUrl = this.baseUrl + 'audiences/' + options.id; 659 | return rest.putAsync(putUrl, { 660 | method: 'put', 661 | headers: this.baseHeaders, 662 | data: JSON.stringify(options) 663 | }); 664 | } 665 | /** 666 | * @public 667 | * @name OptimizelyClient#getAudiences 668 | * @since 0.4.0 669 | * @description Retrieves a list of Audiences in a project from Optimizely 670 | * @param {object} options An object with the following properties: 671 | * { 672 | * @param {string|number} id The Project ID 673 | * } 674 | * @return {promise} A promise fulfilled with an array of all Audiences 675 | * 676 | */ 677 | OptimizelyClient.prototype.getAudiences = function(options){ 678 | if (typeof options === "string" || typeof options === "number") options = { 679 | id: options 680 | }; 681 | options = options || {}; 682 | options.id = options.id || ""; 683 | if (!options.id) throw new Error("required: options.id"); 684 | var theUrl = this.baseUrl + 'projects/' + options.id + '/audiences/'; 685 | return rest.getAsync(theUrl, { 686 | method: 'get', 687 | headers: this.baseHeaders 688 | }); 689 | } 690 | 691 | //////////////// 692 | //5. Dimensions 693 | //////////////// 694 | /** 695 | * @pubilc 696 | * @name OptimizelyClient#getDimension 697 | * @since 0.5.0 698 | * @description Read a dimension in Optimizely 699 | * @param {object} options An object with the following properties: 700 | * { 701 | * @param {string|number} id The Dimension ID 702 | * } 703 | * @returns {promise} A promise fulfilled with the Dimension 704 | * @note the id may be passed as a string instead of a member of an object 705 | */ 706 | OptimizelyClient.prototype.getDimension = function(options) { 707 | if (typeof options === "string") options = { 708 | id: options 709 | }; 710 | if (!options.id) throw new Error("Required: options.id"); 711 | var theUrl = this.baseUrl + 'dimensions/' + options.id; 712 | return rest.getAsync(theUrl, { 713 | method: 'get', 714 | headers: this.baseHeaders 715 | }); 716 | } 717 | /** 718 | * @pubilc 719 | * @name OptimizelyClient#createDimension 720 | * @since 0.5.0 721 | * @description Create an Dimension in Optimizely 722 | * @param {object} options An object with the following properties: 723 | * { 724 | * @param {String} id Project ID 725 | * @param {String} name 726 | * @param {String} [description] 727 | * @param {Boolean} [client_api_name] A unique name to refer to this dimension 728 | * } 729 | * @returns {promise} A promise fulfilled with the created project 730 | */ 731 | OptimizelyClient.prototype.createDimension = function(options) { 732 | var optionsToSend = {}; 733 | options = options || {}; 734 | if (!options.name) throw new Error("Required: options.name"); 735 | if (!options.id) throw new Error("Required: options.id"); 736 | 737 | optionsToSend.name = options.name; 738 | optionsToSend.id = options.id; 739 | optionsToSend.description = options.description || ""; 740 | optionsToSend.client_api_name = options.client_api_name || ""; 741 | 742 | var postUrl = this.baseUrl + 'projects/' + options.id + '/dimensions/'; 743 | return rest.postAsync(postUrl, { 744 | method: 'post', 745 | headers: this.baseHeaders, 746 | data: JSON.stringify(optionsToSend) 747 | }) 748 | } 749 | /** 750 | * @public 751 | * @name OptimizelyClient#updateDimension 752 | * @since 0.5.0 753 | * @description Update an Existing Dimension in Optimizely 754 | * @param {object} options object with the following properties: 755 | * { 756 | * @param {String} id 757 | * @param {String} [name] 758 | * @param {String} [description] 759 | * @param {Boolean} [client_api_name] A unique name to refer to this dimension 760 | * } 761 | * @return {promise} A promise fulfilled with the updated audience 762 | */ 763 | OptimizelyClient.prototype.updateDimension = function(options) { 764 | options = options || {}; 765 | options.id = options.id || false; 766 | if(!options.id) throw new Error('required: options.id'); 767 | var putUrl = this.baseUrl + 'dimensions/' + options.id; 768 | return rest.putAsync(putUrl, { 769 | method: 'put', 770 | headers: this.baseHeaders, 771 | data: JSON.stringify(options) 772 | }); 773 | } 774 | /** 775 | * @public 776 | * @name OptimizelyClient#getDimensions 777 | * @since 0.5.0 778 | * @description Retrieves a list of Dimensions in a project from Optimizely 779 | * @param {object} options An object with the following properties: 780 | * { 781 | * @param {string|number} id The Project ID 782 | * } 783 | * @return {promise} A promise fulfilled with an array of all Audiences 784 | * 785 | */ 786 | OptimizelyClient.prototype.getDimensions = function(options){ 787 | if (typeof options === "string" || typeof options === "number") options = { 788 | id: options 789 | }; 790 | options = options || {}; 791 | options.id = options.id || ""; 792 | if (!options.id) throw new Error("required: options.id"); 793 | var theUrl = this.baseUrl + 'projects/' + options.id + '/dimensions/'; 794 | return rest.getAsync(theUrl, { 795 | method: 'get', 796 | headers: this.baseHeaders 797 | }); 798 | } 799 | 800 | //////////////// 801 | //6. Goals 802 | //////////////// 803 | 804 | /** 805 | * @public 806 | * @name OptimizelyClient#getGoals 807 | * @since 0.5.0 808 | * @description Retrieves a list of Goals in a project from Optimizely 809 | * @param {object} options An object with the following properties: 810 | * { 811 | * @param {string|number} id The Project ID 812 | * } 813 | * @return {promise} A promise fulfilled with an array of all Goals 814 | * 815 | */ 816 | OptimizelyClient.prototype.getGoals = function(options){ 817 | if (typeof options === "string" || typeof options === "number") options = { 818 | id: options 819 | }; 820 | options = options || {}; 821 | options.id = options.id || ""; 822 | if (!options.id) throw new Error("required: options.id"); 823 | var theUrl = this.baseUrl + 'projects/' + options.id + '/goals/'; 824 | return rest.getAsync(theUrl, { 825 | method: 'get', 826 | headers: this.baseHeaders 827 | }); 828 | } 829 | module.exports = OptimizelyClient; 830 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "optimizely-node-client", 3 | "version": "0.6.2", 4 | "description": "Optimizely Client for Node", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha test" 8 | }, 9 | "keywords": [ 10 | "optimizely", 11 | "rest", 12 | "api", 13 | "client" 14 | ], 15 | "author": { 16 | "name": "John Henry", 17 | "email": "john@iamjohnhenry.com" 18 | }, 19 | "contributors": [ 20 | "Nathan Selvidge (http://www.nathanselvidge.com)", 21 | "Nathan Heaps (http://www.github.com/nsheaps)", 22 | "James Marchant (https://github.com/JWMarchant)" 23 | ], 24 | "license": "MIT", 25 | "repository": { 26 | "type": "git", 27 | "url": "https://github.com/funnelenvy/optimizely-node.git" 28 | }, 29 | "homepage": "https://github.com/funnelenvy/optimizely-node", 30 | "dependencies": { 31 | "bluebird": "^2.3.2", 32 | "lodash": "^2.4.1", 33 | "restler": "^3.2.2" 34 | }, 35 | "devDependencies": { 36 | "hat": "0.0.3", 37 | "mocha": "^2.1.0", 38 | "nock": "^0.56.0" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | #Optimizely Node Client 2 | 3 | Access the [Optimizely REST API][opt-api] via javascript 4 | 5 | ### Installation 6 | 7 | ```bash 8 | $ npm install optimizely-node-client 9 | ``` 10 | 11 | ### Usage 12 | 13 | ```js 14 | var OptimizelyClient = require('optimizely-node-client'); 15 | var API_TOKEN = "*";//Get token from www.optimizely.com/tokens 16 | var oc = new OptimizelyClient(API_TOKEN); 17 | ``` 18 | 19 | ```js 20 | // OAuth2 token 21 | var OptimizelyClient = require('optimizely-node-client'); 22 | var API_TOKEN = "*";//Get token from www.optimizely.com/tokens 23 | var oc = new OptimizelyClient(API_TOKEN, { OAuth2: true }); 24 | ``` 25 | 26 | ### Example 27 | ```js 28 | oc.createProject({/*...project properties*/}) 29 | 30 | ``` 31 | ## Contributing 32 | 33 | Please see [contributing.md](contributing.md). 34 | 35 | ## Copyright and license 36 | 37 | Code copyright 2015 Celerius Group Inc. Released under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0). 38 | 39 | [opt-api]:http://developers.optimizely.com/rest/ 40 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var OptimizelyClient = require("../lib/OptimizelyClient"); 2 | var hat = require("hat") 3 | var assert = require("assert"); 4 | var nock = require("nock"); 5 | var token = "84111b0b811e12e1543e6e53a672b5b3:f7534f87"; 6 | 7 | var stripPathEnd = function(path) { 8 | var index = path.lastIndexOf("/"); 9 | return path.substr(index + 1); 10 | } 11 | var PROJECTID = hat(); 12 | var EXPERIMENTID = hat(); 13 | var VARIATIONID = hat(); 14 | var AUDIENCEID = hat(); 15 | var DIMENSIONID = hat(); 16 | var GOALSID = hat(); 17 | var PROJECTNAME = "PROJECTNAME"; 18 | var AUDIENCENAME = "AUDIENCENAME"; 19 | var DIMENSIONNAME = "DIMENSIONNAME"; 20 | var GOALSNAME = "GOALSNAME"; 21 | var EXPERIMENTDESCRIPTION = "DESCRIPTION OF EXPERIMENT"; 22 | var VARIATIONDESCRIPTION = "DESCRIPTION OF VARIATION"; 23 | var baseUrl = 'https://www.optimizelyapis.com/experiment/v1'; 24 | var EDITURL = 'https://www.google.com'; 25 | var FUNNELENVYERROR = "FunnelEnvy is not at fault. YOU did something bad."; 26 | var FAILUREMESSAGE = "This should not be successful"; 27 | //////////////// 28 | //Mocs 29 | //////////////// 30 | var scope = nock(baseUrl); 31 | //Successful API Calls 32 | 33 | //////////////// 34 | //Tests 35 | //////////////// 36 | var client = new OptimizelyClient(token); 37 | describe("Successful API Calls", function() { 38 | //////////////// 39 | //Project Tests 40 | //////////////// 41 | describe("Projects", function() { 42 | scope.post('/projects/') //create 43 | .reply(201, function(uri, requestBody) { 44 | requestBody = JSON.parse(requestBody); 45 | requestBody.id = PROJECTID; 46 | return requestBody; 47 | }); 48 | it('should create a project', function(done) { 49 | var options = { 50 | "name": PROJECTNAME, 51 | "status": "ACTIVE", 52 | "ip_filter": "", 53 | "include_jquery": false 54 | } 55 | client.createProject(options) 56 | .then( 57 | function(project) { 58 | project = JSON.parse(project); 59 | assert.equal(project.id, PROJECTID); 60 | assert.equal(project.name, PROJECTNAME); 61 | assert.equal(project.status, "ACTIVE"); 62 | assert.equal(project.include_jquery, false); 63 | assert.equal(project.ip_filter, ""); 64 | done(); 65 | }, 66 | function(error) { 67 | done(error); 68 | } 69 | ) 70 | }); 71 | scope.get('/projects/' + PROJECTID) //get 72 | .reply(200, function(uri, requestBody) { 73 | return stripPathEnd(uri); 74 | }); 75 | it('should retrieve a project', function(done) { 76 | var options = { 77 | "id": PROJECTID 78 | } 79 | client.getProject(options) 80 | .then( 81 | function(id) { 82 | assert.equal(id, options.id); 83 | done(); 84 | }, 85 | function(error) { 86 | done(error); 87 | } 88 | ) 89 | }); 90 | scope.put('/projects/' + PROJECTID) //update 91 | .reply(202, function(uri, requestBody) { 92 | requestBody.id = stripPathEnd(uri); 93 | return requestBody; 94 | }); 95 | it('should update a project', function(done){ 96 | var newProjectName = PROJECTNAME + '2'; 97 | var options = { 98 | 'id': PROJECTID, 99 | 'project_name': newProjectName 100 | } 101 | client.updateProject(options).then(function(reply){ 102 | reply = JSON.parse(reply); 103 | assert.equal(reply["id"], PROJECTID); 104 | assert.equal(reply["project_name"], newProjectName); 105 | done(); 106 | }, function (error){ 107 | done(error); 108 | }) 109 | }); 110 | scope.get('/projects/') //update 111 | .reply(200, function(uri, requestBody) { 112 | return [ { 113 | "project_id": PROJECTID, 114 | "project_name": PROJECTNAME 115 | } ]; 116 | }); 117 | it('should return a list of projects', function(done){ 118 | client.getProjects().then(function(reply){ 119 | reply = JSON.parse(reply); 120 | assert.equal(reply[0].project_id, PROJECTID); 121 | assert.equal(reply[0].project_name, PROJECTNAME); 122 | done(); 123 | }, function (error){ 124 | done(error); 125 | }) 126 | }) 127 | }); 128 | ////////////////// 129 | //Experiment Tests 130 | ////////////////// 131 | describe("Experiments", function() { 132 | scope.post('/projects/' + PROJECTID + "/experiments/") //create 133 | .reply(201, function(uri, requestBody) { 134 | requestBody = JSON.parse(requestBody); 135 | requestBody.id = EXPERIMENTID; 136 | return requestBody; 137 | }); 138 | it('should create an experiment', function(done) { 139 | var options = { 140 | "project_id": PROJECTID, 141 | "edit_url": EDITURL, 142 | "custom_css": "/*css comment*/", 143 | "custom_js": "//js comment" 144 | } 145 | client.createExperiment(options) 146 | .then( 147 | function(experiment) { 148 | experiment = JSON.parse(experiment); 149 | assert.equal(experiment.id, EXPERIMENTID); 150 | assert.equal(experiment.edit_url, EDITURL); 151 | assert.equal(experiment.custom_css, "/*css comment*/"); 152 | assert.equal(experiment.custom_js, "//js comment"); 153 | done(); 154 | }, 155 | function(error) { 156 | done(error); 157 | } 158 | ) 159 | }); 160 | scope.post('/projects/' + PROJECTID + "/experiments/") //create 161 | .reply(201, function(uri, requestBody) { 162 | requestBody = JSON.parse(requestBody); 163 | requestBody.id = EXPERIMENTID; 164 | return requestBody; 165 | }); 166 | it('should create an experiment (with pushExperiment)', function(done) { 167 | var options = { 168 | "project_id": PROJECTID, 169 | "edit_url": EDITURL, 170 | "custom_css": "/*css comment*/", 171 | "custom_js": "//js comment" 172 | } 173 | client.pushExperiment(options) 174 | .then( 175 | function(experiment) { 176 | experiment = JSON.parse(experiment); 177 | assert.equal(experiment.id, EXPERIMENTID); 178 | assert.equal(experiment.edit_url, EDITURL); 179 | assert.equal(experiment.custom_css, "/*css comment*/"); 180 | assert.equal(experiment.custom_js, "//js comment"); 181 | done(); 182 | }, 183 | function(error) { 184 | done(error); 185 | } 186 | ) 187 | }); 188 | scope.get('/experiments/' + EXPERIMENTID) //get 189 | .reply(200, function(uri, requestBody) { 190 | return stripPathEnd(uri); 191 | }); 192 | it('should retrieve an experiment', function(done) { 193 | var options = { 194 | "id": EXPERIMENTID 195 | } 196 | client.getExperiment(EXPERIMENTID) 197 | .then( 198 | function(id) { 199 | assert(id, options.id) 200 | done(); 201 | }, 202 | function(error) { 203 | done(error); 204 | } 205 | ) 206 | }); 207 | scope.put('/experiments/' + EXPERIMENTID) //update 208 | .reply(202, function(uri, requestBody) { 209 | requestBody.id = stripPathEnd(uri); 210 | return requestBody; 211 | }); 212 | it('should update an experiment', function(done) { 213 | var options = { 214 | "id": EXPERIMENTID, 215 | "description": "New " + EXPERIMENTDESCRIPTION 216 | }; 217 | client.updateExperiment(options) 218 | .then( 219 | function(experiment) { 220 | experiment = JSON.parse(experiment); 221 | assert.equal(experiment.description, options.description); 222 | done(); 223 | }, 224 | function(error) { 225 | done(error); 226 | } 227 | ) 228 | }); 229 | scope.put('/experiments/' + EXPERIMENTID) //update 230 | .reply(202, function(uri, requestBody) { 231 | requestBody.id = stripPathEnd(uri); 232 | return requestBody; 233 | }); 234 | it('should update an experiment (with pushExperiment)', function(done) { 235 | var options = { 236 | "id": EXPERIMENTID, 237 | "description": "New " + EXPERIMENTDESCRIPTION 238 | }; 239 | client.pushExperiment(options) 240 | .then( 241 | function(experiment) { 242 | experiment = JSON.parse(experiment); 243 | assert.equal(experiment.description, options.description); 244 | done(); 245 | }, 246 | function(error) { 247 | done(error); 248 | } 249 | ) 250 | }); 251 | scope.get('/projects/' + PROJECTID + '/experiments/') //get multiple 252 | .reply(200, function(uri, requestBody) { 253 | return [{ 254 | "project_id": PROJECTID, 255 | "description": EXPERIMENTDESCRIPTION 256 | }]; 257 | }); 258 | it('should retrieve a list of experiments', function(done) { 259 | var options = { 260 | "project_id": PROJECTID 261 | } 262 | client.getExperiments(options) 263 | .then( 264 | function(experiments) { 265 | experiments = JSON.parse(experiments); 266 | assert.equal(experiments[0].project_id, options.project_id); 267 | done(); 268 | }, 269 | function(error) { 270 | done(error); 271 | } 272 | ) 273 | }); 274 | scope.get('/projects/' + PROJECTID + '/experiments/') //get by description 275 | .reply(200, function(uri, requestBody) { 276 | return [{ 277 | "project_id": PROJECTID, 278 | "description": EXPERIMENTDESCRIPTION 279 | }]; 280 | }); 281 | it('should retrieve an experiment by description', function(done) { 282 | var options = { 283 | "project_id": PROJECTID, 284 | "description": EXPERIMENTDESCRIPTION 285 | }; 286 | client.getExperimentByDescription(options) 287 | .then( 288 | function(experiment) { 289 | assert.equal(experiment.description, 290 | options.description); 291 | done(); 292 | }, 293 | function(error) { 294 | done(error); 295 | } 296 | ) 297 | }); 298 | scope.intercept('/experiments/' + EXPERIMENTID, 'DELETE') 299 | .reply(204, function(uri, requestBody) { 300 | return requestBody; 301 | }); 302 | it('should delete an experiment', function(done) { 303 | var options = { 304 | "project_id": PROJECTID, 305 | "id": EXPERIMENTID 306 | }; 307 | client.deleteExperiment(options) 308 | .then( 309 | function(reply) { 310 | done(); 311 | }, 312 | function(error) { 313 | done(error); 314 | } 315 | ) 316 | }); 317 | scope.intercept('/experiments/' + EXPERIMENTID + '/results', 'GET') 318 | .reply(200, function(uri, requestBody) { 319 | return requestBody; 320 | }); 321 | it('should get results', function(done) { 322 | var options = { 323 | "id": EXPERIMENTID 324 | }; 325 | client.getResults(options) 326 | .then( 327 | function(reply) { 328 | done(); 329 | }, 330 | function(error) { 331 | done(error); 332 | } 333 | ) 334 | }); 335 | scope.intercept('/experiments/' + EXPERIMENTID + '/stats', 'GET') 336 | .reply(200, function(uri, requestBody) { 337 | return requestBody; 338 | }); 339 | it('should get stats', function(done) { 340 | var options = { 341 | "id": EXPERIMENTID 342 | }; 343 | client.getStats(options) 344 | .then( 345 | function(reply) { 346 | done(); 347 | }, 348 | function(error) { 349 | done(error); 350 | } 351 | ) 352 | }); 353 | }); 354 | ////////////////// 355 | //Variation Tests 356 | ////////////////// 357 | describe("Variations", function() { 358 | scope.post('/experiments/' + EXPERIMENTID + '/variations/') //create 359 | .reply(201, function(uri, requestBody) { 360 | requestBody = JSON.parse(requestBody); 361 | requestBody.id = VARIATIONID; 362 | return requestBody; 363 | }); 364 | it('should create a variation', function(done) { 365 | var options = { 366 | "experiment_id": EXPERIMENTID, 367 | "description": "Variation Description" 368 | } 369 | client.createVariation(options) 370 | .then( 371 | function(variation) { 372 | variation = JSON.parse(variation); 373 | assert.equal(variation.description, 374 | "Variation Description"); 375 | done(); 376 | }, 377 | function(error) { 378 | done(error); 379 | } 380 | ) 381 | }); 382 | scope.post('/experiments/' + EXPERIMENTID + '/variations/') //create 383 | .reply(201, function(uri, requestBody) { 384 | requestBody = JSON.parse(requestBody); 385 | requestBody.id = VARIATIONID; 386 | return requestBody; 387 | }); 388 | it('should create a variation (with pushVariation)', function(done) { 389 | var options = { 390 | "experiment_id": EXPERIMENTID, 391 | "description": "Variation Description" 392 | } 393 | client.pushVariation(options) 394 | .then( 395 | function(variation) { 396 | variation = JSON.parse(variation); 397 | assert.equal(variation.description, 398 | "Variation Description"); 399 | done(); 400 | }, 401 | function(error) { 402 | done(error); 403 | } 404 | ) 405 | }); 406 | scope.get('/variations/' + VARIATIONID) //get 407 | .reply(200, function(uri, requestBody) { 408 | return stripPathEnd(uri); 409 | }); 410 | it('should retrieve a variation', function(done) { 411 | client.getVariation(VARIATIONID) 412 | .then( 413 | function(id) { 414 | assert.equal(id, VARIATIONID); 415 | done(); 416 | }, 417 | function(error) { 418 | done(error); 419 | } 420 | ) 421 | }); 422 | scope.put('/variations/' + VARIATIONID) //update 423 | .reply(202, function(uri, requestBody) { 424 | requestBody = JSON.parse(requestBody); 425 | requestBody.id = VARIATIONID; 426 | return requestBody; 427 | }); 428 | it('should update a variation', function(done) { 429 | var options = { 430 | "id": VARIATIONID, 431 | "description": "New " + "Variation Description" 432 | } 433 | client.updateVariation(options) 434 | .then( 435 | function(variation) { 436 | variation = JSON.parse(variation); 437 | assert.equal(variation.description, 438 | "New " + "Variation Description"); 439 | done(); 440 | }, 441 | function(error) { 442 | done(error); 443 | } 444 | ) 445 | }); 446 | scope.put('/variations/' + VARIATIONID) //update 447 | .reply(202, function(uri, requestBody) { 448 | requestBody = JSON.parse(requestBody); 449 | requestBody.id = VARIATIONID; 450 | return requestBody; 451 | }); 452 | it('should update a variation (with pushVariation)', function(done) { 453 | var options = { 454 | "id": VARIATIONID, 455 | "description": "New " + "Variation Description" 456 | }; 457 | client.pushVariation(options) 458 | .then( 459 | function(variation) { 460 | variation = JSON.parse(variation); 461 | assert.equal(variation.description, 462 | "New " + "Variation Description"); 463 | done(); 464 | }, 465 | function(error) { 466 | done(error); 467 | } 468 | ) 469 | }); 470 | scope.intercept('/variations/' + VARIATIONID, 'DELETE') 471 | .reply(204, function(uri, requestBody) { 472 | return; 473 | }); 474 | it('should delete a variation', function(done) { 475 | var options = { 476 | "id": VARIATIONID 477 | }; 478 | client.deleteVariation(options) 479 | .then( 480 | function(reply) { 481 | done(); 482 | }, 483 | function(error) { 484 | done(error); 485 | } 486 | ) 487 | }); 488 | }) 489 | ////////////////// 490 | //Audience Tests 491 | ////////////////// 492 | describe("Audiences", function() { 493 | /** 494 | * Set up the Audience Test Paths here 495 | */ 496 | before(function(){ 497 | scope.get('/audiences/' + AUDIENCEID) //get 498 | .reply(200, function(uri, requestBody) { 499 | return stripPathEnd(uri); 500 | }); 501 | scope.post('/projects/' + PROJECTID + '/audiences/') //create 502 | .reply(201, function(uri, requestBody) { 503 | requestBody = JSON.parse(requestBody); 504 | requestBody.id = AUDIENCEID; 505 | return requestBody; 506 | }); 507 | scope.put('/audiences/' + AUDIENCEID) //update 508 | .reply(202, function(uri, requestBody) { 509 | requestBody = JSON.parse(requestBody); 510 | requestBody.id = AUDIENCEID; 511 | return requestBody; 512 | }); 513 | scope.get('/projects/' + PROJECTID + '/audiences/') //get 514 | .reply(200, function(uri, requestBody) { 515 | return [ { 516 | "id": AUDIENCEID, 517 | "name": AUDIENCENAME 518 | } ]; 519 | }); 520 | }); 521 | /** 522 | * Describe the Audience functions here 523 | */ 524 | it('should create an audience', function(done) { 525 | var options = { 526 | "id": PROJECTID, 527 | "name": AUDIENCENAME 528 | } 529 | client.createAudience(options) 530 | .then( 531 | function(audience) { 532 | audience = JSON.parse(audience); 533 | assert.equal(audience.name, 534 | AUDIENCENAME); 535 | done(); 536 | }, 537 | function(error) { 538 | done(error); 539 | } 540 | ) 541 | }); 542 | it('should get an audience', function(done) { 543 | var options = { 544 | "id": AUDIENCEID 545 | } 546 | client.getAudience(options) 547 | .then( 548 | function(id) { 549 | assert.equal(id, AUDIENCEID); 550 | done(); 551 | }, 552 | function(error) { 553 | done(error); 554 | } 555 | ) 556 | }); 557 | it('should update a audience', function(done) { 558 | var options = { 559 | "id": AUDIENCEID, 560 | "name": "New " + AUDIENCENAME 561 | } 562 | client.updateAudience(options) 563 | .then( 564 | function(audience) { 565 | audience = JSON.parse(audience); 566 | assert.equal(audience.name, 567 | "New " + AUDIENCENAME); 568 | done(); 569 | }, 570 | function(error) { 571 | done(error); 572 | } 573 | ) 574 | }); 575 | it('should return a list of audiences', function(done){ 576 | var options = { 577 | "id": PROJECTID 578 | } 579 | client.getAudiences(options).then(function(reply){ 580 | reply = JSON.parse(reply); 581 | assert.equal(reply[0].id, AUDIENCEID); 582 | assert.equal(reply[0].name, AUDIENCENAME); 583 | done(); 584 | }, function (error){ 585 | done(error); 586 | }) 587 | }); 588 | }) 589 | ////////////////// 590 | //Dimension Tests 591 | ////////////////// 592 | describe("Dimensions", function() { 593 | /** 594 | * Set up the Audience Test Paths here 595 | */ 596 | before(function(){ 597 | scope.get('/dimensions/' + DIMENSIONID) //get 598 | .reply(200, function(uri, requestBody) { 599 | return stripPathEnd(uri); 600 | }); 601 | scope.post('/projects/' + PROJECTID + '/dimensions/') //create 602 | .reply(201, function(uri, requestBody) { 603 | requestBody = JSON.parse(requestBody); 604 | requestBody.id = DIMENSIONID; 605 | return requestBody; 606 | }); 607 | scope.put('/dimensions/' + DIMENSIONID) //update 608 | .reply(202, function(uri, requestBody) { 609 | requestBody = JSON.parse(requestBody); 610 | requestBody.id = DIMENSIONID; 611 | return requestBody; 612 | }); 613 | scope.get('/projects/' + PROJECTID + '/dimensions/') //get 614 | .reply(200, function(uri, requestBody) { 615 | return [ { 616 | "id": DIMENSIONID, 617 | "name": DIMENSIONNAME 618 | } ]; 619 | }); 620 | }); 621 | /** 622 | * Describe the Dimension functions here 623 | */ 624 | it('should create a dimension', function(done) { 625 | var options = { 626 | "id": PROJECTID, 627 | "name": DIMENSIONNAME 628 | } 629 | client.createDimension(options) 630 | .then( 631 | function(dimension) { 632 | dimension = JSON.parse(dimension); 633 | assert.equal(dimension.name, 634 | DIMENSIONNAME); 635 | done(); 636 | }, 637 | function(error) { 638 | done(error); 639 | } 640 | ) 641 | }); 642 | it('should get a dimension', function(done) { 643 | var options = { 644 | "id": DIMENSIONID 645 | } 646 | client.getDimension(options) 647 | .then( 648 | function(id) { 649 | assert.equal(id, DIMENSIONID); 650 | done(); 651 | }, 652 | function(error) { 653 | done(error); 654 | } 655 | ) 656 | }); 657 | it('should update a dimension', function(done) { 658 | var options = { 659 | "id": DIMENSIONID, 660 | "name": "New " + DIMENSIONNAME 661 | } 662 | client.updateDimension(options) 663 | .then( 664 | function(dimension) { 665 | dimension = JSON.parse(dimension); 666 | assert.equal(dimension.name, 667 | "New " + DIMENSIONNAME); 668 | done(); 669 | }, 670 | function(error) { 671 | done(error); 672 | } 673 | ) 674 | }); 675 | it('should return a list of dimensions', function(done){ 676 | var options = { 677 | "id": PROJECTID 678 | } 679 | client.getDimensions(options).then(function(reply){ 680 | reply = JSON.parse(reply); 681 | assert.equal(reply[0].id, DIMENSIONID); 682 | assert.equal(reply[0].name, DIMENSIONNAME); 683 | done(); 684 | }, function (error){ 685 | done(error); 686 | }) 687 | }); 688 | }) 689 | 690 | ////////////////// 691 | //Goals Tests 692 | ////////////////// 693 | describe("Goals", function() { 694 | /** 695 | * Set up the Goals Test Paths here 696 | */ 697 | before(function(){ 698 | scope.get('/projects/' + PROJECTID + '/goals/') //get 699 | .reply(200, function(uri, requestBody) { 700 | return [ { 701 | "id": GOALSID, 702 | "name": GOALSNAME 703 | } ]; 704 | }); 705 | 706 | }); 707 | /** 708 | * Describe the Goals functions here 709 | */ 710 | 711 | it('should return a list of goals', function(done){ 712 | var options = { 713 | "id": PROJECTID 714 | } 715 | client.getGoals(options).then(function(reply){ 716 | reply = JSON.parse(reply); 717 | assert.equal(reply[0].id, GOALSID); 718 | assert.equal(reply[0].name, GOALSNAME); 719 | done(); 720 | }, function (error){ 721 | done(error); 722 | }) 723 | }); 724 | }) 725 | 726 | }) 727 | 728 | 729 | //////////////////////// 730 | //Unsuccessful API Tests 731 | //////////////////////// 732 | describe("Unsuccessful API Calls", function() { 733 | ////////////////// 734 | //Project Tests 735 | ////////////////// 736 | describe("Projects", function() { 737 | scope.post('/projects/') //create 738 | .reply(400, function(uri, requestBody) { 739 | return { 740 | status: 400, 741 | message: FUNNELENVYERROR, 742 | uuid: hat() 743 | }; 744 | }); 745 | it('should not create a project', function(done) { 746 | var options = { 747 | "description": "Description" 748 | } 749 | client.createProject(options) 750 | .then( 751 | function(variation) { 752 | done(FAILUREMESSAGE); 753 | }, 754 | function(error) { 755 | error = JSON.parse(error); 756 | assert.equal(error.message, FUNNELENVYERROR); 757 | done(); 758 | } 759 | ) 760 | }); 761 | scope.get('/projects/' + PROJECTID) //get 762 | .reply(400, function(uri, requestBody) { 763 | return { 764 | status: 400, 765 | message: FUNNELENVYERROR, 766 | uuid: hat() 767 | }; 768 | }); 769 | it('should not retrieve a project', function(done) { 770 | var options = { 771 | "id": PROJECTID 772 | } 773 | client.getProject(options) 774 | .then( 775 | function(variation) { 776 | done(FAILUREMESSAGE); 777 | }, 778 | function(error) { 779 | error = JSON.parse(error); 780 | assert.equal(error.message, FUNNELENVYERROR); 781 | done(); 782 | } 783 | ) 784 | }); 785 | scope.put('/projects/' + PROJECTID) //update 786 | .reply(400, function(uri, requestBody) { 787 | requestBody.id = stripPathEnd(uri); 788 | return { 789 | status: 400, 790 | message: FUNNELENVYERROR, 791 | uuid: hat() 792 | }; 793 | }); 794 | it('should not update a project', function(done){ 795 | var newProjectName = PROJECTNAME + '2'; 796 | var options = { 797 | 'id': PROJECTID, 798 | 'project_name': newProjectName 799 | } 800 | client.updateProject(options).then(function(reply){ 801 | done(FAILUREMESSAGE); 802 | }, function (error){ 803 | error = JSON.parse(error); 804 | assert.equal(error.message, FUNNELENVYERROR); 805 | done(); 806 | }) 807 | }); 808 | scope.get('/projects/') //update 809 | .reply(400, function(uri, requestBody) { 810 | return { 811 | status: 400, 812 | message: FUNNELENVYERROR, 813 | uuid: hat() 814 | }; 815 | }); 816 | it('should not return a list of projects', function(done){ 817 | client.getProjects().then(function(reply){ 818 | done(FAILUREMESSAGE); 819 | }, function (error){ 820 | error = JSON.parse(error); 821 | assert.equal(error.message, FUNNELENVYERROR); 822 | done(); 823 | }); 824 | }) 825 | }); 826 | ////////////////// 827 | //Experiment Tests 828 | ////////////////// 829 | describe("Experiments", function() { 830 | scope.post('/projects/' + PROJECTID + "/experiments/") //create 831 | .reply(400, function(uri, requestBody) { 832 | return { 833 | status: 400, 834 | message: FUNNELENVYERROR, 835 | uuid: hat() 836 | }; 837 | }); 838 | it('should not create an experiment', function(done) { 839 | var options = { 840 | "project_id": PROJECTID, 841 | "edit_url": EDITURL, 842 | "custom_css": "/*css comment*/", 843 | "custom_js": "//js comment" 844 | } 845 | client.createExperiment(options) 846 | .then( 847 | function(variation) { 848 | done(FAILUREMESSAGE); 849 | }, 850 | function(error) { 851 | error = JSON.parse(error); 852 | assert.equal(error.message, FUNNELENVYERROR); 853 | done(); 854 | } 855 | ) 856 | }); 857 | scope.post('/projects/' + PROJECTID + "/experiments/") //create 858 | .reply(400, function(uri, requestBody) { 859 | return { 860 | status: 400, 861 | message: FUNNELENVYERROR, 862 | uuid: hat() 863 | }; 864 | }); 865 | it('should not create an experiment (with pushExperiment)', function(done) { 866 | var options = { 867 | "project_id": PROJECTID, 868 | "edit_url": EDITURL, 869 | "custom_css": "/*css comment*/", 870 | "custom_js": "//js comment" 871 | } 872 | client.pushExperiment(options) 873 | .then( 874 | function(variation) { 875 | done(FAILUREMESSAGE); 876 | }, 877 | function(error) { 878 | error = JSON.parse(error); 879 | assert.equal(error.message, FUNNELENVYERROR); 880 | done(); 881 | } 882 | ) 883 | }); 884 | scope.get('/experiments/' + EXPERIMENTID) //get 885 | .reply(400, function(uri, requestBody) { 886 | return { 887 | status: 400, 888 | message: FUNNELENVYERROR, 889 | uuid: hat() 890 | }; 891 | }); 892 | it('should not retrieve an experiment', function(done) { 893 | var options = { 894 | "id": EXPERIMENTID 895 | } 896 | client.getExperiment(EXPERIMENTID) 897 | .then( 898 | function(variation) { 899 | done(FAILUREMESSAGE); 900 | }, 901 | function(error) { 902 | error = JSON.parse(error); 903 | assert.equal(error.message, FUNNELENVYERROR); 904 | done(); 905 | } 906 | ) 907 | }); 908 | scope.put('/experiments/' + EXPERIMENTID) //update 909 | .reply(400, function(uri, requestBody) { 910 | requestBody.id = stripPathEnd(uri); 911 | return { 912 | status: 400, 913 | message: FUNNELENVYERROR, 914 | uuid: hat() 915 | }; 916 | }); 917 | it('should not update an experiment', function(done) { 918 | var options = { 919 | "id": EXPERIMENTID, 920 | "description": "New " + EXPERIMENTDESCRIPTION 921 | }; 922 | client.updateExperiment(options) 923 | .then( 924 | function(variation) { 925 | done(FAILUREMESSAGE); 926 | }, 927 | function(error) { 928 | error = JSON.parse(error); 929 | assert.equal(error.message, FUNNELENVYERROR); 930 | done(); 931 | } 932 | ) 933 | }); 934 | scope.put('/experiments/' + EXPERIMENTID) //update 935 | .reply(400, function(uri, requestBody) { 936 | requestBody.id = stripPathEnd(uri); 937 | return { 938 | status: 400, 939 | message: FUNNELENVYERROR, 940 | uuid: hat() 941 | }; 942 | }); 943 | it('should not update an experiment (with pushExperiment)', function(done) { 944 | var options = { 945 | "id": EXPERIMENTID, 946 | "description": "New " + EXPERIMENTDESCRIPTION 947 | }; 948 | client.pushExperiment(options) 949 | .then( 950 | function(variation) { 951 | done(FAILUREMESSAGE); 952 | }, 953 | function(error) { 954 | error = JSON.parse(error); 955 | assert.equal(error.message, FUNNELENVYERROR); 956 | done(); 957 | } 958 | ) 959 | }); 960 | scope.get('/projects/' + PROJECTID + '/experiments/') //get multiple 961 | .reply(400, function(uri, requestBody) { 962 | return { 963 | status: 400, 964 | message: FUNNELENVYERROR, 965 | uuid: hat() 966 | }; 967 | }); 968 | it('should not retrieve a list of experiments', function(done) { 969 | var options = { 970 | "project_id": PROJECTID 971 | } 972 | client.getExperiments(options) 973 | .then( 974 | function(variation) { 975 | done(FAILUREMESSAGE); 976 | }, 977 | function(error) { 978 | error = JSON.parse(error); 979 | assert.equal(error.message, FUNNELENVYERROR); 980 | done(); 981 | } 982 | ) 983 | }); 984 | scope.get('/projects/' + PROJECTID + '/experiments/') //get by description 985 | .reply(400, function(uri, requestBody) { 986 | return { 987 | status: 400, 988 | message: FUNNELENVYERROR, 989 | uuid: hat() 990 | }; 991 | }); 992 | it('should not retrieve an experiment by description', function( 993 | done) { 994 | var options = { 995 | "project_id": PROJECTID, 996 | "description": EXPERIMENTDESCRIPTION 997 | }; 998 | client.getExperimentByDescription(options) 999 | .then( 1000 | function(variation) { 1001 | done(FAILUREMESSAGE); 1002 | }, 1003 | function(error) { 1004 | error = JSON.parse(error); 1005 | assert.equal(error.message, FUNNELENVYERROR); 1006 | done(); 1007 | } 1008 | ) 1009 | }); 1010 | scope.intercept('/experiments/' + EXPERIMENTID, 'DELETE') 1011 | .reply(400, function(uri, requestBody) { 1012 | return { 1013 | status: 400, 1014 | message: FUNNELENVYERROR, 1015 | uuid: hat() 1016 | }; 1017 | }); 1018 | it('should not delete an experiment', function(done) { 1019 | var options = { 1020 | "id": EXPERIMENTID 1021 | }; 1022 | client.deleteExperiment(options) 1023 | .then( 1024 | function(reply) { 1025 | done(error); 1026 | }, 1027 | function(error) { 1028 | error = JSON.parse(error); 1029 | assert.equal(error.message, FUNNELENVYERROR); 1030 | done(); 1031 | } 1032 | ) 1033 | }); 1034 | scope.intercept('/experiments/' + EXPERIMENTID, 'DELETE') 1035 | .reply(400, function(uri, requestBody) { 1036 | return { 1037 | status: 400, 1038 | message: FUNNELENVYERROR, 1039 | uuid: hat() 1040 | }; 1041 | }); 1042 | }) 1043 | ////////////////// 1044 | //Variation Tests 1045 | ////////////////// 1046 | describe("Variations", function() { 1047 | scope.post('/experiments/' + EXPERIMENTID + '/variations/') //create 1048 | .reply(400, function(uri, requestBody) { 1049 | return { 1050 | status: 400, 1051 | message: FUNNELENVYERROR, 1052 | uuid: hat() 1053 | }; 1054 | }); 1055 | it('should not create a variation', function(done) { 1056 | var options = { 1057 | "experiment_id": EXPERIMENTID, 1058 | "description": "Variation Description" 1059 | } 1060 | client.createVariation(options) 1061 | .then( 1062 | function(variation) { 1063 | done(FAILUREMESSAGE); 1064 | }, 1065 | function(error) { 1066 | error = JSON.parse(error); 1067 | assert.equal(error.message, FUNNELENVYERROR); 1068 | done(); 1069 | } 1070 | ) 1071 | }); 1072 | scope.post('/experiments/' + EXPERIMENTID + '/variations/') //create 1073 | .reply(400, function(uri, requestBody) { 1074 | return { 1075 | status: 400, 1076 | message: FUNNELENVYERROR, 1077 | uuid: hat() 1078 | }; 1079 | }); 1080 | it('should not create a variation (with pushVariation)', function(done) { 1081 | var options = { 1082 | "experiment_id": EXPERIMENTID, 1083 | "description": "Variation Description" 1084 | } 1085 | client.pushVariation(options) 1086 | .then( 1087 | function(variation) { 1088 | done(FAILUREMESSAGE); 1089 | }, 1090 | function(error) { 1091 | error = JSON.parse(error); 1092 | assert.equal(error.message, FUNNELENVYERROR); 1093 | done(); 1094 | } 1095 | ) 1096 | }); 1097 | scope.get('/variations/' + VARIATIONID) //get 1098 | .reply(400, function(uri, requestBody) { 1099 | return { 1100 | status: 400, 1101 | message: FUNNELENVYERROR, 1102 | uuid: hat() 1103 | }; 1104 | }); 1105 | it('should not retrieve a variation', function(done) { 1106 | client.getVariation(VARIATIONID) 1107 | .then( 1108 | function(variation) { 1109 | done(FAILUREMESSAGE); 1110 | }, 1111 | function(error) { 1112 | error = JSON.parse(error); 1113 | assert.equal(error.message, FUNNELENVYERROR); 1114 | done(); 1115 | } 1116 | ) 1117 | }); 1118 | scope.put('/variations/' + VARIATIONID) //update 1119 | .reply(400, function(uri, requestBody) { 1120 | return { 1121 | status: 400, 1122 | message: FUNNELENVYERROR, 1123 | uuid: hat() 1124 | }; 1125 | }) 1126 | it('should not update a variation', function(done) { 1127 | var options = { 1128 | "id": VARIATIONID, 1129 | "description": "New " + "Variation Description" 1130 | } 1131 | client.updateVariation(options) 1132 | .then( 1133 | function(variation) { 1134 | done(FAILUREMESSAGE); 1135 | }, 1136 | function(error) { 1137 | error = JSON.parse(error); 1138 | assert.equal(error.message, FUNNELENVYERROR); 1139 | done(); 1140 | } 1141 | ) 1142 | }); 1143 | scope.put('/variations/' + VARIATIONID) //update 1144 | .reply(400, function(uri, requestBody) { 1145 | return { 1146 | status: 400, 1147 | message: FUNNELENVYERROR, 1148 | uuid: hat() 1149 | }; 1150 | }) 1151 | it('should not update a variation (with pushVariation)', function(done) { 1152 | var options = { 1153 | "id": VARIATIONID, 1154 | "description": "New " + "Variation Description" 1155 | } 1156 | client.pushVariation(options) 1157 | .then( 1158 | function(variation) { 1159 | done(FAILUREMESSAGE); 1160 | }, 1161 | function(error) { 1162 | error = JSON.parse(error); 1163 | assert.equal(error.message, FUNNELENVYERROR); 1164 | done(); 1165 | } 1166 | ) 1167 | }); 1168 | scope.intercept('/variations/' + VARIATIONID, 'DELETE') 1169 | .reply(400, function(uri, requestBody) { 1170 | return { 1171 | status: 400, 1172 | message: FUNNELENVYERROR, 1173 | uuid: hat() 1174 | }; 1175 | }); 1176 | it('should not delete a variation', function(done) { 1177 | var options = { 1178 | "id": VARIATIONID 1179 | }; 1180 | client.deleteVariation(options) 1181 | .then( 1182 | function(reply) { 1183 | done(error); 1184 | }, 1185 | function(error) { 1186 | error = JSON.parse(error); 1187 | assert.equal(error.message, FUNNELENVYERROR); 1188 | done(); 1189 | } 1190 | ) 1191 | }); 1192 | }) 1193 | }) 1194 | --------------------------------------------------------------------------------