]> git.openstreetmap.org Git - rails.git/blob - test/unit/message_test.rb
More corrections to the german website localization
[rails.git] / test / unit / message_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class MessageTest < Test::Unit::TestCase
4   api_fixtures
5   fixtures :messages
6
7   EURO = "\xe2\x82\xac" #euro symbol
8
9   # This needs to be updated when new fixtures are added
10   # or removed.
11   def test_check_message_count
12     assert_equal 2, Message.count
13   end
14
15   def test_check_empty_message_fails
16     message = Message.new
17     assert !message.valid?
18     assert message.errors.invalid?(:title)
19     assert message.errors.invalid?(:body)
20     assert message.errors.invalid?(:sent_on)
21     assert true, message.message_read
22   end
23   
24   def test_validating_msgs
25     message = messages(:one)
26     assert message.valid?
27     massage = messages(:two)
28     assert message.valid?
29   end
30   
31   def test_invalid_send_recipient
32     message = messages(:one)
33     message.sender = nil
34     message.recipient = nil
35     assert !message.valid?
36
37     assert_raise(ActiveRecord::RecordNotFound) { User.find(0) }
38     message.from_user_id = 0
39     message.to_user_id = 0
40     assert_raise(ActiveRecord::RecordInvalid) {message.save!}
41   end
42
43   def test_utf8_roundtrip
44     (1..255).each do |i|
45       assert_message_ok('c', i)
46       assert_message_ok(EURO, i)
47     end
48   end
49
50   def test_length_oversize
51     assert_raise(ActiveRecord::RecordInvalid) { make_message('c', 256).save! }
52     assert_raise(ActiveRecord::RecordInvalid) { make_message(EURO, 256).save! }
53   end
54
55   def test_invalid_utf8
56     # See e.g http://en.wikipedia.org/wiki/UTF-8 for byte sequences
57     # FIXME - Invalid Unicode characters can still be encoded into "valid" utf-8 byte sequences - maybe check this too?
58     invalid_sequences = ["\xC0",         # always invalid utf8
59                          "\xC2\x4a",     # 2-byte multibyte identifier, followed by plain ASCII
60                          "\xC2\xC2",     # 2-byte multibyte identifier, followed by another one
61                          "\x4a\x82",     # plain ASCII, followed by multibyte continuation
62                          "\x82\x82",     # multibyte continuations without multibyte identifier
63                          "\xe1\x82\x4a", # three-byte identifier, contination and (incorrectly) plain ASCII
64                         ]
65     invalid_sequences.each do |char|
66       begin
67         # create a message and save to the database
68         msg = make_message(char, 1)
69         # if the save throws, thats fine and the test should pass, as we're
70         # only testing invalid sequences anyway.
71         msg.save! 
72
73         # get the saved message back and check that it is identical - i.e: 
74         # its OK to accept invalid UTF-8 as long as we return it unmodified.
75         db_msg = msg.class.find(msg.id)
76         assert_equal char, db_msg.title, "Database silently truncated message title"
77
78       rescue ActiveRecord::RecordInvalid
79         # because we only test invalid sequences it is OK to barf on them
80       end
81     end
82   end  
83
84   def make_message(char, count)
85     message = messages(:one)
86     message.title = char * count
87     return message
88   end
89
90   def assert_message_ok(char, count)
91     message = make_message(char, count)
92     assert message.save!
93     response = message.class.find(message.id) # stand by for some über-generalisation...
94     assert_equal char * count, response.title, "message with #{count} #{char} chars (i.e. #{char.length*count} bytes) fails"
95   end
96
97 end