2013-06-20 17:20:08 -04:00
|
|
|
module("Discourse.Post");
|
|
|
|
|
2013-07-10 11:30:31 -04:00
|
|
|
var buildPost = function(args) {
|
|
|
|
return Discourse.Post.create(_.merge({
|
|
|
|
id: 1,
|
|
|
|
can_delete: true,
|
|
|
|
version: 1
|
|
|
|
}, args || {}));
|
|
|
|
};
|
|
|
|
|
|
|
|
test('defaults', function() {
|
|
|
|
var post = Discourse.Post.create({id: 1});
|
|
|
|
blank(post.get('deleted_at'), "it has no deleted_at by default");
|
|
|
|
blank(post.get('deleted_by'), "there is no deleted_by by default");
|
2013-08-06 17:42:36 -04:00
|
|
|
equal(post.get('replyHistory.length'), 0, "there is no reply history by default");
|
2013-07-10 11:30:31 -04:00
|
|
|
});
|
|
|
|
|
2013-06-20 17:20:08 -04:00
|
|
|
test('new_user', function() {
|
|
|
|
var post = Discourse.Post.create({trust_level: 0});
|
|
|
|
ok(post.get('new_user'), "post is from a new user");
|
|
|
|
|
|
|
|
post.set('trust_level', 1);
|
|
|
|
ok(!post.get('new_user'), "post is no longer from a new user");
|
|
|
|
});
|
|
|
|
|
|
|
|
test('firstPost', function() {
|
|
|
|
var post = Discourse.Post.create({post_number: 1});
|
|
|
|
ok(post.get('firstPost'), "it's the first post");
|
|
|
|
|
|
|
|
post.set('post_number', 10);
|
|
|
|
ok(!post.get('firstPost'), "post is no longer the first post");
|
|
|
|
});
|
|
|
|
|
|
|
|
test('updateFromPost', function() {
|
|
|
|
var post = Discourse.Post.create({
|
|
|
|
post_number: 1,
|
|
|
|
raw: 'hello world'
|
|
|
|
});
|
|
|
|
|
|
|
|
post.updateFromPost(Discourse.Post.create({
|
|
|
|
raw: 'different raw',
|
|
|
|
wat: function() { return 123; }
|
|
|
|
}));
|
|
|
|
|
|
|
|
equal(post.get('raw'), "different raw", "raw field updated");
|
2013-07-05 11:53:09 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('hasHistory', function() {
|
|
|
|
var post = Discourse.Post.create({id: 1});
|
|
|
|
ok(!post.get('hasHistory'), 'posts without versions have no history');
|
|
|
|
post.set('version', 1);
|
|
|
|
ok(!post.get('hasHistory'), 'posts with one version have no history');
|
|
|
|
post.set('version', 2);
|
|
|
|
ok(post.get('hasHistory'), 'posts with more than one version have a history');
|
2013-07-10 11:30:31 -04:00
|
|
|
});
|
|
|
|
|
2013-07-05 11:53:09 -04:00
|
|
|
|
2013-07-10 11:30:31 -04:00
|
|
|
test('destroy by staff', function() {
|
|
|
|
var user = Discourse.User.create({username: 'staff', staff: true});
|
|
|
|
var post = buildPost({user: user});
|
|
|
|
|
2013-07-22 04:04:20 -04:00
|
|
|
this.stub(Discourse, 'ajax').returns(new Em.Deferred());
|
2013-07-10 11:30:31 -04:00
|
|
|
post.destroy(user);
|
|
|
|
|
|
|
|
present(post.get('deleted_at'), "it has a `deleted_at` field.");
|
|
|
|
equal(post.get('deleted_by'), user, "it has the user in the `deleted_by` field");
|
|
|
|
ok(Discourse.ajax.calledOnce, "it made an AJAX call");
|
2013-07-10 16:19:42 -04:00
|
|
|
|
|
|
|
post.recover();
|
|
|
|
blank(post.get('deleted_at'), "it clears `deleted_at` when recovering");
|
|
|
|
blank(post.get('deleted_by'), "it clears `deleted_by` when recovering");
|
|
|
|
|
2013-07-10 11:30:31 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('destroy by non-staff', function() {
|
|
|
|
var originalCooked = "this is the original cooked value";
|
|
|
|
var user = Discourse.User.create({username: 'evil trout'});
|
|
|
|
var post = buildPost({user: user, cooked: originalCooked});
|
|
|
|
|
|
|
|
this.stub(Discourse, 'ajax');
|
|
|
|
post.destroy(user);
|
|
|
|
|
|
|
|
ok(!post.get('can_delete'), "the post can't be deleted again in this session");
|
|
|
|
ok(post.get('cooked') !== originalCooked, "the cooked content changed");
|
|
|
|
equal(post.get('version'), 2, "the version number increased");
|
|
|
|
ok(Discourse.ajax.calledOnce, "it made an AJAX call");
|
|
|
|
});
|
2013-07-05 11:53:09 -04:00
|
|
|
|