new SGN::Test::WWW::Mechanize, an SGN-specific mech tester with support for schemas
[sgn.git] / t / lib / SGN / Test / WWW / Mechanize.pm
blob951e155e2a9f7695f2cb915a6d67f98f509aa555
1 =head1 NAME
3 SGN::Test::WWW::Mechanize - subclass of
4 L<Test::WWW::Mechanize::Catalyst> with some SGN-specific convenience
6 =head1 SYNOPSIS
8 my $mech = SGN::Test::WWW::Mechanize->new;
10 # look at some pages
11 $mech->get_ok( '/organism/sol100/view' );
12 $mech->content_contains('SOL100 Organisms');
13 $mech->content_contains('presents a summary');
14 $mech->content_contains('click on an organism name');
15 $mech->content_lacks('Add to Tree','not logged in, does not have a form for adding an organism');
17 # do some tests while logged in as a temporary user
18 $mech->while_logged_in( user_type => 'curator', sub {
20 $mech->get_ok( '/organism/sol100/view' );
21 $mech->content_contains( 'Authorized user', 'now says authorized user' );
22 $mech->content_contains( 'Add a SOL100 organism', 'now has an adding form' );
23 $mech->submit_form_ok({
24 form_name => 'sol100_add_form',
25 fields => { species => $test_organism->species },
26 }, 'submitted add organism form');
28 });
30 =head1 SEE ALSO
32 This class has the methods from all of these:
33 L<Test::WWW::Mechanize::Catalyst>, L<Test::WWW::Mechanize>, L<WWW::Mechanize>
36 =head1 METHODS
38 Plus the following:
40 =cut
42 package SGN::Test::WWW::Mechanize;
43 use Moose;
45 use SGN;
47 use CXGN::People::Person;
48 use CXGN::People::Login;
50 extends 'Test::WWW::Mechanize::Catalyst';
52 has '+catalyst_app' => ( default => 'SGN' );
54 has 'test_user' => (
55 is => 'rw',
56 isa => 'HashRef',
57 predicate => 'has_test_user',
58 clearer => 'clear_test_user',
61 sub create_test_user {
62 my $self = shift;
63 my %props = @_;
65 local $SIG{__DIE__} = \&Carp::confess;
66 my %u = qw(
67 first_name testfirstname
68 last_name testlastname
69 user_name testusername
70 password testpassword
73 $self->_delete_user( \%u );
75 # generate a new user for testing purposes
76 # (to be deleted right afterwards)
77 $self->catalyst_app->dbc->txn( ping => sub {
78 my $dbh = $_;
80 my $p = CXGN::People::Person->new( $dbh );
81 $p->set_first_name( $u{first_name} );
82 $p->set_last_name( $u{last_name} );
83 my $p_id = $p->store();
84 $u{ sp_person_id } = $p_id
85 or die "could not create person $u{first_name} $u{last_name}";
87 my $login = CXGN::People::Login->new( $dbh, $p_id );
88 $login->set_username( $u{user_name} );
89 $login->set_password( $u{password} );
90 $login->set_user_type( $props{user_type} || 'user' );
92 $login->store();
93 });
95 $self->test_user(\%u);
99 sub DEMOLISH {
100 shift->delete_test_user;
103 sub set_test_user_type {
104 my $self = shift;
106 CXGN::People::Login
107 ->new( $self->catalyst_app->dbc->dbh, $self->test_user->{sp_person_id} )
108 ->set_user_type(shift);
111 sub delete_test_user {
112 my $self = shift;
114 # delete our test user from the database if one has been created
115 if( $self->has_test_user ) {
116 my $u = $self->test_user;
117 $self->_delete_user( $u );
120 $self->clear_test_user;
123 sub _delete_user {
124 my ( $self, $u ) = @_;
126 $self->catalyst_app->dbc->txn( ping => sub {
127 my $dbh = $_;
128 if ( my $u_id = CXGN::People::Person->get_person_by_username( $dbh, $u->{user_name} ) ) {
129 CXGN::People::Person->new( $dbh, $u_id )->hard_delete;
134 =head2 while_logged_in
136 Execute the given code while logged in. Takes an optional
137 hash-style list of parameters to set on the temp user that is created.
139 Args: hash-style list of props for the temp user to create,
140 followed by a subroutine ref to execute while logged in
142 current supported user properties:
144 user_type 'curator', 'sequencer', etc. default 'user'
145 Ret: nothing meaningful
147 Example:
149 $mech->while_logged_in( user_type => 'curator', sub {
151 $mech->get_ok( '/organism/sol100/view' );
152 $mech->content_contains( 'Authorized user', 'now says authorized user' );
156 =cut
158 sub while_logged_in {
159 my $self = shift;
160 my $sub = pop;
161 my %props = @_;
162 $self->create_test_user( %props );
163 $self->log_in_ok;
164 $sub->();
165 $self->log_out;
168 sub log_in_ok {
169 my ($self) = @_;
171 $self->get_ok("/solpeople/top-level.pl");
172 $self->content_contains("Login");
174 my %form = (
175 form_name => 'login',
176 fields => {
177 username => $self->test_user->{user_name},
178 pd => $self->test_user->{password},
182 $self->submit_form_ok( \%form, "submitted login form" );
183 $self->content_lacks('Incorrect username', 'did not get "Incorrect username"')
184 or Test::More::diag $self->content;
185 $self->content_contains('View or update personal','got MySGN')
186 or Test::More::diag $self->content;
189 sub log_out {
190 my ($self) = @_;
191 $self->get_ok( "/solpeople/login.pl?logout=yes", 'logged out' );
195 __PACKAGE__->meta->make_immutable( inline_constructor => 0 );