diff --git a/Web/t/01_endpoints/04_dashboard/19_blog_history.t b/Web/t/01_endpoints/04_dashboard/19_blog_history.t index a0f3d40..7b51404 100644 --- a/Web/t/01_endpoints/04_dashboard/19_blog_history.t +++ b/Web/t/01_endpoints/04_dashboard/19_blog_history.t @@ -1,11 +1,44 @@ #!/usr/bin/env perl use MJB::Web::Test; +#== +# This test ensures that the blog history panel for the blog can be viewed. +# +# 1. Create user and login. +# 2. Make a new blog. +# 3. Go to the blog history panel +# 4. Confirm the stash has the history arrayref. +#== + my $t = Test::Mojo::MJB->new('MJB::Web'); -# Ensure that a user who hasn't logged in cannot access this page. -$t->get_ok( '/admin' ) - ->status_is( 302 ) - ->header_is( location => '/login' ); +my $blog_id = $t->create_user + ->post_ok( '/blog/domain', form => { + domain => 'blog.example.com', + calling_route => 'show_blog_domain_owned', + }) + ->get_ok( '/dashboard' ) + ->stash->{blogs}->[0]->id; + +$t->get_ok( "/dashboard/blog/$blog_id" ) + ->status_is( 200 ) + ->code_block( sub { + my $self = shift; + ok exists $self->stash->{blog}, "The blog loads!"; + is $self->stash->{blog}->domain->name, 'blog.example.com', "Correct domain name for id."; + }); + +$t->get_ok( "/dashboard/blog/$blog_id/history" ) + ->code_block(sub { + my $self = shift; + ok exists $self->stash->{history}, 'We have media files in the stash.'; + is ref $self->stash->{history}, 'ARRAY', 'We have media files in the stash as an arrayref.'; + }) + ->status_is( 200 ); + +#== +# Remove Jekyll blog repos that were created as a part of this test. +#== +$t->clear_tempdir; -done_testing(); +done_testing; diff --git a/Web/t/01_endpoints/04_dashboard/20_do_blog_history.t b/Web/t/01_endpoints/04_dashboard/20_do_blog_history.t new file mode 100644 index 0000000..0e1b2c6 --- /dev/null +++ b/Web/t/01_endpoints/04_dashboard/20_do_blog_history.t @@ -0,0 +1,72 @@ +#!/usr/bin/env perl +use MJB::Web::Test; + +#== +# This test ensures that the blog history panel for the blog can be used to +# restore the state of an old commit. +# +# 1. Create user and login. +# 2. Make a new blog. +# 3. Go to the blog history panel +# 4. Confirm the stash has the history arrayref and get the current commit id. +# 5. Upload a media file and confirm its on the disk +# 6. Use the history panel to restore the version before the media file +# 7. Confirm the media file has been purged +#== + +my $t = Test::Mojo::MJB->new('MJB::Web'); + +my $blog_id = $t->create_user + ->post_ok( '/blog/domain', form => { + domain => 'blog.example.com', + calling_route => 'show_blog_domain_owned', + }) + ->get_ok( '/dashboard' ) + ->stash->{blogs}->[0]->id; + +$t->get_ok( "/dashboard/blog/$blog_id" ) + ->status_is( 200 ) + ->code_block( sub { + my $self = shift; + ok exists $self->stash->{blog}, "The blog loads!"; + is $self->stash->{blog}->domain->name, 'blog.example.com', "Correct domain name for id."; + }); + + +my $commit = $t->get_ok( "/dashboard/blog/$blog_id/history" ) + ->code_block(sub { + my $self = shift; + ok exists $self->stash->{history}, 'We have media files in the stash.'; + is ref $self->stash->{history}, 'ARRAY', 'We have media files in the stash as an arrayref.'; + }) + ->status_is( 200 ) + ->stash->{history}->[0]->{commit}; + +# Upload the file +$t->post_ok( "/dashboard/blog/$blog_id/media", form => { + upload => { content => 'Hello World', filename => 'hello-world.png' }, + }) + ->status_is( 302 ) + ->header_is( location => "/dashboard/blog/$blog_id/media" ); + +# Confirm the uploaded file exists on disk. +ok -f $t->app->jekyll( 'blog.example.com' )->root . '/blog.example.com/assets/media/hello-world.png', + 'The media file was written to disk.'; + + +# Restore the old commit +$t->post_ok( "/dashboard/blog/$blog_id/history", form => { + commit_hash => $commit, + }) + ->status_is( 302 ) + ->header_is( location => "/dashboard/blog/$blog_id/history" ); + +# Confirm the uploaded file purged from disk. +ok ! -f $t->app->jekyll( 'blog.example.com' )->root . '/blog.example.com/assets/media/hello-world.png', + 'The media file was removed from disk.'; +#== +# Remove Jekyll blog repos that were created as a part of this test. +#== +$t->clear_tempdir; + +done_testing;