github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/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  )
    25  
    26  func TestTrusted(t *testing.T) {
    27  	var testcases = []struct {
    28  		PR       github.PullRequest
    29  		Comments []github.IssueComment
    30  		Trusted  bool
    31  	}{
    32  		// Org member.
    33  		{
    34  			PR: github.PullRequest{
    35  				User: github.User{Login: "t1"},
    36  			},
    37  			Trusted: true,
    38  		},
    39  		// Non org member, no comments.
    40  		{
    41  			PR: github.PullRequest{
    42  				User: github.User{Login: "u"},
    43  			},
    44  			Trusted: false,
    45  		},
    46  		// Non org member, random comment by org member.
    47  		{
    48  			PR: github.PullRequest{
    49  				User: github.User{Login: "u"},
    50  			},
    51  			Comments: []github.IssueComment{
    52  				{
    53  					Body: "this is evil!",
    54  					User: github.User{Login: "t1"},
    55  				},
    56  			},
    57  			Trusted: false,
    58  		},
    59  		// Non org member, "not ok to test" comment by org member.
    60  		{
    61  			PR: github.PullRequest{
    62  				User: github.User{Login: "u"},
    63  			},
    64  			Comments: []github.IssueComment{
    65  				{
    66  					Body: "not ok to test",
    67  					User: github.User{Login: "t1"},
    68  				},
    69  			},
    70  			Trusted: false,
    71  		},
    72  		// Non org member, ok to test comment by org member.
    73  		{
    74  			PR: github.PullRequest{
    75  				User: github.User{Login: "u"},
    76  			},
    77  			Comments: []github.IssueComment{
    78  				{
    79  					Body: "/ok-to-test",
    80  					User: github.User{Login: "t1"},
    81  				},
    82  			},
    83  			Trusted: true,
    84  		},
    85  		// Non org member, ok to test comment with carriage return by org member.
    86  		{
    87  			PR: github.PullRequest{
    88  				User: github.User{Login: "u"},
    89  			},
    90  			Comments: []github.IssueComment{
    91  				{
    92  					Body: "/ok-to-test\r",
    93  					User: github.User{Login: "t1"},
    94  				},
    95  			},
    96  			Trusted: true,
    97  		},
    98  		// Non org member, multiline ok to test comment by org member.
    99  		{
   100  			PR: github.PullRequest{
   101  				User: github.User{Login: "u"},
   102  			},
   103  			Comments: []github.IssueComment{
   104  				{
   105  					Body: "hello\n/ok-to-test\r\nplease",
   106  					User: github.User{Login: "t1"},
   107  				},
   108  			},
   109  			Trusted: true,
   110  		},
   111  		// Non org member, ok to test with additional comment before by org member.
   112  		{
   113  			PR: github.PullRequest{
   114  				User: github.User{Login: "u"},
   115  			},
   116  			Comments: []github.IssueComment{
   117  				{
   118  					Body: "please, /ok-to-test",
   119  					User: github.User{Login: "t1"},
   120  				},
   121  			},
   122  			Trusted: false,
   123  		},
   124  		// Non org member, ok to test comment by non-org member.
   125  		{
   126  			PR: github.PullRequest{
   127  				User: github.User{Login: "u"},
   128  			},
   129  			Comments: []github.IssueComment{
   130  				{
   131  					Body: "/ok-to-test",
   132  					User: github.User{Login: "u2"},
   133  				},
   134  			},
   135  			Trusted: false,
   136  		},
   137  		// Non org member, ok to test comment by bot.
   138  		{
   139  			PR: github.PullRequest{
   140  				User: github.User{Login: "u"},
   141  			},
   142  			Comments: []github.IssueComment{
   143  				{
   144  					Body: "/ok-to-test",
   145  					User: github.User{Login: "k8s-bot"},
   146  				},
   147  			},
   148  			Trusted: false,
   149  		},
   150  		// Non org member, ok to test comment by author.
   151  		{
   152  			PR: github.PullRequest{
   153  				User: github.User{Login: "u"},
   154  			},
   155  			Comments: []github.IssueComment{
   156  				{
   157  					Body: "/ok-to-test",
   158  					User: github.User{Login: "u"},
   159  				},
   160  			},
   161  			Trusted: false,
   162  		},
   163  	}
   164  	for _, tc := range testcases {
   165  		g := &fakegithub.FakeClient{
   166  			OrgMembers: []string{"t1"},
   167  			IssueComments: map[int][]github.IssueComment{
   168  				0: tc.Comments,
   169  			},
   170  		}
   171  		trusted, err := trustedPullRequest(g, tc.PR, "kubernetes")
   172  		if err != nil {
   173  			t.Fatalf("Didn't expect error: %s", err)
   174  		}
   175  		if trusted != tc.Trusted {
   176  			t.Errorf("Wrong trusted: %+v", tc)
   177  		}
   178  	}
   179  }