1 # -*- coding: utf-8 -*-
4 class MessageTest < ActiveSupport::TestCase
5 EURO = "\xe2\x82\xac".freeze # euro symbol
7 def test_check_empty_message_fails
10 assert message.errors[:title].any?
11 assert message.errors[:body].any?
12 assert message.errors[:sent_on].any?
13 assert !message.message_read
16 def test_validating_msgs
17 message = create(:message, :unread)
19 message = create(:message, :read)
23 def test_invalid_send_recipient
24 message = create(:message, :unread)
26 message.recipient = nil
27 assert !message.valid?
29 assert_raise(ActiveRecord::RecordNotFound) { User.find(0) }
30 message.from_user_id = 0
31 message.to_user_id = 0
32 assert_raise(ActiveRecord::RecordInvalid) { message.save! }
35 def test_utf8_roundtrip
37 assert_message_ok("c", i)
38 assert_message_ok(EURO, i)
42 def test_length_oversize
43 assert_raise(ActiveRecord::RecordInvalid) { make_message("c", 256).save! }
44 assert_raise(ActiveRecord::RecordInvalid) { make_message(EURO, 256).save! }
48 # See e.g http://en.wikipedia.org/wiki/UTF-8 for byte sequences
49 # FIXME: Invalid Unicode characters can still be encoded into "valid" utf-8 byte sequences - maybe check this too?
50 invalid_sequences = ["\xC0", # always invalid utf8
51 "\xC2\x4a", # 2-byte multibyte identifier, followed by plain ASCII
52 "\xC2\xC2", # 2-byte multibyte identifier, followed by another one
53 "\x4a\x82", # plain ASCII, followed by multibyte continuation
54 "\x82\x82", # multibyte continuations without multibyte identifier
55 "\xe1\x82\x4a"] # three-byte identifier, contination and (incorrectly) plain ASCII
56 invalid_sequences.each do |char|
58 # create a message and save to the database
59 msg = make_message(char, 1)
60 # if the save throws, thats fine and the test should pass, as we're
61 # only testing invalid sequences anyway.
64 # get the saved message back and check that it is identical - i.e:
65 # its OK to accept invalid UTF-8 as long as we return it unmodified.
66 db_msg = msg.class.find(msg.id)
67 assert_equal char, db_msg.title, "Database silently truncated message title"
68 rescue ArgumentError => ex
69 assert_equal ex.to_s, "invalid byte sequence in UTF-8"
74 def test_from_mail_plain
75 sender_user = create(:user)
76 recipient_user = create(:user)
78 from "from@example.com"
80 subject "Test message"
82 content_type "text/plain; charset=utf-8"
83 body "This is a test & a message"
85 message = Message.from_mail(mail, sender_user, recipient_user)
86 assert_equal sender_user, message.sender
87 assert_equal recipient_user, message.recipient
88 assert_equal mail.date, message.sent_on
89 assert_equal "Test message", message.title
90 assert_equal "This is a test & a message", message.body
91 assert_equal "text", message.body_format
94 def test_from_mail_html
95 sender_user = create(:user)
96 recipient_user = create(:user)
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, sender_user, recipient_user)
106 assert_equal sender_user, message.sender
107 assert_equal recipient_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
115 sender_user = create(:user)
116 recipient_user = create(:user)
118 from "from@example.com"
120 subject "Test message"
124 content_type "text/plain; charset=utf-8"
125 body "This is a test & a message in text format"
129 content_type "text/html; charset=utf-8"
130 body "<p>This is a <b>test</b> & a message in HTML format</p>"
133 message = Message.from_mail(mail, sender_user, recipient_user)
134 assert_equal sender_user, message.sender
135 assert_equal recipient_user, message.recipient
136 assert_equal mail.date, message.sent_on
137 assert_equal "Test message", message.title
138 assert_equal "This is a test & a message in text format", message.body
139 assert_equal "text", message.body_format
142 from "from@example.com"
144 subject "Test message"
148 content_type "text/html; charset=utf-8"
149 body "<p>This is a <b>test</b> & a message in HTML format</p>"
152 message = Message.from_mail(mail, sender_user, recipient_user)
153 assert_equal sender_user, message.sender
154 assert_equal recipient_user, message.recipient
155 assert_equal mail.date, message.sent_on
156 assert_equal "Test message", message.title
157 assert_match /^ *This is a test & a message in HTML format *$/, message.body
158 assert_equal "text", message.body_format
161 def test_from_mail_prefix
162 sender_user = create(:user)
163 recipient_user = create(:user)
165 from "from@example.com"
167 subject "[OpenStreetMap] Test message"
169 content_type "text/plain; charset=utf-8"
170 body "This is a test & a message"
172 message = Message.from_mail(mail, sender_user, recipient_user)
173 assert_equal sender_user, message.sender
174 assert_equal recipient_user, message.recipient
175 assert_equal mail.date, message.sent_on
176 assert_equal "Test message", message.title
177 assert_equal "This is a test & a message", message.body
178 assert_equal "text", message.body_format
183 def make_message(char, count)
184 message = build(:message, :unread)
185 message.title = char * count
189 def assert_message_ok(char, count)
190 message = make_message(char, count)
192 response = message.class.find(message.id) # stand by for some über-generalisation...
193 assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length * count} bytes) fails"