summaryrefslogtreecommitdiffstats
path: root/xt/webservice/bug_comments.t
blob: d66e445cfbfa23cb4c6e1f8447b2a0d959ed51f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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);
}