|
| 1 | +require 'spec_helper' |
| 2 | +require 'post_revisor' |
| 3 | + |
| 4 | +describe PostRevisor do |
| 5 | + |
| 6 | + let(:topic) { Fabricate(:topic) } |
| 7 | + let(:post_args) { {user: topic.user, topic: topic} } |
| 8 | + |
| 9 | + context 'revise' do |
| 10 | + |
| 11 | + let(:post) { Fabricate(:post, post_args) } |
| 12 | + let(:first_version_at) { post.last_version_at } |
| 13 | + |
| 14 | + subject { described_class.new(post) } |
| 15 | + |
| 16 | + describe 'with the same body' do |
| 17 | + |
| 18 | + it 'returns false' do |
| 19 | + subject.revise!(post.user, post.raw).should be_false |
| 20 | + end |
| 21 | + |
| 22 | + it "doesn't change cached_version" do |
| 23 | + lambda { subject.revise!(post.user, post.raw); post.reload }.should_not change(post, :cached_version) |
| 24 | + end |
| 25 | + |
| 26 | + end |
| 27 | + |
| 28 | + describe 'ninja editing' do |
| 29 | + before do |
| 30 | + SiteSetting.expects(:ninja_edit_window).returns(1.minute.to_i) |
| 31 | + subject.revise!(post.user, 'updated body', revised_at: post.updated_at + 10.seconds) |
| 32 | + post.reload |
| 33 | + end |
| 34 | + |
| 35 | + it 'does not update cached_version' do |
| 36 | + post.cached_version.should == 1 |
| 37 | + end |
| 38 | + |
| 39 | + it 'does not create a new version' do |
| 40 | + post.all_versions.size.should == 1 |
| 41 | + end |
| 42 | + |
| 43 | + it "doesn't change the last_version_at" do |
| 44 | + post.last_version_at.should == first_version_at |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + describe 'revision much later' do |
| 49 | + |
| 50 | + let!(:revised_at) { post.updated_at + 2.minutes } |
| 51 | + |
| 52 | + before do |
| 53 | + SiteSetting.stubs(:ninja_edit_window).returns(1.minute.to_i) |
| 54 | + subject.revise!(post.user, 'updated body', revised_at: revised_at) |
| 55 | + post.reload |
| 56 | + end |
| 57 | + |
| 58 | + it 'updates the cached_version' do |
| 59 | + post.cached_version.should == 2 |
| 60 | + end |
| 61 | + |
| 62 | + it 'creates a new version' do |
| 63 | + post.all_versions.size.should == 2 |
| 64 | + end |
| 65 | + |
| 66 | + it "updates the last_version_at" do |
| 67 | + post.last_version_at.to_i.should == revised_at.to_i |
| 68 | + end |
| 69 | + |
| 70 | + describe "new edit window" do |
| 71 | + |
| 72 | + before do |
| 73 | + subject.revise!(post.user, 'yet another updated body', revised_at: revised_at) |
| 74 | + post.reload |
| 75 | + end |
| 76 | + |
| 77 | + it "doesn't create a new version if you do another" do |
| 78 | + post.cached_version.should == 2 |
| 79 | + end |
| 80 | + |
| 81 | + it "doesn't change last_version_at" do |
| 82 | + post.last_version_at.to_i.should == revised_at.to_i |
| 83 | + end |
| 84 | + |
| 85 | + context "after second window" do |
| 86 | + |
| 87 | + let!(:new_revised_at) {revised_at + 2.minutes} |
| 88 | + |
| 89 | + before do |
| 90 | + subject.revise!(post.user, 'yet another, another updated body', revised_at: new_revised_at) |
| 91 | + post.reload |
| 92 | + end |
| 93 | + |
| 94 | + it "does create a new version after the edit window" do |
| 95 | + post.cached_version.should == 3 |
| 96 | + end |
| 97 | + |
| 98 | + it "does create a new version after the edit window" do |
| 99 | + post.last_version_at.to_i.should == new_revised_at.to_i |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + describe 'rate limiter' do |
| 106 | + let(:changed_by) { Fabricate(:coding_horror) } |
| 107 | + |
| 108 | + it "triggers a rate limiter" do |
| 109 | + EditRateLimiter.any_instance.expects(:performed!) |
| 110 | + subject.revise!(changed_by, 'updated body') |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + describe 'with a new body' do |
| 115 | + let(:changed_by) { Fabricate(:coding_horror) } |
| 116 | + let!(:result) { subject.revise!(changed_by, 'updated body') } |
| 117 | + |
| 118 | + it 'returns true' do |
| 119 | + result.should be_true |
| 120 | + end |
| 121 | + |
| 122 | + it 'updates the body' do |
| 123 | + post.raw.should == 'updated body' |
| 124 | + end |
| 125 | + |
| 126 | + it 'sets the invalidate oneboxes attribute' do |
| 127 | + post.invalidate_oneboxes.should == true |
| 128 | + end |
| 129 | + |
| 130 | + it 'increased the cached_version' do |
| 131 | + post.cached_version.should == 2 |
| 132 | + end |
| 133 | + |
| 134 | + it 'has the new version in all_versions' do |
| 135 | + post.all_versions.size.should == 2 |
| 136 | + end |
| 137 | + |
| 138 | + it 'has versions' do |
| 139 | + post.versions.should be_present |
| 140 | + end |
| 141 | + |
| 142 | + it "saved the user who made the change in the version" do |
| 143 | + post.versions.first.user.should be_present |
| 144 | + end |
| 145 | + |
| 146 | + context 'second poster posts again quickly' do |
| 147 | + before do |
| 148 | + SiteSetting.expects(:ninja_edit_window).returns(1.minute.to_i) |
| 149 | + subject.revise!(changed_by, 'yet another updated body', revised_at: post.updated_at + 10.seconds) |
| 150 | + post.reload |
| 151 | + end |
| 152 | + |
| 153 | + it 'is a ninja edit, because the second poster posted again quickly' do |
| 154 | + post.cached_version.should == 2 |
| 155 | + end |
| 156 | + |
| 157 | + it 'is a ninja edit, because the second poster posted again quickly' do |
| 158 | + post.all_versions.size.should == 2 |
| 159 | + end |
| 160 | + end |
| 161 | + end |
| 162 | + end |
| 163 | +end |
0 commit comments