diff --git a/api/app/v1/core/joke_validation.go b/api/app/v1/core/joke_validation.go index 152ec79..95275c8 100644 --- a/api/app/v1/core/joke_validation.go +++ b/api/app/v1/core/joke_validation.go @@ -20,10 +20,10 @@ func CheckImageValidity(client *httpclient.Client, link string) (bool, error) { return false, err } - if res.StatusCode == 200 && res.ContentLength > 0 && utils.IsIn(ValidContentType, res.Header.Get("content-type")) { + if res.StatusCode == 200 && utils.IsIn(ValidContentType, res.Header.Get("content-type")) { return true, nil } - + return false, nil } return false, errors.New("URL must use HTTPS protocol") diff --git a/api/app/v1/handler/joke_add_test.go b/api/app/v1/handler/joke_add_test.go index 5540bd6..760ab38 100644 --- a/api/app/v1/handler/joke_add_test.go +++ b/api/app/v1/handler/joke_add_test.go @@ -30,7 +30,7 @@ func TestAddNewJoke(t *testing.T) { app := v1.New() t.Run("Add - should return 201", func(t *testing.T) { - reqBody := strings.NewReader("{\"link\":\"https://picsum.photos/id/1/200/300\",\"key\":\"very secure\",\"token\":\"password\"}") + reqBody := strings.NewReader("{\"link\":\"https://via.placeholder.com/300/07f/ff0000.png\",\"key\":\"very secure\",\"token\":\"password\"}") req, _ := http.NewRequest("PUT", "/", reqBody) req.Header.Set("content-type", "application/json") req.Header.Add("accept", "application/json") @@ -41,7 +41,21 @@ func TestAddNewJoke(t *testing.T) { assert.NotEqualf(t, 0, res.ContentLength, "joke add") body, err := ioutil.ReadAll(res.Body) assert.Nilf(t, err, "joke add") - assert.Equalf(t, "{\"link\":\"https://picsum.photos/id/1/200/300\"}", string(body), "joke add") - + assert.Equalf(t, "{\"link\":\"https://via.placeholder.com/300/07f/ff0000.png\"}", string(body), "joke add") }) + + t.Run("Add - should not be a valid image", func(t *testing.T) { + reqBody := strings.NewReader("{\"link\":\"https://google.com/\",\"key\":\"very secure\",\"token\":\"password\"}") + req, _ := http.NewRequest("PUT", "/", reqBody) + req.Header.Set("content-type", "application/json") + req.Header.Add("accept", "application/json") + res, err := app.Test(req, -1) + + assert.Equalf(t, false, err != nil, "joke add") + assert.Equalf(t, 400, res.StatusCode, "joke add") + body, err := ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "joke add") + assert.Equalf(t, "{\"error\":\"URL provided is not a valid image\"}", string(body), "joke add") + }) + } diff --git a/api/app/v1/handler/joke_get_test.go b/api/app/v1/handler/joke_get_test.go index a40e200..0f491b6 100644 --- a/api/app/v1/handler/joke_get_test.go +++ b/api/app/v1/handler/joke_get_test.go @@ -15,7 +15,7 @@ import ( ) var db = database.New() -var jokesData = []interface{}{1, "https://picsum.photos/id/1/200/300", 1, 2, "https://picsum.photos/id/2/200/300", 1, 3, "https://picsum.photos/id/3/200/300", 1} +var jokesData = []interface{}{1, "https://via.placeholder.com/300/06f/fff.png", 1, 2, "https://via.placeholder.com/300/07f/fff.png", 1, 3, "https://via.placeholder.com/300/08f/fff.png", 1} func cleanup() { _, err := db.Query(context.Background(), "DROP TABLE \"jokesbapak2\"")