Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Tests for tags
- Loading branch information
Showing
4 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env perl | ||
use Mojo::Base '-signatures'; | ||
use BlogDB::Web::Test; | ||
|
||
my $t = Test::Mojo::BlogDB->new('BlogDB::Web'); | ||
|
||
# Suggesting a tag without a user account will not work. | ||
$t->post_ok( '/tags/suggest', form => { | ||
tag => 'foo', | ||
})->stash_has( { errors => [ 'Login required.' ]}, 'Prompt for login.'); | ||
|
||
# Suggesting a tag with a user account will work, the tag will be in the DB in PendingTag. | ||
$t->create_user->post_ok( '/tags/suggest', form => { | ||
tag => 'foo', | ||
})->code_block( sub { | ||
my ( $t ) = @_; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search( { name => 'foo'})->first); | ||
ok $t->_sg, "The PendingTag was created."; | ||
is $t->_sg->name, 'foo', "Pending tag named correctly."; | ||
is $t->_sg->is_adult, 0, 'Tags are not adult by default.'; | ||
})->status_is( 302, "Redirect after tag add." ) | ||
->post_ok( '/tags/suggest', form => { | ||
tag => 'foo', | ||
})->stash_has( { errors => [ 'There is already a pending tag with that name.']}, | ||
'Duplicate tag results in error' | ||
)->status_is( 200, "Stay on page during an error." ); | ||
|
||
# Suggesting the same tag again will result in an error because the tag already exists. | ||
$t->post_ok( '/tags/suggest', form => { | ||
tag => '9foo', | ||
})->stash_has( { errors => [ 'Tag names must start with a letter, and may only contain letters and numbers.']}, | ||
'Tags starting with numbers result in errors.' | ||
)->status_is( 200, "Stay on page during an error." ); | ||
|
||
# Suggesting a tag that's an adult tag will make one with is_adult = true | ||
$t->post_ok( '/tags/suggest', form => { | ||
tag => 'adult_tag', | ||
is_adult => 1, | ||
})->code_block( sub { | ||
my ( $t ) = @_; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search( { name => 'adult_tag'})->first); | ||
ok $t->_sg, "The PendingTag was created."; | ||
is $t->_sg->name, 'adult_tag', "Pending tag named correctly."; | ||
is $t->_sg->is_adult, 1, 'Tag is set as an adult tag.'; | ||
})->status_is( 302, "Redirect after tag add." ); | ||
|
||
done_testing(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env perl | ||
use Mojo::Base '-signatures'; | ||
use BlogDB::Web::Test; | ||
|
||
my $t = Test::Mojo::BlogDB->new('BlogDB::Web'); | ||
|
||
|
||
# Create a tag, it should have no votes at first. | ||
$t->create_user->create_tag( 'first' )->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first); | ||
ok $t->_sg, "Found tag"; | ||
is $t->_sg->name, 'first', "Tag named correctly."; | ||
is $t->_sg->vote_score, 0, "Vote score = 0"; | ||
}); | ||
|
||
# Upvote them! | ||
$t->post_ok( '/tags/vote', form => { | ||
tag => 'first' | ||
})->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first); | ||
ok $t->_sg, "Found tag"; | ||
is $t->_sg->name, 'first', "Tag named correctly."; | ||
is $t->_sg->vote_score, 1, "Vote score = 1"; | ||
}); | ||
|
||
# Upvote them again to undo it! | ||
$t->post_ok( '/tags/vote', form => { | ||
tag => 'first' | ||
})->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first); | ||
ok $t->_sg, "Found tag"; | ||
is $t->_sg->name, 'first', "Tag named correctly."; | ||
is $t->_sg->vote_score, 0, "Vote score = 0"; | ||
}); | ||
|
||
# Make three new users and upvote it, then check the vote count. | ||
$t = Test::Mojo::BlogDB->new('BlogDB::Web') | ||
->create_user | ||
->post_ok( '/tags/vote', form => { tag => 'first'}); | ||
|
||
$t = Test::Mojo::BlogDB->new('BlogDB::Web') | ||
->create_user | ||
->post_ok( '/tags/vote', form => { tag => 'first'}); | ||
|
||
$t = Test::Mojo::BlogDB->new('BlogDB::Web') | ||
->create_user | ||
->post_ok( '/tags/vote', form => { tag => 'first'}) | ||
->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first); | ||
ok $t->_sg, "Found tag"; | ||
is $t->_sg->name, 'first', "Tag named correctly."; | ||
is $t->_sg->vote_score, 3, "Vote score = 3"; | ||
}); | ||
|
||
# Voting on a tag without a user account will not work. | ||
$t = Test::Mojo::BlogDB->new('BlogDB::Web') | ||
->post_ok( '/tags/vote', form => { tag => 'first'}) | ||
->stash_has( { errors => [ 'Login required.' ] }, 'Need user account to vote.' ) | ||
->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first); | ||
ok $t->_sg, "Found tag"; | ||
is $t->_sg->name, 'first', "Tag named correctly."; | ||
is $t->_sg->vote_score, 3, "Same vote count after unauthed vote attempt."; | ||
}); | ||
|
||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env perl | ||
use Mojo::Base '-signatures'; | ||
use BlogDB::Web::Test; | ||
|
||
my $t = Test::Mojo::BlogDB->new('BlogDB::Web'); | ||
|
||
# Create a user and a tag. | ||
$t->create_user->create_tag( 'first' )->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first); | ||
ok $t->_sg, "Found tag"; | ||
is $t->_sg->name, 'first', "Tag named correctly."; | ||
is $t->_sg->vote_score, 0, "Vote score = 0"; | ||
})->create_tag( 'second' )->create_tag( 'third'); | ||
|
||
# The user cannot approve the tag. | ||
$t->post_ok( '/tags/approve', form => { tag => 'first'}) | ||
->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('Tag')->search({name => 'first'})->first); | ||
is $t->_sg, undef, "The tag has not been approved."; | ||
}) | ||
->stash_has( { errors => [ 'Not authorized.' ] }, 'Rejected error message.'); | ||
|
||
# A user who is not logged in cannot approve the tag. | ||
$t = Test::Mojo::BlogDB->new('BlogDB::Web') | ||
->post_ok( '/tags/approve', form => { tag => 'second'}) | ||
->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('Tag')->search({name => 'second'})->first); | ||
is $t->_sg, undef, "The tag has not been approved."; | ||
}); | ||
|
||
# A user with the setting can_add_tags can approve the tag. | ||
$t->create_user( { can_manage_tags => 1 } ) | ||
->post_ok( '/tags/approve', form => { tag => 'second'} ) | ||
->code_block( sub { | ||
is( shift->stash->{person}->setting('can_manage_tags'), 1, "User has permission."); | ||
}) | ||
->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('Tag')->search({name => 'second'})->first); | ||
ok $t->_sg, "The tag exists in the Tag list."; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'second'})->first); | ||
is $t->_sg, undef, "The tag has been deleted from the PendingTag list."; | ||
}); | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env perl | ||
use Mojo::Base '-signatures'; | ||
use BlogDB::Web::Test; | ||
|
||
my $t = Test::Mojo::BlogDB->new('BlogDB::Web'); | ||
|
||
# Create a user and a tag. | ||
$t->create_user->create_tag( 'first' )->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first); | ||
ok $t->_sg, "Found tag"; | ||
is $t->_sg->name, 'first', "Tag named correctly."; | ||
is $t->_sg->vote_score, 0, "Vote score = 0"; | ||
})->create_tag( 'second' )->create_tag( 'third'); | ||
|
||
# The user cannot delete the tag. | ||
$t->post_ok( '/tags/delete', form => { tag => 'first'}) | ||
->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first); | ||
ok $t->_sg, "The tag exists and has not been deleted."; | ||
}) | ||
->stash_has( { errors => [ 'Not authorized.' ] }, 'Rejected error message.'); | ||
|
||
# A user who is not logged in cannot delete the tag. | ||
$t = Test::Mojo::BlogDB->new('BlogDB::Web') | ||
->post_ok( '/tags/delete', form => { tag => 'first'}) | ||
->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first); | ||
ok $t->_sg, "The tag exists and has not been deleted."; | ||
}); | ||
|
||
# A user with the setting can_manage_tags can delete the tag. | ||
$t->create_user( { can_manage_tags => 1 } ) | ||
->post_ok( '/tags/delete', form => { tag => 'second'} ) | ||
->code_block( sub { | ||
is( shift->stash->{person}->setting('can_manage_tags'), 1, "User has permission."); | ||
}) | ||
->code_block( sub { | ||
my $t = shift; | ||
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'second'})->first); | ||
is $t->_sg, undef, "The tag has been deleted from the PendingTag list."; | ||
}); | ||
|
||
done_testing; |