Skip to content

Instantly share code, notes, and snippets.

@yko

yko/TestEnv.pm Secret

Created February 2, 2011 09:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yko/a00a1a9f1e57ae1e2a03 to your computer and use it in GitHub Desktop.
Save yko/a00a1a9f1e57ae1e2a03 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use lib 't/lib', '../lib';
use Test::More tests => 14;
use Test::Mojo;
use Mojo::Client;
use Mojo::IOLoop;
use TestEnv 'users'; # Init dummy usesrs
use_ok('App');
use_ok('App::Controller::Main');
my $app = new_ok('App');
my $c = new_ok('App::Controller::Main', [app => $app]);
can_ok $c, qw/login_ui chk_access login index/;
my $t = Test::Mojo->new(app => $app);
note "Wrong login";
$t->post_form_ok($t->app->url_for('login'), {l => 'dummy', p => 'wrong password'})
->status_is(302);
# Make an url to check
my $url = $t->app->url_for('login_ui')->base($t->tx->req->url);
$t->header_is('location' => $url->to_abs);
note "Right login";
$t->post_form_ok($t->app->url_for('login'), {l => 'dummy', p => 'password'})
->status_is(302);
# Make an url to check
my $url = $t->app->url_for('/')->base($t->tx->req->url);
$t->header_is('location' => $url->to_abs);
note "Index redirect (logged in)";
$t->get_ok(scalar $t->tx->res->headers->header('location'))->status_is(302);
$url = $t->app->url_for('list watchs')->base($t->tx->req->url);
$t->header_is('location' => $url->to_abs);
package TestEnv;
use FindBin;
use lib "t/lib";
use lib "lib";
require App::Model;
require Mojo::Client;
require Test::More;
use strict;
use warnings;
BEGIN {
$ENV{APP_TEST} = 1;
}
sub import {
my ($caller, @params) = @_;
foreach (@params) {
unless (UNIVERSAL::can($caller, $_)) {
die "$caller can not $_!";
}
no strict 'refs';
&{__PACKAGE__ . "::$_"}();
use strict 'refs';
}
}
sub connection {
# Set up db connection to test database here
# Create if not exists
while (<DATA>) {
# Create tables in test database
}
}
my $users;
sub users {
return if $users++;
connection();
# Create few dummy users
}
my $items;
sub items {
return if $items++;
connection();
# Create few dummy items
}
__DATA__
CREATE TABLE `users` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`user` varchar(20) CHARACTER SET latin1 NOT NULL,
`password` varchar(200) NOT NULL,
/* and so on */
PRIMARY KEY (`id`)
);
/* and so on.... */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment