forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_scrubber_spec.rb
More file actions
121 lines (100 loc) · 3.51 KB
/
Copy pathmessage_scrubber_spec.rb
File metadata and controls
121 lines (100 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#
# Copyright (C) 2013 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../sharding_spec_helper')
describe MessageScrubber do
# Helpers
def message(updated_at)
Timecop.travel(updated_at) do
message = Message.new(notification: @notification, context: @context,
communication_channel: @recipient.communication_channel)
message.save!
message
end
end
def old_messages(count = 2)
(1..count).map do
message(700.days.ago)
end
end
def new_messages(count = 2)
(1..count).map do
message(Time.now)
end
end
describe '#scrub' do
before(:each) do
@context = course_factory
@notification = Notification.create!(name: 'Test Notification', category: 'Test')
@recipient = user_factory
@recipient.communication_channels.create!(path_type: 'email', path: 'user@example.com')
end
it 'should delete delayed messages older than 360 days' do
messages = old_messages(2)
scrubber = MessageScrubber.new
scrubber.scrub
expect(Message.where(id: messages.map(&:id)).count).to eq 0
end
it 'should not delete messages younger than 360 days' do
messages = old_messages(1) + new_messages(1)
scrubber = MessageScrubber.new
scrubber.scrub
expect(Message.where(id: messages.map(&:id)).count).to eq 1
end
it 'should log predicted results if passed dry_run=true' do
logger = mock
messages = old_messages(2)
scrubber = MessageScrubber.new(logger: logger)
logger.expects(:info).with("MessageScrubber: 2 records would be deleted (older than #{scrubber.limit})")
scrubber.scrub(dry_run: true)
end
end
describe '#scrub_all' do
specs_require_sharding
before(:each) do
@messages = []
@notification = Notification.create!(name: 'Test Notification', category: 'Test')
@shard1.activate do
@context = course_factory
@recipient = user_factory(name: 'User One')
@recipient.communication_channels.create!(path_type: 'email', path: 'user1@example.com')
@messages.concat(old_messages(1))
end
@shard2.activate do
@context = course_factory
@recipient = user_factory(name: 'User Two')
@recipient.communication_channels.create!(path_type: 'email', path: 'user2@example.com')
@messages.concat(old_messages(1))
end
end
it 'should scrub all shards' do
scrubber = MessageScrubber.new
scrubber.scrub_all
[@shard1, @shard2].each do |shard|
shard.activate do
expect(Message.where(id: @messages.map(&:id)).count).to eq 0
end
end
end
it 'should log each shard separately' do
logger = mock
scrubber = MessageScrubber.new(logger: logger)
logger.expects(:info).times(Shard.all.count)
scrubber.scrub_all(dry_run: true)
end
end
end