AproJo

 view release on metacpan or  search on metacpan

t/basic.t  view on Meta::CPAN


use AproJo;
use AproJo::DB::Schema;
use AproJo::Command::setup;

use Mojo::JSON qw(decode_json encode_json);

use Test::More;
END { done_testing(); }

use Test::Mojo;

my $db = AproJo::DB::Schema->connect('dbi:SQLite:dbname=:memory:');

AproJo::Command::setup->inject_sample_data('admin', 'pass', 'Joe Admin', $db);

ok($db->resultset('User')->single({name => 'admin'}), 'DB user exists');

is($db->resultset('User')->single({name => 'admin'})->password(),'pass','DB user has password');

ok($db->resultset('Role')->single({name => 'admin'}), 'DB role exists');

my $admin = $db->resultset('User')->single({name => 'admin'});
my $admin_id = $admin->user_id();

my $role = $db->resultset('Role')->single({name => 'admin'});
my $role_id = $role->role_id();

my $userroles = $db->resultset('UserRole')->search({user_id => $admin_id,role_id => $admin_id});

ok($userroles, 'admin has UserRole');

my $adminroles = $admin->roles();

my $adminrolename = $admin->roles()->single({name => 'admin'})->name();

while (my $adminrole = $adminroles->next) {
  my $rolename = $db->resultset('Role')->single({role_id => $adminrole->role_id})->name();
}


my $t = Test::Mojo->new(AproJo->new(db => $db));
$t->ua->max_redirects(2);

subtest 'Static File' => sub {
  $t->get_ok('/robots.txt')->status_is(200);
};

subtest 'Serverinfo' => sub {
  $t->get_ok('/serverinfo')->status_is(200)->text_is(h1 => 'Serverinfo');
};

subtest 'DBInfo' => sub {
  $t->get_ok('/dbinfo')->status_is(200)->text_is(h1 => 'Database Information');
};

subtest 'Anonymous User' => sub {
  $t->get_ok('/')->status_is(200)->text_is(h2 => 'Testpage for AproJo')
    ->element_exists('a');

  $t->get_ok('/page/doesntexist')->status_is(404);
};

subtest 'Anonymous User front/' => sub {
  $t->get_ok('/front/index')->status_is(200)->text_is(h2 => 'Testpage for AproJo')
    ->element_exists('a');

  #$t->get_ok('/front/doesntexist')->status_is(404);
};

subtest 'Do Login' => sub {

  # fail username
  $t->post_ok('/login' =>
      form => {from => '/', username => 'wronguser', password => 'pass'})
    ->status_is(200);

  # fail password
  $t->post_ok('/login' =>
      form => {from => '/', username => 'admin', password => 'wrongpass'})
    ->status_is(200);

  # successfully login
  $t->post_ok('/login' =>
      form => {username => 'admin', password => 'pass'})
    ->status_is(200)
    ->text_like(span => qr/.*admin.*/)
    ->text_like( 'a[href*="logout"]' => qr/Logout/ );

};

subtest 'Logging Out' => sub {
  # This is essentially a repeat of the first test
  $t->get_ok('/logout')
    ->status_is(200)
    ->text_like( 'a[href*="login"]' => qr/Login/ );
};

=comment




subtest 'Edit Page' => sub {

  # page editor
  $t->get_ok('/edit/home')
    ->status_is(200)
    ->text_like( '#wmd-input' => qr/Welcome to AproJo!/ )
    ->element_exists( '#wmd-preview' );

  # save page
  my $text = 'I changed this text';
  my $data = encode_json({
    name  => 'home',
    title => 'New Home',
    html  => "<p>$text</p>",
    md    => $text,
  });
  $t->websocket_ok( '/store/page' )
    ->send_ok( $data )
    ->message_is( 'Changes saved' )
    ->finish_ok;

  # see that the changes are reflected
  $t->get_ok('/page/home')
    ->status_is(200)
    ->text_is( h1 => 'New Home' )



( run in 1.123 second using v1.01-cache-2.11-cpan-39bf76dae61 )