From 9b6ec1f545da1cc4088ddf9cc117747954e58e65 Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Fri, 26 Feb 2016 17:57:55 +0000 Subject: Bug 1069799 - move the QA repository into the main repository r=LpSolit --- xt/webservice/bug_comments.t | 178 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 xt/webservice/bug_comments.t (limited to 'xt/webservice/bug_comments.t') diff --git a/xt/webservice/bug_comments.t b/xt/webservice/bug_comments.t new file mode 100644 index 000000000..d66e445cf --- /dev/null +++ b/xt/webservice/bug_comments.t @@ -0,0 +1,178 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +########################################## +# Test for xmlrpc call to Bug.comments() # +########################################## + +use 5.10.1; +use strict; +use warnings; + +use FindBin qw($RealBin); +use lib "$RealBin/../lib"; + +use DateTime; +use QA::Util; +use QA::Tests qw(STANDARD_BUG_TESTS PRIVATE_BUG_USER); +use Test::More tests => 331; +my ($config, @clients) = get_rpc_clients(); + +# These gets populated when we call Bug.add_comment. +our $creation_time; +our %comments = ( + public_comment_public_bug => 0, + public_comment_private_bug => 0, + private_comment_public_bug => 0, + private_comment_private_bug => 0, +); + +sub test_comments { + my ($comments_returned, $call, $t, $rpc) = @_; + + my $comment = $comments_returned->[0]; + ok($comment->{bug_id}, "bug_id exists"); + # FIXME At some point we should test attachment_id here. + + if ($t->{args}->{comment_ids}) { + my $expected_id = $t->{args}->{comment_ids}->[0]; + is($comment->{id}, $expected_id, "comment id is correct"); + + my %reverse_map = reverse %comments; + my $expected_text = $reverse_map{$expected_id}; + is($comment->{text}, $expected_text, "comment has the correct text"); + + my $priv_login = $rpc->bz_config->{PRIVATE_BUG_USER . '_user_login'}; + is($comment->{creator}, $priv_login, "comment creator is correct"); + + my $creation_day; + if ($rpc->isa('QA::RPC::XMLRPC')) { + $creation_day = $creation_time->ymd(''); + } + else { + $creation_day = $creation_time->ymd; + } + like($comment->{time}, qr/^\Q${creation_day}\ET\d\d:\d\d:\d\d/, + "comment time has the right format"); + } + else { + foreach my $field (qw(id text creator time)) { + ok(defined $comment->{$field}, "$field is defined"); + } + } +} + +################ +# Bug ID Tests # +################ + +sub post_bug_success { + my ($call, $t) = @_; + my @bugs = values %{ $call->result->{bugs} }; + is(scalar @bugs, 1, "Got exactly one bug"); + my @comments = map { @{ $_->{comments} } } @bugs; + test_comments(\@comments, @_); +} + +foreach my $rpc (@clients) { + $rpc->bz_run_tests(tests => STANDARD_BUG_TESTS, method => 'Bug.comments', + post_success => \&post_bug_success); +} + +#################### +# Comment ID Tests # +#################### + +# First, create comments using add_comment. +my @add_comment_tests; +foreach my $key (keys %comments) { + $key =~ /^([a-z]+)_comment_(\w+)$/; + my $is_private = ($1 eq 'private' ? 1 : 0); + my $bug_alias = $2; + push(@add_comment_tests, { args => { id => $bug_alias, comment => $key, + private => $is_private }, + test => "Add comment: $key", + user => PRIVATE_BUG_USER }); +} + +# Set the comment id for each comment that we add, so we can test getting +# them back, later. +sub post_add { + my ($call, $t) = @_; + my $key = $t->{args}->{comment}; + $comments{$key} = $call->result->{id}; +} + +$creation_time = DateTime->now(); +# We only need to create these comments once, with one of the interfaces. +$clients[0]->bz_run_tests( + tests => \@add_comment_tests, method => 'Bug.add_comment', + post_success => \&post_add); + +# Now check access on each private and public comment + +my @comment_tests = ( + # Logged-out user + { args => { comment_ids => [$comments{'public_comment_public_bug'}] }, + test => 'Logged-out user can access public comment on public bug by id', + }, + { args => { comment_ids => [$comments{'private_comment_public_bug'}] }, + test => 'Logged-out user cannot access private comment on public bug', + error => 'is private', + }, + { args => { comment_ids => [$comments{'public_comment_private_bug'}] }, + test => 'Logged-out user cannot access comments by id on private bug', + error => 'You are not authorized to access', + }, + { args => { comment_ids => [$comments{'private_comment_private_bug'}] }, + test => 'Logged-out user cannot access private comment on private bug', + error => 'You are not authorized to access', + }, + + # Logged-in, unprivileged user. + { user => 'unprivileged', + args => { comment_ids => [$comments{'public_comment_public_bug'}] }, + test => 'Logged-in user can see a public comment on a public bug by id', + }, + { user => 'unprivileged', + args => { comment_ids => [$comments{'private_comment_public_bug'}] }, + test => 'Logged-in user cannot access private comment on public bug', + error => 'is private', + }, + { user => 'unprivileged', + args => { comment_ids => [$comments{'public_comment_private_bug'}] }, + test => 'Logged-in user cannot access comments by id on private bug', + error => "You are not authorized to access", + }, + { user => 'unprivileged', + args => { comment_ids => [$comments{'private_comment_private_bug'}] }, + test => 'Logged-in user cannot access private comment on private bug', + error => "You are not authorized to access", + }, + + # User who can see private bugs and private comments + { user => PRIVATE_BUG_USER, + args => { comment_ids => [$comments{'private_comment_public_bug'}] }, + test => PRIVATE_BUG_USER . ' can see private comment on public bug', + }, + { user => PRIVATE_BUG_USER, + args => { comment_ids => [$comments{'private_comment_private_bug'}] }, + test => PRIVATE_BUG_USER . ' can see private comment on private bug', + }, +); + +sub post_comments { + my ($call) = @_; + my @comments = values %{ $call->result->{comments} }; + is(scalar @comments, 1, "Got exactly one comment"); + test_comments(\@comments, @_); +} + +foreach my $rpc (@clients) { + $rpc->bz_run_tests(tests => \@comment_tests, method => 'Bug.comments', + post_success => \&post_comments); +} -- cgit v1.2.3-24-g4f1b