3 class MessageTest < ActiveSupport::TestCase
4 EURO = "\xe2\x82\xac".freeze # euro symbol
6 def test_check_empty_message_fails
7 message = build(:message, :title => nil, :body => nil, :sent_on => nil)
8 assert_not_predicate message, :valid?
9 assert_predicate message.errors[:title], :any?
10 assert_predicate message.errors[:body], :any?
11 assert_predicate message.errors[:sent_on], :any?
12 assert_not message.message_read
15 def test_validating_msgs
16 message = create(:message, :unread)
17 assert_predicate message, :valid?
18 message = create(:message, :read)
19 assert_predicate message, :valid?
22 def test_invalid_send_recipient
23 message = create(:message, :unread)
25 message.recipient = nil
26 assert_not_predicate message, :valid?
28 assert_raise(ActiveRecord::RecordNotFound) { User.find(0) }
29 message.from_user_id = 0
30 message.to_user_id = 0
31 assert_raise(ActiveRecord::RecordInvalid) { message.save! }
34 def test_utf8_roundtrip
36 assert_message_ok("c", i)
37 assert_message_ok(EURO, i)
41 def test_length_oversize
42 assert_raise(ActiveRecord::RecordInvalid) { make_message("c", 256).save! }
43 assert_raise(ActiveRecord::RecordInvalid) { make_message(EURO, 256).save! }
47 # See e.g http://en.wikipedia.org/wiki/UTF-8 for byte sequences
48 # FIXME: Invalid Unicode characters can still be encoded into "valid" utf-8 byte sequences - maybe check this too?
49 invalid_sequences = ["\xC0", # always invalid utf8
50 "\xC2\x4a", # 2-byte multibyte identifier, followed by plain ASCII
51 "\xC2\xC2", # 2-byte multibyte identifier, followed by another one
52 "\x4a\x82", # plain ASCII, followed by multibyte continuation
53 "\x82\x82", # multibyte continuations without multibyte identifier
54 "\xe1\x82\x4a"] # three-byte identifier, continuation and (incorrectly) plain ASCII
55 invalid_sequences.each do |char|
56 # create a message and save to the database
57 msg = make_message(char, 1)
58 # if the save throws, thats fine and the test should pass, as we're
59 # only testing invalid sequences anyway.
62 # get the saved message back and check that it is identical - i.e:
63 # its OK to accept invalid UTF-8 as long as we return it unmodified.
64 db_msg = msg.class.find(msg.id)
65 assert_equal char, db_msg.title, "Database silently truncated message title"
66 rescue ArgumentError => e
67 assert_equal("invalid byte sequence in UTF-8", e.to_s)
71 def test_from_mail_plain
72 sender_user = create(:user)
73 recipient_user = create(:user)
75 from "from@example.com"
77 subject "Test message"
79 content_type "text/plain; charset=utf-8"
80 body "This is a test & a message"
82 message = Message.from_mail(mail, sender_user, recipient_user)
83 assert_equal sender_user, message.sender
84 assert_equal recipient_user, message.recipient
85 assert_equal mail.date, message.sent_on
86 assert_equal "Test message", message.title
87 assert_equal "This is a test & a message", message.body
88 assert_equal "text", message.body_format
91 def test_from_mail_html
92 sender_user = create(:user)
93 recipient_user = create(:user)
95 from "from@example.com"
97 subject "Test message"
99 content_type "text/html; charset=utf-8"
100 body "<p>This is a <b>test</b> & a message</p>"
102 message = Message.from_mail(mail, sender_user, recipient_user)
103 assert_equal sender_user, message.sender
104 assert_equal recipient_user, message.recipient
105 assert_equal mail.date, message.sent_on
106 assert_equal "Test message", message.title
107 assert_match(/^ *This is a test & a message *$/, message.body)
108 assert_equal "text", message.body_format
111 def test_from_mail_multipart
112 sender_user = create(:user)
113 recipient_user = create(:user)
115 from "from@example.com"
117 subject "Test message"
121 content_type "text/plain; charset=utf-8"
122 body "This is a test & a message in text format"
126 content_type "text/html; charset=utf-8"
127 body "<p>This is a <b>test</b> & a message in HTML format</p>"
130 message = Message.from_mail(mail, sender_user, recipient_user)
131 assert_equal sender_user, message.sender
132 assert_equal recipient_user, message.recipient
133 assert_equal mail.date, message.sent_on
134 assert_equal "Test message", message.title
135 assert_equal "This is a test & a message in text format", message.body
136 assert_equal "text", message.body_format
139 from "from@example.com"
141 subject "Test message"
145 content_type "text/html; charset=utf-8"
146 body "<p>This is a <b>test</b> & a message in HTML format</p>"
149 message = Message.from_mail(mail, sender_user, recipient_user)
150 assert_equal sender_user, message.sender
151 assert_equal recipient_user, message.recipient
152 assert_equal mail.date, message.sent_on
153 assert_equal "Test message", message.title
154 assert_match(/^ *This is a test & a message in HTML format *$/, message.body)
155 assert_equal "text", message.body_format
158 def test_from_mail_prefix
159 sender_user = create(:user)
160 recipient_user = create(:user)
162 from "from@example.com"
164 subject "[OpenStreetMap] Test message"
166 content_type "text/plain; charset=utf-8"
167 body "This is a test & a message"
169 message = Message.from_mail(mail, sender_user, recipient_user)
170 assert_equal sender_user, message.sender
171 assert_equal recipient_user, message.recipient
172 assert_equal mail.date, message.sent_on
173 assert_equal "Test message", message.title
174 assert_equal "This is a test & a message", message.body
175 assert_equal "text", message.body_format
178 def test_notify_recipient
179 message = create(:message)
180 assert_not_predicate message, :muted?
181 assert_predicate message, :notify_recipient?
184 def test_notify_recipient_for_muted_messages
185 message = create(:message, :muted)
186 assert_predicate message, :muted?
187 assert_not_predicate message, :notify_recipient?
190 def test_unmuting_a_muted_message
191 message = create(:message, :muted)
192 assert_predicate message, :muted?
195 assert_not_predicate message, :muted?
200 def make_message(char, count)
201 message = build(:message, :unread)
202 message.title = char * count
206 def assert_message_ok(char, count)
207 message = make_message(char, count)
209 response = message.class.find(message.id) # stand by for some uber-generalisation...
210 assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length * count} bytes) fails"