| 1 | package Smolder::Mech; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Test::WWW::Mechanize'; | ||
| 5 | use Smolder::Conf; | ||
| 6 | use Smolder::TestData qw(base_url); | ||
| 7 | use JSON qw(from_json); | ||
| 8 | use Test::Builder; | ||
| 9 | use Test::More; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Smolder::Mech | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | L<Test::WWW::Mechanize> subclass with some Smolder specific | ||
| 18 | helper methods | ||
| 19 | |||
| 20 | my $mech = Smolder::Mech->new(); | ||
| 21 | $mech->login($user); | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | =head2 login | ||
| 26 | |||
| 27 | This routine will go to the login form, provide credentials | ||
| 28 | and login. It receives the following named args, all required: | ||
| 29 | |||
| 30 | =over | ||
| 31 | |||
| 32 | =item username | ||
| 33 | |||
| 34 | The text to use for the username field | ||
| 35 | |||
| 36 | =item password | ||
| 37 | |||
| 38 | The text to use for the password field. If none is given, it will use | ||
| 39 | 'testing'. | ||
| 40 | |||
| 41 | =back | ||
| 42 | |||
| 43 | $mech->login( | ||
| 44 | username => $username, | ||
| 45 | password => 's3cr3t', | ||
| 46 | ); | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub login { | ||
| 51 | my $self = shift; | ||
| 52 | my %args = @_; | ||
| 53 | my $url = base_url() . '/public_auth/login'; | ||
| 54 | $self->get($url); | ||
| 55 | $self->form_name('login'); | ||
| 56 | $self->set_fields( | ||
| 57 | username => $args{username}, | ||
| 58 | password => $args{password}, | ||
| 59 | ); | ||
| 60 | $self->submit(); | ||
| 61 | } | ||
| 62 | |||
| 63 | =head2 logout | ||
| 64 | |||
| 65 | Logout the current user. | ||
| 66 | |||
| 67 | $mech->logout(); | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub logout { | ||
| 72 | my $self = shift; | ||
| 73 | my $url = base_url() . '/public_auth/logout'; | ||
| 74 | $self->get($url); | ||
| 75 | } | ||
| 76 | |||
| 77 | =head1 get | ||
| 78 | |||
| 79 | Extends C<get()> from L<Test::WWW::Mechanize> to also disconnect | ||
| 80 | any open Database handles before making a request if we are using | ||
| 81 | SQLite to avoid locking the database | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub get { | ||
| 86 | my $self = shift; | ||
| 87 | Smolder::DB->disconnect(); | ||
| 88 | return $self->SUPER::get(@_); | ||
| 89 | } | ||
| 90 | |||
| 91 | =head1 submit | ||
| 92 | |||
| 93 | Extends C<submit()> from L<Test::WWW::Mechanize> to also disconnect | ||
| 94 | any open Database handles before making a request if we are using | ||
| 95 | SQLite to avoid locking the database | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | sub submit { | ||
| 100 | my $self = shift; | ||
| 101 | Smolder::DB->disconnect(); | ||
| 102 | return $self->SUPER::submit(@_); | ||
| 103 | } | ||
| 104 | |||
| 105 | =head1 request | ||
| 106 | |||
| 107 | Extends C<request()> from L<Test::WWW::Mechanize> to also disconnect | ||
| 108 | any open Database handles before making a request if we are using | ||
| 109 | SQLite to avoid locking the database | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub request { | ||
| 114 | my $self = shift; | ||
| 115 | Smolder::DB->disconnect(); | ||
| 116 | return $self->SUPER::request(@_); | ||
| 117 | } | ||
| 118 | |||
| 119 | =head1 contains_message | ||
| 120 | |||
| 121 | This method will look in the C<X-JSON> HTTP header | ||
| 122 | of the response, look through each message in the | ||
| 123 | C<messages> array and see if any of them match | ||
| 124 | the given message. | ||
| 125 | |||
| 126 | If given message is a scalar, the message must match | ||
| 127 | exactly, else if it's a regex, then it will be matched | ||
| 128 | against that. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub contains_message { | ||
| 133 | my ($self, $match) = @_; | ||
| 134 | my $resp = $self->response(); | ||
| 135 | my $json = from_json($self->response->header('X-JSON') || '{}'); | ||
| 136 | my $msgs = $json->{messages} || []; | ||
| 137 | my $diag = "contains message $match."; | ||
| 138 | |||
| 139 | # so test diagnostics are right | ||
| 140 | local $Test::Builder::Level = $Test::Builder::Level + 1; | ||
| 141 | |||
| 142 | foreach my $msg (@$msgs) { | ||
| 143 | if (ref $match eq 'Regexp') { | ||
| 144 | if ($msg->{msg} =~ $match) { | ||
| 145 | ok(1, $diag); | ||
| 146 | return 1; | ||
| 147 | } | ||
| 148 | } else { | ||
| 149 | if (index($msg->{msg}, $match) != -1) { | ||
| 150 | ok(1, $diag); | ||
| 151 | return 1; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | $diag = qq(Could not find message "$match". Existing messages are:\n) | ||
| 157 | . join("\n", map { $_->{msg} } @$msgs); | ||
| 158 | ok(0, $diag); | ||
| 159 | return 0; | ||
| 160 | } | ||
| 161 | |||
| 162 | 1; | ||
| 163 |