/
test_base_store.rb
179 lines (157 loc) · 6.72 KB
/
test_base_store.rb
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# frozen_string_literal: true
require_relative '../test_helper'
require 'logster/base_store'
require 'logster/ignore_pattern'
class TestBaseStore < Minitest::Test
def setup
@store = Logster::TestStore.new
@store.clear_all
end
def teardown
@store.clear_all
end
def test_report_skip_empty
@store.skip_empty = true
@store.report(Logger::WARN, "test", nil)
@store.report(Logger::WARN, "test", '')
@store.report(Logger::WARN, "test", "foo") #
@store.skip_empty = false
@store.report(Logger::WARN, "test", nil) #
assert_equal(2, @store.count)
end
def test_report_skip_level
@store.level = nil
@store.report(Logger::DEBUG, "test", "A") #
@store.level = Logger::WARN
@store.report(Logger::DEBUG, "test", "A")
@store.report(Logger::INFO, "test", "B")
@store.report(Logger::WARN, "test", "C") #
@store.report(Logger::ERROR, "test", "D") #
assert_equal(3, @store.count)
end
def test_report_skip_ignore
@store.report(Logger::WARN, "test", "Can't verify CSRF token authenticity")
@store.report(Logger::FATAL, "test", "ActiveRecord::RecordNotFound (Couldn't find Upload with 'id'=9947)")
@store.report(Logger::WARN, "test", "B")
@store.ignore = [
/^ActiveRecord::RecordNotFound \(Couldn't find Upload/,
/^Can't verify CSRF token authenticity/
]
@store.report(Logger::WARN, "test", "Can't verify CSRF token authenticity")
@store.report(Logger::FATAL, "test", "ActiveRecord::RecordNotFound (Couldn't find Upload with 'id'=9947)")
@store.report(Logger::FATAL, "test", "ActiveRecord::RecordNotFound (Couldn't find Upload with 'id'=9489+78946947)")
@store.report(Logger::WARN, "test", "B")
assert_equal(4, @store.count)
end
def test_ignore_pattern_basic
@store.ignore = [
Logster::IgnorePattern.new(nil, username: 'CausingErrors')
]
@store.report(Logger::WARN, "test", "Foobar") #
@store.report(Logger::WARN, "test", "Foobar", env: { username: 'CausingErrors' })
@store.report(Logger::WARN, "test", "Foobar", env: nil)
@store.report(Logger::WARN, "test", "Something Else", env: { username: 'CausingErrors' })
@store.report(Logger::WARN, "test", "Something Else", env: { 'username' => 'CausingErrors' })
@store.report(Logger::WARN, "test", "Something Else", env: { username: 'GoodPerson' }) #
@store.report(Logger::WARN, "test", "Can't verify CSRF token authenticity") #
assert_equal(4, @store.count)
end
def test_ignore_pattern_real
@store.ignore = [
/^ActionController::RoutingError \(No route matches/,
Logster::IgnorePattern.new("Can't verify CSRF token authenticity", REQUEST_URI: /\/trackback\/$/)
]
# blocked
@store.report(Logger::WARN, "whatever", "Can't verify CSRF token authenticity",
env: {
HTTP_HOST: 'meta.discourse.org',
REQUEST_URI: '/t/use-more-standard-smiley-codes-instead-of-smile/1822/trackback/',
REQUEST_METHOD: 'POST',
HTTP_USER_AGENT: 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
params: {
title: 'Something Spammy',
url: 'http://spam.example.net/whatever/spam.html',
excerpt: 'http://spam.example.com/pdf/blahblah.html free viagra',
blog_name: 'get free spam for cheap'
}
}
)
# logged
@store.report(Logger::WARN, "whatever", "Can't verify CSRF token authenticity",
env: {
HTTP_HOST: 'meta.discourse.org',
REQUEST_URI: '/session',
REQUEST_METHOD: 'POST',
HTTP_USER_AGENT: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
params: {
username: 'user',
password: 'password',
form_authenticity_token: 'incorrect'
}
}
)
assert_equal(1, @store.count)
end
def test_timestamp
time = Time.now - 24 * 60 * 60
message = @store.report(Logger::WARN, "test", "B", timestamp: time)
assert_equal(time, message.timestamp)
end
def test_backtrace
# Create an error with a backtrace
error = TypeError.new
begin
raise error
rescue => e
error = e
end
# Backtrace can be passed via backtrace param or env
message = @store.report(Logger::WARN, "test", "A", backtrace: error.backtrace)
assert_equal(error.backtrace.join("\n"), message.backtrace)
message = @store.report(Logger::WARN, "test", "B", env: { backtrace: error.backtrace })
assert_equal(error.backtrace.join("\n"), message.backtrace)
# Via env takes priority
message = @store.report(Logger::WARN, "test", "C", backtrace: "Garbage", env: { backtrace: error.backtrace })
assert_equal(error.backtrace.join("\n"), message.backtrace)
# Backtrace is always a string
# Cannot do an equal assert here, because it uses `caller` when not provided
message = @store.report(Logger::WARN, "test", "D", backtrace: nil)
assert_kind_of(String, message.backtrace)
message = @store.report(Logger::WARN, "test", "E", env: { backtrace: nil })
assert_kind_of(String, message.backtrace)
message = @store.report(Logger::WARN, "test", "F", backtrace: nil, env: { backtrace: nil })
assert_kind_of(String, message.backtrace)
message = @store.report(Logger::WARN, "test", "G")
assert_kind_of(String, message.backtrace)
# Arrays are turned into strings via join \n
message = @store.report(Logger::WARN, "test", "H", backtrace: ["Foo", "Bar"])
assert_equal("Foo\nBar", message.backtrace)
end
def test_chained_loggers_dont_have_superfluous_frames_in_backtrace
logger = Logster::Logger.new(@store)
other_store = Logster::TestStore.new
other_logger = Logster::Logger.new(other_store)
logger.chain(other_logger)
logger.warn("this is warning")
[@store, other_store].each do |store|
message = store.reported.first
assert_equal("this is warning", message.message)
# the first line in the backtrace should be the method that
# called the warn/info/error etc. method.
# in this case the first line should be this test method
assert_includes(message.backtrace.lines.first, __method__.to_s)
end
end
def test_log_message_is_truncated_when_above_maximum_message_length
orig = Logster.config.maximum_message_length
Logster.config.maximum_message_length = 300
msg = @store.report(Logger::WARN, '', 'a' * 400)
# 3 is the ... at the end to indicate truncated message
assert_equal(300 + 3, msg.message.size)
Logster.config.maximum_message_length = 100
msg = @store.report(Logger::WARN, '', 'a' * 200)
assert_equal(100 + 3, msg.message.size)
ensure
Logster.config.maximum_message_length = orig
end
end