2021-08-03 18:14:32 +00:00
|
|
|
package joke_test
|
|
|
|
|
|
|
|
import (
|
2021-08-04 10:05:58 +00:00
|
|
|
"context"
|
2021-08-03 18:14:32 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
func TestDeleteJoke_200(t *testing.T) {
|
2021-08-03 18:14:32 +00:00
|
|
|
// TODO: Remove this line below, make this test works
|
|
|
|
t.SkipNow()
|
2021-08-04 05:56:14 +00:00
|
|
|
err := setup()
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-04 05:56:14 +00:00
|
|
|
|
2021-08-04 10:05:58 +00:00
|
|
|
j, err := db.Query(context.Background(), "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3);", 100, "https://via.placeholder.com/300/01f/fff.png", 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer j.Close()
|
2021-08-04 05:56:14 +00:00
|
|
|
defer cleanup()
|
2021-08-04 09:03:53 +00:00
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
reqBody := strings.NewReader("{\"key\":\"very secure\",\"token\":\"password\"}")
|
2021-08-04 10:05:58 +00:00
|
|
|
req, _ := http.NewRequest("DELETE", "/id/100", reqBody)
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2021-08-04 05:56:14 +00:00
|
|
|
res, err := app.Test(req, -1)
|
|
|
|
|
|
|
|
assert.Equalf(t, false, err != nil, "joke delete")
|
|
|
|
assert.Equalf(t, 200, res.StatusCode, "joke delete")
|
|
|
|
assert.NotEqualf(t, 0, res.ContentLength, "joke delete")
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
assert.Nilf(t, err, "joke delete")
|
|
|
|
assert.Equalf(t, "{\"message\":\"specified joke id has been deleted\"}", string(body), "joke delete")
|
|
|
|
}
|
|
|
|
func TestDeleteJoke_NotExists(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-03 18:14:32 +00:00
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
reqBody := strings.NewReader("{\"key\":\"very secure\",\"token\":\"password\"}")
|
|
|
|
req, _ := http.NewRequest("DELETE", "/id/100", reqBody)
|
2021-08-04 10:05:58 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("Accept", "application/json")
|
2021-08-04 05:56:14 +00:00
|
|
|
res, err := app.Test(req, -1)
|
2021-08-03 18:14:32 +00:00
|
|
|
|
2021-08-04 05:56:14 +00:00
|
|
|
assert.Equalf(t, false, err != nil, "joke delete")
|
|
|
|
assert.Equalf(t, 406, res.StatusCode, "joke delete")
|
|
|
|
assert.NotEqualf(t, 0, res.ContentLength, "joke delete")
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
|
|
assert.Nilf(t, err, "joke delete")
|
|
|
|
assert.Equalf(t, "{\"message\":\"specified joke id does not exists\"}", string(body), "joke delete")
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|