2024-04-09 03:00:09 +08:00
|
|
|
import { getOwner } from "@ember/application";
|
2022-11-02 21:41:30 +08:00
|
|
|
import { set } from "@ember/object";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { test } from "qunit";
|
|
|
|
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
|
2024-04-09 03:00:09 +08:00
|
|
|
import ChatFabricators from "discourse/plugins/chat/discourse/lib/fabricators";
|
2022-11-02 21:41:30 +08:00
|
|
|
|
|
|
|
acceptance("Discourse Chat | Unit | Service | chat-guardian", function (needs) {
|
|
|
|
needs.hooks.beforeEach(function () {
|
|
|
|
Object.defineProperty(this, "chatGuardian", {
|
|
|
|
get: () => this.container.lookup("service:chat-guardian"),
|
|
|
|
});
|
|
|
|
Object.defineProperty(this, "siteSettings", {
|
|
|
|
get: () => this.container.lookup("service:site-settings"),
|
|
|
|
});
|
|
|
|
Object.defineProperty(this, "currentUser", {
|
|
|
|
get: () => this.container.lookup("service:current-user"),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
needs.user();
|
|
|
|
needs.settings();
|
|
|
|
|
|
|
|
test("#canEditChatChannel", async function (assert) {
|
|
|
|
set(this.currentUser, "has_chat_enabled", false);
|
|
|
|
set(this.currentUser, "admin", false);
|
|
|
|
set(this.currentUser, "moderator", false);
|
|
|
|
this.siteSettings.chat_enabled = false;
|
|
|
|
assert.notOk(this.chatGuardian.canEditChatChannel());
|
|
|
|
|
|
|
|
set(this.currentUser, "has_chat_enabled", true);
|
|
|
|
set(this.currentUser, "admin", true);
|
|
|
|
this.siteSettings.chat_enabled = false;
|
|
|
|
assert.notOk(this.chatGuardian.canEditChatChannel());
|
|
|
|
|
|
|
|
set(this.currentUser, "has_chat_enabled", false);
|
|
|
|
set(this.currentUser, "admin", false);
|
|
|
|
set(this.currentUser, "moderator", false);
|
|
|
|
this.siteSettings.chat_enabled = true;
|
|
|
|
assert.notOk(this.chatGuardian.canEditChatChannel());
|
|
|
|
|
|
|
|
set(this.currentUser, "has_chat_enabled", false);
|
|
|
|
set(this.currentUser, "admin", true);
|
|
|
|
this.siteSettings.chat_enabled = true;
|
|
|
|
assert.notOk(this.chatGuardian.canEditChatChannel());
|
|
|
|
|
|
|
|
set(this.currentUser, "has_chat_enabled", true);
|
|
|
|
set(this.currentUser, "admin", false);
|
|
|
|
set(this.currentUser, "moderator", false);
|
|
|
|
this.siteSettings.chat_enabled = true;
|
|
|
|
assert.notOk(this.chatGuardian.canEditChatChannel());
|
|
|
|
|
|
|
|
set(this.currentUser, "has_chat_enabled", true);
|
|
|
|
set(this.currentUser, "admin", true);
|
|
|
|
this.siteSettings.chat_enabled = true;
|
|
|
|
assert.ok(this.chatGuardian.canEditChatChannel());
|
|
|
|
});
|
|
|
|
|
|
|
|
test("#canUseChat", async function (assert) {
|
|
|
|
set(this.currentUser, "has_chat_enabled", false);
|
|
|
|
this.siteSettings.chat_enabled = true;
|
|
|
|
assert.notOk(this.chatGuardian.canUseChat());
|
|
|
|
|
|
|
|
set(this.currentUser, "has_chat_enabled", true);
|
|
|
|
this.siteSettings.chat_enabled = false;
|
|
|
|
assert.notOk(this.chatGuardian.canUseChat());
|
|
|
|
|
|
|
|
set(this.currentUser, "has_chat_enabled", true);
|
|
|
|
this.siteSettings.chat_enabled = true;
|
|
|
|
assert.ok(this.chatGuardian.canUseChat());
|
|
|
|
});
|
|
|
|
|
|
|
|
test("#canArchiveChannel", async function (assert) {
|
2024-04-09 03:00:09 +08:00
|
|
|
const channel = new ChatFabricators(getOwner(this)).channel();
|
2022-11-02 21:41:30 +08:00
|
|
|
|
|
|
|
set(this.currentUser, "has_chat_enabled", true);
|
|
|
|
set(this.currentUser, "admin", true);
|
|
|
|
this.siteSettings.chat_enabled = true;
|
|
|
|
this.siteSettings.chat_allow_archiving_channels = true;
|
|
|
|
assert.ok(this.chatGuardian.canArchiveChannel(channel));
|
|
|
|
|
|
|
|
set(this.currentUser, "admin", false);
|
|
|
|
set(this.currentUser, "moderator", false);
|
|
|
|
assert.notOk(this.chatGuardian.canArchiveChannel(channel));
|
|
|
|
set(this.currentUser, "admin", true);
|
|
|
|
set(this.currentUser, "moderator", true);
|
|
|
|
|
2023-05-09 00:24:41 +08:00
|
|
|
channel.status = "read_only";
|
2022-11-02 21:41:30 +08:00
|
|
|
assert.notOk(this.chatGuardian.canArchiveChannel(channel));
|
2023-05-09 00:24:41 +08:00
|
|
|
channel.status = "open";
|
2022-11-02 21:41:30 +08:00
|
|
|
|
2023-05-09 00:24:41 +08:00
|
|
|
channel.status = "archived";
|
2022-11-02 21:41:30 +08:00
|
|
|
assert.notOk(this.chatGuardian.canArchiveChannel(channel));
|
2023-05-09 00:24:41 +08:00
|
|
|
channel.status = "open";
|
2022-11-02 21:41:30 +08:00
|
|
|
});
|
|
|
|
});
|