#!/usr/bin/perl -w
#
# Test CAS PA login page accessibility
#
# By Igor Gubenko (igubenko@Princeton.EDU), 05/21/2012
#

use lib qw(/usr/local/perl/lib/perl5);
#use lib qw(/usr/local/perl/modules/lib/perl5/site_perl/5.8.8 /usr/local/perl/modules/lib/perl5/5.8.8 /usr/local/perl/modules/lib64/perl5/site_perl/5.8.8 /usr/local/perl/modules/lib64/perl5/5.8.8 /var/local/groundwork/perl/lib/site_perl/5.8.8/);

use strict;
use WWW::Mechanize;

sub usage;

my $progname = $0;

usage unless @ARGV == 0;

my $mech = WWW::Mechanize->new (agent => 'Mozilla/5.0', stack_depth => '1', noproxy => '1', quiet => '1', timeout => '20') or die ("Unable to instantiate WWW::Mechanize!\n");
my $url_ini = "https://cas.princeton.edu/parent/index.jsp";

# Retrieve initial URL
$mech->get ($url_ini);

unless ($mech -> success() && $mech->title() =~ m#Parental Access Site#) {
	print "Failed to fetch \"$url_ini\". Answer: " . $mech->status() . "; " . $mech->title() . "\n";
	exit 2;
}

# Go to login link
$mech->follow_link (text => 'Click here to Log In');

unless ($mech -> success() && $mech->title() =~ m#Parental Access - Log In#) {
	print "Error going to the login page. Web server returned " . $mech->status() . "; " . $mech->title() . "\n";
	exit 2;
}

print "Successfully accessed \"$url_ini\", and navigated to the login page.\n";

exit 0;

sub usage {
	print "Usage: $progname\n";
	exit 2;
}
