]> git.openstreetmap.org Git - rails.git/blob - test/models/message_test.rb
Avoid using format as a URL parameter name
[rails.git] / test / models / message_test.rb
1 # -*- coding: utf-8 -*-
2 require "test_helper"
3
4 class MessageTest < ActiveSupport::TestCase
5   EURO = "\xe2\x82\xac".freeze # euro symbol
6
7   def test_check_empty_message_fails
8     message = Message.new
9     assert !message.valid?
10     assert message.errors[:title].any?
11     assert message.errors[:body].any?
12     assert message.errors[:sent_on].any?
13     assert !message.message_read
14   end
15
16   def test_validating_msgs
17     message = create(:message, :unread)
18     assert message.valid?
19     message = create(:message, :read)
20     assert message.valid?
21   end
22
23   def test_invalid_send_recipient
24     message = create(:message, :unread)
25     message.sender = nil
26     message.recipient = nil
27     assert !message.valid?
28
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! }
33   end
34
35   def test_utf8_roundtrip
36     (1..255).each do |i|
37       assert_message_ok("c", i)
38       assert_message_ok(EURO, i)
39     end
40   end
41
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! }
45   end
46
47   def test_invalid_utf8
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|
57       begin
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.
62         msg.save!
63
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"
70       end
71     end
72   end
73
74   def test_from_mail_plain
75     sender_user = create(:user)
76     recipient_user = create(:user)
77     mail = Mail.new do
78       from "from@example.com"
79       to "to@example.com"
80       subject "Test message"
81       date Time.now
82       content_type "text/plain; charset=utf-8"
83       body "This is a test & a message"
84     end
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
92   end
93
94   def test_from_mail_html
95     sender_user = create(:user)
96     recipient_user = create(:user)
97     mail = Mail.new do
98       from "from@example.com"
99       to "to@example.com"
100       subject "Test message"
101       date Time.now
102       content_type "text/html; charset=utf-8"
103       body "<p>This is a <b>test</b> &amp; a message</p>"
104     end
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
112   end
113
114   def test_from_mail_multipart
115     sender_user = create(:user)
116     recipient_user = create(:user)
117     mail = Mail.new do
118       from "from@example.com"
119       to "to@example.com"
120       subject "Test message"
121       date Time.now
122
123       text_part do
124         content_type "text/plain; charset=utf-8"
125         body "This is a test & a message in text format"
126       end
127
128       html_part do
129         content_type "text/html; charset=utf-8"
130         body "<p>This is a <b>test</b> &amp; a message in HTML format</p>"
131       end
132     end
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
140
141     mail = Mail.new do
142       from "from@example.com"
143       to "to@example.com"
144       subject "Test message"
145       date Time.now
146
147       html_part do
148         content_type "text/html; charset=utf-8"
149         body "<p>This is a <b>test</b> &amp; a message in HTML format</p>"
150       end
151     end
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
159   end
160
161   def test_from_mail_prefix
162     sender_user = create(:user)
163     recipient_user = create(:user)
164     mail = Mail.new do
165       from "from@example.com"
166       to "to@example.com"
167       subject "[OpenStreetMap] Test message"
168       date Time.now
169       content_type "text/plain; charset=utf-8"
170       body "This is a test & a message"
171     end
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
179   end
180
181   private
182
183   def make_message(char, count)
184     message = build(:message, :unread)
185     message.title = char * count
186     message
187   end
188
189   def assert_message_ok(char, count)
190     message = make_message(char, count)
191     assert message.save!
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"
194   end
195 end