summaryrefslogtreecommitdiff
path: root/FS-Test/lib/FS/Test.pm
blob: bc97977797e7973ea6355baaa380b4571c8a8c13 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package FS::Test;

use 5.006;
use strict;
use warnings FATAL => 'all';

#use File::ShareDir 'dist_dir';
use WWW::Mechanize;
use File::chdir;
use URI;
use File::Slurp qw(write_file);
use Class::Accessor 'antlers';
use File::Spec;

our $VERSION = '0.02';

=head1 NAME

Freeside testing suite

=head1 CLASS METHODS

=over 4

=item share_dir

Returns the path to the shared data directory, which contains the reference
database image, the test plan, and probably other stuff.

=cut

sub share_dir {
#  dist_dir('FS-Test')
#  we no longer install this anywhere
  my @dirs = File::Spec->splitdir(File::Spec->rel2abs(__FILE__));
  splice @dirs, -3; # lib/FS/Test.pm
  File::Spec->catdir( @dirs, 'share' );
}

=item new OPTIONS

Creates a test session. OPTIONS must contain 'dir', a directory to save the 
output files into (this may eventually default to a temp directory). It can
optionally contain:

- fsurl: the root Freeside url [http://localhost/freeside]
- user: the Freeside test username [test]
- pass: the Freeside test password [test]

=cut

has dir   => ( is => 'rw' );
has fsurl => ( is => 'rw' );
has user  => ( is => 'rw' );
has pass  => ( is => 'rw' );
has mech  => ( is => 'rw' );

sub new {
  my $class = shift;
  my $self = {
    fsurl => 'http://localhost/freeside',
    user  => 'test',
    pass  => 'test',
    @_
  };
  bless $self;

  # strip trailing slash, if any; it causes problems
  $self->{fsurl} =~ s(/$)();

  die "FS::Test->new: 'dir' required" unless $self->dir;
  if ( ! -d $self->dir ) {
    mkdir $self->dir
      or die "can't create '".$self->dir."': $!";
  }
  if ( ! -w $self->dir ) {
    die "FS::Test->new: can't write to '". $self->dir . "'";
  }

  $self->mech( WWW::Mechanize->new( autocheck => 0 ) );

  #freeside v4_
  my $login = $self->fsurl . '/index.html';
  $self->mech->get($login)
    or die "FS::Test->new: couldn't fetch $login";
  $self->mech->submit_form(
    with_fields => {
      credential_0 => $self->user,
      credential_1 => $self->pass,
    },
  );

  return $self;
}

=back

=head1 METHODS

=over 4

=item fetch PATHS...

Takes one or more PATHS (Freeside URIs, relative to $self->fsurl, including
query parameters) and downloads them from the web server, into the output
directory. Currently this will write progress messages to standard output.
If you don't like that, it's open source, fix it.

=cut

sub fetch {
  my $self = shift;

  local $CWD = $self->dir;

  my $base_uri = URI->new($self->fsurl);
  my $basedirs = () = $base_uri->path_segments;

  foreach my $path (@_) {
    $path =~ s/^\s+//;
    $path =~ s/\s+$//;
    next if !$path;

    if ($path =~ /^#(.*)/) {
      print "$path\n";
      next;
    }

    my $uri = URI->new( $self->fsurl . '/' . $path);
    print $uri->path;
    my $response = $self->mech->get($uri);
    print " - " . $self->mech->status . "\n";
    next unless $response->is_success;

    local $CWD;
    my @dirs = $uri->path_segments;
    splice @dirs, 0, $basedirs;

    if ( length($uri->query) ) {
      # if there's a query string, use the (server-side) file name as the 
      # last directory, and the query string as the local file name; this 
      # allows multiple tests that differ only in the query string.
      push @dirs, $uri->query;
    }
    my $file = pop @dirs;
    # make the filename safe for inclusion in a makefile/shell script.
    # & and ; are both bad; using ":" is reversible and unambiguous (because
    # it can't appear in query params)
    $file =~ s/&/:/g;
    foreach my $dir (@dirs) {
      mkdir $dir unless -d $dir;
      push @CWD, $dir;
    }
    write_file($file, {binmode => ':utf8'}, $response->decoded_content);

    # Detect Mason errors and make noise about them; they're presumably
    # _never_ correct.  Mason errors have one convenient property: there's no
    # <title> element on the page.
    if ( $self->mech->ct eq 'text/html' and !$self->mech->title ) {
      print "***error***\n";
    }
  }
}

# what we don't do in here is diff the results.
# Test::HTML::Differences from CPAN would be one way to do that.

1; # End of FS::Test