github.com/Financial-Times/publish-availability-monitor@v1.12.0/content/video_test.go (about)

     1  package content
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  
     9  	"github.com/Financial-Times/go-logger/v2"
    10  	"github.com/Financial-Times/publish-availability-monitor/httpcaller"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestIsVideoValid_Valid(t *testing.T) {
    15  	var videoValid = Video{
    16  		ID:            "e28b12f7-9796-3331-b030-05082f0b8157",
    17  		BinaryContent: []byte("valid-json"),
    18  	}
    19  
    20  	tid := "tid_1234"
    21  	pamTID := httpcaller.ConstructPamTID(tid)
    22  
    23  	testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    24  		assert.Equal(t, "/map", req.RequestURI)
    25  		assert.Equal(t, pamTID, req.Header.Get("X-Request-Id"))
    26  
    27  		defer req.Body.Close()
    28  		reqBody, err := io.ReadAll(req.Body)
    29  		assert.NoError(t, err)
    30  		assert.Equal(t, videoValid.BinaryContent, reqBody)
    31  	}))
    32  
    33  	log := logger.NewUPPLogger("test", "PANIC")
    34  
    35  	validationResponse := videoValid.Validate(testServer.URL+"/map", tid, "", "", log)
    36  	assert.True(t, validationResponse.IsValid, "Video should be valid.")
    37  }
    38  
    39  func TestIsVideoValid_NoId(t *testing.T) {
    40  	var videoNoID = Video{}
    41  	log := logger.NewUPPLogger("test", "PANIC")
    42  
    43  	validationResponse := videoNoID.Validate("", "", "", "", log)
    44  	assert.False(t, validationResponse.IsValid, "Video should be invalid as it has no Id.")
    45  }
    46  
    47  func TestIsVideoValid_failedExternalValidation(t *testing.T) {
    48  	var videoInvalid = Video{
    49  		ID:            "e28b12f7-9796-3331-b030-05082f0b8157",
    50  		BinaryContent: []byte("invalid-json"),
    51  	}
    52  
    53  	tid := "tid_1234"
    54  	pamTID := httpcaller.ConstructPamTID(tid)
    55  
    56  	testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    57  		assert.Equal(t, "/map", req.RequestURI)
    58  		assert.Equal(t, pamTID, req.Header.Get("X-Request-Id"))
    59  
    60  		defer req.Body.Close()
    61  		reqBody, err := io.ReadAll(req.Body)
    62  		assert.NoError(t, err)
    63  		assert.Equal(t, videoInvalid.BinaryContent, reqBody)
    64  
    65  		w.WriteHeader(http.StatusBadRequest)
    66  	}))
    67  
    68  	log := logger.NewUPPLogger("test", "PANIC")
    69  	validationResponse := videoInvalid.Validate(testServer.URL+"/map", tid, "", "", log)
    70  	assert.False(t, validationResponse.IsMarkedDeleted, "Video should fail external validation.")
    71  }
    72  
    73  func TestIsDeleted(t *testing.T) {
    74  	var videoNoDates = Video{
    75  		ID:      "e28b12f7-9796-3331-b030-05082f0b8157",
    76  		Deleted: true,
    77  	}
    78  
    79  	log := logger.NewUPPLogger("test", "PANIC")
    80  	validationResponse := videoNoDates.Validate("", "", "", "", log)
    81  	assert.True(t, validationResponse.IsMarkedDeleted, "Video should be evaluated as deleted.")
    82  }