Skip to content

Commit 8c3cdd9

Browse files
committed
Tests for tags
1 parent b2f73a4 commit 8c3cdd9

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
# Suggesting a tag without a user account will not work.
8+
$t->post_ok( '/tags/suggest', form => {
9+
tag => 'foo',
10+
})->stash_has( { errors => [ 'Login required.' ]}, 'Prompt for login.');
11+
12+
# Suggesting a tag with a user account will work, the tag will be in the DB in PendingTag.
13+
$t->create_user->post_ok( '/tags/suggest', form => {
14+
tag => 'foo',
15+
})->code_block( sub {
16+
my ( $t ) = @_;
17+
$t->_ss( $t->app->db->resultset('PendingTag')->search( { name => 'foo'})->first);
18+
ok $t->_sg, "The PendingTag was created.";
19+
is $t->_sg->name, 'foo', "Pending tag named correctly.";
20+
is $t->_sg->is_adult, 0, 'Tags are not adult by default.';
21+
})->status_is( 302, "Redirect after tag add." )
22+
->post_ok( '/tags/suggest', form => {
23+
tag => 'foo',
24+
})->stash_has( { errors => [ 'There is already a pending tag with that name.']},
25+
'Duplicate tag results in error'
26+
)->status_is( 200, "Stay on page during an error." );
27+
28+
# Suggesting the same tag again will result in an error because the tag already exists.
29+
$t->post_ok( '/tags/suggest', form => {
30+
tag => '9foo',
31+
})->stash_has( { errors => [ 'Tag names must start with a letter, and may only contain letters and numbers.']},
32+
'Tags starting with numbers result in errors.'
33+
)->status_is( 200, "Stay on page during an error." );
34+
35+
# Suggesting a tag that's an adult tag will make one with is_adult = true
36+
$t->post_ok( '/tags/suggest', form => {
37+
tag => 'adult_tag',
38+
is_adult => 1,
39+
})->code_block( sub {
40+
my ( $t ) = @_;
41+
$t->_ss( $t->app->db->resultset('PendingTag')->search( { name => 'adult_tag'})->first);
42+
ok $t->_sg, "The PendingTag was created.";
43+
is $t->_sg->name, 'adult_tag', "Pending tag named correctly.";
44+
is $t->_sg->is_adult, 1, 'Tag is set as an adult tag.';
45+
})->status_is( 302, "Redirect after tag add." );
46+
47+
done_testing();

Web/t/01_endpoints/02_tags/02_vote.t

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
8+
# Create a tag, it should have no votes at first.
9+
$t->create_user->create_tag( 'first' )->code_block( sub {
10+
my $t = shift;
11+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first);
12+
ok $t->_sg, "Found tag";
13+
is $t->_sg->name, 'first', "Tag named correctly.";
14+
is $t->_sg->vote_score, 0, "Vote score = 0";
15+
});
16+
17+
# Upvote them!
18+
$t->post_ok( '/tags/vote', form => {
19+
tag => 'first'
20+
})->code_block( sub {
21+
my $t = shift;
22+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first);
23+
ok $t->_sg, "Found tag";
24+
is $t->_sg->name, 'first', "Tag named correctly.";
25+
is $t->_sg->vote_score, 1, "Vote score = 1";
26+
});
27+
28+
# Upvote them again to undo it!
29+
$t->post_ok( '/tags/vote', form => {
30+
tag => 'first'
31+
})->code_block( sub {
32+
my $t = shift;
33+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first);
34+
ok $t->_sg, "Found tag";
35+
is $t->_sg->name, 'first', "Tag named correctly.";
36+
is $t->_sg->vote_score, 0, "Vote score = 0";
37+
});
38+
39+
# Make three new users and upvote it, then check the vote count.
40+
$t = Test::Mojo::BlogDB->new('BlogDB::Web')
41+
->create_user
42+
->post_ok( '/tags/vote', form => { tag => 'first'});
43+
44+
$t = Test::Mojo::BlogDB->new('BlogDB::Web')
45+
->create_user
46+
->post_ok( '/tags/vote', form => { tag => 'first'});
47+
48+
$t = Test::Mojo::BlogDB->new('BlogDB::Web')
49+
->create_user
50+
->post_ok( '/tags/vote', form => { tag => 'first'})
51+
->code_block( sub {
52+
my $t = shift;
53+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first);
54+
ok $t->_sg, "Found tag";
55+
is $t->_sg->name, 'first', "Tag named correctly.";
56+
is $t->_sg->vote_score, 3, "Vote score = 3";
57+
});
58+
59+
# Voting on a tag without a user account will not work.
60+
$t = Test::Mojo::BlogDB->new('BlogDB::Web')
61+
->post_ok( '/tags/vote', form => { tag => 'first'})
62+
->stash_has( { errors => [ 'Login required.' ] }, 'Need user account to vote.' )
63+
->code_block( sub {
64+
my $t = shift;
65+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first);
66+
ok $t->_sg, "Found tag";
67+
is $t->_sg->name, 'first', "Tag named correctly.";
68+
is $t->_sg->vote_score, 3, "Same vote count after unauthed vote attempt.";
69+
});
70+
71+
72+
done_testing;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
# Create a user and a tag.
8+
$t->create_user->create_tag( 'first' )->code_block( sub {
9+
my $t = shift;
10+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first);
11+
ok $t->_sg, "Found tag";
12+
is $t->_sg->name, 'first', "Tag named correctly.";
13+
is $t->_sg->vote_score, 0, "Vote score = 0";
14+
})->create_tag( 'second' )->create_tag( 'third');
15+
16+
# The user cannot approve the tag.
17+
$t->post_ok( '/tags/approve', form => { tag => 'first'})
18+
->code_block( sub {
19+
my $t = shift;
20+
$t->_ss( $t->app->db->resultset('Tag')->search({name => 'first'})->first);
21+
is $t->_sg, undef, "The tag has not been approved.";
22+
})
23+
->stash_has( { errors => [ 'Not authorized.' ] }, 'Rejected error message.');
24+
25+
# A user who is not logged in cannot approve the tag.
26+
$t = Test::Mojo::BlogDB->new('BlogDB::Web')
27+
->post_ok( '/tags/approve', form => { tag => 'second'})
28+
->code_block( sub {
29+
my $t = shift;
30+
$t->_ss( $t->app->db->resultset('Tag')->search({name => 'second'})->first);
31+
is $t->_sg, undef, "The tag has not been approved.";
32+
});
33+
34+
# A user with the setting can_add_tags can approve the tag.
35+
$t->create_user( { can_manage_tags => 1 } )
36+
->post_ok( '/tags/approve', form => { tag => 'second'} )
37+
->code_block( sub {
38+
is( shift->stash->{person}->setting('can_manage_tags'), 1, "User has permission.");
39+
})
40+
->code_block( sub {
41+
my $t = shift;
42+
$t->_ss( $t->app->db->resultset('Tag')->search({name => 'second'})->first);
43+
ok $t->_sg, "The tag exists in the Tag list.";
44+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'second'})->first);
45+
is $t->_sg, undef, "The tag has been deleted from the PendingTag list.";
46+
});
47+
48+
done_testing;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
# Create a user and a tag.
8+
$t->create_user->create_tag( 'first' )->code_block( sub {
9+
my $t = shift;
10+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first);
11+
ok $t->_sg, "Found tag";
12+
is $t->_sg->name, 'first', "Tag named correctly.";
13+
is $t->_sg->vote_score, 0, "Vote score = 0";
14+
})->create_tag( 'second' )->create_tag( 'third');
15+
16+
# The user cannot delete the tag.
17+
$t->post_ok( '/tags/delete', form => { tag => 'first'})
18+
->code_block( sub {
19+
my $t = shift;
20+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first);
21+
ok $t->_sg, "The tag exists and has not been deleted.";
22+
})
23+
->stash_has( { errors => [ 'Not authorized.' ] }, 'Rejected error message.');
24+
25+
# A user who is not logged in cannot delete the tag.
26+
$t = Test::Mojo::BlogDB->new('BlogDB::Web')
27+
->post_ok( '/tags/delete', form => { tag => 'first'})
28+
->code_block( sub {
29+
my $t = shift;
30+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'first'})->first);
31+
ok $t->_sg, "The tag exists and has not been deleted.";
32+
});
33+
34+
# A user with the setting can_manage_tags can delete the tag.
35+
$t->create_user( { can_manage_tags => 1 } )
36+
->post_ok( '/tags/delete', form => { tag => 'second'} )
37+
->code_block( sub {
38+
is( shift->stash->{person}->setting('can_manage_tags'), 1, "User has permission.");
39+
})
40+
->code_block( sub {
41+
my $t = shift;
42+
$t->_ss( $t->app->db->resultset('PendingTag')->search({name => 'second'})->first);
43+
is $t->_sg, undef, "The tag has been deleted from the PendingTag list.";
44+
});
45+
46+
done_testing;

0 commit comments

Comments
 (0)