1 # frozen_string_literal: true
5 class MessageTest < ActiveSupport::TestCase
6 EURO = "\xe2\x82\xac" # euro symbol
8 def test_check_empty_message_fails
9 message = build(:message, :title => nil, :body => nil, :sent_on => nil)
10 assert_not_predicate message, :valid?
11 assert_predicate message.errors[:title], :any?
12 assert_predicate message.errors[:body], :any?
13 assert_predicate message.errors[:sent_on], :any?
14 assert_not message.message_read
17 def test_validating_msgs
18 message = create(:message, :unread)
19 assert_predicate message, :valid?
20 message = create(:message, :read)
21 assert_predicate message, :valid?
24 def test_invalid_send_recipient
25 message = create(:message, :unread)
27 message.recipient = nil
28 assert_not_predicate message, :valid?
30 assert_raise(ActiveRecord::RecordNotFound) { User.find(0) }
31 message.from_user_id = 0
32 message.to_user_id = 0
33 assert_raise(ActiveRecord::RecordInvalid) { message.save! }
36 def test_utf8_roundtrip
38 assert_message_ok("c", i)
39 assert_message_ok(EURO, i)
43 def test_length_oversize
44 assert_raise(ActiveRecord::RecordInvalid) { make_message("c", 256).save! }
45 assert_raise(ActiveRecord::RecordInvalid) { make_message(EURO, 256).save! }
49 # See e.g http://en.wikipedia.org/wiki/UTF-8 for byte sequences
50 # FIXME: Invalid Unicode characters can still be encoded into "valid" utf-8 byte sequences - maybe check this too?
51 invalid_sequences = ["\xC0", # always invalid utf8
52 "\xC2\x4a", # 2-byte multibyte identifier, followed by plain ASCII
53 "\xC2\xC2", # 2-byte multibyte identifier, followed by another one
54 "\x4a\x82", # plain ASCII, followed by multibyte continuation
55 "\x82\x82", # multibyte continuations without multibyte identifier
56 "\xe1\x82\x4a"] # three-byte identifier, continuation and (incorrectly) plain ASCII
57 invalid_sequences.each do |char|
58 # create a message and save to the database
59 msg = make_message(char, 1)
60 # if the save throws, thats fine and the test should pass, as we're
61 # only testing invalid sequences anyway.
64 # get the saved message back and check that it is identical - i.e:
65 # its OK to accept invalid UTF-8 as long as we return it unmodified.
66 db_msg = msg.class.find(msg.id)
67 assert_equal char, db_msg.title, "Database silently truncated message title"
68 rescue ArgumentError => e
69 assert_equal("invalid byte sequence in UTF-8", e.to_s)
73 def test_from_mail_plain
74 sender_user = create(:user)
75 recipient_user = create(:user)
77 from "from@example.com"
79 subject "Test message"
81 content_type "text/plain; charset=utf-8"
82 body "This is a test & a message"
84 message = Message.from_mail(mail, sender_user, recipient_user)
85 assert_equal sender_user, message.sender
86 assert_equal recipient_user, message.recipient
87 assert_equal mail.date, message.sent_on
88 assert_equal "Test message", message.title
89 assert_equal "This is a test & a message", message.body
90 assert_equal "text", message.body_format
93 def test_from_mail_html
94 sender_user = create(:user)
95 recipient_user = create(:user)
97 from "from@example.com"
99 subject "Test message"
101 content_type "text/html; charset=utf-8"
102 body "<p>This is a <b>test</b> & a message</p>"
104 message = Message.from_mail(mail, sender_user, recipient_user)
105 assert_equal sender_user, message.sender
106 assert_equal recipient_user, message.recipient
107 assert_equal mail.date, message.sent_on
108 assert_equal "Test message", message.title
109 assert_match(/^ *This is a test & a message *$/, message.body)
110 assert_equal "text", message.body_format
113 def test_from_mail_multipart
114 sender_user = create(:user)
115 recipient_user = create(:user)
117 from "from@example.com"
119 subject "Test message"
123 content_type "text/plain; charset=utf-8"
124 body "This is a test & a message in text format"
128 content_type "text/html; charset=utf-8"
129 body "<p>This is a <b>test</b> & a message in HTML format</p>"
132 message = Message.from_mail(mail, sender_user, recipient_user)
133 assert_equal sender_user, message.sender
134 assert_equal recipient_user, message.recipient
135 assert_equal mail.date, message.sent_on
136 assert_equal "Test message", message.title
137 assert_equal "This is a test & a message in text format", message.body
138 assert_equal "text", message.body_format
141 from "from@example.com"
143 subject "Test message"
147 content_type "text/html; charset=utf-8"
148 body "<p>This is a <b>test</b> & a message in HTML format</p>"
151 message = Message.from_mail(mail, sender_user, recipient_user)
152 assert_equal sender_user, message.sender
153 assert_equal recipient_user, message.recipient
154 assert_equal mail.date, message.sent_on
155 assert_equal "Test message", message.title
156 assert_match(/^ *This is a test & a message in HTML format *$/, message.body)
157 assert_equal "text", message.body_format
160 def test_from_mail_prefix
161 sender_user = create(:user)
162 recipient_user = create(:user)
164 from "from@example.com"
166 subject "[OpenStreetMap] Test message"
168 content_type "text/plain; charset=utf-8"
169 body "This is a test & a message"
171 message = Message.from_mail(mail, sender_user, recipient_user)
172 assert_equal sender_user, message.sender
173 assert_equal recipient_user, message.recipient
174 assert_equal mail.date, message.sent_on
175 assert_equal "Test message", message.title
176 assert_equal "This is a test & a message", message.body
177 assert_equal "text", message.body_format
180 def test_notify_recipient
181 message = create(:message)
182 assert_not_predicate message, :muted?
183 assert_predicate message, :notify_recipient?
186 def test_notify_recipient_for_muted_messages
187 message = create(:message, :muted)
188 assert_predicate message, :muted?
189 assert_not_predicate message, :notify_recipient?
192 def test_unmuting_a_muted_message
193 message = create(:message, :muted)
194 assert_predicate message, :muted?
197 assert_not_predicate message, :muted?
202 def make_message(char, count)
203 message = build(:message, :unread)
204 message.title = char * count
208 def assert_message_ok(char, count)
209 message = make_message(char, count)
211 response = message.class.find(message.id) # stand by for some uber-generalisation...
212 assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length * count} bytes) fails"