3 class MessageTest < ActiveSupport::TestCase
4 EURO = "\xe2\x82\xac".freeze # euro symbol
6 def test_check_empty_message_fails
8 assert_not message.valid?
9 assert message.errors[:title].any?
10 assert message.errors[:body].any?
11 assert message.errors[:sent_on].any?
12 assert_not message.message_read
15 def test_validating_msgs
16 message = create(:message, :unread)
18 message = create(:message, :read)
22 def test_invalid_send_recipient
23 message = create(:message, :unread)
25 message.recipient = nil
26 assert_not 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, contination and (incorrectly) plain ASCII
55 invalid_sequences.each do |char|
57 # create a message and save to the database
58 msg = make_message(char, 1)
59 # if the save throws, thats fine and the test should pass, as we're
60 # only testing invalid sequences anyway.
63 # get the saved message back and check that it is identical - i.e:
64 # its OK to accept invalid UTF-8 as long as we return it unmodified.
65 db_msg = msg.class.find(msg.id)
66 assert_equal char, db_msg.title, "Database silently truncated message title"
67 rescue ArgumentError => ex
68 assert_equal ex.to_s, "invalid byte sequence in UTF-8"
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
182 def make_message(char, count)
183 message = build(:message, :unread)
184 message.title = char * count
188 def assert_message_ok(char, count)
189 message = make_message(char, count)
191 response = message.class.find(message.id) # stand by for some über-generalisation...
192 assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length * count} bytes) fails"