Skip to content

A platform for community discussion. Free, open, simple.

2.5 KB
Raw Blame
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import sinon from "sinon";
import { getOwnerWithFallback, setDefaultOwner } from "discourse/lib/get-owner";
import liveLog from "discourse/plugins/live/discourse/lib/live/logger";

module("Live | Unit | Lib | logger", function (hooks) {
  setupTest(hooks);

  hooks.beforeEach(function () {
    this.originalOwner = getOwnerWithFallback();
    setDefaultOwner(this.owner);
    this.info = sinon.stub(console, "info");
    this.warn = sinon.stub(console, "warn");
    this.siteSettings = this.owner.lookup("service:site-settings");
  });

  hooks.afterEach(function () {
    setDefaultOwner(this.originalOwner);
    this.info.restore();
    this.warn.restore();
  });

  test("logging is disabled by default", function (assert) {
    assert.false(
      this.siteSettings.live_verbose_logging,
      "verbose logging defaults to off"
    );

    liveLog.info("[live] local stream obtained");
    liveLog.warn("[live] failed to obtain local stream");

    assert.false(this.info.called, "info is silent");
    assert.false(this.warn.called, "warnings are silent");
  });

  test("logging follows changes to the setting", function (assert) {
    this.siteSettings.live_verbose_logging = true;
    liveLog.info("[live] local stream obtained");
    liveLog.warn("[live] failed to obtain local stream");
    this.siteSettings.live_verbose_logging = false;
    liveLog.info("[live] local stream obtained");
    liveLog.warn("[live] failed to obtain local stream");

    assert.true(
      this.info.calledOnceWithExactly("[live] local stream obtained"),
      "info logs only when enabled"
    );
    assert.true(
      this.warn.calledOnceWithExactly("[live] failed to obtain local stream"),
      "warnings log only when enabled"
    );
  });

  test("only sanitized error fields are logged", function (assert) {
    this.siteSettings.live_verbose_logging = true;
    const error = new Error("publish failed: token=secret");
    const payload = { candidate: "private IP address", token: "secret token" };

    liveLog.info("[live] received signal", payload);
    liveLog.warn("[live] failed to obtain local stream", error, payload);

    assert.deepEqual(
      this.info.firstCall.args,
      ["[live] received signal"],
      "payloads are omitted"
    );
    assert.deepEqual(
      this.warn.firstCall.args,
      [
        "[live] failed to obtain local stream",
        { name: "Error", message: "publish failed: [FILTERED]" },
      ],
      "only sanitized error details are included"
    );
  });
});