fix: validation didnt run properly

This commit is contained in:
Reinaldy Rafli 2021-07-23 22:03:04 +07:00
parent 2237bd7efd
commit 600385b96e
3 changed files with 20 additions and 6 deletions

View File

@ -20,10 +20,10 @@ func CheckImageValidity(client *httpclient.Client, link string) (bool, error) {
return false, err 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 true, nil
} }
return false, nil return false, nil
} }
return false, errors.New("URL must use HTTPS protocol") return false, errors.New("URL must use HTTPS protocol")

View File

@ -30,7 +30,7 @@ func TestAddNewJoke(t *testing.T) {
app := v1.New() app := v1.New()
t.Run("Add - should return 201", func(t *testing.T) { 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, _ := http.NewRequest("PUT", "/", reqBody)
req.Header.Set("content-type", "application/json") req.Header.Set("content-type", "application/json")
req.Header.Add("accept", "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") assert.NotEqualf(t, 0, res.ContentLength, "joke add")
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
assert.Nilf(t, err, "joke add") 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")
})
} }

View File

@ -15,7 +15,7 @@ import (
) )
var db = database.New() 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() { func cleanup() {
_, err := db.Query(context.Background(), "DROP TABLE \"jokesbapak2\"") _, err := db.Query(context.Background(), "DROP TABLE \"jokesbapak2\"")