2017-03-13 23:44:52 -04:00
|
|
|
import User from "discourse/models/user";
|
|
|
|
import Group from "discourse/models/group";
|
2020-03-23 21:39:09 -04:00
|
|
|
import * as ajaxlib from "discourse/lib/ajax";
|
|
|
|
import pretender from "helpers/create-pretender";
|
2017-03-13 23:44:52 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.module("model:user");
|
2013-07-26 17:09:54 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("staff", assert => {
|
2017-03-13 23:44:52 -04:00
|
|
|
var user = User.create({ id: 1, username: "eviltrout" });
|
2013-07-26 17:09:54 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(!user.get("staff"), "user is not staff");
|
2013-07-26 17:09:54 -04:00
|
|
|
|
|
|
|
user.toggleProperty("moderator");
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(user.get("staff"), "moderators are staff");
|
2013-07-26 17:09:54 -04:00
|
|
|
|
|
|
|
user.setProperties({ moderator: false, admin: true });
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(user.get("staff"), "admins are staff");
|
2013-07-26 17:09:54 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("searchContext", assert => {
|
2017-03-13 23:44:52 -04:00
|
|
|
var user = User.create({ id: 1, username: "EvilTrout" });
|
2013-07-26 17:09:54 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.deepEqual(
|
|
|
|
user.get("searchContext"),
|
|
|
|
{ type: "user", id: "eviltrout", user: user },
|
|
|
|
"has a search context"
|
|
|
|
);
|
2013-09-07 04:49:11 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("isAllowedToUploadAFile", assert => {
|
2017-03-13 23:44:52 -04:00
|
|
|
var user = User.create({ trust_level: 0, admin: true });
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(
|
|
|
|
user.isAllowedToUploadAFile("image"),
|
|
|
|
"admin can always upload a file"
|
|
|
|
);
|
2013-09-07 04:49:11 -04:00
|
|
|
|
|
|
|
user.setProperties({ admin: false, moderator: true });
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(
|
|
|
|
user.isAllowedToUploadAFile("image"),
|
|
|
|
"moderator can always upload a file"
|
|
|
|
);
|
2013-09-07 04:49:11 -04:00
|
|
|
});
|
2017-03-13 23:44:52 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("canMangeGroup", assert => {
|
2017-03-13 23:44:52 -04:00
|
|
|
let user = User.create({ admin: true });
|
|
|
|
let group = Group.create({ automatic: true });
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(
|
|
|
|
user.canManageGroup(group),
|
|
|
|
false,
|
|
|
|
"automatic groups cannot be managed."
|
|
|
|
);
|
2017-03-13 23:44:52 -04:00
|
|
|
|
|
|
|
group.set("automatic", false);
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(
|
|
|
|
user.canManageGroup(group),
|
|
|
|
true,
|
|
|
|
"an admin should be able to manage the group"
|
|
|
|
);
|
2017-03-13 23:44:52 -04:00
|
|
|
|
|
|
|
user.set("admin", false);
|
|
|
|
group.setProperties({ is_group_owner: true });
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(
|
|
|
|
user.canManageGroup(group),
|
|
|
|
true,
|
|
|
|
"a group owner should be able to manage the group"
|
|
|
|
);
|
|
|
|
});
|
2020-03-23 21:39:09 -04:00
|
|
|
|
|
|
|
QUnit.test("resolvedTimezone", assert => {
|
|
|
|
const tz = "Australia/Brisbane";
|
2020-05-10 21:01:47 -04:00
|
|
|
let user = User.create({ timezone: tz, username: "chuck", id: 111 });
|
2020-03-23 21:39:09 -04:00
|
|
|
let stub = sandbox.stub(moment.tz, "guess").returns("America/Chicago");
|
|
|
|
|
|
|
|
pretender.put("/u/chuck.json", () => {
|
|
|
|
return [200, { "Content-Type": "application/json" }, {}];
|
|
|
|
});
|
|
|
|
|
|
|
|
let spy = sandbox.spy(ajaxlib, "ajax");
|
|
|
|
assert.equal(
|
2020-05-10 21:01:47 -04:00
|
|
|
user.resolvedTimezone(user),
|
2020-03-23 21:39:09 -04:00
|
|
|
tz,
|
|
|
|
"if the user already has a timezone return it"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
spy.notCalled,
|
|
|
|
"if the user already has a timezone do not call AJAX update"
|
|
|
|
);
|
2020-05-10 21:01:47 -04:00
|
|
|
user = User.create({ username: "chuck", id: 111 });
|
2020-03-23 21:39:09 -04:00
|
|
|
assert.equal(
|
2020-05-10 21:01:47 -04:00
|
|
|
user.resolvedTimezone(user),
|
2020-03-23 21:39:09 -04:00
|
|
|
"America/Chicago",
|
|
|
|
"if the user has no timezone guess it with moment"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWith("/u/chuck.json", {
|
|
|
|
type: "PUT",
|
|
|
|
dataType: "json",
|
|
|
|
data: { timezone: "America/Chicago" }
|
|
|
|
}),
|
|
|
|
"if the user has no timezone save it with an AJAX update"
|
|
|
|
);
|
2020-05-10 21:01:47 -04:00
|
|
|
|
|
|
|
let otherUser = User.create({ username: "howardhamlin", id: 999 });
|
|
|
|
assert.equal(
|
|
|
|
otherUser.resolvedTimezone(user),
|
|
|
|
null,
|
|
|
|
"if the user has no timezone and the user is not the current user, do NOT guess with moment"
|
|
|
|
);
|
|
|
|
assert.not(
|
|
|
|
spy.calledWith("/u/howardhamlin.json", {
|
|
|
|
type: "PUT",
|
|
|
|
dataType: "json",
|
|
|
|
data: { timezone: "America/Chicago" }
|
|
|
|
}),
|
|
|
|
"if the user has no timezone, and the user is not the current user, do NOT save it with an AJAX update"
|
|
|
|
);
|
|
|
|
|
2020-03-23 21:39:09 -04:00
|
|
|
stub.restore();
|
|
|
|
});
|
2020-04-20 18:33:55 -04:00
|
|
|
|
|
|
|
QUnit.test("muted ids", assert => {
|
|
|
|
let user = User.create({ username: "chuck", muted_category_ids: [] });
|
|
|
|
|
|
|
|
assert.deepEqual(user.calculateMutedIds(0, 1, "muted_category_ids"), [1]);
|
|
|
|
assert.deepEqual(user.calculateMutedIds(1, 1, "muted_category_ids"), []);
|
|
|
|
});
|