]> git.openstreetmap.org Git - rails.git/blob - test/models/message_test.rb
Merge branch 'master' into moderation
[rails.git] / test / models / message_test.rb
1 require "test_helper"
2
3 class MessageTest < ActiveSupport::TestCase
4   EURO = "\xe2\x82\xac".freeze # euro symbol
5
6   def test_check_empty_message_fails
7     message = Message.new
8     assert !message.valid?
9     assert message.errors[:title].any?
10     assert message.errors[:body].any?
11     assert message.errors[:sent_on].any?
12     assert !message.message_read
13   end
14
15   def test_validating_msgs
16     message = create(:message, :unread)
17     assert message.valid?
18     message = create(:message, :read)
19     assert message.valid?
20   end
21
22   def test_invalid_send_recipient
23     message = create(:message, :unread)
24     message.sender = nil
25     message.recipient = nil
26     assert !message.valid?
27
28     assert_raise(ActiveRecord::RecordNotFound) { User.find(0) }
29     message.from_user_id = 0
30     message.to_user_id = 0
31     assert_raise(ActiveRecord::RecordInvalid) { message.save! }
32   end
33
34   def test_utf8_roundtrip
35     (1..255).each do |i|
36       assert_message_ok("c", i)
37       assert_message_ok(EURO, i)
38     end
39   end
40
41   def test_length_oversize
42     assert_raise(ActiveRecord::RecordInvalid) { make_message("c", 256).save! }
43     assert_raise(ActiveRecord::RecordInvalid) { make_message(EURO, 256).save! }
44   end
45
46   def test_invalid_utf8
47     # See e.g http://en.wikipedia.org/wiki/UTF-8 for byte sequences
48     # FIXME: Invalid Unicode characters can still be encoded into "valid" utf-8 byte sequences - maybe check this too?
49     invalid_sequences = ["\xC0",         # always invalid utf8
50                          "\xC2\x4a",     # 2-byte multibyte identifier, followed by plain ASCII
51                          "\xC2\xC2",     # 2-byte multibyte identifier, followed by another one
52                          "\x4a\x82",     # plain ASCII, followed by multibyte continuation
53                          "\x82\x82",     # multibyte continuations without multibyte identifier
54                          "\xe1\x82\x4a"] # three-byte identifier, contination and (incorrectly) plain ASCII
55     invalid_sequences.each do |char|
56       begin
57         # create a message and save to the database
58         msg = make_message(char, 1)
59         # if the save throws, thats fine and the test should pass, as we're
60         # only testing invalid sequences anyway.
61         msg.save!
62
63         # get the saved message back and check that it is identical - i.e:
64         # its OK to accept invalid UTF-8 as long as we return it unmodified.
65         db_msg = msg.class.find(msg.id)
66         assert_equal char, db_msg.title, "Database silently truncated message title"
67       rescue ArgumentError => ex
68         assert_equal ex.to_s, "invalid byte sequence in UTF-8"
69       end
70     end
71   end
72
73   def test_from_mail_plain
74     sender_user = create(:user)
75     recipient_user = create(:user)
76     mail = Mail.new do
77       from "from@example.com"
78       to "to@example.com"
79       subject "Test message"
80       date Time.now
81       content_type "text/plain; charset=utf-8"
82       body "This is a test & a message"
83     end
84     message = Message.from_mail(mail, sender_user, recipient_user)
85     assert_equal sender_user, message.sender
86     assert_equal recipient_user, message.recipient
87     assert_equal mail.date, message.sent_on
88     assert_equal "Test message", message.title
89     assert_equal "This is a test & a message", message.body
90     assert_equal "text", message.body_format
91   end
92
93   def test_from_mail_html
94     sender_user = create(:user)
95     recipient_user = create(:user)
96     mail = Mail.new do
97       from "from@example.com"
98       to "to@example.com"
99       subject "Test message"
100       date Time.now
101       content_type "text/html; charset=utf-8"
102       body "<p>This is a <b>test</b> &amp; a message</p>"
103     end
104     message = Message.from_mail(mail, sender_user, recipient_user)
105     assert_equal sender_user, message.sender
106     assert_equal recipient_user, message.recipient
107     assert_equal mail.date, message.sent_on
108     assert_equal "Test message", message.title
109     assert_match /^ *This is a test & a message *$/, message.body
110     assert_equal "text", message.body_format
111   end
112
113   def test_from_mail_multipart
114     sender_user = create(:user)
115     recipient_user = create(:user)
116     mail = Mail.new do
117       from "from@example.com"
118       to "to@example.com"
119       subject "Test message"
120       date Time.now
121
122       text_part do
123         content_type "text/plain; charset=utf-8"
124         body "This is a test & a message in text format"
125       end
126
127       html_part do
128         content_type "text/html; charset=utf-8"
129         body "<p>This is a <b>test</b> &amp; a message in HTML format</p>"
130       end
131     end
132     message = Message.from_mail(mail, sender_user, recipient_user)
133     assert_equal sender_user, message.sender
134     assert_equal recipient_user, message.recipient
135     assert_equal mail.date, message.sent_on
136     assert_equal "Test message", message.title
137     assert_equal "This is a test & a message in text format", message.body
138     assert_equal "text", message.body_format
139
140     mail = Mail.new do
141       from "from@example.com"
142       to "to@example.com"
143       subject "Test message"
144       date Time.now
145
146       html_part do
147         content_type "text/html; charset=utf-8"
148         body "<p>This is a <b>test</b> &amp; a message in HTML format</p>"
149       end
150     end
151     message = Message.from_mail(mail, sender_user, recipient_user)
152     assert_equal sender_user, message.sender
153     assert_equal recipient_user, message.recipient
154     assert_equal mail.date, message.sent_on
155     assert_equal "Test message", message.title
156     assert_match /^ *This is a test & a message in HTML format *$/, message.body
157     assert_equal "text", message.body_format
158   end
159
160   def test_from_mail_prefix
161     sender_user = create(:user)
162     recipient_user = create(:user)
163     mail = Mail.new do
164       from "from@example.com"
165       to "to@example.com"
166       subject "[OpenStreetMap] Test message"
167       date Time.now
168       content_type "text/plain; charset=utf-8"
169       body "This is a test & a message"
170     end
171     message = Message.from_mail(mail, sender_user, recipient_user)
172     assert_equal sender_user, message.sender
173     assert_equal recipient_user, message.recipient
174     assert_equal mail.date, message.sent_on
175     assert_equal "Test message", message.title
176     assert_equal "This is a test & a message", message.body
177     assert_equal "text", message.body_format
178   end
179
180   private
181
182   def make_message(char, count)
183     message = build(:message, :unread)
184     message.title = char * count
185     message
186   end
187
188   def assert_message_ok(char, count)
189     message = make_message(char, count)
190     assert message.save!
191     response = message.class.find(message.id) # stand by for some über-generalisation...
192     assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length * count} bytes) fails"
193   end
194 end