1 # -*- coding: utf-8 -*-
4 class MessageTest < ActiveSupport::TestCase
8 EURO = "\xe2\x82\xac" # 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
66 invalid_sequences.each do |char|
68 # create a message and save to the database
69 msg = make_message(char, 1)
70 # if the save throws, thats fine and the test should pass, as we're
71 # only testing invalid sequences anyway.
74 # get the saved message back and check that it is identical - i.e:
75 # its OK to accept invalid UTF-8 as long as we return it unmodified.
76 db_msg = msg.class.find(msg.id)
77 assert_equal char, db_msg.title, "Database silently truncated message title"
79 rescue ArgumentError => ex
80 assert_equal ex.to_s, "invalid byte sequence in UTF-8"
86 def test_from_mail_plain
88 from "from@example.com"
90 subject "Test message"
92 content_type 'text/plain; charset=utf-8'
93 body "This is a test & a message"
95 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
96 assert_equal users(:normal_user), message.sender
97 assert_equal users(:public_user), message.recipient
98 assert_equal mail.date, message.sent_on
99 assert_equal "Test message", message.title
100 assert_equal "This is a test & a message", message.body
101 assert_equal "text", message.body_format
104 def test_from_mail_html
106 from "from@example.com"
108 subject "Test message"
110 content_type 'text/html; charset=utf-8'
111 body "<p>This is a <b>test</b> & a message</p>"
113 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
114 assert_equal users(:normal_user), message.sender
115 assert_equal users(:public_user), message.recipient
116 assert_equal mail.date, message.sent_on
117 assert_equal "Test message", message.title
118 assert_match /^ *This is a test & a message *$/, message.body
119 assert_equal "text", message.body_format
122 def test_from_mail_multipart
124 from "from@example.com"
126 subject "Test message"
130 content_type 'text/plain; charset=utf-8'
131 body "This is a test & a message in text format"
135 content_type 'text/html; charset=utf-8'
136 body "<p>This is a <b>test</b> & a message in HTML format</p>"
139 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
140 assert_equal users(:normal_user), message.sender
141 assert_equal users(:public_user), message.recipient
142 assert_equal mail.date, message.sent_on
143 assert_equal "Test message", message.title
144 assert_equal "This is a test & a message in text format", message.body
145 assert_equal "text", message.body_format
148 from "from@example.com"
150 subject "Test message"
154 content_type 'text/html; charset=utf-8'
155 body "<p>This is a <b>test</b> & a message in HTML format</p>"
158 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
159 assert_equal users(:normal_user), message.sender
160 assert_equal users(:public_user), message.recipient
161 assert_equal mail.date, message.sent_on
162 assert_equal "Test message", message.title
163 assert_match /^ *This is a test & a message in HTML format *$/, message.body
164 assert_equal "text", message.body_format
167 def test_from_mail_prefix
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, users(:normal_user), users(:public_user))
177 assert_equal users(:normal_user), message.sender
178 assert_equal users(:public_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 = messages(:unread_message)
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"