github.com/Financial-Times/publish-availability-monitor@v1.12.0/content/genericContent_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 TestGenericContent_Validate(t *testing.T) {
    15  	tests := map[string]struct {
    16  		Content                        GenericContent
    17  		ExternalValidationResponseCode int
    18  		Expected                       ValidationResponse
    19  	}{
    20  		"valid generic content": {
    21  			Content: GenericContent{
    22  				UUID:    "077f5ac2-0491-420e-a5d0-982e0f86204b",
    23  				Type:    "application/vnd.ft-upp-article-internal+json",
    24  				Deleted: false,
    25  				BinaryContent: []byte(`{
    26  					"uuid": "077f5ac2-0491-420e-a5d0-982e0f86204b",
    27  					"title": "A title",
    28  					"type": "Article",
    29  					"byline": "A byline",
    30  					"publishedDate": "2014-12-23T20:45:54.000Z",
    31  					"firstPublishedDate": "2014-12-22T20:45:54.000Z",
    32  					"bodyXML": "<body>Lorem ipsum</body>",
    33  					"editorialDesk": "some string editorial desk identifier",
    34  					"description": "Some descriptive explanation for this content",
    35  					"mainImage": "0000aa3c-0056-506b-2b73-ed90e21b3e64",
    36  					"someUnknownProperty" : " is totally fine, we don't validate for unknown fields/properties",
    37  					"deleted": false
    38  				  }`),
    39  			},
    40  			ExternalValidationResponseCode: http.StatusOK,
    41  			Expected:                       ValidationResponse{IsValid: true, IsMarkedDeleted: false},
    42  		},
    43  		"valid deleted generic content": {
    44  			Content: GenericContent{
    45  				UUID:    "077f5ac2-0491-420e-a5d0-982e0f86204b",
    46  				Type:    "application/vnd.ft-upp-article-internal+json",
    47  				Deleted: true,
    48  				BinaryContent: []byte(`{
    49  					"uuid": "077f5ac2-0491-420e-a5d0-982e0f86204b",
    50  					"title": "A title",
    51  					"type": "Article",
    52  					"deleted": true
    53  				  }`),
    54  			},
    55  			ExternalValidationResponseCode: http.StatusOK,
    56  			Expected:                       ValidationResponse{IsValid: true, IsMarkedDeleted: true},
    57  		},
    58  		"generic content with missing uuid is invalid": {
    59  			Content: GenericContent{
    60  				UUID: "",
    61  			},
    62  			ExternalValidationResponseCode: http.StatusOK,
    63  			Expected:                       ValidationResponse{IsValid: false, IsMarkedDeleted: false},
    64  		},
    65  		"generic content with invalid uuid is invalid": {
    66  			Content: GenericContent{
    67  				UUID: "this-string-is-not-uuid",
    68  			},
    69  			Expected: ValidationResponse{IsValid: false, IsMarkedDeleted: false},
    70  		},
    71  		"generic content with failed external validatotion is invalid": {
    72  			Content: GenericContent{
    73  				UUID:          "077f5ac2-0491-420e-a5d0-982e0f86204b",
    74  				Type:          "application/vnd.ft-upp-article-internal+json",
    75  				BinaryContent: []byte(`invalid payload`),
    76  			},
    77  			ExternalValidationResponseCode: http.StatusUnsupportedMediaType,
    78  			Expected:                       ValidationResponse{IsValid: false, IsMarkedDeleted: false},
    79  		},
    80  	}
    81  
    82  	log := logger.NewUPPLogger("test", "PANIC")
    83  
    84  	for name, test := range tests {
    85  		t.Run(name, func(t *testing.T) {
    86  			tid := "tid_1234"
    87  			testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    88  				assert.Equal(t, "/validate", req.RequestURI)
    89  				assert.Equal(t, httpcaller.ConstructPamTID(tid), req.Header.Get("X-Request-Id"))
    90  				assert.Equal(t, "POST", req.Method)
    91  				assert.Equal(t, test.Content.Type, req.Header.Get("Content-Type"))
    92  
    93  				reqBody, err := io.ReadAll(req.Body)
    94  				assert.NoError(t, err)
    95  				defer req.Body.Close()
    96  
    97  				assert.Equal(t, test.Content.BinaryContent, reqBody)
    98  				w.WriteHeader(test.ExternalValidationResponseCode)
    99  			}))
   100  
   101  			validationResponse := test.Content.Validate(testServer.URL+"/validate", tid, "", "", log)
   102  			assert.Equal(t, test.Content.isMarkedDeleted(), validationResponse.IsMarkedDeleted)
   103  			assert.Equal(t, test.Expected, validationResponse)
   104  		})
   105  	}
   106  }