Skip to content

Commit a239a0c

Browse files
committed
Blog publish tests and fixes
1 parent fb22b06 commit a239a0c

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

Web/lib/BlogDB/Web.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ sub startup ($self) {
9595
$r->post ( '/blog/new' )->to( 'Blog#post_new_blog' )->name( 'do_new_blog' ); # Create a new blog.
9696
$r->get ( '/blog/new/:id' )->to( 'Blog#get_edit_new_blog' )->name( 'edit_new_blog' ); # Show edit a new blog page.
9797
$r->post ( '/blog/new/:id' )->to( 'Blog#post_edit_new_blog' )->name( 'do_edit_new_blog' ); # Update a new blog.
98-
$r->post ( '/blog/publish/:id' )->to( 'Blog#post_publish_new_blog')->name( 'do_publish_new_blog' ); # Publish (PendingBlog -> Blog.)
98+
$auth->post( '/blog/publish/:id' )->to( 'Blog#post_publish_new_blog')->name( 'do_publish_new_blog' ); # Publish (PendingBlog -> Blog.)
9999

100100
$r->get ( '/blog/v/:slug' )->to( 'Blog#get_view_blog' )->name( 'view_blog');
101101
$r->get ( '/blog/e/:slug' )->to( 'Blog#get_edit_blog' )->name( 'edit_blog');

Web/lib/BlogDB/Web/Controller/Blog.pm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,17 @@ sub post_edit_new_blog ($c) {
289289
sub post_publish_new_blog ($c) {
290290
my $pb = $c->db->resultset('PendingBlog')->find( $c->param('id') );
291291

292+
push @{$c->stash->{errors}}, 'No such blog id.'
293+
unless $pb;
294+
295+
push @{$c->stash->{errors}}, 'Not Authorized.'
296+
unless $c->stash->{person}->setting( 'can_manage_blogs' );
297+
298+
if ( @{$c->stash->{errors} || []} ) {
299+
$c->redirect_to( $c->url_for( 'homepage' ) );
300+
return 0;
301+
}
302+
292303
my $blog = $c->db->resultset('Blog')->create({
293304
title => $pb->title,
294305
url => $pb->url,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env perl
2+
use Mojo::Base '-signatures';
3+
use BlogDB::Web::Test;
4+
5+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
6+
7+
# Post a new blog as a logged in user.
8+
$t->create_user->post_ok( '/blog/new',
9+
form => {
10+
url => 'https://modfoss.com/',
11+
})->code_block( sub {
12+
my ( $t ) = @_;
13+
$t->_ss($t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'}));
14+
ok $t->_sg, "Created blog entry.";
15+
});
16+
17+
my $blog_id = $t->_sg->id;
18+
19+
# New Session, update the blog as a can_manage_blogs user..
20+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
21+
22+
$t->create_user({ can_manage_blogs => 1 })->post_ok( "/blog/new/$blog_id", form => {
23+
title => 'modFoss',
24+
url => 'https://modfoss.com/',
25+
rss_url => 'https://modfoss.com/feed',
26+
tagline => 'Articles on technical matters.',
27+
about => 'A technical blog.'
28+
})->code_block( sub {
29+
my ( $t ) = @_;
30+
$t->_ss($t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'}));
31+
ok $t->_sg, "Found blog entry";
32+
is $t->_sg->title, 'modFoss', 'Title updated.';
33+
is $t->_sg->url, 'https://modfoss.com/', 'URL updated.';
34+
is $t->_sg->rss_url , 'https://modfoss.com/feed', 'RSS URL updated.';
35+
is $t->_sg->tagline , 'Articles on technical matters.', 'Tagline updated.';
36+
is $t->_sg->about , 'A technical blog.', 'About updated.';
37+
})->stash_has( { authorization => [ 'setting:can_manage_blogs' ] } );
38+
39+
# Now we publish the blog, we're still in the user account with can_manage_blogs
40+
$t->post_ok( "/blog/publish/$blog_id", form => {})
41+
->code_block( sub {
42+
my ( $t ) = @_;
43+
$t->_ss($t->app->db->resultset('Blog')->find( { url => 'https://modfoss.com/'}));
44+
ok $t->_sg, "Found published blog";
45+
is $t->_sg->title, 'modFoss', 'Blog has correct title.';
46+
47+
48+
$t->_ss($t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'}));
49+
is $t->_sg, undef, "Blog has been deleted from PendingBlogs.";
50+
});
51+
52+
done_testing;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env perl
2+
# Test to make sure that a normal user (w/o can_manage_blogs) cannot approve a blog.
3+
use Mojo::Base '-signatures';
4+
use BlogDB::Web::Test;
5+
6+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
7+
8+
# Post a new blog as a logged in user.
9+
$t->create_user->post_ok( '/blog/new',
10+
form => {
11+
url => 'https://modfoss.com/',
12+
})->code_block( sub {
13+
my ( $t ) = @_;
14+
$t->_ss($t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'}));
15+
ok $t->_sg, "Created blog entry.";
16+
});
17+
18+
my $blog_id = $t->_sg->id;
19+
20+
# Update the blog as the same user.
21+
$t->post_ok( "/blog/new/$blog_id", form => {
22+
title => 'modFoss',
23+
url => 'https://modfoss.com/',
24+
rss_url => 'https://modfoss.com/feed',
25+
tagline => 'Articles on technical matters.',
26+
about => 'A technical blog.'
27+
})->code_block( sub {
28+
my ( $t ) = @_;
29+
$t->_ss($t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'}));
30+
ok $t->_sg, "Found blog entry";
31+
is $t->_sg->title, 'modFoss', 'Title updated.';
32+
})->stash_has( { authorization => [ 'submitter' ] } );
33+
34+
# Now we try to publish the blog, it should fail for no user account.
35+
$t->post_ok( "/blog/publish/$blog_id", form => {})
36+
->stash_has( { errors => [ 'Not Authorized.' ] });
37+
38+
39+
40+
done_testing;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env perl
2+
# Test to make sure that an anonymous user cannot approve a blog.
3+
use Mojo::Base '-signatures';
4+
use BlogDB::Web::Test;
5+
6+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
7+
8+
# Post a new blog as a logged in user.
9+
$t->create_user->post_ok( '/blog/new',
10+
form => {
11+
url => 'https://modfoss.com/',
12+
})->code_block( sub {
13+
my ( $t ) = @_;
14+
$t->_ss($t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'}));
15+
ok $t->_sg, "Created blog entry.";
16+
});
17+
18+
my $blog_id = $t->_sg->id;
19+
20+
# Update the blog as the same user.
21+
$t->post_ok( "/blog/new/$blog_id", form => {
22+
title => 'modFoss',
23+
url => 'https://modfoss.com/',
24+
rss_url => 'https://modfoss.com/feed',
25+
tagline => 'Articles on technical matters.',
26+
about => 'A technical blog.'
27+
})->code_block( sub {
28+
my ( $t ) = @_;
29+
$t->_ss($t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'}));
30+
ok $t->_sg, "Found blog entry";
31+
is $t->_sg->title, 'modFoss', 'Title updated.';
32+
})->stash_has( { authorization => [ 'submitter' ] } );
33+
34+
# Now we try to publish the blog, it should fail because no logged in user.
35+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
36+
$t->post_ok( "/blog/publish/$blog_id", form => {})
37+
->stash_has( { errors => [ 'Login required.']}, 'Thrown out for no login.');
38+
39+
done_testing;

0 commit comments

Comments
 (0)