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
80 from "from@example.com"
82 subject "Test message"
84 content_type "text/plain; charset=utf-8"
85 body "This is a test & a message"
87 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
88 assert_equal users(:normal_user), message.sender
89 assert_equal users(:public_user), message.recipient
90 assert_equal mail.date, message.sent_on
91 assert_equal "Test message", message.title
92 assert_equal "This is a test & a message", message.body
93 assert_equal "text", message.body_format
96 def test_from_mail_html
98 from "from@example.com"
100 subject "Test message"
102 content_type "text/html; charset=utf-8"
103 body "<p>This is a <b>test</b> & a message</p>"
105 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
106 assert_equal users(:normal_user), message.sender
107 assert_equal users(:public_user), message.recipient
108 assert_equal mail.date, message.sent_on
109 assert_equal "Test message", message.title
110 assert_match /^ *This is a test & a message *$/, message.body
111 assert_equal "text", message.body_format
114 def test_from_mail_multipart
116 from "from@example.com"
118 subject "Test message"
122 content_type "text/plain; charset=utf-8"
123 body "This is a test & a message in text format"
127 content_type "text/html; charset=utf-8"
128 body "<p>This is a <b>test</b> & a message in HTML format</p>"
131 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
132 assert_equal users(:normal_user), message.sender
133 assert_equal users(:public_user), message.recipient
134 assert_equal mail.date, message.sent_on
135 assert_equal "Test message", message.title
136 assert_equal "This is a test & a message in text format", message.body
137 assert_equal "text", message.body_format
140 from "from@example.com"
142 subject "Test message"
146 content_type "text/html; charset=utf-8"
147 body "<p>This is a <b>test</b> & a message in HTML format</p>"
150 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
151 assert_equal users(:normal_user), message.sender
152 assert_equal users(:public_user), message.recipient
153 assert_equal mail.date, message.sent_on
154 assert_equal "Test message", message.title
155 assert_match /^ *This is a test & a message in HTML format *$/, message.body
156 assert_equal "text", message.body_format
159 def test_from_mail_prefix
161 from "from@example.com"
163 subject "[OpenStreetMap] Test message"
165 content_type "text/plain; charset=utf-8"
166 body "This is a test & a message"
168 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
169 assert_equal users(:normal_user), message.sender
170 assert_equal users(:public_user), message.recipient
171 assert_equal mail.date, message.sent_on
172 assert_equal "Test message", message.title
173 assert_equal "This is a test & a message", message.body
174 assert_equal "text", message.body_format
179 def make_message(char, count)
180 message = build(:message, :unread)
181 message.title = char * count
185 def assert_message_ok(char, count)
186 message = make_message(char, count)
188 response = message.class.find(message.id) # stand by for some über-generalisation...
189 assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length * count} bytes) fails"