Skip to content

Commit fb35c3e

Browse files
committed
More tests and auth fixes
1 parent 86479a7 commit fb35c3e

6 files changed

Lines changed: 244 additions & 33 deletions

File tree

‎Web/lib/BlogDB/Web.pm‎

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,46 +81,28 @@ sub startup ($self) {
8181
$r->post( '/logout' )->to( 'Root#post_logout' )->name('do_logout' );
8282

8383
# /user/ routes
84-
$r->get ( '/user/:name' )->to( 'User#get_user' )->name( 'user' );
85-
$auth->post( '/user/:name/follow' )->to( 'User#post_follow' )->name( 'do_follow_user' );
86-
$auth->post( '/user/:name/unfollow' )->to( 'User#post_unfollow')->name( 'do_unfollow_user' );
87-
$auth->get ( '/user/settings' )->to( 'User#get_settings' )->name( 'user_settings' );
88-
$auth->post( '/user/settings/bio' )->to( 'User#post_bio' )->name( 'do_user_bio' );
89-
$auth->post( '/user/settings/about' )->to( 'User#post_about' )->name( 'do_user_about' );
90-
$auth->post( '/user/settings/password')->to( 'User#post_password')->name( 'do_user_password' );
91-
$auth->post( '/user/settings/email' )->to( 'User#post_email' )->name( 'do_user_email' );
84+
#$r->get ( '/user/:name' )->to( 'User#get_user' )->name( 'user' );
85+
#$auth->post( '/user/:name/follow' )->to( 'User#post_follow' )->name( 'do_follow_user' );
86+
#$auth->post( '/user/:name/unfollow' )->to( 'User#post_unfollow')->name( 'do_unfollow_user' );
87+
#$auth->get ( '/user/settings' )->to( 'User#get_settings' )->name( 'user_settings' );
88+
#$auth->post( '/user/settings/bio' )->to( 'User#post_bio' )->name( 'do_user_bio' );
89+
#$auth->post( '/user/settings/about' )->to( 'User#post_about' )->name( 'do_user_about' );
90+
#$auth->post( '/user/settings/password')->to( 'User#post_password')->name( 'do_user_password' );
91+
#$auth->post( '/user/settings/email' )->to( 'User#post_email' )->name( 'do_user_email' );
9292

9393
# /blog/ routes
9494
$r->get ( '/blog/new' )->to( 'Blog#get_new_blogs' )->name( 'new_blogs' ); # List new blogs.
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.
9898
$auth->post( '/blog/publish/:id' )->to( 'Blog#post_publish_new_blog')->name( 'do_publish_new_blog' ); # Publish (PendingBlog -> Blog.)
99-
100-
$r->get ( '/blog/v/:slug' )->to( 'Blog#get_view_blog' )->name( 'view_blog');
101-
$r->get ( '/blog/e/:slug' )->to( 'Blog#get_edit_blog' )->name( 'edit_blog');
102-
$r->post ( '/blog/e/:slug' )->to( 'Blog#post_edit_blog' )->name( 'do_edit_blog');
103-
104-
$r->post ( '/blog/follow' )->to( 'Blog#post_blog_follow' )->name( 'do_follow_blog' );
105-
$r->post ( '/blog/unfollow' )->to( 'Blog#post_blog_unfollow' )->name( 'do_unfollow_blog');
106-
107-
$r->post ( '/blog/comment' )->to( 'Blog#post_blog_comment' )->name( 'do_blog_comment');
99+
$r->get ( '/blog/v/:slug' )->to( 'Blog#get_view_blog' )->name( 'view_blog' ); # View specific blog
100+
$auth->get ( '/blog/e/:slug' )->to( 'Blog#get_edit_blog' )->name( 'edit_blog' ); # View edit page
101+
$auth->post( '/blog/e/:slug' )->to( 'Blog#post_edit_blog' )->name( 'do_edit_blog' ); # Post edits to blog
102+
$auth->post( '/blog/follow' )->to( 'Blog#post_blog_follow' )->name( 'do_follow_blog' ); # Follow a blog
103+
$auth->post( '/blog/unfollow' )->to( 'Blog#post_blog_unfollow' )->name( 'do_unfollow_blog' ); # Unfollow a blog
104+
$auth->post( '/blog/comment' )->to( 'Blog#post_blog_comment' )->name( 'do_blog_comment' ); # Comment on blog/reply.
108105

109-
# $r->post ( '/blog/unfollow' )->to( 'Blog#post_blog_unfollow' )->name( 'do_unfollow_blog');
110-
# $r->get ( '/view/:id/:name' )->to( 'Blog#get_blog' )->name( 'blog' ); # View A Specific Blog.
111-
#
112-
# $auth->get ( '/blog/new' )->to( 'Blog#get_new_blogs' )->name( 'new_blogs' ); # List pending blogs for approval.
113-
# $r->get ( '/blog/new/:id/:title' )->to( 'Blog#get_edit_blog' )->name( 'edit_new_blog' ); # Get the edit page for a new blog.
114-
# $r->post ( '/blog/new/:name' )->to( 'Blog#post_edit_blog' )->name( 'do_edit_new_blog' ); # Post an update with the edit page.
115-
#
116-
# $auth->post( '/blog/:name/follow' )->to( 'Blog#post_follow' )->name( 'do_follow_blog' );
117-
# $auth->post( '/blog/:name/unfollow' )->to( 'Blog#post_unfollow' )->name( 'do_unfollow_blog' );
118-
# $auth->get ( '/blog/:name/settings' )->to( 'Blog#get_settings' )->name( 'blog_settings' );
119-
# $auth->post( '/blog/:name' )->to( 'Blog#post_settings' )->name( 'do_blog_settings' );
120-
# $auth->post( '/blog/:name/publish' )->to( 'Blog#post_publish' )->name( 'do_publish' );
121-
# $auth->post( '/blog/:name/unpublish' )->to( 'Blog#post_unpublish')->name( 'do_unpublish' );
122-
123-
124106
# /tags/
125107
$r->get ( '/tags' )->to( 'Tags#get_tags' )->name( 'tags' );
126108
$auth->post( '/tags/suggest' )->to( 'Tags#post_suggest_tag')->name( 'do_suggest_tag' );

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ sub get_view_blog ($c) {
2626

2727
sub get_edit_blog ($c) {
2828
$c->set_template( 'blog/edit' );
29+
30+
push @{$c->stash->{errors}}, 'Not Authorized.'
31+
unless $c->stash->{person}->setting( 'can_manage_blogs' );
32+
33+
if ( @{$c->stash->{errors} || []} ) {
34+
$c->redirect_to( $c->url_for( 'homepage' ) );
35+
return 0;
36+
}
2937

3038
my $blog = $c->stash->{blog} = $c->db->resultset('Blog')->find(
3139
$c->_slug_to_id($c->param('slug'))
@@ -53,6 +61,13 @@ sub get_edit_blog ($c) {
5361
sub post_edit_blog ($c) {
5462
$c->set_template( 'blog/edit' );
5563

64+
push @{$c->stash->{errors}}, 'Not Authorized.'
65+
unless $c->stash->{person}->setting( 'can_manage_blogs' );
66+
67+
if ( @{$c->stash->{errors} || []} ) {
68+
$c->redirect_to( $c->url_for( 'homepage' ) );
69+
return 0;
70+
}
5671
my $blog = $c->stash->{blog} = $c->db->resultset('Blog')->find(
5772
$c->_slug_to_id($c->param('slug'))
5873
);
@@ -112,13 +127,14 @@ sub post_blog_comment ($c) {
112127
# pos = 1, neg = -1, otherwise 0
113128
my $vote = $rev_pos ? 1 : ( $rev_neg ? -1 : 0 );
114129

115-
$c->stash->{person}->create_related('messages', {
130+
my $obj = $c->stash->{person}->create_related('messages', {
116131
blog_id => $blog_id,
117132
content => $message,
118133
parent_id => $parent,
119134
vote => $vote,
120135
});
121136

137+
$c->stash->{created_comment_id} = $obj->id;
122138
$c->redirect_to( $c->url_for( 'view_blog', slug => $blog_id ) );
123139
}
124140

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env perl
2+
use Mojo::Base '-signatures';
3+
use BlogDB::Web::Test;
4+
5+
# Create and publish the modFoss blog.
6+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
7+
8+
my ($blog_id, $slug);
9+
$t->create_user({can_manage_blogs => 1})->post_ok( '/blog/new',
10+
form => { url => 'https://modfoss.com/',
11+
})->code_block( sub {
12+
$blog_id = $t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'})->id;
13+
})->post_ok( "/blog/new/$blog_id", form => {
14+
title => 'modFoss',
15+
url => 'https://modfoss.com/',
16+
rss_url => 'https://modfoss.com/feed',
17+
tagline => 'Articles on technical matters.',
18+
about => 'A technical blog.'
19+
})->post_ok( "/blog/publish/$blog_id", form => {
20+
21+
})->code_block( sub {
22+
$slug = $t->app->db->resultset('Blog')->find( { url => 'https://modfoss.com/'})->slug;
23+
});
24+
25+
# View the blog as an anonymous user.
26+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
27+
28+
$t->get_ok( "/blog/v/$slug" )->code_block( sub {
29+
my $t = shift;
30+
ok $t->stash->{blog};
31+
is $t->stash->{person}, undef, 'No person object for anon.';
32+
is $t->stash->{blog}->title, 'modFoss', 'Blog object found.';
33+
});
34+
35+
# View the blog as a logged in user.
36+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
37+
38+
$t->create_user->get_ok( "/blog/v/$slug" )->code_block( sub {
39+
my $t = shift;
40+
ok $t->stash->{blog};
41+
ok $t->stash->{person}, 'Person object for logged in user.';
42+
is $t->stash->{blog}->title, 'modFoss', 'Blog object found.';
43+
});
44+
45+
done_testing;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env perl
2+
use Mojo::Base '-signatures';
3+
use BlogDB::Web::Test;
4+
5+
# Create and publish the modFoss blog.
6+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
7+
8+
my ($blog_id, $slug);
9+
$t->create_user({can_manage_blogs => 1})->post_ok( '/blog/new',
10+
form => { url => 'https://modfoss.com/',
11+
})->code_block( sub {
12+
$blog_id = $t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'})->id;
13+
})->post_ok( "/blog/new/$blog_id", form => {
14+
title => 'modFoss',
15+
url => 'https://modfoss.com/',
16+
rss_url => 'https://modfoss.com/feed',
17+
tagline => 'Articles on technical matters.',
18+
about => 'A technical blog.'
19+
})->post_ok( "/blog/publish/$blog_id", form => {
20+
})->code_block( sub {
21+
$slug = $t->app->db->resultset('Blog')->find( { url => 'https://modfoss.com/'})->slug;
22+
});
23+
24+
# Trying to edit the blog as an anonymous user doesn't work.
25+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
26+
27+
$t->get_ok( "/blog/e/$slug" )->stash_has( {
28+
errors => [ 'Login required.' ]
29+
}, 'Cannot view edit blog page without login.' );
30+
31+
$t->post_ok( "/blog/e/$slug" )->stash_has( {
32+
errors => [ 'Login required.' ]
33+
}, 'Cannot view edit blog page without login.' );
34+
35+
# Trying to edit the blog as a logged in user doesn't work,
36+
# without the can_manage_blogs permission.
37+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
38+
39+
$t->create_user->get_ok( "/blog/e/$slug" )->stash_has( {
40+
errors => [ 'Not Authorized.' ]
41+
}, 'Cannot view edit blog page without can_manage_blogs.' );
42+
43+
$t->post_ok( "/blog/e/$slug" )->stash_has( {
44+
errors => [ 'Not Authorized.' ]
45+
}, 'Cannot post to edit blog page without can_manage_blogs.' );
46+
47+
# Editing the blog with can_manage_blogs does work.
48+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
49+
50+
$t->create_user({ can_manage_blogs => 1 })->get_ok( "/blog/e/$slug" )->code_block( sub {
51+
ok $t->stash->{blog}, "Have blog object.";
52+
is $t->stash->{form_title}, 'modFoss', 'Form stash works.';
53+
});
54+
55+
$t->post_ok( "/blog/e/$slug", form => {
56+
title => 'modFoss Blog',
57+
url => 'http://modfoss.com/',
58+
rss_url => 'http://modfoss.com/feed',
59+
tagline => 'Technical Matters under Articles',
60+
about => 'Blog Technical, A?',
61+
})->code_block( sub {
62+
$t->_ss($t->app->db->resultset('Blog')->find( { url => 'https://modfoss.com/'}));
63+
is $t->_sg, undef, 'Blog cannot be found by the old URL.';
64+
$t->_ss($t->app->db->resultset('Blog')->find( { url => 'http://modfoss.com/'}));
65+
ok $t->_sg, 'Blog is found by the new URL now.';
66+
is $t->_sg->title, 'modFoss Blog', 'The blog updated.';
67+
68+
});
69+
70+
done_testing;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env perl
2+
use Mojo::Base '-signatures';
3+
use BlogDB::Web::Test;
4+
5+
# Create and publish the modFoss blog.
6+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
7+
8+
my ($blog_id, $slug);
9+
$t->create_user({can_manage_blogs => 1})->post_ok( '/blog/new',
10+
form => { url => 'https://modfoss.com/',
11+
})->code_block( sub {
12+
$blog_id = $t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'})->id;
13+
})->post_ok( "/blog/new/$blog_id", form => {
14+
title => 'modFoss',
15+
url => 'https://modfoss.com/',
16+
rss_url => 'https://modfoss.com/feed',
17+
tagline => 'Articles on technical matters.',
18+
about => 'A technical blog.'
19+
})->post_ok( "/blog/publish/$blog_id", form => {
20+
})->code_block( sub {
21+
$slug = $t->app->db->resultset('Blog')->find( { url => 'https://modfoss.com/'})->slug;
22+
});
23+
24+
# User who isn't logged in cannot follow a blog.
25+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
26+
$t->post_ok( '/blog/follow', form => { blog_id => $blog_id })
27+
->stash_has( { errors => [ 'Login required.']}, 'Login required to follow blogs.');
28+
29+
# Regular user can follow a blog.
30+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
31+
$t->create_user->post_ok( '/blog/follow', form => { blog_id => $blog_id })
32+
->code_block( sub {
33+
my $t = shift;
34+
my $blogs = $t->stash->{person}->get_followed_blogs;
35+
is $blogs->[0]->id, $blog_id, "A logged in user can follow a blog.";
36+
});
37+
38+
done_testing;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env perl
2+
use Mojo::Base '-signatures';
3+
use BlogDB::Web::Test;
4+
5+
# Create and publish the modFoss blog.
6+
my $t = Test::Mojo::BlogDB->new('BlogDB::Web');
7+
8+
my ($blog_id, $slug);
9+
$t->create_user({can_manage_blogs => 1})->post_ok( '/blog/new',
10+
form => { url => 'https://modfoss.com/',
11+
})->code_block( sub {
12+
$blog_id = $t->app->db->resultset('PendingBlog')->find( { url => 'https://modfoss.com/'})->id;
13+
})->post_ok( "/blog/new/$blog_id", form => {
14+
title => 'modFoss',
15+
url => 'https://modfoss.com/',
16+
rss_url => 'https://modfoss.com/feed',
17+
tagline => 'Articles on technical matters.',
18+
about => 'A technical blog.'
19+
})->post_ok( "/blog/publish/$blog_id", form => {
20+
})->code_block( sub {
21+
$slug = $t->app->db->resultset('Blog')->find( { url => 'https://modfoss.com/'})->slug;
22+
});
23+
24+
# User who isn't logged in cannot comment on a blog..
25+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
26+
$t->post_ok( '/blog/comment', form => {
27+
blog_id => $blog_id,
28+
message => 'First Comment',
29+
rev_pos => 1,
30+
})->stash_has( { errors => [ 'Login required.']}, 'Login required to post comment.');
31+
32+
# User can post a comment.
33+
# They (or another user) can then reply to comments.
34+
# Create a comment, then reply to it, verify both comments show up.
35+
$t = Test::Mojo::BlogDB->new('BlogDB::Web');
36+
$t->create_user->post_ok( '/blog/comment', form => {
37+
blog_id => $blog_id,
38+
message => 'First Comment',
39+
rev_pos => 1,
40+
})->code_block( sub {
41+
my $t = shift;
42+
$t->_ss($t->app->db->resultset('Message')->find($t->stash->{created_comment_id}));
43+
ok $t->_sg, 'Got comment object.';
44+
is $t->_sg->content, 'First Comment', 'Comment object has correct message.';
45+
$t->_ss( $t->_sg->id );
46+
})->post_ok( '/blog/comment', form => {
47+
blog_id => $blog_id,
48+
message => 'Child Comment',
49+
rev_pos => 1,
50+
parent_id => $t->_sg,
51+
})->code_block( sub {
52+
$t->_ss($t->app->db->resultset('Message')->find($t->_sg));
53+
ok $t->_sg, 'Got comment object.';
54+
is $t->_sg->content, 'First Comment', 'Comment object has correct message.';
55+
$t->_ss( @{$t->_sg->get_children} );
56+
ok $t->_sg, 'Got comment object\'s child.';
57+
is $t->_sg->content, 'Child Comment', 'Comment object has correct message.';
58+
});
59+
60+
done_testing;

0 commit comments

Comments
 (0)