RSS Git Download  Clone
Raw Blame History
#!/usr/bin/perl

use Test::WWW::Mechanize::CGIApp;

use strict;
use warnings;

use Test::More tests => 22; #use Test::More 'no_plan';

=begin # tests:
0) tests mysql_supports_innodb in BEGIN block
1) login as default user
2) try to re-login as default user
3) logout
4) try to login with invalid pwd
5) test forgotten password with invalid email address
6) re-login & try to change password - submit too short value
7) submit mis-matched new pwds
8) submit correctly formatted new pwd
9) check logged out & redirected to login page
=cut

BEGIN {
    require 't/test-lib.pl';

    # 1st test MySQL supports InnoDB tables:
    die 'no innodb support' unless mysql_supports_innodb();
}

my $test_username = 'admin_fname.admin_lname';
my $test_password = 'adm1n';

my $mech = get_mech();

# login/logout tests handled by test-lib.pl

do_login($test_username, $test_password);                     # print_and_exit();

# invoke login again:
$mech->get_ok('/login');                                      # print_and_exit();

my $user_map = get_user_map(); # warn Dumper $user_map;

# confirmed already logged in
$mech->content_contains(
    'You are already logged in',
    'confirmed already logged in',
);                                                            # print_and_exit();

# logout:
do_logout();

# test redirect_after_login:
$mech->get_ok('/resources');

# not logged in yet but check destination set correctly:
$mech->content_contains(
    qq!<input type="hidden" name="destination" value="http://localhost/resources" />!,
    'intended destination detected',
);

### select 1st form:
$mech->form_name('login_form');

# login:
$mech->submit_form(
    fields => {
        authen_username => $test_username,
        authen_password => $test_password,
    }
);

=begin # redirect_after_login() doesn't go to hidden destination page now (user messages)
# check we've reached detination:
$mech->content_contains(
    'HMDS Information Management System &raquo; General Resources',
    'intended destination reached',
);                                                             print_and_exit();
=cut

# logout:
do_logout();

### select 1st form:
$mech->form_name('login_form');

# test invalid login:
$mech->submit_form( # WWW::Mechanize function
    fields => {
        authen_username => $test_username,
        authen_password => 'invalid',
    }
);

# invalid login detected:
$mech->content_contains(
    'Login failed (login attempt 1)',
    'catches invalid login',
);                                                            # print_and_exit();

# test forgotten password with invalid email:
$mech->form_name('forgotten_pwd');

my $email = 'noone@here.com';

$mech->submit_form( 
    fields => {
        email_address => $email,
    }
);

$mech->content_contains(
    get_messages('login')->{email_not_found},
    'flash working - email not recognised',
);                                          # print_and_exit();

# login & change pwd:
$mech->submit_form( 
    fields => {
        authen_username => $test_username,
        authen_password => $test_password,
    }
);                                         # print_and_exit();

# change password:
$mech->get_ok('/user/change_password');      # print_and_exit();

# submit too short new pwd:
$mech->submit_form(
    fields => {
        old_password => 'adm1n',
        new_password => 'pwd',
        new_password_confirm => 'pwd',
    }
);                                               # print_and_exit();

has_dfv_errors();

$mech->content_contains(
    get_messages('form_validator')->{user}->{new_password}->{LENGTH},
    'new password: incorrect length detected ok',
);

$mech->content_contains(
    get_messages('form_validator')->{user}->{new_password_confirm}->{LENGTH},
    'confirm new password: incorrect length detected ok',
);

# submit mis-matched new pwds:
$mech->submit_form(
    fields => {
        old_password => 'adm1n',
        new_password => 'new_pwd',
        new_password_confirm => 'new-pwd',
    }
);

has_dfv_errors();

$mech->content_contains(
    get_messages('form_validator')->{user}->{pwds}->{DUPLICATION},
    'new passwords do not match: detected ok',
);

# submit correct data:
$mech->submit_form(
    fields => {
        old_password => 'adm1n',
        new_password => 'new_pwd',
        new_password_confirm => 'new_pwd',
    }
);                                         # print_and_exit();

$mech->content_contains(
    get_messages('user')->{pwd_change_success},
    'change password: success',
);

$mech->content_contains(
    'Please enter your login details',
    'redirect to login page: success',
);                                         # print_and_exit();

# no need - already logged out:
# do_logout();