2019-11-08 13:13:13 -05:00
|
|
|
import Post from "discourse/models/post";
|
2019-11-13 15:55:32 -05:00
|
|
|
import User from "discourse/models/user";
|
2020-09-02 12:04:14 -04:00
|
|
|
import { deepMerge } from "discourse-common/lib/object";
|
2019-11-08 13:13:13 -05:00
|
|
|
|
|
|
|
QUnit.module("model: Post");
|
2013-06-20 17:20:08 -04:00
|
|
|
|
2013-07-10 11:30:31 -04:00
|
|
|
var buildPost = function (args) {
|
2019-11-08 13:13:13 -05:00
|
|
|
return Post.create(
|
2020-09-02 12:04:14 -04:00
|
|
|
deepMerge(
|
2013-07-10 11:30:31 -04:00
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
can_delete: true,
|
|
|
|
version: 1,
|
|
|
|
},
|
|
|
|
args || {}
|
2018-06-15 11:03:24 -04:00
|
|
|
)
|
2013-07-10 11:30:31 -04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("defaults", (assert) => {
|
2019-11-08 13:13:13 -05:00
|
|
|
var post = Post.create({ id: 1 });
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.blank(post.get("deleted_at"), "it has no deleted_at by default");
|
|
|
|
assert.blank(post.get("deleted_by"), "there is no deleted_by by default");
|
2013-07-10 11:30:31 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("new_user", (assert) => {
|
2019-11-08 13:13:13 -05:00
|
|
|
var post = Post.create({ trust_level: 0 });
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(post.get("new_user"), "post is from a new user");
|
2013-06-20 17:20:08 -04:00
|
|
|
|
|
|
|
post.set("trust_level", 1);
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(!post.get("new_user"), "post is no longer from a new user");
|
2013-06-20 17:20:08 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("firstPost", (assert) => {
|
2019-11-08 13:13:13 -05:00
|
|
|
var post = Post.create({ post_number: 1 });
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(post.get("firstPost"), "it's the first post");
|
2013-06-20 17:20:08 -04:00
|
|
|
|
|
|
|
post.set("post_number", 10);
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.ok(!post.get("firstPost"), "post is no longer the first post");
|
2013-06-20 17:20:08 -04:00
|
|
|
});
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
QUnit.test("updateFromPost", (assert) => {
|
2019-11-08 13:13:13 -05:00
|
|
|
var post = Post.create({
|
2013-06-20 17:20:08 -04:00
|
|
|
post_number: 1,
|
|
|
|
raw: "hello world",
|
|
|
|
});
|
|
|
|
|
|
|
|
post.updateFromPost(
|
2019-11-08 13:13:13 -05:00
|
|
|
Post.create({
|
2013-06-20 17:20:08 -04:00
|
|
|
raw: "different raw",
|
|
|
|
wat: function () {
|
|
|
|
return 123;
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.equal(post.get("raw"), "different raw", "raw field updated");
|
2013-07-05 11:53:09 -04:00
|
|
|
});
|
|
|
|
|
2020-08-31 11:46:28 -04:00
|
|
|
QUnit.test("destroy by staff", async (assert) => {
|
|
|
|
let user = User.create({ username: "staff", moderator: true });
|
|
|
|
let post = buildPost({ user: user });
|
2013-07-10 11:30:31 -04:00
|
|
|
|
2020-08-31 11:46:28 -04:00
|
|
|
await post.destroy(user);
|
2013-07-10 11:30:31 -04:00
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.present(post.get("deleted_at"), "it has a `deleted_at` field.");
|
|
|
|
assert.equal(
|
|
|
|
post.get("deleted_by"),
|
|
|
|
user,
|
|
|
|
"it has the user in the `deleted_by` field"
|
|
|
|
);
|
2013-07-10 16:19:42 -04:00
|
|
|
|
2020-08-31 11:46:28 -04:00
|
|
|
await post.recover();
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
assert.blank(
|
|
|
|
post.get("deleted_at"),
|
|
|
|
"it clears `deleted_at` when recovering"
|
|
|
|
);
|
|
|
|
assert.blank(
|
|
|
|
post.get("deleted_by"),
|
|
|
|
"it clears `deleted_by` when recovering"
|
2018-06-15 11:03:24 -04:00
|
|
|
);
|
2013-07-10 11:30:31 -04:00
|
|
|
});
|
|
|
|
|
2020-03-02 15:20:19 -05:00
|
|
|
QUnit.test("destroy by non-staff", async (assert) => {
|
|
|
|
const originalCooked = "this is the original cooked value";
|
|
|
|
const user = User.create({ username: "evil trout" });
|
|
|
|
const post = buildPost({ user: user, cooked: originalCooked });
|
|
|
|
|
|
|
|
await post.destroy(user);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
!post.get("can_delete"),
|
|
|
|
"the post can't be deleted again in this session"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
post.get("cooked") !== originalCooked,
|
|
|
|
"the cooked content changed"
|
|
|
|
);
|
|
|
|
assert.equal(post.get("version"), 2, "the version number increased");
|
2017-07-28 10:50:18 -04:00
|
|
|
});
|