1 # -*- coding: utf-8 -*-
2 require File.dirname(__FILE__) + '/../test_helper'
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 massage = 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"
82 rescue ActiveRecord::RecordInvalid
83 # because we only test invalid sequences it is OK to barf on them
88 def test_from_mail_plain
90 from "from@example.com"
92 subject "Test message"
94 content_type 'text/plain; charset=utf-8'
95 body "This is a test & a message"
97 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
98 assert_equal users(:normal_user), message.sender
99 assert_equal users(:public_user), message.recipient
100 assert_equal mail.date, message.sent_on
101 assert_equal "Test message", message.title
102 assert_equal "This is a test & a message", message.body
103 assert_equal "text", message.body_format
106 def test_from_mail_html
108 from "from@example.com"
110 subject "Test message"
112 content_type 'text/html; charset=utf-8'
113 body "<p>This is a <b>test</b> & a message</p>"
115 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
116 assert_equal users(:normal_user), message.sender
117 assert_equal users(:public_user), message.recipient
118 assert_equal mail.date, message.sent_on
119 assert_equal "Test message", message.title
120 assert_match /^ *This is a test & a message *$/, message.body
121 assert_equal "text", message.body_format
124 def test_from_mail_multipart
126 from "from@example.com"
128 subject "Test message"
132 content_type 'text/plain; charset=utf-8'
133 body "This is a test & a message in text format"
137 content_type 'text/html; charset=utf-8'
138 body "<p>This is a <b>test</b> & a message in HTML format</p>"
141 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
142 assert_equal users(:normal_user), message.sender
143 assert_equal users(:public_user), message.recipient
144 assert_equal mail.date, message.sent_on
145 assert_equal "Test message", message.title
146 assert_equal "This is a test & a message in text format", message.body
147 assert_equal "text", message.body_format
150 from "from@example.com"
152 subject "Test message"
156 content_type 'text/html; charset=utf-8'
157 body "<p>This is a <b>test</b> & a message in HTML format</p>"
160 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
161 assert_equal users(:normal_user), message.sender
162 assert_equal users(:public_user), message.recipient
163 assert_equal mail.date, message.sent_on
164 assert_equal "Test message", message.title
165 assert_match /^ *This is a test & a message in HTML format *$/, message.body
166 assert_equal "text", message.body_format
169 def test_from_mail_prefix
171 from "from@example.com"
173 subject "[OpenStreetMap] Test message"
175 content_type 'text/plain; charset=utf-8'
176 body "This is a test & a message"
178 message = Message.from_mail(mail, users(:normal_user), users(:public_user))
179 assert_equal users(:normal_user), message.sender
180 assert_equal users(:public_user), message.recipient
181 assert_equal mail.date, message.sent_on
182 assert_equal "Test message", message.title
183 assert_equal "This is a test & a message", message.body
184 assert_equal "text", message.body_format
189 def make_message(char, count)
190 message = messages(:unread_message)
191 message.title = char * count
195 def assert_message_ok(char, count)
196 message = make_message(char, count)
198 response = message.class.find(message.id) # stand by for some über-generalisation...
199 assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length*count} bytes) fails"