3 SGN::Test::WWW::Mechanize - subclass of
4 L<Test::WWW::Mechanize::Catalyst> with some SGN-specific convenience
8 my $mech = SGN::Test::WWW::Mechanize->new;
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');
32 This class has the methods from all of these:
33 L<Test::WWW::Mechanize::Catalyst>, L<Test::WWW::Mechanize>, L<WWW::Mechanize>
42 package SGN
::Test
::WWW
::Mechanize
;
47 use CXGN
::People
::Person
;
48 use CXGN
::People
::Login
;
50 extends
'Test::WWW::Mechanize::Catalyst';
52 has
'+catalyst_app' => ( default => 'SGN' );
57 predicate
=> 'has_test_user',
58 clearer
=> 'clear_test_user',
61 sub create_test_user
{
65 local $SIG{__DIE__
} = \
&Carp
::confess
;
67 first_name testfirstname
68 last_name testlastname
69 user_name testusername
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 {
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' );
95 $self->test_user(\
%u);
100 shift->delete_test_user;
103 sub set_test_user_type
{
107 ->new( $self->catalyst_app->dbc->dbh, $self->test_user->{sp_person_id
} )
108 ->set_user_type(shift);
111 sub delete_test_user
{
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;
124 my ( $self, $u ) = @_;
126 $self->catalyst_app->dbc->txn( ping
=> sub {
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
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' );
158 sub while_logged_in
{
162 $self->create_test_user( %props );
171 $self->get_ok("/solpeople/top-level.pl");
172 $self->content_contains("Login");
175 form_name
=> 'login',
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;
191 $self->get_ok( "/solpeople/login.pl?logout=yes", 'logged out' );
195 __PACKAGE__
->meta->make_immutable( inline_constructor
=> 0 );