github.com/samcontesse/bitbucket-cascade-merge@v0.0.0-20230227091349-c5ec053235b5/handler_test.go (about)

     1  package main
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  // The body is a pull request event. Happy path. We expect a status 201
    11  func TestEventHandler_HandleValidPayload(t *testing.T) {
    12  	c := make(chan PullRequestEvent, 1)
    13  	eh := EventHandler{channel: c}
    14  
    15  	rr, err := request("test/fixtures/hook-pull-request-fulfilled.json", eh.Handle())
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  
    20  	event := <-c
    21  	if &event.Repository == nil {
    22  		t.Error("repository must be defined")
    23  	}
    24  
    25  	if status := rr.Code; status != http.StatusCreated {
    26  		t.Errorf("handler returned wrong status code: got %v want %v",
    27  			status, http.StatusCreated)
    28  	}
    29  }
    30  
    31  // The body is a pull request event but state is not set to MERGED. We expect a status 422
    32  func TestEventHandler_HandleUnsupportedState(t *testing.T) {
    33  	c := make(chan PullRequestEvent, 1)
    34  	eh := EventHandler{channel: c}
    35  
    36  	rr, err := request("test/fixtures/hook-pull-request-created.json", eh.Handle())
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	if status := rr.Code; status != http.StatusUnprocessableEntity {
    42  		t.Errorf("handler returned wrong status code: got %v want %v",
    43  			status, http.StatusUnprocessableEntity)
    44  	}
    45  }
    46  
    47  // The body is a push event (not a pull request event). We expect a status 400
    48  func TestEventHandler_HandleUnsupportedEventType(t *testing.T) {
    49  	c := make(chan PullRequestEvent, 1)
    50  	eh := EventHandler{channel: c}
    51  
    52  	rr, err := request("test/fixtures/hook-pr-merged-develop.json", eh.Handle())
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	if status := rr.Code; status != http.StatusBadRequest {
    58  		t.Errorf("handler returned wrong status code: got %v want %v",
    59  			status, http.StatusBadRequest)
    60  	}
    61  }
    62  
    63  // The body contains some random model that we cannot deserialize. We expect a status 400.
    64  func TestEventHandler_HandleBadRequest(t *testing.T) {
    65  	c := make(chan PullRequestEvent, 1)
    66  	eh := EventHandler{channel: c}
    67  
    68  	rr, err := request("test/fixtures/hook-bad-request.json", eh.Handle())
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	if status := rr.Code; status != http.StatusBadRequest {
    74  		t.Errorf("handler returned wrong status code: got %v want %v",
    75  			status, http.StatusBadRequest)
    76  	}
    77  }
    78  
    79  func request(filename string, hf http.Handler) (*httptest.ResponseRecorder, error) {
    80  	file, _ := os.Open(filename)
    81  	req, err := http.NewRequest("POST", "/hook", file)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	rr := httptest.NewRecorder()
    87  	hf.ServeHTTP(rr, req)
    88  
    89  	return rr, nil
    90  }