Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Tests for tags
  • Loading branch information
symkat committed Nov 27, 2021
1 parent b2f73a4 commit 8c3cdd9
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Web/t/01_endpoints/02_tags/01_suggest.t
@@ -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();
72 changes: 72 additions & 0 deletions Web/t/01_endpoints/02_tags/02_vote.t
@@ -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;
48 changes: 48 additions & 0 deletions Web/t/01_endpoints/02_tags/03_approve.t
@@ -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;
46 changes: 46 additions & 0 deletions Web/t/01_endpoints/02_tags/04_delete.t
@@ -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;

0 comments on commit 8c3cdd9

Please sign in to comment.