distinguish between user, admin, and public traditions; add preliminary app test
[scpubgit/stemmaweb.git] / t / 01app.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use File::Temp;
5 use Test::More;
6 use Text::Tradition::Directory;
7
8 use Catalyst::Test 'stemmaweb';
9
10 use vars qw( $orig_db, $was_link );
11 my $textids;
12 my $dbfile = 'db/traditions.db';
13 ( $orig_db, $was_link, $textids ) = _make_testing_database();
14
15 ok( request('/')->is_success, 'Got root HTML' );
16 ok( request('/directory')->is_success, 'Got the text directory' );
17 ok( request('/text/' . $textids->{'public'} . '/info')->is_success,
18         'Got information listing for public text' );
19 is( request('/text/' . $textids->{'public'} . '/info')->code, 403,
20         'Denied information listing for public text' );
21
22
23 done_testing();
24
25
26 sub _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
64 END {
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 }