Qunit tests for deleting posts on the front end. Support for `deleted_by`

property.
This commit is contained in:
Robin Ward 2013-07-10 11:30:31 -04:00
parent cdf0754626
commit 1fef617818
3 changed files with 57 additions and 13 deletions

View File

@ -378,15 +378,7 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected
},
deletePost: function(post) {
// Moderators can delete posts. Regular users can only create a deleted at message.
if (Discourse.User.current('staff')) {
post.set('deleted_at', new Date());
} else {
post.set('cooked', Discourse.Markdown.cook(I18n.t("post.deleted_by_author")));
post.set('can_delete', false);
post.set('version', post.get('version') + 1);
}
post.destroy();
post.destroy(Discourse.User.current());
},
removeAllowedUser: function(username) {

View File

@ -189,7 +189,21 @@ Discourse.Post = Discourse.Model.extend({
return Discourse.ajax("/posts/" + (this.get('id')) + "/recover", { type: 'PUT', cache: false });
},
destroy: function(complete) {
destroy: function(deleted_by) {
// Moderators can delete posts. Regular users can only trigger a deleted at message.
if (deleted_by.get('staff')) {
this.setProperties({
deleted_at: new Date(),
deleted_by: deleted_by
});
} else {
this.setProperties({
cooked: Discourse.Markdown.cook(I18n.t("post.deleted_by_author")),
can_delete: false,
version: this.get('version') + 1
});
}
return Discourse.ajax("/posts/" + (this.get('id')), { type: 'DELETE' });
},

View File

@ -1,5 +1,19 @@
module("Discourse.Post");
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");
});
test('new_user', function() {
var post = Discourse.Post.create({trust_level: 0});
ok(post.get('new_user'), "post is from a new user");
@ -16,7 +30,6 @@ test('firstPost', function() {
ok(!post.get('firstPost'), "post is no longer the first post");
});
test('updateFromPost', function() {
var post = Discourse.Post.create({
post_number: 1,
@ -32,13 +45,38 @@ test('updateFromPost', function() {
});
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');
});
});
test('destroy by staff', function() {
var user = Discourse.User.create({username: 'staff', staff: true});
var post = buildPost({user: user});
this.stub(Discourse, 'ajax');
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");
});
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");
});