aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib/attachment_to_html/adapters/rtf_spec.rb
blob: 2c53b5272983de6611897b6fb082189a0bc5ba33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')

describe AttachmentToHTML::Adapters::RTF do

    let(:attachment) { FactoryGirl.build(:rtf_attachment) }
    let(:adapter) { AttachmentToHTML::Adapters::RTF.new(attachment) }

    describe :tmpdir do

        it 'defaults to the rails tmp directory' do
           adapter.tmpdir.should == Rails.root.join('tmp')
        end

        it 'allows a tmpdir to be specified to store the converted document' do
            adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :tmpdir => '/tmp')
            adapter.tmpdir.should == '/tmp'
        end
  
    end

    describe :title do

        it 'uses the attachment filename for the title' do
            adapter.title.should == attachment.display_filename
        end
 
    end

    describe :body do

        it 'extracts the body from the document' do
            adapter.body.should include('thisisthebody')
        end

        it 'operates in the context of the supplied tmpdir' do
            adapter = AttachmentToHTML::Adapters::RTF.new(attachment, :tmpdir => '/tmp')
            Dir.should_receive(:chdir).with('/tmp').and_call_original
            adapter.body
        end

        it 'does not result in incorrect conversion when unrtf returns an invalid doctype' do
            # Doctype public identifier is unquoted
            # Valid doctype would be:
            # <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
            # See bug report http://savannah.gnu.org/bugs/?42015
            invalid = <<-DOC
            <!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>
            <html>
            <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8">
            <!-- Translation from RTF performed by UnRTF, version 0.21.5 -->
            <!--font table contains 0 fonts total-->
            <!--invalid font number 0-->
            </head>
            <body><font size="3"><font color="#000000">thisisthebody</font></font></body>
            </html>
            DOC
            AlaveteliExternalCommand.stub(:run).and_return(invalid)

            adapter.body.should_not include('//W3C//DTD HTML 4.01 Transitional//EN')
        end

        it 'converts empty files' do
            attachment = FactoryGirl.build(:rtf_attachment, :body => load_file_fixture('empty.rtf'))
            adapter = AttachmentToHTML::Adapters::RTF.new(attachment)
            adapter.body.should == ''
        end

        it 'doesnt fail if the external command returns nil' do
            AlaveteliExternalCommand.stub(:run).and_return(nil)
            adapter.body.should == ''
        end

    end


    describe :success? do

        it 'is successful if the body has content excluding the tags' do
            adapter.stub(:body).and_return('<p>some content</p>')
            adapter.success?.should be_true
        end

        it 'is successful if the body contains images' do
            adapter.stub(:body).and_return(%Q(<img src="logo.png" />))
            adapter.success?.should be_true
        end

        it 'is not successful if the body has no content other than tags' do
            adapter.stub(:body).and_return('<p></p>')
            adapter.success?.should be_false
        end

    end

end