github.com/abayer/test-infra@v0.0.5/prow/plugins/trigger/pr_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package trigger
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/test-infra/prow/github"
    23  	"k8s.io/test-infra/prow/github/fakegithub"
    24  	"k8s.io/test-infra/prow/plugins"
    25  )
    26  
    27  func TestTrusted(t *testing.T) {
    28  	const rando = "random-person"
    29  	const member = "org-member"
    30  	const sister = "trusted-org-member"
    31  	const friend = "repo-collaborator"
    32  
    33  	const accept = "/ok-to-test"
    34  	const chatter = "ignore random stuff"
    35  
    36  	var testcases = []struct {
    37  		name      string
    38  		author    string
    39  		comment   string
    40  		commenter string
    41  		onlyOrg   bool
    42  		expected  bool
    43  	}{
    44  		{
    45  			name:     "trust org member",
    46  			author:   member,
    47  			expected: true,
    48  		},
    49  		{
    50  			name:     "trust member of other trusted org",
    51  			author:   sister,
    52  			expected: true,
    53  		},
    54  		{
    55  			name: "reject random author",
    56  		},
    57  		{
    58  			name:      "reject random author on random org member commentary",
    59  			comment:   chatter,
    60  			commenter: member,
    61  		},
    62  		{
    63  			name:      "accept random PR after org member ok",
    64  			comment:   accept,
    65  			commenter: member,
    66  			expected:  true,
    67  		},
    68  		{
    69  			name:      "accept random PR after ok from trusted org member",
    70  			comment:   accept,
    71  			commenter: sister,
    72  			expected:  true,
    73  		},
    74  		{
    75  			name:      "ok may end with a \\r",
    76  			comment:   accept + "\r",
    77  			commenter: member,
    78  			expected:  true,
    79  		},
    80  		{
    81  			name:      "ok start on a middle line",
    82  			comment:   "hello\n" + accept + "\r\nplease",
    83  			commenter: member,
    84  			expected:  true,
    85  		},
    86  		{
    87  			name:      "require ok on start of line",
    88  			comment:   "please, " + accept,
    89  			commenter: member,
    90  		},
    91  		{
    92  			name:      "reject acceptance from random person",
    93  			comment:   accept,
    94  			commenter: rando + " III",
    95  		},
    96  		{
    97  			name:      "reject acceptance from this bot",
    98  			comment:   accept,
    99  			commenter: fakegithub.Bot,
   100  		},
   101  		{
   102  			name:      "reject acceptance from random author",
   103  			comment:   accept,
   104  			commenter: rando,
   105  		},
   106  		{
   107  			name:      "reject acceptance from repo collaborator in org-only mode",
   108  			comment:   accept,
   109  			commenter: friend,
   110  			onlyOrg:   true,
   111  		},
   112  		{
   113  			name:      "accept ok from repo collaborator",
   114  			comment:   accept,
   115  			commenter: friend,
   116  			expected:  true,
   117  		},
   118  	}
   119  	for _, tc := range testcases {
   120  		t.Run(tc.name, func(t *testing.T) {
   121  			if tc.author == "" {
   122  				tc.author = rando
   123  			}
   124  			g := &fakegithub.FakeClient{
   125  				OrgMembers:    map[string][]string{"kubernetes": {sister}, "kubernetes-incubator": {member, fakegithub.Bot}},
   126  				Collaborators: []string{friend},
   127  				IssueComments: map[int][]github.IssueComment{},
   128  			}
   129  			trigger := plugins.Trigger{
   130  				TrustedOrg:     "kubernetes",
   131  				OnlyOrgMembers: tc.onlyOrg,
   132  			}
   133  			var comments []github.IssueComment
   134  			if tc.comment != "" {
   135  				comments = append(comments, github.IssueComment{
   136  					Body: tc.comment,
   137  					User: github.User{Login: tc.commenter},
   138  				})
   139  			}
   140  			actual, err := trustedPullRequest(g, &trigger, tc.author, "kubernetes-incubator", "random-repo", comments)
   141  			if err != nil {
   142  				t.Fatalf("Didn't expect error: %s", err)
   143  			}
   144  			if actual != tc.expected {
   145  				t.Errorf("actual result %t != expected %t", actual, tc.expected)
   146  			}
   147  		})
   148  	}
   149  }