]> git.donarmstrong.com Git - debbugs.git/blob - t/07_bugreport.t
test for Etag support in bugreport
[debbugs.git] / t / 07_bugreport.t
1 # -*- mode: cperl;-*-
2
3
4 use Test::More tests => 18;
5
6 use warnings;
7 use strict;
8
9 # Here, we're going to shoot messages through a set of things that can
10 # happen.
11
12 # First, we're going to send mesages to receive.
13 # To do so, we'll first send a message to submit,
14 # then send messages to the newly created bugnumber.
15
16 use IO::File;
17 use File::Temp qw(tempdir);
18 use Cwd qw(getcwd);
19 use Debbugs::MIME qw(create_mime_message);
20 use File::Basename qw(dirname basename);
21 use Test::WWW::Mechanize;
22 use HTTP::Status qw(RC_NOT_MODIFIED);
23 # The test functions are placed here to make things easier
24 use lib qw(t/lib);
25 use DebbugsTest qw(:all);
26
27 my %config;
28 eval {
29      %config = create_debbugs_configuration(debug => exists $ENV{DEBUG}?$ENV{DEBUG}:0);
30 };
31 if ($@) {
32      BAIL_OUT($@);
33 }
34
35 # Output some debugging information if there's an error
36 END{
37      if ($ENV{DEBUG}) {
38           foreach my $key (keys %config) {
39                diag("$key: $config{$key}\n");
40           }
41      }
42 }
43
44 # create a bug
45 send_message(to=>'submit@bugs.something',
46              headers => [To   => 'submit@bugs.something',
47                          From => 'foo@bugs.something',
48                          Subject => 'Submitting a bug',
49                         ],
50              body => <<EOF) or fail('Unable to send message');
51 Package: foo
52 Severity: normal
53
54 This is a silly bug
55 EOF
56
57
58 # test bugreport.cgi
59
60 # start up an HTTP::Server::Simple
61 my $bugreport_cgi_handler = sub {
62     # I do not understand why this is necessary.
63     $ENV{DEBBUGS_CONFIG_FILE} = "$config{config_dir}/debbugs_config";
64     my $fh;
65     open($fh,'-|',-e './cgi/version.cgi'? 'perl -I. -T ./cgi/bugreport.cgi' : 'perl -I. -T ../cgi/bugreport.cgi');
66     my $headers;
67     my $status = 200;
68     while (<$fh>) {
69         if (/^\s*$/ and $status) {
70             print "HTTP/1.1 $status OK\n";
71             print $headers;
72             $status = 0;
73             print $_;
74         } elsif ($status) {
75             $headers .= $_;
76             if (/^Status:\s*(\d+)/i) {
77                 $status = $1;
78             }
79         } else {
80             print $_;
81         }
82     }
83 };
84
85 my $port = 11342;
86
87 ok(DebbugsTest::HTTPServer::fork_and_create_webserver($bugreport_cgi_handler,$port),
88    'forked HTTP::Server::Simple successfully');
89
90 my $mech = Test::WWW::Mechanize->new();
91
92 $mech->get_ok('http://localhost:'.$port.'/?bug=1',
93               'Page received ok');
94 ok($mech->content() =~ qr/\<title\>\#1.+Submitting a bug/i,
95    'Title of bug is submitting a bug');
96 my $etag = $mech->response->header('Etag');
97 $mech->get('http://localhost:'.$port.'/?bug=1',
98            'If-None-Match' => $etag);
99 is($mech->res->code, RC_NOT_MODIFIED,
100    'Not modified when the same ETag sent for bug');
101
102 $mech->get_ok('http://localhost:'.$port.'/?bug=1;mbox=yes',
103               'Page received ok');
104 ok($mech->content() =~ qr/Subject: Submitting a bug/i,
105    'Subject of bug maibox is right');
106 ok($mech->content() =~ qr/^From /m,
107    'Starts with a From appropriately');
108
109 $mech->get_ok('http://localhost:'.$port.'/?bug=1;mboxmaint=yes',
110               'Page received ok');
111 ok($mech->content() !~ qr/[\x01\x02\x03\x05\x06\x07]/i,
112    'No unescaped states');
113 $etag = $mech->response->header('Etag');
114 $mech->get('http://localhost:'.$port.'/?bug=1;mboxmaint=yes',
115            'If-None-Match' => $etag);
116 is($mech->res->code, RC_NOT_MODIFIED,
117    'Not modified when the same ETag sent for bug maintmbox');
118
119 # now test the output of some control commands
120 my @control_commands =
121      (
122       reassign_foo => {command => 'reassign',
123                        value   => 'bar',
124                        regex => qr{<strong>bug reassigned from package &#39;<a href="pkgreport\.cgi\?package=foo">foo</a>&#39; to &#39;<a href="pkgreport\.cgi\?package=bar">bar</a>},
125                       },
126       forwarded_foo      => {command => 'forwarded',
127                              value   => 'https://foo.invalid/bugs?id=1',
128                              regex   => qr{<strong>Set bug forwarded-to-address to &#39;<a href="https://foo\.invalid/bugs\?id=1">https://foo\.invalid/bugs\?id=1</a>&#39;\.},
129                             },
130       forwarded_foo_2    => {command => 'forwarded',
131                              value   => 'https://foo.example/bugs?id=1',
132                              regex   => qr{<strong>Changed bug forwarded-to-address to &#39;<a href="https://foo\.example/bugs\?id=1">https://foo\.example/bugs\?id=1</a>&#39; from &#39;<a href="https://foo\.invalid/bugs\?id=1">https://foo\.invalid/bugs\?id=1</a>&#39;\.},
133                             },
134       clone        => {command => 'clone',
135                        value   => '-1',
136                        regex   => qr{<strong>Bug <a href="bugreport.cgi\?bug=1">1</a> cloned as bug <a href="bugreport.cgi\?bug=2">2</a>},
137                       },
138      );
139
140 while (my ($command,$control_command) = splice(@control_commands,0,2)) {
141   # just check to see that control doesn't explode
142   $control_command->{value} = " $control_command->{value}" if length $control_command->{value}
143     and $control_command->{value} !~ /^\s/;
144   send_message(to => 'control@bugs.something',
145                headers => [To   => 'control@bugs.something',
146                            From => 'foo@bugs.something',
147                            Subject => "Munging a bug with $command",
148                           ],
149                body => <<EOF) or fail 'message to control@bugs.something failed';
150 debug 10
151 $control_command->{command} 1$control_command->{value}
152 thanks
153 EOF
154                                   ;
155   # Now test that the output has changed accordingly
156   $mech->get_ok('http://localhost:'.$port.'/?bug=1',
157                 'Page received ok');
158   like($mech->content(), $control_command->{regex},
159        'Page matches regex');
160 }
161
162 # Other tests for bugs in the page should be added here eventually
163