1 # -*- coding: utf-8 -*-
4 class MessageTest < ActiveSupport::TestCase
8 EURO = "\xe2\x82\xac".freeze # euro symbol
10 # This needs to be updated when new fixtures are added
12 def test_check_message_count
13 assert_equal 2, Message.count
16 def test_check_empty_message_fails
18 assert !message.valid?
19 assert message.errors[:title].any?
20 assert message.errors[:body].any?
21 assert message.errors[:sent_on].any?
22 assert !message.message_read
25 def test_validating_msgs
26 message = messages(:unread_message)
28 message = messages(:read_message)
32 def test_invalid_send_recipient
33 message = messages(:unread_message)
35 message.recipient = nil
36 assert !message.valid?
38 assert_raise(ActiveRecord::RecordNotFound) { User.find(0) }
39 message.from_user_id = 0
40 message.to_user_id = 0
41 assert_raise(ActiveRecord::RecordInvalid) { message.save! }
44 def test_utf8_roundtrip
46 assert_message_ok("c", i)
47 assert_message_ok(EURO, i)
51 def test_length_oversize
52 assert_raise(ActiveRecord::RecordInvalid) { make_message("c", 256).save! }
53 assert_raise(ActiveRecord::RecordInvalid) { make_message(EURO, 256).save! }
57 # See e.g http://en.wikipedia.org/wiki/UTF-8 for byte sequences
58 # FIXME: Invalid Unicode characters can still be encoded into "valid" utf-8 byte sequences - maybe check this too?
59 invalid_sequences = ["\xC0", # always invalid utf8
60 "\xC2\x4a", # 2-byte multibyte identifier, followed by plain ASCII
61 "\xC2\xC2", # 2-byte multibyte identifier, followed by another one
62 "\x4a\x82", # plain ASCII, followed by multibyte continuation
63 "\x82\x82", # multibyte continuations without multibyte identifier
64 "\xe1\x82\x4a"] # three-byte identifier, contination and (incorrectly) plain ASCII
65 invalid_sequences.each do |char|
67 # create a message and save to the database
68 msg = make_message(char, 1)
69 # if the save throws, thats fine and the test should pass, as we're
70 # only testing invalid sequences anyway.
73 # get the saved message back and check that it is identical - i.e:
74 # its OK to accept invalid UTF-8 as long as we return it unmodified.
75 db_msg = msg.class.find(msg.id)
76 assert_equal char, db_msg.title, "Database silently truncated message title"
78 rescue ArgumentError => ex
79 assert_equal ex.to_s, "invalid byte sequence in UTF-8"
85 def test_from_mail_plain
87 from "from@example.com"
89 subject "Test message"
91 content_type "text/plain; charset=utf-8"
92 body "This is a test & a message"
94 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
95 assert_equal users(:normal_user), message.sender
96 assert_equal users(:public_user), message.recipient
97 assert_equal mail.date, message.sent_on
98 assert_equal "Test message", message.title
99 assert_equal "This is a test & a message", message.body
100 assert_equal "text", message.body_format
103 def test_from_mail_html
105 from "from@example.com"
107 subject "Test message"
109 content_type "text/html; charset=utf-8"
110 body "<p>This is a <b>test</b> & a message</p>"
112 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
113 assert_equal users(:normal_user), message.sender
114 assert_equal users(:public_user), message.recipient
115 assert_equal mail.date, message.sent_on
116 assert_equal "Test message", message.title
117 assert_match /^ *This is a test & a message *$/, message.body
118 assert_equal "text", message.body_format
121 def test_from_mail_multipart
123 from "from@example.com"
125 subject "Test message"
129 content_type "text/plain; charset=utf-8"
130 body "This is a test & a message in text format"
134 content_type "text/html; charset=utf-8"
135 body "<p>This is a <b>test</b> & a message in HTML format</p>"
138 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
139 assert_equal users(:normal_user), message.sender
140 assert_equal users(:public_user), message.recipient
141 assert_equal mail.date, message.sent_on
142 assert_equal "Test message", message.title
143 assert_equal "This is a test & a message in text format", message.body
144 assert_equal "text", message.body_format
147 from "from@example.com"
149 subject "Test message"
153 content_type "text/html; charset=utf-8"
154 body "<p>This is a <b>test</b> & a message in HTML format</p>"
157 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
158 assert_equal users(:normal_user), message.sender
159 assert_equal users(:public_user), message.recipient
160 assert_equal mail.date, message.sent_on
161 assert_equal "Test message", message.title
162 assert_match /^ *This is a test & a message in HTML format *$/, message.body
163 assert_equal "text", message.body_format
166 def test_from_mail_prefix
168 from "from@example.com"
170 subject "[OpenStreetMap] Test message"
172 content_type "text/plain; charset=utf-8"
173 body "This is a test & a message"
175 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
176 assert_equal users(:normal_user), message.sender
177 assert_equal users(:public_user), message.recipient
178 assert_equal mail.date, message.sent_on
179 assert_equal "Test message", message.title
180 assert_equal "This is a test & a message", message.body
181 assert_equal "text", message.body_format
186 def make_message(char, count)
187 message = messages(:unread_message)
188 message.title = char * count
192 def assert_message_ok(char, count)
193 message = make_message(char, count)
195 response = message.class.find(message.id) # stand by for some über-generalisation...
196 assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length * count} bytes) fails"