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"
69 rescue ArgumentError => ex
70 assert_equal ex.to_s, "invalid byte sequence in UTF-8"
76 def test_from_mail_plain
77 sender_user = create(:user)
78 recipient_user = create(:user)
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, sender_user, recipient_user)
88 assert_equal sender_user, message.sender
89 assert_equal recipient_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
97 sender_user = create(:user)
98 recipient_user = create(:user)
100 from "from@example.com"
102 subject "Test message"
104 content_type "text/html; charset=utf-8"
105 body "<p>This is a <b>test</b> & a message</p>"
107 message = Message.from_mail(mail, sender_user, recipient_user)
108 assert_equal sender_user, message.sender
109 assert_equal recipient_user, message.recipient
110 assert_equal mail.date, message.sent_on
111 assert_equal "Test message", message.title
112 assert_match /^ *This is a test & a message *$/, message.body
113 assert_equal "text", message.body_format
116 def test_from_mail_multipart
117 sender_user = create(:user)
118 recipient_user = create(:user)
120 from "from@example.com"
122 subject "Test message"
126 content_type "text/plain; charset=utf-8"
127 body "This is a test & a message in text format"
131 content_type "text/html; charset=utf-8"
132 body "<p>This is a <b>test</b> & a message in HTML format</p>"
135 message = Message.from_mail(mail, sender_user, recipient_user)
136 assert_equal sender_user, message.sender
137 assert_equal recipient_user, message.recipient
138 assert_equal mail.date, message.sent_on
139 assert_equal "Test message", message.title
140 assert_equal "This is a test & a message in text format", message.body
141 assert_equal "text", message.body_format
144 from "from@example.com"
146 subject "Test message"
150 content_type "text/html; charset=utf-8"
151 body "<p>This is a <b>test</b> & a message in HTML format</p>"
154 message = Message.from_mail(mail, sender_user, recipient_user)
155 assert_equal sender_user, message.sender
156 assert_equal recipient_user, message.recipient
157 assert_equal mail.date, message.sent_on
158 assert_equal "Test message", message.title
159 assert_match /^ *This is a test & a message in HTML format *$/, message.body
160 assert_equal "text", message.body_format
163 def test_from_mail_prefix
164 sender_user = create(:user)
165 recipient_user = create(:user)
167 from "from@example.com"
169 subject "[OpenStreetMap] Test message"
171 content_type "text/plain; charset=utf-8"
172 body "This is a test & a message"
174 message = Message.from_mail(mail, sender_user, recipient_user)
175 assert_equal sender_user, message.sender
176 assert_equal recipient_user, message.recipient
177 assert_equal mail.date, message.sent_on
178 assert_equal "Test message", message.title
179 assert_equal "This is a test & a message", message.body
180 assert_equal "text", message.body_format
185 def make_message(char, count)
186 message = build(:message, :unread)
187 message.title = char * count
191 def assert_message_ok(char, count)
192 message = make_message(char, count)
194 response = message.class.find(message.id) # stand by for some über-generalisation...
195 assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length * count} bytes) fails"