1 # -*- coding: utf-8 -*-
4 class MessageTest < ActiveSupport::TestCase
7 EURO = "\xe2\x82\xac".freeze # euro symbol
9 def test_check_empty_message_fails
11 assert !message.valid?
12 assert message.errors[:title].any?
13 assert message.errors[:body].any?
14 assert message.errors[:sent_on].any?
15 assert !message.message_read
18 def test_validating_msgs
19 message = create(:message, :unread)
21 message = create(:message, :read)
25 def test_invalid_send_recipient
26 message = create(:message, :unread)
28 message.recipient = nil
29 assert !message.valid?
31 assert_raise(ActiveRecord::RecordNotFound) { User.find(0) }
32 message.from_user_id = 0
33 message.to_user_id = 0
34 assert_raise(ActiveRecord::RecordInvalid) { message.save! }
37 def test_utf8_roundtrip
39 assert_message_ok("c", i)
40 assert_message_ok(EURO, i)
44 def test_length_oversize
45 assert_raise(ActiveRecord::RecordInvalid) { make_message("c", 256).save! }
46 assert_raise(ActiveRecord::RecordInvalid) { make_message(EURO, 256).save! }
50 # See e.g http://en.wikipedia.org/wiki/UTF-8 for byte sequences
51 # FIXME: Invalid Unicode characters can still be encoded into "valid" utf-8 byte sequences - maybe check this too?
52 invalid_sequences = ["\xC0", # always invalid utf8
53 "\xC2\x4a", # 2-byte multibyte identifier, followed by plain ASCII
54 "\xC2\xC2", # 2-byte multibyte identifier, followed by another one
55 "\x4a\x82", # plain ASCII, followed by multibyte continuation
56 "\x82\x82", # multibyte continuations without multibyte identifier
57 "\xe1\x82\x4a"] # three-byte identifier, contination and (incorrectly) plain ASCII
58 invalid_sequences.each do |char|
60 # create a message and save to the database
61 msg = make_message(char, 1)
62 # if the save throws, thats fine and the test should pass, as we're
63 # only testing invalid sequences anyway.
66 # get the saved message back and check that it is identical - i.e:
67 # its OK to accept invalid UTF-8 as long as we return it unmodified.
68 db_msg = msg.class.find(msg.id)
69 assert_equal char, db_msg.title, "Database silently truncated message title"
71 rescue ArgumentError => ex
72 assert_equal ex.to_s, "invalid byte sequence in UTF-8"
78 def test_from_mail_plain
79 sender_user = create(:user)
80 recipient_user = create(:user)
82 from "from@example.com"
84 subject "Test message"
86 content_type "text/plain; charset=utf-8"
87 body "This is a test & a message"
89 message = Message.from_mail(mail, sender_user, recipient_user)
90 assert_equal sender_user, message.sender
91 assert_equal recipient_user, message.recipient
92 assert_equal mail.date, message.sent_on
93 assert_equal "Test message", message.title
94 assert_equal "This is a test & a message", message.body
95 assert_equal "text", message.body_format
98 def test_from_mail_html
99 sender_user = create(:user)
100 recipient_user = create(:user)
102 from "from@example.com"
104 subject "Test message"
106 content_type "text/html; charset=utf-8"
107 body "<p>This is a <b>test</b> & a message</p>"
109 message = Message.from_mail(mail, sender_user, recipient_user)
110 assert_equal sender_user, message.sender
111 assert_equal recipient_user, message.recipient
112 assert_equal mail.date, message.sent_on
113 assert_equal "Test message", message.title
114 assert_match /^ *This is a test & a message *$/, message.body
115 assert_equal "text", message.body_format
118 def test_from_mail_multipart
119 sender_user = create(:user)
120 recipient_user = create(:user)
122 from "from@example.com"
124 subject "Test message"
128 content_type "text/plain; charset=utf-8"
129 body "This is a test & a message in text format"
133 content_type "text/html; charset=utf-8"
134 body "<p>This is a <b>test</b> & a message in HTML format</p>"
137 message = Message.from_mail(mail, sender_user, recipient_user)
138 assert_equal sender_user, message.sender
139 assert_equal recipient_user, message.recipient
140 assert_equal mail.date, message.sent_on
141 assert_equal "Test message", message.title
142 assert_equal "This is a test & a message in text format", message.body
143 assert_equal "text", message.body_format
146 from "from@example.com"
148 subject "Test message"
152 content_type "text/html; charset=utf-8"
153 body "<p>This is a <b>test</b> & a message in HTML format</p>"
156 message = Message.from_mail(mail, sender_user, recipient_user)
157 assert_equal sender_user, message.sender
158 assert_equal recipient_user, message.recipient
159 assert_equal mail.date, message.sent_on
160 assert_equal "Test message", message.title
161 assert_match /^ *This is a test & a message in HTML format *$/, message.body
162 assert_equal "text", message.body_format
165 def test_from_mail_prefix
166 sender_user = create(:user)
167 recipient_user = create(:user)
169 from "from@example.com"
171 subject "[OpenStreetMap] Test message"
173 content_type "text/plain; charset=utf-8"
174 body "This is a test & a message"
176 message = Message.from_mail(mail, sender_user, recipient_user)
177 assert_equal sender_user, message.sender
178 assert_equal recipient_user, message.recipient
179 assert_equal mail.date, message.sent_on
180 assert_equal "Test message", message.title
181 assert_equal "This is a test & a message", message.body
182 assert_equal "text", message.body_format
187 def make_message(char, count)
188 message = build(:message, :unread)
189 message.title = char * count
193 def assert_message_ok(char, count)
194 message = make_message(char, count)
196 response = message.class.find(message.id) # stand by for some über-generalisation...
197 assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length * count} bytes) fails"