2021-08-03 18:14:32 +00:00
|
|
|
package joke_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2021-09-26 19:13:38 +00:00
|
|
|
"time"
|
2021-08-03 18:14:32 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
func TestAddNewJoke_201(t *testing.T) {
|
2021-08-04 10:05:58 +00:00
|
|
|
// TODO: Remove this line below, make this test works
|
2021-08-04 05:56:14 +00:00
|
|
|
t.SkipNow()
|
|
|
|
err := setup()
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-04 05:56:14 +00:00
|
|
|
|
|
|
|
defer cleanup()
|
|
|
|
|
2021-08-04 10:05:58 +00:00
|
|
|
reqBody := strings.NewReader("{\"link\":\"https://via.placeholder.com/300/07f/ff0000.png\",\"key\":\"test\",\"token\":\"password\"}")
|
2021-08-04 05:56:14 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/", reqBody)
|
|
|
|
req.Header.Set("content-type", "application/json")
|
|
|
|
req.Header.Add("accept", "application/json")
|
2021-09-26 19:13:38 +00:00
|
|
|
res, err := app.Test(req, int(time.Minute * 2))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
assert.Equalf(t, false, err != nil, "joke add")
|
|
|
|
assert.Equalf(t, 201, res.StatusCode, "joke add")
|
|
|
|
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://via.placeholder.com/300/07f/ff0000.png\"}", string(body), "joke add")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddNewJoke_NotValidImage(t *testing.T) {
|
2021-08-04 10:05:58 +00:00
|
|
|
// TODO: Remove this line below, make this test works
|
2021-08-04 05:56:14 +00:00
|
|
|
t.SkipNow()
|
|
|
|
err := setup()
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2021-08-04 10:05:58 +00:00
|
|
|
reqBody := strings.NewReader("{\"link\":\"https://google.com/\",\"key\":\"test\",\"token\":\"password\"}")
|
2021-08-04 05:56:14 +00:00
|
|
|
req, _ := http.NewRequest("PUT", "/", reqBody)
|
|
|
|
req.Header.Set("content-type", "application/json")
|
|
|
|
req.Header.Add("accept", "application/json")
|
2021-09-26 19:13:38 +00:00
|
|
|
res, err := app.Test(req, int(time.Minute * 2))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-03 18:14:32 +00:00
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
assert.Equalf(t, false, err != nil, "joke add")
|
|
|
|
assert.Equalf(t, 400, res.StatusCode, "joke add")
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
2021-09-26 19:13:38 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
assert.Nilf(t, err, "joke add")
|
|
|
|
assert.Equalf(t, "{\"error\":\"URL provided is not a valid image\"}", string(body), "joke add")
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|