From 4f13a85cea6ef21c54d380222f304f6d39998f87 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Mon, 19 Jul 2021 19:38:01 +0700 Subject: [PATCH] test: added CUD tests --- api/app/v1/handler/joke_add_test.go | 47 +++++++++++++++++++ api/app/v1/handler/joke_delete_test.go | 62 ++++++++++++++++++++++++++ api/app/v1/handler/joke_update_test.go | 62 ++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 api/app/v1/handler/joke_add_test.go create mode 100644 api/app/v1/handler/joke_delete_test.go create mode 100644 api/app/v1/handler/joke_update_test.go diff --git a/api/app/v1/handler/joke_add_test.go b/api/app/v1/handler/joke_add_test.go new file mode 100644 index 0000000..d95bd52 --- /dev/null +++ b/api/app/v1/handler/joke_add_test.go @@ -0,0 +1,47 @@ +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) { + 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() + + t.Run("Add - should return 200", func(t *testing.T) { + t.SkipNow() + reqBody := strings.NewReader("{\"link\":\"https://picsum.photos/id/1/200/300\",\"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, 200, 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://picsum.photos/id/1/200/300\"}", string(body), "joke add") + + }) +} diff --git a/api/app/v1/handler/joke_delete_test.go b/api/app/v1/handler/joke_delete_test.go new file mode 100644 index 0000000..979e420 --- /dev/null +++ b/api/app/v1/handler/joke_delete_test.go @@ -0,0 +1,62 @@ +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 TestDeleteJoke(t *testing.T) { + 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) + } + _, err = db.Query(context.Background(), "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...) + if err != nil { + t.Fatal(err) + } + + t.Cleanup(cleanup) + + app := v1.New() + + t.Run("Delete - should return 200", func(t *testing.T) { + t.SkipNow() + reqBody := strings.NewReader("{\"key\":\"very secure\",\"token\":\"password\"}") + req, _ := http.NewRequest("DELETE", "/id/1", reqBody) + 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") + }) + + t.Run("Delete - id doesn't exists", func(t *testing.T) { + t.SkipNow() + reqBody := strings.NewReader("{\"key\":\"very secure\",\"token\":\"password\"}") + req, _ := http.NewRequest("DELETE", "/id/100", reqBody) + res, err := app.Test(req, -1) + + 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") + }) +} diff --git a/api/app/v1/handler/joke_update_test.go b/api/app/v1/handler/joke_update_test.go new file mode 100644 index 0000000..3062dbe --- /dev/null +++ b/api/app/v1/handler/joke_update_test.go @@ -0,0 +1,62 @@ +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 TestUpdateJoke(t *testing.T) { + 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) + } + _, err = db.Query(context.Background(), "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...) + if err != nil { + t.Fatal(err) + } + + t.Cleanup(cleanup) + + app := v1.New() + + t.Run("Update - should return 200", func(t *testing.T) { + t.SkipNow() + reqBody := strings.NewReader("{\"link\":\"https://picsum.photos/id/9/200/300\",\"key\":\"very secure\",\"token\":\"password\"}") + req, _ := http.NewRequest("PATCH", "/id/1", reqBody) + res, err := app.Test(req, -1) + + assert.Equalf(t, false, err != nil, "joke update") + assert.Equalf(t, 200, res.StatusCode, "joke update") + assert.NotEqualf(t, 0, res.ContentLength, "joke update") + body, err := ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "joke update") + assert.Equalf(t, "{\"message\":\"specified joke id has been deleted\"}", string(body), "joke update") + }) + + t.Run("Update - id doesn't exists", func(t *testing.T) { + t.SkipNow() + reqBody := strings.NewReader("{\"link\":\"https://picsum.photos/id/9/200/300\",\"key\":\"very secure\",\"token\":\"password\"}") + req, _ := http.NewRequest("PATCH", "/id/100", reqBody) + res, err := app.Test(req, -1) + + assert.Equalf(t, false, err != nil, "joke update") + assert.Equalf(t, 406, res.StatusCode, "joke update") + assert.NotEqualf(t, 0, res.ContentLength, "joke update") + body, err := ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "joke update") + assert.Equalf(t, "{\"message\":\"specified joke id does not exists\"}", string(body), "joke update") + }) +}