github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/api/checks/webhook_test.go (about)

     1  //go:build small
     2  // +build small
     3  
     4  // Copyright 2018 The WPT Dashboard Project. All rights reserved.
     5  // Use of this source code is governed by a BSD-style license that can be
     6  // found in the LICENSE file.
     7  package checks
     8  
     9  import (
    10  	"encoding/json"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/golang/mock/gomock"
    15  	"github.com/google/go-github/v47/github"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/web-platform-tests/wpt.fyi/api/checks/mock_checks"
    18  	"github.com/web-platform-tests/wpt.fyi/shared"
    19  	"github.com/web-platform-tests/wpt.fyi/shared/sharedtest"
    20  )
    21  
    22  func TestHandleCheckRunEvent_InvalidApp(t *testing.T) {
    23  	mockCtrl := gomock.NewController(t)
    24  	defer mockCtrl.Finish()
    25  
    26  	id := int64(123)
    27  	chrome := "chrome"
    28  	event := github.CheckRunEvent{
    29  		CheckRun: &github.CheckRun{
    30  			App: &github.App{
    31  				ID: &id,
    32  			},
    33  			Name: &chrome,
    34  		},
    35  	}
    36  	payload, _ := json.Marshal(event)
    37  
    38  	api := mock_checks.NewMockAPI(mockCtrl)
    39  	api.EXPECT().Context().AnyTimes().Return(sharedtest.NewTestContext())
    40  
    41  	processed, err := handleCheckRunEvent(api, payload)
    42  	assert.Nil(t, err)
    43  	assert.False(t, processed)
    44  }
    45  
    46  func TestHandleCheckRunEvent_Created_Completed(t *testing.T) {
    47  	mockCtrl := gomock.NewController(t)
    48  	defer mockCtrl.Finish()
    49  
    50  	sha := strings.Repeat("1234567890", 4)
    51  	event := getCheckRunCreatedEvent("completed", "lukebjerring", sha)
    52  	payload, _ := json.Marshal(event)
    53  
    54  	api := mock_checks.NewMockAPI(mockCtrl)
    55  	api.EXPECT().Context().AnyTimes().Return(sharedtest.NewTestContext())
    56  	api.EXPECT().IsFeatureEnabled(checksForAllUsersFeature).Return(false)
    57  
    58  	processed, err := handleCheckRunEvent(api, payload)
    59  	assert.Nil(t, err)
    60  	assert.False(t, processed)
    61  }
    62  
    63  func TestHandleCheckRunEvent_Created_Pending_ChecksNotEnabledForUser(t *testing.T) {
    64  	mockCtrl := gomock.NewController(t)
    65  	defer mockCtrl.Finish()
    66  
    67  	sha := strings.Repeat("0123456789", 4)
    68  	event := getCheckRunCreatedEvent("pending", "user-without-checks", sha)
    69  	payload, _ := json.Marshal(event)
    70  
    71  	api := mock_checks.NewMockAPI(mockCtrl)
    72  	api.EXPECT().Context().AnyTimes().Return(sharedtest.NewTestContext())
    73  	api.EXPECT().IsFeatureEnabled(checksForAllUsersFeature).Return(false)
    74  
    75  	processed, err := handleCheckRunEvent(api, payload)
    76  	assert.Nil(t, err)
    77  	assert.False(t, processed)
    78  }
    79  
    80  func TestHandleCheckRunEvent_Created_Pending(t *testing.T) {
    81  	mockCtrl := gomock.NewController(t)
    82  	defer mockCtrl.Finish()
    83  
    84  	sha := strings.Repeat("0123456789", 4)
    85  	event := getCheckRunCreatedEvent("pending", "lukebjerring", sha)
    86  	payload, _ := json.Marshal(event)
    87  
    88  	api := mock_checks.NewMockAPI(mockCtrl)
    89  	api.EXPECT().Context().AnyTimes().Return(sharedtest.NewTestContext())
    90  	api.EXPECT().IsFeatureEnabled(checksForAllUsersFeature).Return(false)
    91  	api.EXPECT().ScheduleResultsProcessing(sha, sharedtest.SameProductSpec("chrome")).Return(nil)
    92  
    93  	processed, err := handleCheckRunEvent(api, payload)
    94  	assert.Nil(t, err)
    95  	assert.True(t, processed)
    96  }
    97  
    98  func TestHandleCheckRunEvent_ActionRequested_Ignore(t *testing.T) {
    99  	for _, prefix := range []string{"staging.wpt.fyi - ", "wpt.fyi - ", ""} {
   100  		t.Run(prefix, func(t *testing.T) {
   101  			mockCtrl := gomock.NewController(t)
   102  			defer mockCtrl.Finish()
   103  
   104  			id := int64(wptfyiStagingCheckAppID)
   105  			sha := strings.Repeat("0123456789", 4)
   106  			name := prefix + "chrome"
   107  			requestedAction := "requested_action"
   108  			pending := "pending"
   109  			username := "lukebjerring"
   110  			owner := shared.WPTRepoOwner
   111  			repo := shared.WPTRepoName
   112  			appID := int64(wptfyiStagingCheckAppID)
   113  			event := github.CheckRunEvent{
   114  				Action: &requestedAction,
   115  				CheckRun: &github.CheckRun{
   116  					App:     &github.App{ID: &id},
   117  					Name:    &name,
   118  					Status:  &pending,
   119  					HeadSHA: &sha,
   120  				},
   121  				Repo: &github.Repository{
   122  					Owner: &github.User{Login: &owner},
   123  					Name:  &repo,
   124  				},
   125  				RequestedAction: &github.RequestedAction{Identifier: "ignore"},
   126  				Installation:    &github.Installation{AppID: &appID},
   127  				Sender:          &github.User{Login: &username},
   128  			}
   129  			payload, _ := json.Marshal(event)
   130  
   131  			api := mock_checks.NewMockAPI(mockCtrl)
   132  			api.EXPECT().Context().AnyTimes().Return(sharedtest.NewTestContext())
   133  			api.EXPECT().IsFeatureEnabled(checksForAllUsersFeature).Return(false)
   134  			api.EXPECT().IgnoreFailure(username, owner, repo, event.GetCheckRun(), event.GetInstallation())
   135  
   136  			processed, err := handleCheckRunEvent(api, payload)
   137  			assert.Nil(t, err)
   138  			assert.True(t, processed)
   139  		})
   140  	}
   141  }
   142  
   143  func TestHandleCheckRunEvent_ActionRequested_Recompute(t *testing.T) {
   144  	for _, prefix := range []string{"staging.wpt.fyi - ", "wpt.fyi - ", ""} {
   145  		t.Run(prefix, func(t *testing.T) {
   146  			mockCtrl := gomock.NewController(t)
   147  			defer mockCtrl.Finish()
   148  
   149  			id := int64(wptfyiStagingCheckAppID)
   150  			sha := strings.Repeat("0123456789", 4)
   151  			name := prefix + "chrome[experimental]"
   152  			requestedAction := "requested_action"
   153  			pending := "pending"
   154  			username := "lukebjerring"
   155  			owner := shared.WPTRepoOwner
   156  			repo := shared.WPTRepoName
   157  			appID := int64(wptfyiStagingCheckAppID)
   158  			event := github.CheckRunEvent{
   159  				Action: &requestedAction,
   160  				CheckRun: &github.CheckRun{
   161  					App:     &github.App{ID: &id},
   162  					Name:    &name,
   163  					Status:  &pending,
   164  					HeadSHA: &sha,
   165  				},
   166  				Repo: &github.Repository{
   167  					Owner: &github.User{Login: &owner},
   168  					Name:  &repo,
   169  				},
   170  				RequestedAction: &github.RequestedAction{Identifier: "recompute"},
   171  				Installation:    &github.Installation{AppID: &appID},
   172  				Sender:          &github.User{Login: &username},
   173  			}
   174  			payload, _ := json.Marshal(event)
   175  
   176  			api := mock_checks.NewMockAPI(mockCtrl)
   177  			api.EXPECT().Context().AnyTimes().Return(sharedtest.NewTestContext())
   178  			api.EXPECT().IsFeatureEnabled(checksForAllUsersFeature).Return(false)
   179  			api.EXPECT().ScheduleResultsProcessing(sha, sharedtest.SameProductSpec("chrome[experimental]")).Return(nil)
   180  
   181  			processed, err := handleCheckRunEvent(api, payload)
   182  			assert.Nil(t, err)
   183  			assert.True(t, processed)
   184  		})
   185  	}
   186  }
   187  
   188  func TestHandleCheckRunEvent_ActionRequested_Cancel(t *testing.T) {
   189  	mockCtrl := gomock.NewController(t)
   190  	defer mockCtrl.Finish()
   191  
   192  	sha := strings.Repeat("0123456789", 4)
   193  	username := "lukebjerring"
   194  	event := getCheckRunCreatedEvent("completed", username, sha)
   195  	requestedAction := "requested_action"
   196  	event.Action = &requestedAction
   197  	event.RequestedAction = &github.RequestedAction{Identifier: "cancel"}
   198  	payload, _ := json.Marshal(event)
   199  
   200  	api := mock_checks.NewMockAPI(mockCtrl)
   201  	api.EXPECT().Context().AnyTimes().Return(sharedtest.NewTestContext())
   202  	api.EXPECT().IsFeatureEnabled(checksForAllUsersFeature).Return(false)
   203  	api.EXPECT().CancelRun(username, shared.WPTRepoOwner, shared.WPTRepoName, event.GetCheckRun(), event.GetInstallation())
   204  
   205  	processed, err := handleCheckRunEvent(api, payload)
   206  	assert.Nil(t, err)
   207  	assert.True(t, processed)
   208  }
   209  
   210  func getCheckRunCreatedEvent(status, sender, sha string) github.CheckRunEvent {
   211  	id := int64(wptfyiStagingCheckAppID)
   212  	chrome := "chrome"
   213  	created := "created"
   214  	repoName := shared.WPTRepoName
   215  	repoOwner := shared.WPTRepoOwner
   216  	return github.CheckRunEvent{
   217  		Action: &created,
   218  		CheckRun: &github.CheckRun{
   219  			App:     &github.App{ID: &id},
   220  			Name:    &chrome,
   221  			Status:  &status,
   222  			HeadSHA: &sha,
   223  		},
   224  		Repo: &github.Repository{
   225  			Name:  &repoName,
   226  			Owner: &github.User{Login: &repoOwner},
   227  		},
   228  		Sender: &github.User{Login: &sender},
   229  	}
   230  }
   231  
   232  func TestHandlePullRequestEvent_ChecksNotEnabledForUser(t *testing.T) {
   233  	mockCtrl := gomock.NewController(t)
   234  	defer mockCtrl.Finish()
   235  
   236  	sha := strings.Repeat("1234567890", 4)
   237  	event := getOpenedPREvent("user-without-checks", sha)
   238  	payload, _ := json.Marshal(event)
   239  
   240  	api := mock_checks.NewMockAPI(mockCtrl)
   241  	api.EXPECT().Context().AnyTimes().Return(sharedtest.NewTestContext())
   242  	api.EXPECT().IsFeatureEnabled(checksForAllUsersFeature).Return(false)
   243  
   244  	processed, err := handlePullRequestEvent(api, payload)
   245  	assert.Nil(t, err)
   246  	assert.False(t, processed)
   247  }
   248  
   249  func TestHandlePullRequestEvent_ChecksEnabledForUser(t *testing.T) {
   250  	mockCtrl := gomock.NewController(t)
   251  	defer mockCtrl.Finish()
   252  
   253  	sha := strings.Repeat("1234567890", 4)
   254  	event := getOpenedPREvent("lukebjerring", sha)
   255  	payload, _ := json.Marshal(event)
   256  
   257  	api := mock_checks.NewMockAPI(mockCtrl)
   258  	api.EXPECT().Context().AnyTimes().Return(sharedtest.NewTestContext())
   259  	api.EXPECT().IsFeatureEnabled(checksForAllUsersFeature).Return(false)
   260  	api.EXPECT().GetWPTRepoAppInstallationIDs().Return(wptfyiStagingCheckAppID, wptRepoStagingInstallationID)
   261  	api.EXPECT().CreateWPTCheckSuite(wptfyiStagingCheckAppID, wptRepoStagingInstallationID, sha, 123).Return(true, nil)
   262  
   263  	processed, err := handlePullRequestEvent(api, payload)
   264  	assert.Nil(t, err)
   265  	assert.True(t, processed)
   266  }
   267  
   268  func getOpenedPREvent(user, sha string) github.PullRequestEvent {
   269  	opened := "opened"
   270  	// handlePullRequestEvent only operates on pull requests from forks, so
   271  	// the head repo must be different from the base.
   272  	headRepoID := wptRepoID - 1
   273  	baseRepoID := wptRepoID
   274  	number := 123
   275  	return github.PullRequestEvent{
   276  		Number: &number,
   277  		PullRequest: &github.PullRequest{
   278  			User: &github.User{Login: &user},
   279  			Head: &github.PullRequestBranch{
   280  				SHA:  &sha,
   281  				Repo: &github.Repository{ID: &headRepoID},
   282  			},
   283  			Base: &github.PullRequestBranch{
   284  				Repo: &github.Repository{ID: &baseRepoID},
   285  			},
   286  		},
   287  		Action: &opened,
   288  	}
   289  }