]> git.openstreetmap.org Git - rails.git/blob - test/models/message_test.rb
Remove the api_fixtures helper
[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
69       rescue ArgumentError => ex
70         assert_equal ex.to_s, "invalid byte sequence in UTF-8"
71
72       end
73     end
74   end
75
76   def test_from_mail_plain
77     sender_user = create(:user)
78     recipient_user = create(:user)
79     mail = Mail.new do
80       from "from@example.com"
81       to "to@example.com"
82       subject "Test message"
83       date Time.now
84       content_type "text/plain; charset=utf-8"
85       body "This is a test & a message"
86     end
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
94   end
95
96   def test_from_mail_html
97     sender_user = create(:user)
98     recipient_user = create(:user)
99     mail = Mail.new do
100       from "from@example.com"
101       to "to@example.com"
102       subject "Test message"
103       date Time.now
104       content_type "text/html; charset=utf-8"
105       body "<p>This is a <b>test</b> &amp; a message</p>"
106     end
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
114   end
115
116   def test_from_mail_multipart
117     sender_user = create(:user)
118     recipient_user = create(:user)
119     mail = Mail.new do
120       from "from@example.com"
121       to "to@example.com"
122       subject "Test message"
123       date Time.now
124
125       text_part do
126         content_type "text/plain; charset=utf-8"
127         body "This is a test & a message in text format"
128       end
129
130       html_part do
131         content_type "text/html; charset=utf-8"
132         body "<p>This is a <b>test</b> &amp; a message in HTML format</p>"
133       end
134     end
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
142
143     mail = Mail.new do
144       from "from@example.com"
145       to "to@example.com"
146       subject "Test message"
147       date Time.now
148
149       html_part do
150         content_type "text/html; charset=utf-8"
151         body "<p>This is a <b>test</b> &amp; a message in HTML format</p>"
152       end
153     end
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
161   end
162
163   def test_from_mail_prefix
164     sender_user = create(:user)
165     recipient_user = create(:user)
166     mail = Mail.new do
167       from "from@example.com"
168       to "to@example.com"
169       subject "[OpenStreetMap] Test message"
170       date Time.now
171       content_type "text/plain; charset=utf-8"
172       body "This is a test & a message"
173     end
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
181   end
182
183   private
184
185   def make_message(char, count)
186     message = build(:message, :unread)
187     message.title = char * count
188     message
189   end
190
191   def assert_message_ok(char, count)
192     message = make_message(char, count)
193     assert message.save!
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"
196   end
197 end