test/unit/test_thread_view_mode.rb (2843B) - raw
1 require "test_helper"
2 require "dummy_buffer"
3 require "dummy_source"
4
5 require "sup"
6
7 module Redwood
8
9 class TestThreadViewMode < Minitest::Test
10 def setup
11 $config = {
12 :indent_spaces => 2,
13 :thread_by_subject => false,
14 :stem_language => "english",
15 :default_attachment_save_dir => "",
16 }
17 @path = Dir.mktmpdir
18 @out = File.open File::NULL, "w"
19 @inp = File.open File::NULL, "r"
20 @screen = Ncurses.newterm "dumb", @out, @inp
21 BufferManager.init
22 Logger.remove_sink $stderr
23 HookManager.init File.join(@path, "hooks")
24 UpdateManager.init
25 SourceManager.init
26 PollManager.init
27 LabelManager.init File.join(@path, "labels.txt")
28 ContactManager.init File.join(@path, "contacts.txt")
29 Index.init File.join(@path, "xapian")
30 Index.load
31 @source = DummySource.new("sup-test://test_thread_view_mode")
32 SourceManager.instance.add_source @source
33 end
34
35 def teardown
36 ObjectSpace.each_object(Class).select {|a| a < Singleton}.each do |klass|
37 klass.deinstantiate! unless klass == Logger
38 end
39 FileUtils.rm_r @path
40 Ncurses.delscreen @screen
41 @inp.close
42 @out.close
43 $config = nil
44 end
45
46 def populate_threadset
47 PollManager.poll_from @source
48 Index.save_index
49 ts = ThreadSet.new Index.instance
50 Index.instance.each_message { |m| ts.load_thread_for_message m }
51 ts
52 end
53
54 def make_mode thread
55 mode = ThreadViewMode.new thread
56 mode.buffer = DummyBuffer.new 100, 24
57 mode.send :regen_text
58 mode
59 end
60
61 def test_attachment_filenames_escaping
62 @source.messages = [ fixture_path("malicious-attachment-names.eml") ]
63 ts = populate_threadset
64 t = ts.threads.first
65 mode = make_mode t
66 mode.jump_to_message t.first
67
68 ## Test saving all attachments to disk.
69 destdir = File.join(@path, "saved")
70 Dir.mkdir destdir
71 stub(BufferManager.instance).ask(:filename, "Save all attachments to folder: ", "") { destdir }
72 stub(BufferManager.instance).flash("Wrote 1 attachment to #{destdir}.")
73 mode.save_all_to_disk
74 expected_path = File.join destdir, "sup_.travis.yml"
75 assert File.exist? expected_path
76 assert_equal 260, File.size(expected_path)
77
78 ## Scroll to the attachment and save it to a directory.
79 destdir = File.join(@path, "saved2")
80 Dir.mkdir destdir
81 stub(BufferManager.instance).ask(:filename, "Save attachment to file or directory: ", "#{ENV["HOME"]}/sup_.travis.yml") { destdir }
82 stub(BufferManager.instance).flash("Successfully wrote #{destdir}/sup_.travis.yml.")
83 assert_equal "x Attachment: sup/.travis.yml (text/x-yaml; 260B)", mode[10][0][1]
84 mode.send :set_cursor_pos, 10
85 mode.save_to_disk
86 expected_path = File.join destdir, "sup_.travis.yml"
87 assert File.exist? expected_path
88 assert_equal 260, File.size(expected_path)
89 end
90 end
91
92 end