jokes-bapak2/api/app/v1/handler/joke_add_test.go

62 lines
2.2 KiB
Go
Raw Normal View History

2021-07-19 12:38:01 +00:00
package handler_test
import (
"context"
"io/ioutil"
v1 "jokes-bapak2-api/app/v1"
"jokes-bapak2-api/app/v1/platform/database"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAddNewJoke(t *testing.T) {
2021-07-19 18:35:30 +00:00
// t.SkipNow()
2021-07-19 12:38:01 +00:00
err := database.Setup()
if err != nil {
t.Fatal(err)
}
hashedToken := "$argon2id$v=19$m=65536,t=16,p=4$48beb241490caa57fbca8e63df1e1b5fba8934baf78205ee775f96a85f45b889$e6dfca3f69adbe7653dbb353f366d741a3640313c45e33eabaca0c217c16417de80d70ac67f217c9ca46634b0abaad5f4ea2b064caa44ce218fb110b4cba9d36"
_, err = db.Query(context.Background(), "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4);", 1, "very secure", hashedToken, time.Now().Format(time.RFC3339))
if err != nil {
t.Fatal(err)
}
t.Cleanup(cleanup)
app := v1.New()
2021-07-19 18:35:30 +00:00
t.Run("Add - should return 201", func(t *testing.T) {
2021-07-23 15:03:04 +00:00
reqBody := strings.NewReader("{\"link\":\"https://via.placeholder.com/300/07f/ff0000.png\",\"key\":\"very secure\",\"token\":\"password\"}")
2021-07-19 12:38:01 +00:00
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")
2021-07-19 18:35:30 +00:00
assert.Equalf(t, 201, res.StatusCode, "joke add")
2021-07-19 12:38:01 +00:00
assert.NotEqualf(t, 0, res.ContentLength, "joke add")
body, err := ioutil.ReadAll(res.Body)
assert.Nilf(t, err, "joke add")
2021-07-23 15:03:04 +00:00
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)
2021-07-19 12:38:01 +00:00
2021-07-23 15:03:04 +00:00
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")
2021-07-19 12:38:01 +00:00
})
2021-07-23 15:03:04 +00:00
2021-07-19 12:38:01 +00:00
}