github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/lifecycle/reopen_test.go (about)

     1  /*
     2  Copyright 2017 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 lifecycle
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/sirupsen/logrus"
    23  
    24  	"k8s.io/test-infra/prow/github"
    25  )
    26  
    27  type fakeClientReopen struct {
    28  	commented bool
    29  	open      bool
    30  }
    31  
    32  func (c *fakeClientReopen) CreateComment(owner, repo string, number int, comment string) error {
    33  	c.commented = true
    34  	return nil
    35  }
    36  
    37  func (c *fakeClientReopen) ReopenIssue(owner, repo string, number int) error {
    38  	c.open = true
    39  	return nil
    40  }
    41  
    42  func (c *fakeClientReopen) ReopenPR(owner, repo string, number int) error {
    43  	c.open = true
    44  	return nil
    45  }
    46  
    47  func (c *fakeClientReopen) IsCollaborator(owner, repo, login string) (bool, error) {
    48  	if login == "collaborator" {
    49  		return true, nil
    50  	}
    51  	return false, nil
    52  }
    53  
    54  func TestReopenComment(t *testing.T) {
    55  	var testcases = []struct {
    56  		name          string
    57  		action        github.GenericCommentEventAction
    58  		state         string
    59  		body          string
    60  		commenter     string
    61  		shouldReopen  bool
    62  		shouldComment bool
    63  	}{
    64  		{
    65  			name:          "non-open comment",
    66  			action:        github.GenericCommentActionCreated,
    67  			state:         "open",
    68  			body:          "does not matter",
    69  			commenter:     "random-person",
    70  			shouldReopen:  false,
    71  			shouldComment: false,
    72  		},
    73  		{
    74  			name:          "re-open by author",
    75  			action:        github.GenericCommentActionCreated,
    76  			state:         "closed",
    77  			body:          "/reopen",
    78  			commenter:     "author",
    79  			shouldReopen:  true,
    80  			shouldComment: true,
    81  		},
    82  		{
    83  			name:          "re-open by collaborator",
    84  			action:        github.GenericCommentActionCreated,
    85  			state:         "closed",
    86  			body:          "/reopen",
    87  			commenter:     "collaborator",
    88  			shouldReopen:  true,
    89  			shouldComment: true,
    90  		},
    91  		{
    92  			name:          "re-open by collaborator, trailing space.",
    93  			action:        github.GenericCommentActionCreated,
    94  			state:         "closed",
    95  			body:          "/reopen \r",
    96  			commenter:     "collaborator",
    97  			shouldReopen:  true,
    98  			shouldComment: true,
    99  		},
   100  		{
   101  			name:          "re-open edited by author",
   102  			action:        github.GenericCommentActionEdited,
   103  			state:         "closed",
   104  			body:          "/reopen",
   105  			commenter:     "author",
   106  			shouldReopen:  false,
   107  			shouldComment: false,
   108  		},
   109  		{
   110  			name:          "open by author on already open issue",
   111  			action:        github.GenericCommentActionCreated,
   112  			state:         "open",
   113  			body:          "/reopen",
   114  			commenter:     "author",
   115  			shouldReopen:  false,
   116  			shouldComment: false,
   117  		},
   118  		{
   119  			name:          "re-open by non-collaborator, cannot reopen",
   120  			action:        github.GenericCommentActionCreated,
   121  			state:         "closed",
   122  			body:          "/reopen",
   123  			commenter:     "non-collaborator",
   124  			shouldReopen:  false,
   125  			shouldComment: true,
   126  		},
   127  	}
   128  	for _, tc := range testcases {
   129  		fc := &fakeClientReopen{}
   130  		e := &github.GenericCommentEvent{
   131  			Action:      tc.action,
   132  			IssueState:  tc.state,
   133  			Body:        tc.body,
   134  			User:        github.User{Login: tc.commenter},
   135  			Number:      5,
   136  			IssueAuthor: github.User{Login: "author"},
   137  		}
   138  		if err := handleReopen(fc, logrus.WithField("plugin", "fake-reopen"), e); err != nil {
   139  			t.Errorf("For case %s, didn't expect error from handle: %v", tc.name, err)
   140  			continue
   141  		}
   142  		if tc.shouldReopen && !fc.open {
   143  			t.Errorf("For case %s, should have reopened but didn't.", tc.name)
   144  		} else if !tc.shouldReopen && fc.open {
   145  			t.Errorf("For case %s, should not have reopened but did.", tc.name)
   146  		}
   147  		if tc.shouldComment && !fc.commented {
   148  			t.Errorf("For case %s, should have commented but didn't.", tc.name)
   149  		} else if !tc.shouldComment && fc.commented {
   150  			t.Errorf("For case %s, should not have commented but did.", tc.name)
   151  		}
   152  	}
   153  }