# Copyright (C) 2009–2015  Alex Schroeder <alex@gnu.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require './t/test.pl';
package OddMuse;
use Test::More tests => 18;
use utf8;

# Basic usage that broke when I last changed the cookie handling code.
test_page(get_page('username=Alex'), 'Status: 404');
test_page(get_page('action=browse id=Alex'), 'Alex');

# Username
test_page(get_page('action=browse id=HomePage username=Alex'), "\nSet-Cookie: Wiki=username%251eAlex");
test_page(get_page('action=browse id=HomePage username=01234567890123456789012345678901234567890123456789'),
	  "\nSet-Cookie: Wiki=username%251e01234567890123456789012345678901234567890123456789");
test_page(get_page('action=browse id=HomePage username=01234567890123456789012345678901234567890123456789X'),
	  'UserName must be 50 characters or less: not saved');
test_page(get_page('action=browse id=HomePage username=AlexSchroeder'),
	  "\nSet-Cookie: Wiki=username%251eAlexSchroeder");
test_page(get_page('action=browse id=HomePage username=Alex%20Schroeder'),
	  "\nSet-Cookie: Wiki=username%251eAlex%20Schroeder");
AppendStringToFile($ConfigFile, "\$FreeLinks = 0;\n");
AppendStringToFile($ConfigFile, "\$WikiLinks = 1;\n");
test_page(get_page('action=browse id=HomePage username=Alex%20Schroeder'),
	  'Invalid UserName Alex Schroeder: not saved');
test_page(get_page('action=browse id=HomePage username=AlexSchroeder'),
	  "\nSet-Cookie: Wiki=username%251eAlexSchroeder");
test_page(get_page('action=browse id=HomePage username=Alex'),
	  'Invalid UserName Alex: not saved');
# single words are ok if we switch off $WikiLinks as well!
AppendStringToFile($ConfigFile, "\$WikiLinks = 0;\n");
test_page(get_page('action=browse id=HomePage username=Alex'),
	  "\nSet-Cookie: Wiki=username%251eAlex");

SKIP: {

  eval { require LWP::UserAgent; };
  skip "LWP::UserAgent not installed", 7 if $@;

  eval { require HTTP::Cookies; };
  skip "HTTP::Cookies not installed", 7 if $@;

  my $wiki = 'http://127.0.0.1/cgi-bin/wiki.pl';
  my $ua = LWP::UserAgent->new;
  my $response = $ua->get("$wiki?action=version");
  skip("No wiki running at $wiki", 7)
    unless $response->is_success;

  $ua = LWP::UserAgent->new;
  my $cookie = HTTP::Cookies->new;
  $ua ->cookie_jar($cookie);

  # Set the cookie
  $response = $ua->get("$wiki?action=debug;pwd=foo");
  ok($response->is_success, 'request the page');
  $ua->cookie_jar->as_string =~ /Set-Cookie.*: ([^=]+)=pwd%251efoo/;
  my $cookie = $1;
  ok($cookie, 'pwd was set in the cookie');
  test_page_negative($response->content, 'pwd');

  # Change the cookie
  $response = $ua->get("$wiki?action=debug;pwd=test");
  test_page($ua->cookie_jar->as_string, qq{Set-Cookie.*: $cookie=pwd%251etest});

  # Delete the cookie
  $response = $ua->get("$wiki?action=debug;pwd=");
  test_page($ua->cookie_jar->as_string, qq{Set-Cookie.*: $cookie=""});

  # Encoding issues
  $response = $ua->get("$wiki?action=rc;username=Alex\%20Schr\%C3\%B6der");
  test_page($response->header('Set-Cookie'),
	    qq{^Wiki=username%251eAlex%20Schr%C3%B6der});
  test_page($ua->cookie_jar->as_string,
	    qq{Set-Cookie.*: $cookie=username%251eAlex%20Schr%C3%B6der});
};
