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