distinguish between user, admin, and public traditions; add preliminary app test
[scpubgit/stemmaweb.git] / t / 01app.t
CommitLineData
b8a92065 1#!/usr/bin/env perl
2use strict;
3use warnings;
69799996 4use File::Temp;
b8a92065 5use Test::More;
69799996 6use Text::Tradition::Directory;
b8a92065 7
8use Catalyst::Test 'stemmaweb';
9
69799996 10use vars qw( $orig_db, $was_link );
11my $textids;
12my $dbfile = 'db/traditions.db';
13( $orig_db, $was_link, $textids ) = _make_testing_database();
14
15ok( request('/')->is_success, 'Got root HTML' );
16ok( request('/directory')->is_success, 'Got the text directory' );
17ok( request('/text/' . $textids->{'public'} . '/info')->is_success,
18 'Got information listing for public text' );
19is( request('/text/' . $textids->{'public'} . '/info')->code, 403,
20 'Denied information listing for public text' );
21
b8a92065 22
23done_testing();
69799996 24
25
26sub _make_testing_database {
27 my $fh = File::Temp->new();
28 my $file = $fh->filename;
29 $fh->unlink_on_destroy( 0 );
30 $fh->close;
31 my $dsn = 'dbi:SQLite:dbname=' . $file;
32 my $dir = Text::Tradition::Directory->new( 'dsn' => $dsn,
33 'extra_args' => { 'create' => 1 } );
34 my $scope = $dir->new_scope;
35
36 my $textids = {};
37 # Create a (public) tradition
38 my $pubtrad = Text::Tradition->new( input => 'Self', file => 't/data/john.xml' );
39 $textids->{'public'} = $dir->store( $pubtrad );
40
41 # Create a user
42 my $userobj = $dir->add_user( { username => 'swtest', password => 'swtestpass' } );
43 # Create a tradition for the user
44 my $privtrad = Text::Tradition->new( input => 'Tabular', sep_char => ','
45 file => 't/data/florilegium.csv', user => $userobj );
46 $privtrad->add_stemma( dotfile => 't/data/florilegium.dot' );
47 $textids->{'private'} = $dir->store( $privtrad );
48
49 ## Now replace the existing traditions database with the test one
50 my( $orig, $was_link );
51 if( -l $dbfile ) {
52 $was_link = 1;
53 $orig = readlink( $dbfile );
54 unlink( $dbfile ) or die "Could not replace database file $dbfile";
55 } else {
56 my $suffix = '.backup.' . time();
57 $orig = $dbfile.$suffix;
58 rename( $dbfile, $orig ) or die "Could not move database file $dbfile";
59 }
60 link( $file, $dbfile );
61 return( $orig, $was_link, $textids );
62}
63
64END {
65 # Restore the original database
66 unlink( readlink( $dbfile ) );
67 unlink( $dbfile );
68 if( $was_link ) {
69 link( $orig_db, $dbfile );
70 } else {
71 rename( $orig_db, $dbfile );
72 }
73}