diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index 52cec12..0128afb 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.pm @@ -7,6 +7,12 @@ sub startup ($self) { ? 'mjb.yml' : '/etc/mjb.yml' }); + + # Make an attribute that is true when running in test mode. + $self->helper( is_testmode => sub { + return 1 if exists $ENV{MJB_TESTMODE} && $ENV{MJB_TESTMODE} == 1; + return 0; + }); # Some quick configs.... $self->config->{register}->{require_invite} = 1; @@ -32,9 +38,10 @@ sub startup ($self) { # Load the MJB::Web::Plugin::Jekyll plugin for $c->jekyll $self->plugin('Jekyll'); + # Create $self->db as an MJB::DB connection. - $self->helper( db => sub { - return state $db = exists $ENV{MJB_TESTMODE} && $ENV{MJB_TESTMODE} == 1 + $self->helper( db => sub ($c) { + return state $db = $c->is_testmode ? MJB::DB->connect( $ENV{MJB_DSN}, '', '' ) : MJB::DB->connect($self->config->{database}->{mjb}); }); diff --git a/Web/lib/MJB/Web/Controller/Blog.pm b/Web/lib/MJB/Web/Controller/Blog.pm index d288bb4..845883c 100644 --- a/Web/lib/MJB/Web/Controller/Blog.pm +++ b/Web/lib/MJB/Web/Controller/Blog.pm @@ -61,8 +61,8 @@ sub domain_owned ( $c ) { # show_blog_domain_owned #== sub do_domain ( $c ) { - my $domain = $c->stash->{form}->{domain} = lc($c->param('domain')); - my $hosted_subdomain = $c->stash->{form}->{hosted_subdomain} = lc($c->param('hosted_subdomain')); + my $domain = $c->stash->{form}->{domain} = lc($c->param('domain') || ""); + my $hosted_subdomain = $c->stash->{form}->{hosted_subdomain} = lc($c->param('hosted_subdomain') || ""); my $hosted_domain_id = $c->stash->{form}->{hosted_domain_id} = $c->param('hosted_domain_id'); my $calling_route = $c->stash->{form}->{calling_route} = $c->param('calling_route'); @@ -142,14 +142,18 @@ sub do_domain ( $c ) { return $c->redirect_error( $calling_route ) if $c->stash->{errors}; - # Schedule a job to deploy the website + # If we don't have an SSL domain, that means we need to get a cert through an + # http challenge. if ( ! $ssl_domain ) { - my $ssl_job_id = $c->minion->enqueue( 'create_ssl_cert', [ $blog->id ], { - notes => { '_bid_' . $blog->id => 1 }, - priority => $blog->build_priority, - queue => 'certbot', - }); - $blog->create_related( 'jobs', { minion_job_id => $ssl_job_id } ); + # Do not run this job in test mode. + if ( ! $c->is_testmode ) { + my $ssl_job_id = $c->minion->enqueue( 'create_ssl_cert', [ $blog->id ], { + notes => { '_bid_' . $blog->id => 1 }, + priority => $blog->build_priority, + queue => 'certbot', + }); + $blog->create_related( 'jobs', { minion_job_id => $ssl_job_id } ); + } } $c->redirect_to( $c->url_for( 'show_blog_settings', { id => $blog->id } ) ); diff --git a/Web/lib/MJB/Web/Plugin/Jekyll.pm b/Web/lib/MJB/Web/Plugin/Jekyll.pm index 5bcdfaf..327ff3f 100644 --- a/Web/lib/MJB/Web/Plugin/Jekyll.pm +++ b/Web/lib/MJB/Web/Plugin/Jekyll.pm @@ -3,6 +3,11 @@ use Mojo::Base 'Mojolicious::Plugin', -signatures; use MJB::Web::Plugin::Jekyll::Blog; sub register ( $self, $app, $config ) { + my %opts = (); + if ( $app->is_testmode ) { + $opts{push_on_change} = 0; + $opts{root} = $ENV{MJB_TESTMODE_TEMPDIR}, + } $app->helper( jekyll => sub ($c, $domain) { return MJB::Web::Plugin::Jekyll::Blog->new( @@ -10,8 +15,10 @@ sub register ( $self, $app, $config ) { domain => $domain, init_from => $c->config->{jekyll_init_repo}, repo => $c->config->{store_repo_base} . "$domain.git", + %opts, ); }); } + 1; diff --git a/Web/lib/MJB/Web/Plugin/Jekyll/Blog.pm b/Web/lib/MJB/Web/Plugin/Jekyll/Blog.pm index 8c0a9f6..d8fca94 100644 --- a/Web/lib/MJB/Web/Plugin/Jekyll/Blog.pm +++ b/Web/lib/MJB/Web/Plugin/Jekyll/Blog.pm @@ -44,6 +44,14 @@ has repo => ( required => 1, ); +#== +# When set to true, push the repo after any action that changes it. +#== +has push_on_change => ( + is => 'ro', + default => sub { 1 }, +); + #== # The full local path (i.e. /var/repos/domain.com ) to the git repository that we will execute git commands in. #== @@ -117,7 +125,7 @@ sub init { # Push the repo to the store $self->system_command( [ qw( git push origin master ) ], { chdir => $self->repo_path, - }); + }) if $self->push_on_change; return $self; } @@ -206,7 +214,7 @@ sub remove_markdown_file { # Push the changes $self->system_command( [ qw( git push origin master ) ], { chdir => $self->repo_path, - }); + }) if $self->push_on_change; return $self; } @@ -349,7 +357,7 @@ sub write_config { # Push the repo to the store server $self->system_command( [ qw( git push origin master ) ], { chdir => $self->repo_path, - }); + }) if $self->push_on_change; return 1; } @@ -382,7 +390,7 @@ sub write_post { # Push the repo to the store server $self->system_command( [ qw( git push origin master ) ], { chdir => $self->repo_path, - }); + }) if $self->push_on_change; return 1; } @@ -412,7 +420,7 @@ sub commit_file { # Push the repo to the store server $self->system_command( [ qw( git push origin master ) ], { chdir => $self->repo_path, - }); + }) if $self->push_on_change; return 1; @@ -443,7 +451,7 @@ sub remove_file { # Push the repo to the store server $self->system_command( [ qw( git push origin master ) ], { chdir => $self->repo_path, - }); + }) if $self->push_on_change; return 1; @@ -471,7 +479,7 @@ sub restore_commit { # Push the repo to the store server $self->system_command( [ qw( git push origin master ) ], { chdir => $self->repo_path, - }); + }) if $self->push_on_change; return 1; @@ -578,11 +586,11 @@ sub system_command { die "Error: directory " . $settings->{chdir} . "doesn't exist." unless -d $settings->{chdir}; + $settings->{return_chdir} = getcwd(); + # Change to that directory, or die with error. chdir $settings->{chdir} or die "Failed to chdir to " . $settings->{chdir} . ": $!"; - - $settings->{return_chdir} = getcwd(); } # Mask values we don't want exposed in the logs. @@ -634,7 +642,7 @@ sub system_command { # Return to the directory we started in if we chdir'ed. if ( $settings->{return_chdir} ) { chdir $settings->{return_chdir} - or die "Failed to chdir to " . $settings->{chdir} . ": $!"; + or die "Failed to chdir to " . $settings->{return_chdir} . ": $!"; } if ( $ENV{MJB_DEBUG} ) { diff --git a/Web/lib/MJB/Web/Test.pm b/Web/lib/MJB/Web/Test.pm index 1260bcd..cf8c111 100644 --- a/Web/lib/MJB/Web/Test.pm +++ b/Web/lib/MJB/Web/Test.pm @@ -4,6 +4,7 @@ use Test::More; use Test::Deep; use Test::Mojo::MJB; use Test::Postgresql58; +use Mojo::File; push our @ISA, qw( Exporter ); push our @EXPORT, qw( $run_code ); @@ -25,8 +26,9 @@ our $pgsql = Test::Postgresql58->new() load_psql_file("../DB/etc/schema.sql"); -$ENV{MJB_TESTMODE} = 1; -$ENV{MJB_DSN} = $pgsql->dsn; +$ENV{MJB_TESTMODE} = 1; +$ENV{MJB_DSN} = $pgsql->dsn; +$ENV{MJB_TESTMODE_TEMPDIR} = Mojo::File::tempdir->to_string; sub load_psql_file { my ( $file ) = @_; diff --git a/Web/lib/Test/Mojo/MJB.pm b/Web/lib/Test/Mojo/MJB.pm index 939b4b1..8344177 100644 --- a/Web/lib/Test/Mojo/MJB.pm +++ b/Web/lib/Test/Mojo/MJB.pm @@ -34,6 +34,7 @@ use parent 'Test::Mojo'; use Data::Dumper; use Test::Deep; use Test::More; +use File::Path qw( remove_tree ); sub new { my $class = shift; @@ -150,4 +151,11 @@ sub _sg { return shift->{data_stack}; } +sub clear_tempdir { + my ( $t ) = @_; + + remove_tree( $ENV{MJB_TESTMODE_TEMPDIR} ) + if -d $ENV{MJB_TESTMODE_TEMPDIR}; +} + 1;