All files / lib steps.js

26.44% Statements 55/208
22.45% Branches 22/98
14.29% Functions 2/14
26.96% Lines 55/204

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666                            1x 1x   1x 1x 1x   1x 1x 1x   1x   1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               4x   4x 4x   4x 4x   4x 4x   4x 4x     4x 4x 2x     4x 4x 3x       4x 3x         4x 4x 3x       4x 3x             4x 4x   4x                                       1x   1x 1x   1x 1x   1x 1x   1x 1x     1x 1x 1x     1x       1x 1x     1x           1x 1x   1x                                                                                                                                                                                                                                                                                                                                                                                                                                                     1x  
"use strict";
/**
 * Steps to manage proxy.
 *
 * These methods are mixed with glacejs [Steps](https://glacejs.github.io/glace-core/Steps.html)
 * class and available via its instance [$](https://glacejs.github.io/glace-core/global.html#$)
 * in tests.
 *
 * @mixin ProxySteps
 * @prop {string} webUrl - Web application URL.
 * @prop {GlobalProxy} globalProxy - Global proxy instance.
 * @prop {HttpProxy} httpProxy - HTTP proxy instance.
 */
 
var url = require("url");
var util = require("util");
 
var _ = require("lodash");
var expect = require("chai").expect;
var U = require("glace-utils");
 
require("./fixtures");
var GlobalProxy = require("./globalProxy");
var HttpProxy = require("./httpProxy");
 
var LOG = U.logger;
 
var ProxySteps = {
    __GlobalProxy: GlobalProxy,
    __HttpProxy: HttpProxy,
 
    registerProxy: function (opts) {
        /**
         * Helper to register proxy classes.
         * 
         * @memberOf ProxySteps
         * @method registerProxy
         * @instance
         * @arg {object} [opts] - Options.
         * @arg {object} [opts.GlobalProxy] - Global proxy class.
         * @arg {object} [opts.HttpProxy] - HTTP proxy class.
         * @example
         *
         * $.registerProxy({ GlobalProxy: MyGlobalProxy, HttpProxy: MyHttpProxy });
         */
 
        opts = U.defVal(opts, {});
        this.__GlobalProxy = U.defVal(opts.GlobalProxy, this.__GlobalProxy);
        this.__HttpProxy = U.defVal(opts.HttpProxy, this.__HttpProxy);
    },
 
    startHttpProxy: async function (opts) {
        /**
         * Step to start HTTP proxy. Step recall will be skipped if proxy wasn't
         *  stopped before.
         *
         * @async
         * @memberOf ProxySteps
         * @method startHttpProxy
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {string} [opts.webUrl] - Web application url which will
         *  be proxied. Default value will be requested from `config.web.url`
         *  if it is specified.
         * @arg {boolean} [opts.useCache] - Flag to use proxy cache for
         *  responses. Default value will be requested from `config.useCache`
         *  if it is specified.
         * @arg {number} [opts.timeout] - Proxy timeout to break connection.
         *  Default value will be requested from `config.timeout.proxy`.
         * @arg {number} [opts.reconnect] - Number of proxy reconnects on failure.
         * @arg {number} [opts.port] - Proxy port. Default value will be
         *  requested from `config.proxyPort`.
         * @arg {boolean} [opts.check=true] - Flag to check that proxy was launched.
         * @return {Promise<string>} Proxied URL.
         * @return {Promise<boolean>} `false` if step was skipped
         * @throws {AssertionError} If proxy was not launched.
         * @example
         *
         * var proxiedUrl = await $.startHttpProxy();
         * var proxiedUrl = await $.startHttpProxy({ webUrl: "http://example.com", });
         * var proxiedUrl = await $.startHttpProxy({ webUrl: "http://example.com", port: 8080 });
         */
 
        if (this._isProxyStarted) {
            LOG.warn("Step to start proxy was passed already");
            return false;
        };
 
        opts = U.defVal(opts, {});
        var webUrl = U.defVal(opts.webUrl, CONF.web && CONF.web.url);
        var useCache = U.defVal(opts.useCache, CONF.cache.use);
        var timeout = U.defVal(opts.timeout, CONF.proxy.timeout);
        var reconnect = U.defVal(opts.reconnect, CONF.proxy.reconnect);
        var port = U.defVal(opts.port, CONF.proxy.port);
        var check = U.defVal(opts.check, true);
 
        allure.step("Start http proxy");
        expect(webUrl, "Web URL for http proxy isn't specified").to.exist;
 
        LOG.info("Starting http proxy...");
 
        this.httpProxy = this.httpProxy || new this.__HttpProxy({
            url: webUrl,
            useCache: useCache,
            timeout: timeout,
            reconnect: reconnect,
            port: port });
        await this.httpProxy.start();
 
        if (check) {
            expect(this.httpProxy.isRunning,
                "HTTP proxy was not launched").be.true;
        };
        this._isProxyStarted = true;
 
        this.webUrl = _.trim(
            this.httpProxy.url + url.parse(CONF.web.url).pathname, "/");
 
        LOG.info("Http proxy is started");
        allure.pass();
 
        return this.webUrl;
    },
 
    getProxyUrl: function (opts) {
        /**
         * Step to get proxy URL.
         *
         * @memberOf ProxySteps
         * @method getProxyUrl
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check that proxy URL is
         *  defined.
         * @return {string} Proxy URL.
         * @throws {AssertionError} If proxy URL isn't defined.
         * @example
         *
         * var proxiedUrl = $.getProxiedUrl();
         */
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        allure.step("Get http proxy url");
        LOG.info("Getting http proxy url...");
 
        if (check) {
            expect(this.httpProxy.url,
                "HTTP proxy was not launched").to.exist;
        };
 
        LOG.info("Http proxy url is got");
        allure.pass();
 
        return this.httpProxy.url;
    },
 
    stopHttpProxy: async function (opts) {
        /**
         * Step to stop HTTP proxy. Step call will be skipped if proxy wasn't
         *  launched before.
         *
         * @async
         * @memberOf ProxySteps
         * @method stopHttpProxy
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check that proxy was
         *  stopped.
         * @return {Promise<boolean>} `true` if step was passed, `false` if was
         *  skipped.
         * @throws {AssertionError} If proxy wasn't stopped.
         * @example
         *
         * await $.stopHttpProxy();
         */
 
        if (!this._isProxyStarted) {
            LOG.warn("Step to start HTTP proxy wasn't passed yet");
            return false;
        };
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        allure.step("Stop http proxy");
        LOG.info("Stopping http proxy...");
 
        await this.httpProxy.stop();
 
        if (check) {
            expect(this.httpProxy.isRunning,
                "HTTP proxy wasn't stopped").be.false;
        };
 
        this.webUrl = CONF.web.url;
        this._isProxyStarted = false;
 
        LOG.info("Http proxy is stopped");
        allure.pass();
 
        return true;
    },
 
    startGlobalProxy: async function (opts) {
        /**
         * Step to start global proxy. Step recall will be skipped if global
         *  proxy wasn't stopped before.
         *
         * @async
         * @memberOf ProxySteps
         * @method startGlobalProxy
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.useCache] - Flag to use proxy cache for
         *  responses. Default value will be requested from `config.useCache`
         *  if it is specified.
         * @arg {number} [opts.timeout] - Proxy timeout to break connection.
         *  Default value will be requested from `config.timeout.proxy`.
         * @arg {number} [opts.reconnect] - Number of proxy reconnects on failure.
         * @arg {number} [opts.port] - Proxy port. Default value will be
         *  requested from `config.globalProxyPort`.
         * @arg {boolean} [opts.check=true] - Flag to check that proxy was launched.
         * @return {Promise<boolean>} `true` if step was passed, `false` if was
         *  skipped.
         * @throws {AssertionError} If global proxy was not launched.
         * @example
         *
         * await $.startGlobalProxy();
         * await $.startGlobalProxy({ timeout: 30000 });
         */
 
        if (this._isGlobalProxyStarted) {
            LOG.warn("Step to start global proxy was passed already");
            return false;
        };
 
        opts = U.defVal(opts, {});
        var useCache = U.defVal(opts.useCache, CONF.cache.use);
        var timeout = U.defVal(opts.timeout, CONF.proxy.timeout);
        var reconnect = U.defVal(opts.reconnect, CONF.proxy.reconnect);
        var port = U.defVal(opts.port, CONF.proxy.globalPort);
        const sslCaDir = U.defVal(opts.sslCaDir, CONF.proxy.sslCaDir);
        var check = U.defVal(opts.check, true);
 
        allure.step("Start global proxy");
        LOG.info("Starting global proxy...");
 
        this.globalProxy = this.globalProxy || new this.__GlobalProxy({
            useCache: useCache,
            timeout: timeout,
            reconnect: reconnect,
            port: port,
            sslCaDir: sslCaDir,
            installCertificate: CONF.proxy.installCertificate });
 
        await this.globalProxy.start();
 
        if (check) {
            expect(this.globalProxy.isRunning,
                "Global proxy was not launched")
                .to.be.true;
        };
 
        this._isGlobalProxyStarted = true;
 
        LOG.info("Global proxy is started");
        allure.pass();
 
        return true;
    },
 
    stopGlobalProxy: async function (opts) {
        /**
         * Step to stop global proxy. Step call will be skipped if global proxy
         *  wasn't launched before.
         *
         * @async
         * @memberOf ProxySteps
         * @method stopGlobalProxy
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check that global
         *  proxy was stopped.
         * @return {Promise<boolean>} `true` if step was passed, `false` if was
         *  skipped.
         * @throws {AssertionError} If global proxy wasn't stopped.
         * @example
         *
         * await $.stopGlobalProxy();
         */
 
        if (!this._isGlobalProxyStarted) {
            LOG.warn("Step to start global proxy wasn't passed yet");
            return false;
        };
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        allure.step("Stop global proxy");
        LOG.info("Stopping global proxy...");
 
        await this.globalProxy.stop();
 
        if (check) {
            expect(this.globalProxy.isRunning,
                "Global proxy wasn't stopped").be.false;
        };
        this._isGlobalProxyStarted = false;
 
        LOG.info("Global proxy is stopped");
        allure.pass();
 
        return true;
    },
 
    limitProxySpeed: function (speed, opts) {
        /**
         * Step to limit proxy speed.
         *
         * @memberOf ProxySteps
         * @method limitProxySpeed
         * @instance
         * @arg {number|object} speed - Proxy limited speed, kb/s.
         * @arg {?number} [speed.req] - Requests speed, kb/s.
         * @arg {?number} [speed.res] - Responses speed, kb/s.
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check that proxy speed
         *  is limited.
         * @return {boolean} `true` if step was passed.
         * @throws {AssertionError} If proxy speed is not limited.
         * @example
         *
         * $.limitProxySpeed(512);
         * $.limitProxySpeed({ res: 512 });
         */
 
        this._checkProxy();
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        allure.step(util.format("Limit proxy speed to", speed, "kb/s"));
        LOG.info(util.format("Limiting proxy speed to", speed, "kb/s..."));
 
        Eif (this.httpProxy) {
            this.httpProxy.setSpeed(speed);
        };
        Eif (this.globalProxy) {
            this.globalProxy.setSpeed(speed);
        };
 
        Eif (check) {
            if (speed.req === undefined && speed.res === undefined) {
                speed = { req: speed, res: speed };
            };
 
            Eif (this.httpProxy) {
                if (speed.req !== undefined) {
                    expect(this.httpProxy.reqSpeed,
                        "HTTP proxy requests speed is not limited")
                        .be.equal(speed.req);
                };
                if (speed.res !== undefined) {
                    expect(this.httpProxy.resSpeed,
                        "HTTP proxy responses speed is not limited")
                        .be.equal(speed.res);
                };
            };
            Eif (this.globalProxy) {
                if (speed.req !== undefined) {
                    expect(this.globalProxy.reqSpeed,
                        "Global proxy requests speed is not limited")
                        .be.equal(speed.req);
                };
                if (speed.res !== undefined) {
                    expect(this.globalProxy.resSpeed,
                        "Global proxy responses speed is not limited")
                        .be.equal(speed.res);
                };
            };
        };
 
        LOG.info("Proxy speed is limited");
        allure.pass();
 
        return true;
    },
 
    unlimitProxySpeed: function (opts) {
        /**
         * Step to unlimit proxy speed.
         *
         * @memberOf ProxySteps
         * @method unlimitProxySpeed
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check that proxy speed
         *  is unlimited.
         * @return {boolean} `true` if step is passed.
         * @throws {AssertionError} If proxy speed is still limited.
         * @example
         *
         * $.unlimitProxySpeed();
         */
 
        this._checkProxy();
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        allure.step("Unlimit proxy speed");
        LOG.info("Unlimiting proxy speed...");
 
        Eif (this.httpProxy) {
            this.httpProxy.resetSpeed();
        };
        Eif (this.globalProxy) {
            this.globalProxy.resetSpeed();
        };
 
        Eif (check) {
            Eif (this.httpProxy) {
                expect(this.httpProxy.reqSpeed,
                    "HTTP proxy requests speed still has limited value")
                    .be.null;
                expect(this.httpProxy.resSpeed,
                    "HTTP proxy responses speed still has limited value")
                    .be.null;
            };
            Eif (this.globalProxy) {
                expect(this.globalProxy.reqSpeed,
                    "Global proxy requests speed still has limited value")
                    .be.null;
                expect(this.globalProxy.resSpeed,
                    "Global proxy responses speed still has limited value")
                    .be.null;
            };
        };
 
        LOG.info("Proxy speed is unlimited");
        allure.pass();
 
        return true;
    },
 
    measureResponses: function (opts) {
        /**
         * Step to start responses measurement.
         *
         * @memberOf ProxySteps
         * @method measureResponses
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check that responses
         *  measurement is launched.
         * @return {boolean} `true` if step is passed.
         * @throws {AssertionError} If responses measurement is not launched.
         * @example
         *
         * $.measureResponses();
         */
 
        this._checkProxy();
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        allure.step("Start to measure proxy responses");
        LOG.info("Starting to measure proxy responses...");
 
        if (this.httpProxy) {
            this.httpProxy.measureResponses();
        };
        if (this.globalProxy) {
            this.globalProxy.measureResponses();
        };
 
        if (check) {
            if (this.httpProxy) {
                expect(this.httpProxy.getResponsesData(),
                    "HTTP proxy responses measurement is not launched")
                    .to.exist;
            };
            if (this.globalProxy) {
                expect(this.globalProxy.getResponsesData(),
                    "Global proxy responses measurement is not launched")
                    .to.exist;
            };
        };
 
        LOG.info("Proxy responses are measuring");
        allure.pass();
 
        return true;
    },
 
    getResponsesData: function (opts) {
        /**
         * Step to get measured responses data.
         *
         * @memberOf ProxySteps
         * @method getResponsesData
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check that responses
         *  data are present.
         * @throws {AssertionError} If responses data are absent.
         * @return {object[]} List of captured response data.
         * @example
         *
         * var responses = $.getResponsesData();
         */
 
        this._checkProxy();
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        var data = [];
 
        allure.step("Get responses data");
        LOG.info("Getting responses data...");
 
        if (this.httpProxy && this.httpProxy.getResponsesData()) {
            data = data.concat(this.httpProxy.getResponsesData());
        };
        if (this.globalProxy && this.globalProxy.getResponsesData()) {
            data = data.concat(this.globalProxy.getResponsesData());
        };
 
        if (check) {
            expect(_.isEmpty(data),
                "Responses data are absent").be.false;
        };
 
        LOG.info("Responses data is got");
        allure.pass();
 
        return data;
    },
 
    unmeasureResponses: function (opts) {
        /**
         * Step to stop responses measurement.
         *
         * @memberOf ProxySteps
         * @method unmeasureResponses
         * @instance
         * @arg {object} [opts] - Step options.
         * @arg {boolean} [opts.check=true] - Flag to check that responses
         *  measurement is stopped.
         * @return {boolean} `true` if step is passed.
         * @throws {AssertionError} If responses measurement is still running.
         * @example
         *
         * $.unmeasureResponses();
         */
 
        this._checkProxy();
 
        opts = U.defVal(opts, {});
        var check = U.defVal(opts.check, true);
 
        allure.step("Stop to measure proxy responses");
        LOG.info("Stopping to measure proxy responses...");
 
        if (this.httpProxy) {
            this.httpProxy.unmeasureResponses();
        };
        if (this.globalProxy) {
            this.globalProxy.unmeasureResponses();
        };
 
        if (check) {
            if (this.httpProxy) {
                expect(this.httpProxy.getResponsesData(),
                    "HTTP proxy responses measurement is still running")
                    .to.be.null;
            };
            if (this.globalProxy) {
                expect(this.globalProxy.getResponsesData(),
                    "Global proxy responses measurement is still running")
                    .to.be.null;
            };
        };
 
        LOG.info("Proxy responses are not measured");
        allure.pass();
 
        return true;
    },
 
    enableCache: function () {
        /**
         * Step to enable cache.
         *
         * @memberOf ProxySteps
         * @method enableCache
         * @instance
         * @return {boolean} `true` if step is passed.
         * @example
         *
         * $.enableCache();
         */
 
        allure.step("Enable proxy cache");
        this._checkProxy();
 
        LOG.info("Enabling proxy cache...");
 
        if (this.httpProxy) this.httpProxy.useCache = true;
        if (this.globalProxy) this.globalProxy.useCache = true;
 
        LOG.info("Proxy cache is enabled");
        allure.pass();
 
        return true;
    },
 
    disableCache: function () {
        /**
         * Step to disable cache.
         *
         * @memberOf ProxySteps
         * @method disableCache
         * @instance
         * @return {boolean} `true` if step is passed.
         * @example
         *
         * $.disableCache();
         */
 
        allure.step("Disable proxy cache");
        this._checkProxy();
 
        LOG.info("Disabling proxy cache...");
 
        if (this.httpProxy) this.httpProxy.useCache = false;
        if (this.globalProxy) this.globalProxy.useCache = false;
 
        LOG.info("Proxy cache is disabled");
        allure.pass();
 
        return true;
    },
    /**
     * Helper to check that proxy is launched.
     *
     * @ignore
     * @method
     * @protected
     * @instance
     * @throws {AssertionError} - If no one proxy is launched.
     */
    _checkProxy: function () {
        expect(
            (this.httpProxy && this.httpProxy.isRunning) ||
            (this.globalProxy && this.globalProxy.isRunning),
            "No one proxy is launched").to.be.true;
    },
};
module.exports = ProxySteps;