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

     1  //go:build small
     2  // +build small
     3  
     4  // Copyright 2019 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  
     8  package receiver
     9  
    10  import (
    11  	"encoding/json"
    12  	"net/http"
    13  	"net/http/httptest"
    14  	"strings"
    15  	"testing"
    16  
    17  	"github.com/golang/mock/gomock"
    18  	"github.com/gorilla/mux"
    19  	"github.com/stretchr/testify/assert"
    20  
    21  	"github.com/web-platform-tests/wpt.fyi/api/receiver/mock_receiver"
    22  	"github.com/web-platform-tests/wpt.fyi/shared"
    23  	"github.com/web-platform-tests/wpt.fyi/shared/sharedtest"
    24  )
    25  
    26  func TestApiPendingTestRunUpdateHandler(t *testing.T) {
    27  	mockCtrl := gomock.NewController(t)
    28  	defer mockCtrl.Finish()
    29  
    30  	pendingRun := shared.PendingTestRun{
    31  		ID:    12345,
    32  		Stage: shared.StageWptFyiProcessing,
    33  	}
    34  	payload := map[string]interface{}{
    35  		"id":    12345,
    36  		"stage": "WPTFYI_PROCESSING",
    37  	}
    38  	body, err := json.Marshal(payload)
    39  	assert.Nil(t, err)
    40  	req := httptest.NewRequest("PATCH", "/api/status/12345", strings.NewReader(string(body)))
    41  	req.SetBasicAuth("_processor", "secret-token")
    42  	req = mux.SetURLVars(req, map[string]string{"id": "12345"})
    43  
    44  	mockAE := mock_receiver.NewMockAPI(mockCtrl)
    45  	mockAE.EXPECT().Context().AnyTimes().Return(sharedtest.NewTestContext())
    46  	gomock.InOrder(
    47  		mockAE.EXPECT().GetUploader("_processor").Return(shared.Uploader{"_processor", "secret-token"}, nil),
    48  		mockAE.EXPECT().UpdatePendingTestRun(pendingRun).Return(nil),
    49  	)
    50  
    51  	w := httptest.NewRecorder()
    52  	HandleUpdatePendingTestRun(mockAE, w, req)
    53  	resp := w.Result()
    54  	assert.Equal(t, http.StatusCreated, resp.StatusCode)
    55  }