#!/usr/bin/perl -wT

#----------------------------------------------------------------------
# heading     : Configuration
# description : E-mail
# navigation  : 6000 6700
#
# copyright (C) 1999-2003 Mitel Networks Corporation
# 
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
# 
# Technical support for this program is available from Mitel Networks 
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------

use strict;
use esmith::TestUtils;
use esmith::FormMagick::Panel::emailsettings;
my $f = esmith::FormMagick::Panel::emailsettings->new();
$f->display();

=head1 TESTING


=begin testing

use esmith::FormMagick::Tester;
use esmith::TestUtils;
use esmith::ConfigDB;
my $panel = 'emailsettings';
my $panel_path = "/etc/e-smith/web/functions/".$panel;
my $ua = esmith::FormMagick::Tester->new();



is  (mode($panel_path), '4750',              "Check permissions on script");
ok  ($ua->get_panel($panel),            "ABOUT TO RUN L10N TESTS");
is  ($ua->{status}, 200,                "200 OK");
like($ua->{content}, qr/FORM_TITLE/,    "Saw untranslated form title");
ok  ($ua->set_language("en-us"),        "Set language to U.S. English");
ok  ($ua->get_panel($panel),            "Get panel");

is  ($ua->{status}, 200,                "200 OK");

like($ua->{content}, qr/E-mail settings/, "Saw translated form title");

# Testing changes

ok  ($ua->get_panel($panel),            "Testing panel retrieval");
can_ok($ua, "field");


ok ($ua->{form}->find_input('FreqWeekend'), 'Finding the FreqWeekend field');
ok ($ua->{form}->find_input('SecondaryMailEnvelope'), 'Finding the SecondaryMailEnvelope field');
ok ($ua->{form}->find_input('SpecifyHeader'), 'Finding the SpecifyHeader field');
ok ($ua->{form}->find_input('SecondaryMailPassword'), 'Finding the SecondaryMailPassword field');
ok ($ua->{form}->find_input('FreqOffice'), 'Finding the FreqOffice field');
ok ($ua->{form}->find_input('SecondaryMailAccount'), 'Finding the SecondaryMailAccount field');
ok ($ua->{form}->find_input('SecondaryMailServer'), 'Finding the SecondaryMailServer field');
ok ($ua->{form}->find_input('FreqOutside'), 'Finding the FreqOutside field');
ok ($ua->{form}->find_input('DelegateMailServer'), 'Finding the DelegateMailServer field');
ok ($ua->{form}->find_input('FetchmailMethod'), 'Finding the FetchmailMethod field');

ok  ($ua->get_panel($panel),            "Testing panel retrieval");
can_ok($ua, "field");
ok  ($ua->{form}->find_input('AdminEmail'),        "Finding form field");
ok  ($ua->{form}->find_input('EmailUnknownUser'),  "Finding form field");
ok  ($ua->{form}->find_input('SMTPSmartHost'),     "Finding form field");
ok  ($ua->{form}->find_input('POPAccess'),         "Finding form field");
ok  ($ua->{form}->find_input('WebMail'),           "Finding form field");

SKIP: {

    skip 16, "Unsafe!" unless destruction_ok();

    $ua->field('FreqWeekend' => 'never');
    $ua->field('SecondaryMailEnvelope' => 'X-Recipient');
    $ua->field('SpecifyHeader' => 'on');
    $ua->field('SecondaryMailPassword' => 'poppassword');
    $ua->field('FreqOffice' => 'every5min');
    $ua->field('SecondaryMailAccount' => 'popaccount');
    $ua->field('SecondaryMailServer' => 'ivy.fsck.com');
    $ua->field('FreqOutside' => 'every30min');
    $ua->field('DelegateMailServer' => '192.168.23.1');
    $ua->field('FetchmailMethod' => 'etrn');

    ok  ($ua->click("Save"),                "Click Save");

    is  ($ua->{status}, 200,                "200 OK");
    like($ua->{content}, qr/settings have been saved/, "Saw validation messages");

# Gotta open this later, so we don't cache stale data
    my $db = esmith::ConfigDB->open;

    is ($db->get('fetchmail')->prop('FreqWeekend'), 'never', 'Validated value for FreqWeekend');
    is ($db->get('fetchmail')->prop('SecondaryMailEnvelope'), 'X-Recipient', 'Validated value for SecondaryMailEnvelope');
    is ($db->get('fetchmail')->prop('SecondaryMailPassword'), 'poppassword', 'Validated value for SecondaryMailPassword');
    is ($db->get('fetchmail')->prop('FreqOffice'), 'every5min', 'Validated value for FreqOffice');
    is ($db->get('fetchmail')->prop('SecondaryMailAccount'), 'popaccount', 'Validated value for SecondaryMailAccount');
    is ($db->get('fetchmail')->prop('SecondaryMailServer'), 'ivy.fsck.com', 'Validated value for SecondaryMailServer');
    is ($db->get('fetchmail')->prop('FreqOutside'), 'every30min', 'Validated value for FreqOutside');
    is ($db->get('DelegateMailServer')->value(), '192.168.23.1', 'Validated value for DelegateMailServer');
    is ($db->get('fetchmail')->prop('Method'), 'etrn', 'Validated value for FetchmailMethod');

    ok  ($ua->get_panel($panel),            "Testing bogus IP Address");
    can_ok($ua, "field");

    $ua->field('DelegateMailServer' => '192.168.256.1');
    is  ($ua->{status}, 200,                "200 OK");
    unlike($ua->{content}, qr/settings have been saved/, "Didn't see validation message");
}

SKIP: {
    skip 8, "Unsafe!" unless destruction_ok();

    $ua->field("AdminEmail" => 'bob');
    $ua->field("EmailUnknownUser" => 'return');
    $ua->field("SMTPSmartHost" => 'foo.example.com');
    $ua->field("POPAccess" => 'public');
    $ua->field("WebMail" => 'enabled');
    ok  ($ua->click("Save"),                "Click Save");
    is  ($ua->{status}, 200,                "200 OK");
    like($ua->{content}, qr/settings have been saved/, "Settings saved");

    # Gotta open this later, so we don't cache stale data
    my $db = esmith::ConfigDB->open;

    is ($db->get('AdminEmail')->value(), 'bob', "Set the value for Email");

    is ($db->get('EmailUnknownUser')->value(), 'return', "Set the value for Email");
    is ($db->get('SMTPSmartHost')->value(), 'foo.example.com', "Set the value for Smarthost");
    is ($db->get('popd')->prop('access'), 'public', "Set the value for POPAccess");
    is ($db->get('horde')->prop('status'), 'enabled', "Set the value for web mail");
}

=end testing

=cut

__DATA__
<form title="FORM_TITLE" header="/etc/e-smith/web/common/head.tmpl"
footer="/etc/e-smith/web/common/foot.tmpl">
    <page name="First"  post-event="change_settings()" 
	pre-event="print_status_message()">
      <field type="select" 
	     id="FetchmailMethod" 
	     options="get_options()"
	     value="get_prop('fetchmail', 'Method')">
	<label>LABEL_MODE</label>
	<description>DESC_MODE</description>
      </field>
      
            <field type="text" id="AdminEmail" value="get_value('AdminEmail')">
	<label>LABEL_ADDRESS</label>
	<description>DESC_ADDRESS</description>
      </field>
      
      <field type="select" 
	     id="EmailUnknownUser" 
	     options="'return' => 'RETURN_TO_SENDER', 'admin' => 'FORWARD_TO_ADMIN'" 
	     value="get_value('EmailUnknownUser')">
	<label>LABEL_UNKNOWN</label>
	<description>DESC_UNKNOWN</description>
      </field>

      <field type="select" 
	     id="POPAccess" 
	     options="    'private' => 'PRIVATE',
	                  'public'  => 'PUBLIC'"
	     value="get_prop('popd', 'access')">
	<label>LABEL_ACCESS_CONTROL</label>
	<description>DESC_ACCESS_CONTROL</description>
      </field>

      <field type="select" 
	     id="WebMail" 
	     options="        'disabled'   => 'DISABLED',
	                      'enabledSSL' => 'ENABLED_SECURE_ONLY',
	                      'enabled'    => 'ENABLED'"
	     value="get_current_webmail_status()">
	<label>LABEL_WEBMAIL</label>
	<description>DESC_WEBMAIL</description>
      </field>
      
      <field type="text" id="DelegateMailServer" validation="blank_or_ip_number()" value="get_value('DelegateMailServer')">
	<label>LABEL_DELEGATE</label>
	<description>DESC_DELEGATE</description>
      </field>

      <field type="text" id="SMTPSmartHost" validation="validate_smarthost()"
	     value="get_value('SMTPSmartHost')">
	<label>LABEL_SMARTHOST</label>
	<description>DESC_SMARTHOST</description>
      </field>

      <field type="text" id="SecondaryMailServer" value="get_prop('fetchmail','SecondaryMailServer')">
	<label>LABEL_SECONDARY</label>
	<description>DESC_SECONDARY</description>
      </field>
      
      <field type="select" 
	     id="FreqOffice" 
	     options="fetchmail_frequencies()"
	     value="get_prop('fetchmail', 'FreqOffice')">
	<label>LABEL_FETCH_PERIOD</label>
	<description>DESC_FETCH_PERIOD</description>
      </field>
      <field type="select" 
	     id="FreqOutside" 
	     options="fetchmail_frequencies()"
	     value="get_prop('fetchmail', 'FreqOutside')">
	<label>LABEL_FETCH_PERIOD_NIGHTS</label>
      </field>
      <field type="select" 
	     id="FreqWeekend" 
	     options="fetchmail_frequencies()"
	     value="get_prop('fetchmail', 'FreqWeekend')">
	<label>LABEL_FETCH_PERIOD_WEEKENDS</label>
      </field>
      
      <field type="text" id="SecondaryMailAccount" value="get_prop('fetchmail','SecondaryMailAccount')">
	<label>LABEL_POP_ACCOUNT</label>
      </field>
      <field type="password" id="SecondaryMailPassword" value="get_prop('fetchmail','SecondaryMailPassword')">
	<label>LABEL_POP_PASS</label>
      </field>
      
      <field type="select" 
	     id="SpecifyHeader" 
	     options="'off'=> 'DEFAULT', 'on' => 'SPECIFY_BELOW'"
	     value="get_secondary_mail_use_envelope()">
	<label>LABEL_SORT_METHOD</label>
      </field>
      
      <field type="text" id="SecondaryMailEnvelope" validation=""
	     value="get_prop('fetchmail','SecondaryMailEnvelope')">
	<label>LABEL_SORT_HEADER</label>
      </field>
      <subroutine src="print_button('SAVE')" />
    </page>

    <page name="Done" pre-event="turn_off_buttons">
      <description>SUCCESS</description>
    </page>
  </form>
