File size: 2,286 Bytes
5d53000
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*!
 * Matomo - free/libre analytics platform
 *
 * AjaxHelper session timeout UI test.
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

describe('AjaxHelperSessionTimeout', function () {
  this.fixture = "Piwik\\Tests\\Fixtures\\OneVisitorTwoVisits";

  const reportUrl = '?module=CoreHome&action=index&idSite=1&period=day&date=yesterday';

  async function loadReportPage() {
    await page.goto(reportUrl);
    await page.waitForNetworkIdle();
    await page.waitForFunction(() => window.ajaxHelper && window.piwikHelper);
  }

  const cases = [
    {
      name: 'should refresh when a request indicates the session has timed out',
      headerValue: '1',
      expectedRefresh: true,
    },
    {
      name: 'should not refresh when the session timeout header is missing',
      headerValue: null,
      expectedRefresh: false,
    },
  ];

  cases.forEach(({ name, headerValue, expectedRefresh }) => {
    it(name, async function () {
      await loadReportPage();

      const refreshCalled = await page.evaluate((value) => {
        document.cookie = 'matomo_session_timed_out=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
        window._ajaxSessionTimedOutRefresh = false;
        const originalRefresh = window.piwikHelper.refreshAfter;
        const originalAjax = window.$.ajax;

        window.piwikHelper.refreshAfter = (timeout) => {
          window._ajaxSessionTimedOutRefresh = timeout === 0;
        };

        const mockXhr = {
          status: 401,
          statusText: 'error',
          getResponseHeader: (name) => (name === 'X-Matomo-Session-Timed-Out' ? value : null),
          then() {
            return this;
          },
          fail(callback) {
            this._fail = callback;
            return this;
          },
        };

        window.$.ajax = () => mockXhr;

        const helper = new window.ajaxHelper();
        helper.send();
        if (typeof mockXhr._fail === 'function') {
          mockXhr._fail(mockXhr);
        }

        window.$.ajax = originalAjax;
        window.piwikHelper.refreshAfter = originalRefresh;

        return window._ajaxSessionTimedOutRefresh;
      }, headerValue);

      expect(refreshCalled).to.equal(expectedRefresh);
    });
  });
});