File: formatter_file.t

package info (click to toggle)
libmojomojo-perl 1.01%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,272 kB
  • ctags: 879
  • sloc: perl: 14,055; sh: 145; xml: 120; ruby: 6; makefile: 2
file content (83 lines) | stat: -rwxr-xr-x 2,915 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl -w
use strict;
use warnings;
use Test::More;
#use MojoMojo::Formatter::Dir;
use MojoMojo::Formatter::File;

BEGIN { 
    plan skip_all => 'Requirements not installed for Dir Formatter'
        unless MojoMojo::Formatter::File->module_loaded;

    plan tests => 14;

    $ENV{CATALYST_CONFIG} = 't/var/mojomojo.yml';
    $ENV{CATALYST_DEBUG}  = 0;
    use_ok 'Catalyst::Test', 'MojoMojo';
};

use_ok('MojoMojo::Formatter::File');

my $c;
my $ret;
( undef, $c ) = ctx_request('/');
my $dir       = "t/var/files/";
my $content;

my %files = (
    'test.pod' => 'Pod',
    'test.txt' => 'Text',
);

 foreach my $file ( keys %files ){
     my $plugin   = MojoMojo::Formatter::File->plugin($file);
     SKIP:{
         my $package="MojoMojo::Formatter::File::".$plugin;
         skip("$file formatter not loaded",2) unless $package->module_loaded;
         is($plugin, $files{$file});
         ok(MojoMojo::Formatter::File->format($plugin, "$dir/$file"));
     }
 }


$c->config->{'Formatter::Dir'}{whitelisting} = [ $dir ];

# check checkfile with good file
ok(! MojoMojo::Formatter::File->checkfile("$dir/test.txt", $c));

# Check good text file
$content = "<p>{{file Text $dir/test.txt}}</p>";
$ret = MojoMojo::Formatter::File->format_content(\$content, $c);
like($$ret, qr{<div class="formatter_txt">\n<p>Text file</p> <p><a href="http://mojomojo.org/">http://mojomojo.org</a></p></div>}s, "Text file is formated");

# check checkfile with file not include in whitelist
$ret = MojoMojo::Formatter::File->checkfile("/etc/passwd", $c);
like($ret, qr{Directory '/etc/' must be include in whitelisting ! see Formatter::Dir:whitelisting in mojomojo.conf}s, "Checkfile with file not in whitelist");

# check checkfile with '..' in directory 
$ret = MojoMojo::Formatter::File->checkfile("$dir/../", $c);
like($ret, qr{You can't use '..' in the name of file}s, "Checkfile with '..' in directory");

# Errors in format_content return a ref SCALAR
# format content directory not include in whitelist
$content = '<p>{{file Text /tmp/test.txt}}</p>';
$ret = MojoMojo::Formatter::File->format_content(\$content, $c);
like($$ret, qr{Directory '/tmp/' must be include in whitelisting ! see Formatter::Dir:whitelisting in mojomojo.conf}s, "Format content not include in whitelist");

# file does not exist
$content = "<p>{{file Pod $dir/bla.txt}}</p>";
$ret = MojoMojo::Formatter::File->format_content(\$content, $c);
like($$ret, qr|Can not read 't/var/files//bla.txt' !|s, "Can not read file");

# format with no plugin
$content = '<p>{{file $dir/test.txt}}</p>';
$ret = MojoMojo::Formatter::File->format_content(\$content, $c);
like($$ret, qr/{{file \$dir\/test.txt}}/s, "No plugin is provided");


# Check bad plugin
$content = "<p>{{file Bla $dir/test.txt}}</p>";
$ret = MojoMojo::Formatter::File->format_content(\$content, $c);
like($$ret, qr/Can't find plugin for $dir\/test.txt !/s, "This is a bad plugin");