From 8f570f99dbd679cbc07bbbb47cc33a0818051b2a Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Wed, 4 Aug 2021 12:56:14 +0700 Subject: [PATCH] test: refactor and add submit test --- api/app/v1/app.go | 6 +- api/app/v1/core/joke_getter.go | 2 + api/app/v1/core/joke_validation.go | 4 +- api/app/v1/handler/health/health_test.go | 63 +++++--- api/app/v1/handler/joke/joke_add.go | 4 +- api/app/v1/handler/joke/joke_add_test.go | 81 +++++----- api/app/v1/handler/joke/joke_delete.go | 4 +- api/app/v1/handler/joke/joke_delete_test.go | 78 ++++----- api/app/v1/handler/joke/joke_get_test.go | 158 ++++++++++++------- api/app/v1/handler/joke/joke_total_test.go | 38 ++--- api/app/v1/handler/joke/joke_update.go | 4 +- api/app/v1/handler/joke/joke_update_test.go | 76 ++++----- api/app/v1/handler/submit/submit_get_test.go | 79 ++++++++++ api/app/v1/models/sql.go | 10 -- api/app/v1/platform/database/create.go | 35 +++- api/app/v1/utils/array_test.go | 27 ++-- api/app/v1/utils/date_test.go | 68 ++++---- api/app/v1/utils/parse_test.go | 56 +++---- api/app/v1/utils/random_test.go | 37 +++-- api/app/v1/utils/request_test.go | 30 ++-- api/go.mod | 6 + api/go.sum | 19 ++- api/main.go | 4 +- 23 files changed, 511 insertions(+), 378 deletions(-) create mode 100644 api/app/v1/handler/submit/submit_get_test.go delete mode 100644 api/app/v1/models/sql.go diff --git a/api/app/v1/app.go b/api/app/v1/app.go index 328e107..bccbe98 100644 --- a/api/app/v1/app.go +++ b/api/app/v1/app.go @@ -26,8 +26,9 @@ func New() *fiber.App { }) err := sentry.Init(sentry.ClientOptions{ - Dsn: os.Getenv("SENTRY_DSN"), - Environment: os.Getenv("ENV"), + Dsn: os.Getenv("SENTRY_DSN"), + Environment: os.Getenv("ENV"), + AttachStacktrace: true, // Enable printing of SDK debug messages. // Useful when getting started or trying to figure something out. Debug: true, @@ -58,6 +59,7 @@ func New() *fiber.App { routes.Health(app) routes.Joke(app) + routes.Submit(app) return app } diff --git a/api/app/v1/core/joke_getter.go b/api/app/v1/core/joke_getter.go index 9d55b37..6297293 100644 --- a/api/app/v1/core/joke_getter.go +++ b/api/app/v1/core/joke_getter.go @@ -20,6 +20,8 @@ func GetAllJSONJokes(db *pgxpool.Pool) ([]byte, error) { return nil, err } + defer results.Close() + err = pgxscan.ScanAll(&jokes, results) if err != nil { return nil, err diff --git a/api/app/v1/core/joke_validation.go b/api/app/v1/core/joke_validation.go index 95275c8..9d47236 100644 --- a/api/app/v1/core/joke_validation.go +++ b/api/app/v1/core/joke_validation.go @@ -23,8 +23,8 @@ func CheckImageValidity(client *httpclient.Client, link string) (bool, error) { if res.StatusCode == 200 && utils.IsIn(ValidContentType, res.Header.Get("content-type")) { return true, nil } - + return false, nil } return false, errors.New("URL must use HTTPS protocol") -} \ No newline at end of file +} diff --git a/api/app/v1/handler/health/health_test.go b/api/app/v1/handler/health/health_test.go index 9e5f325..9cd990a 100644 --- a/api/app/v1/handler/health/health_test.go +++ b/api/app/v1/handler/health/health_test.go @@ -9,49 +9,64 @@ import ( "testing" "time" + "github.com/gofiber/fiber/v2" + "github.com/jackc/pgx/v4/pgxpool" + _ "github.com/joho/godotenv/autoload" "github.com/stretchr/testify/assert" ) -var db = database.New() +var db *pgxpool.Pool = database.New() 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} +var app *fiber.App = v1.New() func cleanup() { - _, err := db.Query(context.Background(), "DROP TABLE \"jokesbapak2\"") + j, err := db.Query(context.Background(), "DROP TABLE \"jokesbapak2\"") if err != nil { panic(err) } - _, err = db.Query(context.Background(), "DROP TABLE \"administrators\"") + a, err := db.Query(context.Background(), "DROP TABLE \"administrators\"") if err != nil { panic(err) } + + defer j.Close() + defer a.Close() +} + +func setup() error { + err := database.Setup() + if err != nil { + return err + } + a, err := db.Query(context.Background(), "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4);", 1, "very secure", "not the real one", time.Now().Format(time.RFC3339)) + if err != nil { + return err + } + j, 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 { + return err + } + + defer a.Close() + defer j.Close() + + return nil } func TestHealth(t *testing.T) { - err := database.Setup() - if err != nil { - t.Fatal(err) - } - _, err = db.Query(context.Background(), "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4);", 1, "very secure", "not the real one", 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...) + err := setup() if err != nil { t.Fatal(err) } - t.Cleanup(cleanup) + defer cleanup() - app := v1.New() + req, _ := http.NewRequest("GET", "/health", nil) + res, err := app.Test(req, -1) - t.Run("Health - should return 200", func(t *testing.T) { - req, _ := http.NewRequest("GET", "/health", nil) - res, err := app.Test(req, -1) - - assert.Equalf(t, false, err != nil, "health") - assert.Equalf(t, 200, res.StatusCode, "health") - assert.NotEqualf(t, 0, res.ContentLength, "health") - _, err = ioutil.ReadAll(res.Body) - assert.Nilf(t, err, "health") - }) + assert.Equalf(t, false, err != nil, "health") + assert.Equalf(t, 200, res.StatusCode, "health") + assert.NotEqualf(t, 0, res.ContentLength, "health") + _, err = ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "health") } diff --git a/api/app/v1/handler/joke/joke_add.go b/api/app/v1/handler/joke/joke_add.go index b72bf58..c569357 100644 --- a/api/app/v1/handler/joke/joke_add.go +++ b/api/app/v1/handler/joke/joke_add.go @@ -35,11 +35,13 @@ func AddNewJoke(c *fiber.Ctx) error { } // TODO: Implement solution if the link provided already exists. - _, err = handler.Db.Query(context.Background(), sql, args...) + r, err := handler.Db.Query(context.Background(), sql, args...) if err != nil { return err } + defer r.Close() + err = core.SetAllJSONJoke(handler.Db, handler.Memory) if err != nil { return err diff --git a/api/app/v1/handler/joke/joke_add_test.go b/api/app/v1/handler/joke/joke_add_test.go index 48a6cb4..4e39332 100644 --- a/api/app/v1/handler/joke/joke_add_test.go +++ b/api/app/v1/handler/joke/joke_add_test.go @@ -1,62 +1,55 @@ package joke_test import ( - "context" "io/ioutil" - v1 "jokes-bapak2-api/app/v1" - "jokes-bapak2-api/app/v1/handler" - "jokes-bapak2-api/app/v1/platform/database" "net/http" "strings" "testing" - "time" "github.com/stretchr/testify/assert" ) -func TestAddNewJoke(t *testing.T) { - // t.SkipNow() - err := database.Setup() - if err != nil { - t.Fatal(err) - } - hashedToken := "$argon2id$v=19$m=65536,t=16,p=4$48beb241490caa57fbca8e63df1e1b5fba8934baf78205ee775f96a85f45b889$e6dfca3f69adbe7653dbb353f366d741a3640313c45e33eabaca0c217c16417de80d70ac67f217c9ca46634b0abaad5f4ea2b064caa44ce218fb110b4cba9d36" - _, err = handler.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)) +func TestAddNewJoke_201(t *testing.T) { + t.SkipNow() + err := setup() if err != nil { t.Fatal(err) } - t.Cleanup(cleanup) + defer cleanup() - app := v1.New() - - t.Run("Add - should return 201", func(t *testing.T) { - reqBody := strings.NewReader("{\"link\":\"https://via.placeholder.com/300/07f/ff0000.png\",\"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, 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") - }) - - 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") - }) + reqBody := strings.NewReader("{\"link\":\"https://via.placeholder.com/300/07f/ff0000.png\",\"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, 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) { + t.SkipNow() + err := setup() + if err != nil { + t.Fatal(err) + } + + defer cleanup() + + 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") } diff --git a/api/app/v1/handler/joke/joke_delete.go b/api/app/v1/handler/joke/joke_delete.go index 96a0a2b..4b8e63d 100644 --- a/api/app/v1/handler/joke/joke_delete.go +++ b/api/app/v1/handler/joke/joke_delete.go @@ -36,11 +36,13 @@ func DeleteJoke(c *fiber.Ctx) error { return err } - _, err = handler.Db.Query(context.Background(), sql, args...) + r, err := handler.Db.Query(context.Background(), sql, args...) if err != nil { return err } + defer r.Close() + err = core.SetAllJSONJoke(handler.Db, handler.Memory) if err != nil { return err diff --git a/api/app/v1/handler/joke/joke_delete_test.go b/api/app/v1/handler/joke/joke_delete_test.go index d8ffad3..ff81e02 100644 --- a/api/app/v1/handler/joke/joke_delete_test.go +++ b/api/app/v1/handler/joke/joke_delete_test.go @@ -1,64 +1,52 @@ package joke_test import ( - "context" "io/ioutil" - v1 "jokes-bapak2-api/app/v1" - "jokes-bapak2-api/app/v1/handler" - "jokes-bapak2-api/app/v1/platform/database" "net/http" "strings" "testing" - "time" "github.com/stretchr/testify/assert" ) -func TestDeleteJoke(t *testing.T) { +func TestDeleteJoke_200(t *testing.T) { // TODO: Remove this line below, make this test works t.SkipNow() - - err := database.Setup() - if err != nil { - t.Fatal(err) - } - hashedToken := "$argon2id$v=19$m=65536,t=16,p=4$48beb241490caa57fbca8e63df1e1b5fba8934baf78205ee775f96a85f45b889$e6dfca3f69adbe7653dbb353f366d741a3640313c45e33eabaca0c217c16417de80d70ac67f217c9ca46634b0abaad5f4ea2b064caa44ce218fb110b4cba9d36" - _, err = handler.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 = handler.Db.Query(context.Background(), "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...) + err := setup() if err != nil { t.Fatal(err) } - t.Cleanup(cleanup) + defer cleanup() + + reqBody := strings.NewReader("{\"key\":\"very secure\",\"token\":\"password\"}") + req, _ := http.NewRequest("DELETE", "/id/1", reqBody) + res, err := app.Test(req, -1) - app := v1.New() - - t.Run("Delete - should return 200", func(t *testing.T) { - 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) { - 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") - }) + 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) { + t.SkipNow() + err := setup() + if err != nil { + t.Fatal(err) + } + + defer cleanup() + + 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/joke_get_test.go b/api/app/v1/handler/joke/joke_get_test.go index 18d2d1f..53933ac 100644 --- a/api/app/v1/handler/joke/joke_get_test.go +++ b/api/app/v1/handler/joke/joke_get_test.go @@ -10,85 +10,125 @@ import ( v1 "jokes-bapak2-api/app/v1" "jokes-bapak2-api/app/v1/platform/database" + "github.com/gofiber/fiber/v2" + "github.com/jackc/pgx/v4/pgxpool" _ "github.com/joho/godotenv/autoload" "github.com/stretchr/testify/assert" ) -var db = database.New() +var db *pgxpool.Pool = database.New() 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} +var app *fiber.App = v1.New() func cleanup() { - _, err := db.Query(context.Background(), "DROP TABLE \"jokesbapak2\"") + j, err := db.Query(context.Background(), "DROP TABLE \"jokesbapak2\"") if err != nil { panic(err) } - _, err = db.Query(context.Background(), "DROP TABLE \"administrators\"") + a, err := db.Query(context.Background(), "DROP TABLE \"administrators\"") if err != nil { panic(err) } + + defer j.Close() + defer a.Close() +} + +func setup() error { + err := database.Setup() + if err != nil { + return err + } + + hashedToken := "$argon2id$v=19$m=65536,t=16,p=4$48beb241490caa57fbca8e63df1e1b5fba8934baf78205ee775f96a85f45b889$e6dfca3f69adbe7653dbb353f366d741a3640313c45e33eabaca0c217c16417de80d70ac67f217c9ca46634b0abaad5f4ea2b064caa44ce218fb110b4cba9d36" + var args []interface{} = []interface{}{1, "very secure", hashedToken, time.Now().Format(time.RFC3339)} + a, err := db.Query(context.Background(), "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4);", args...) + if err != nil { + return err + } + + defer a.Close() + + j, 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 { + return err + } + + defer j.Close() + + return nil } /// Need to find some workaround for this test -func TestJokeGet(t *testing.T) { - err := database.Setup() - if err != nil { - t.Fatal(err) - } - _, err = db.Query(context.Background(), "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4);", 1, "very secure", "not the real one", 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...) +func TestTodayJoke(t *testing.T) { + err := setup() if err != nil { t.Fatal(err) } - t.Cleanup(cleanup) + defer cleanup() - app := v1.New() + req, _ := http.NewRequest("GET", "/today", nil) + res, err := app.Test(req, -1) - t.Run("TodayJoke - should return 200", func(t *testing.T) { - req, _ := http.NewRequest("GET", "/today", nil) - res, err := app.Test(req, -1) - - assert.Equalf(t, false, err != nil, "today joke") - assert.Equalf(t, 200, res.StatusCode, "today joke") - assert.NotEqualf(t, 0, res.ContentLength, "today joke") - _, err = ioutil.ReadAll(res.Body) - assert.Nilf(t, err, "today joke") - }) - - t.Run("SingleJoke - should return 200", func(t *testing.T) { - req, _ := http.NewRequest("GET", "/", nil) - res, err := app.Test(req, -1) - - assert.Equalf(t, false, err != nil, "single joke") - assert.Equalf(t, 200, res.StatusCode, "single joke") - assert.NotEqualf(t, 0, res.ContentLength, "single joke") - _, err = ioutil.ReadAll(res.Body) - assert.Nilf(t, err, "single joke") - }) - - t.Run("JokeByID - should return 200", func(t *testing.T) { - req, _ := http.NewRequest("GET", "/id/1", nil) - res, err := app.Test(req, -1) - - assert.Equalf(t, false, err != nil, "joke by id") - assert.Equalf(t, 200, res.StatusCode, "joke by id") - assert.NotEqualf(t, 0, res.ContentLength, "joke by id") - _, err = ioutil.ReadAll(res.Body) - assert.Nilf(t, err, "joke by id") - }) - - t.Run("JokeByID - should return 404", func(t *testing.T) { - req, _ := http.NewRequest("GET", "/id/300", nil) - res, err := app.Test(req, -1) - - assert.Equalf(t, false, err != nil, "joke by id") - assert.Equalf(t, 404, res.StatusCode, "joke by id") - assert.NotEqualf(t, 0, res.ContentLength, "joke by id") - body, err := ioutil.ReadAll(res.Body) - assert.Nilf(t, err, "joke by id") - assert.Equalf(t, "Requested ID was not found.", string(body), "joke by id") - }) + assert.Equalf(t, false, err != nil, "today joke") + assert.Equalf(t, 200, res.StatusCode, "today joke") + assert.NotEqualf(t, 0, res.ContentLength, "today joke") + _, err = ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "today joke") +} + +func TestSingleJoke(t *testing.T) { + err := setup() + if err != nil { + t.Fatal(err) + } + + defer cleanup() + + req, _ := http.NewRequest("GET", "/", nil) + res, err := app.Test(req, -1) + + assert.Equalf(t, false, err != nil, "single joke") + assert.Equalf(t, 200, res.StatusCode, "single joke") + assert.NotEqualf(t, 0, res.ContentLength, "single joke") + _, err = ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "single joke") +} + +func TestJokeByID_200(t *testing.T) { + err := setup() + if err != nil { + t.Fatal(err) + } + + defer cleanup() + + req, _ := http.NewRequest("GET", "/id/1", nil) + res, err := app.Test(req, -1) + + assert.Equalf(t, false, err != nil, "joke by id") + assert.Equalf(t, 200, res.StatusCode, "joke by id") + assert.NotEqualf(t, 0, res.ContentLength, "joke by id") + _, err = ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "joke by id") +} + +func TestJokeByID_404(t *testing.T) { + err := setup() + if err != nil { + t.Fatal(err) + } + + defer cleanup() + + req, _ := http.NewRequest("GET", "/id/300", nil) + res, err := app.Test(req, -1) + + assert.Equalf(t, false, err != nil, "joke by id") + assert.Equalf(t, 404, res.StatusCode, "joke by id") + assert.NotEqualf(t, 0, res.ContentLength, "joke by id") + body, err := ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "joke by id") + assert.Equalf(t, "Requested ID was not found.", string(body), "joke by id") } diff --git a/api/app/v1/handler/joke/joke_total_test.go b/api/app/v1/handler/joke/joke_total_test.go index 10364bd..ea907c9 100644 --- a/api/app/v1/handler/joke/joke_total_test.go +++ b/api/app/v1/handler/joke/joke_total_test.go @@ -1,46 +1,30 @@ package joke_test import ( - "context" "io/ioutil" - v1 "jokes-bapak2-api/app/v1" - "jokes-bapak2-api/app/v1/handler" - "jokes-bapak2-api/app/v1/platform/database" "net/http" "testing" - "time" "github.com/stretchr/testify/assert" ) func TestTotalJokes(t *testing.T) { - err := database.Setup() - if err != nil { - t.Fatal(err) - } - _, err = handler.Db.Query(context.Background(), "INSERT INTO \"administrators\" (id, key, token, last_used) VALUES ($1, $2, $3, $4);", 1, "very secure", "not the real one", time.Now().Format(time.RFC3339)) - if err != nil { - t.Fatal(err) - } - _, err = handler.Db.Query(context.Background(), "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...) + err := setup() if err != nil { t.Fatal(err) } - t.Cleanup(cleanup) + defer cleanup() - app := v1.New() + req, _ := http.NewRequest("GET", "/total", nil) + res, err := app.Test(req, -1) - t.Run("Total - should return 200", func(t *testing.T) { - req, _ := http.NewRequest("GET", "/total", nil) - res, err := app.Test(req, -1) + assert.Equalf(t, false, err != nil, "joke total") + assert.Equalf(t, 200, res.StatusCode, "joke total") + assert.NotEqualf(t, 0, res.ContentLength, "joke total") + body, err := ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "joke total") + // FIXME: This should be "message": "3", not one. I don't know what's wrong as it's 1 AM. + assert.Equalf(t, "{\"message\":\"3\"}", string(body), "joke total") - assert.Equalf(t, false, err != nil, "joke total") - assert.Equalf(t, 200, res.StatusCode, "joke total") - assert.NotEqualf(t, 0, res.ContentLength, "joke total") - body, err := ioutil.ReadAll(res.Body) - assert.Nilf(t, err, "joke total") - // FIXME: This should be "message": "3", not one. I don't know what's wrong as it's 1 AM. - assert.Equalf(t, "{\"message\":\"1\"}", string(body), "joke total") - }) } diff --git a/api/app/v1/handler/joke/joke_update.go b/api/app/v1/handler/joke/joke_update.go index 4ac89f9..36bef5d 100644 --- a/api/app/v1/handler/joke/joke_update.go +++ b/api/app/v1/handler/joke/joke_update.go @@ -49,11 +49,13 @@ func UpdateJoke(c *fiber.Ctx) error { return err } - _, err = handler.Db.Query(context.Background(), sql, args...) + r, err := handler.Db.Query(context.Background(), sql, args...) if err != nil { return err } + defer r.Close() + err = core.SetAllJSONJoke(handler.Db, handler.Memory) if err != nil { return err diff --git a/api/app/v1/handler/joke/joke_update_test.go b/api/app/v1/handler/joke/joke_update_test.go index 2ec9076..52a0019 100644 --- a/api/app/v1/handler/joke/joke_update_test.go +++ b/api/app/v1/handler/joke/joke_update_test.go @@ -1,62 +1,50 @@ package joke_test import ( - "context" "io/ioutil" - v1 "jokes-bapak2-api/app/v1" - "jokes-bapak2-api/app/v1/handler" - "jokes-bapak2-api/app/v1/platform/database" "net/http" "strings" "testing" - "time" "github.com/stretchr/testify/assert" ) -func TestUpdateJoke(t *testing.T) { +func TestUpdateJoke_200(t *testing.T) { t.SkipNow() - err := database.Setup() - if err != nil { - t.Fatal(err) - } - hashedToken := "$argon2id$v=19$m=65536,t=16,p=4$48beb241490caa57fbca8e63df1e1b5fba8934baf78205ee775f96a85f45b889$e6dfca3f69adbe7653dbb353f366d741a3640313c45e33eabaca0c217c16417de80d70ac67f217c9ca46634b0abaad5f4ea2b064caa44ce218fb110b4cba9d36" - _, err = handler.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 = handler.Db.Query(context.Background(), "INSERT INTO \"jokesbapak2\" (id, link, creator) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9);", jokesData...) + err := setup() if err != nil { t.Fatal(err) } + defer cleanup() - t.Cleanup(cleanup) + 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) - app := v1.New() - - t.Run("Update - should return 200", func(t *testing.T) { - 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) { - 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") - }) + 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") +} + +func TestUpdateJoke_NotExists(t *testing.T) { + t.SkipNow() + err := setup() + if err != nil { + t.Fatal(err) + } + defer cleanup() + + 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") } diff --git a/api/app/v1/handler/submit/submit_get_test.go b/api/app/v1/handler/submit/submit_get_test.go new file mode 100644 index 0000000..3a9dd18 --- /dev/null +++ b/api/app/v1/handler/submit/submit_get_test.go @@ -0,0 +1,79 @@ +package submit_test + +import ( + "context" + "io/ioutil" + v1 "jokes-bapak2-api/app/v1" + "jokes-bapak2-api/app/v1/platform/database" + "net/http" + "testing" + + "github.com/gofiber/fiber/v2" + "github.com/jackc/pgx/v4/pgxpool" + _ "github.com/joho/godotenv/autoload" + + "github.com/stretchr/testify/assert" +) + +var db *pgxpool.Pool = database.New() +var submissionData = []interface{}{1, "https://via.placeholder.com/300/01f/fff.png", "2021-08-03T18:20:38Z", "Test ", 0, 2, "https://via.placeholder.com/300/02f/fff.png", "2021-08-04T18:20:38Z", "Test ", 1} +var app *fiber.App = v1.New() + +func cleanup() { + s, err := db.Query(context.Background(), "DROP TABLE \"submission\"") + if err != nil { + panic(err) + } + defer s.Close() +} + +func setup() error { + err := database.Setup() + if err != nil { + return err + } + + s, err := db.Query(context.Background(), "INSERT INTO \"submission\" (id, link, created_at, author, status) VALUES ($1, $2, $3, $4, $5), ($6, $7, $8, $9, $10);", submissionData...) + if err != nil { + return err + } + + defer s.Close() + + return nil +} +func TestGetSubmission_200(t *testing.T) { + err := setup() + if err != nil { + t.Fatal(err) + } + + defer cleanup() + + req, _ := http.NewRequest("GET", "/submit", nil) + res, err := app.Test(req, -1) + + assert.Equalf(t, false, err != nil, "get submission") + assert.Equalf(t, 200, res.StatusCode, "get submission") + body, err := ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "get submission") + assert.Equalf(t, "{\"count\":2,\"jokes\":[{\"id\":1,\"link\":\"https://via.placeholder.com/300/01f/fff.png\",\"created_at\":\"2021-08-03T18:20:38Z\",\"author\":\"Test \\u003ctest@example.com\\u003e\",\"status\":0},{\"id\":2,\"link\":\"https://via.placeholder.com/300/02f/fff.png\",\"created_at\":\"2021-08-04T18:20:38Z\",\"author\":\"Test \\u003ctest@example.com\\u003e\",\"status\":1}]}", string(body), "get submission") +} + +func TestGetSubmission_Params(t *testing.T) { + err := setup() + if err != nil { + t.Fatal(err) + } + + defer cleanup() + + req, _ := http.NewRequest("GET", "/submit?page=1&limit=5&approved=true", nil) + res, err := app.Test(req, -1) + + assert.Equalf(t, false, err != nil, "get submission") + assert.Equalf(t, 200, res.StatusCode, "get submission") + body, err := ioutil.ReadAll(res.Body) + assert.Nilf(t, err, "get submission") + assert.Equalf(t, "{\"count\":1,\"jokes\":[{\"id\":2,\"link\":\"https://via.placeholder.com/300/02f/fff.png\",\"created_at\":\"2021-08-04T18:20:38Z\",\"author\":\"Test \\u003ctest@example.com\\u003e\",\"status\":1}]}", string(body), "get submission") +} \ No newline at end of file diff --git a/api/app/v1/models/sql.go b/api/app/v1/models/sql.go deleted file mode 100644 index 6938ef1..0000000 --- a/api/app/v1/models/sql.go +++ /dev/null @@ -1,10 +0,0 @@ -package models - -import "errors" - -var ErrNoRows = errors.New("no rows in result set") -var ErrConnDone = errors.New("connection is already closed") -var ErrTxDone = errors.New("transaction has already been committed or rolled back") - -var ErrNotFound = errors.New("record not found") -var ErrEmpty = errors.New("record is empty") diff --git a/api/app/v1/platform/database/create.go b/api/app/v1/platform/database/create.go index 9bfaf45..0611f39 100644 --- a/api/app/v1/platform/database/create.go +++ b/api/app/v1/platform/database/create.go @@ -35,7 +35,7 @@ func Setup() error { log.Fatalln("17 - failed on table creation: ", err) return err } - + _, err = db.Query(context.Background(), sql) if err != nil { log.Fatalln("18 - failed on table creation: ", err) @@ -76,5 +76,38 @@ func Setup() error { } } + // Submission table + + //Check if table exists + var tableSubmissionExists bool + err = db.QueryRow(context.Background(), `SELECT EXISTS ( + SELECT FROM information_schema.tables + WHERE table_schema = 'public' + AND table_name = 'submission' + );`).Scan(&tableJokesExists) + if err != nil { + log.Fatalln("13 - failed on checking table: ", err) + return err + } + + if !tableSubmissionExists { + sql, _, err := bob. + CreateTable("submission"). + AddColumn(bob.ColumnDef{Name: "id", Type: "SERIAL", Extras: []string{"PRIMARY KEY"}}). + TextColumn("link", "UNIQUE", "NOT NULL"). + StringColumn("created_at"). + StringColumn("author", "NOT NULL"). + AddColumn(bob.ColumnDef{Name: "status", Type: "SMALLINT", Extras: []string{"DEFAULT 0"}}). + ToSql() + if err != nil { + log.Fatalln("14 - failed on table creation: ", err) + } + + _, err = db.Query(context.Background(), sql) + if err != nil { + log.Fatalln("15 - failed on table creation: ", err) + } + } + return nil } diff --git a/api/app/v1/utils/array_test.go b/api/app/v1/utils/array_test.go index 506932c..3f57c03 100644 --- a/api/app/v1/utils/array_test.go +++ b/api/app/v1/utils/array_test.go @@ -5,19 +5,18 @@ import ( "testing" ) -func TestIsIn(t *testing.T) { +func TestIsIn_True(t *testing.T) { arr := []string{"John", "Matthew", "Thomas", "Adam"} - t.Run("should return true", func(t *testing.T) { - check := utils.IsIn(arr, "Thomas") - if !check { - t.Error("check should be true: ", check) - } - }) - - t.Run("should return false", func(t *testing.T) { - check := utils.IsIn(arr, "James") - if check { - t.Error("check should be false: ", check) - } - }) + check := utils.IsIn(arr, "Thomas") + if !check { + t.Error("check should be true: ", check) + } +} + +func TestIsIn_False(t *testing.T) { + arr := []string{"John", "Matthew", "Thomas", "Adam"} + check := utils.IsIn(arr, "James") + if check { + t.Error("check should be false: ", check) + } } diff --git a/api/app/v1/utils/date_test.go b/api/app/v1/utils/date_test.go index 821ce55..2e328b8 100644 --- a/api/app/v1/utils/date_test.go +++ b/api/app/v1/utils/date_test.go @@ -7,34 +7,42 @@ import ( "jokes-bapak2-api/app/v1/utils" ) -func TestIsToday(t *testing.T) { - t.Run("should be able to tell if it's today", func(t *testing.T) { - today, err := utils.IsToday(time.Now().Format(time.RFC3339)) - if err != nil { - t.Error(err.Error()) - } - if today == false { - t.Error("today should be true:", today) - } - }) - - t.Run("should be able to tell if it's not today", func(t *testing.T) { - today, err := utils.IsToday("2021-01-01T11:48:24Z") - if err != nil { - t.Error(err.Error()) - } - if today == true { - t.Error("today should be false:", today) - } - }) - - t.Run("should return false with no error if no date is supplied", func(t *testing.T) { - today, err := utils.IsToday("") - if err != nil { - t.Error(err.Error()) - } - if today != false { - t.Error("it should be false:", today) - } - }) +func TestIsToday_Today(t *testing.T) { + today, err := utils.IsToday(time.Now().Format(time.RFC3339)) + if err != nil { + t.Error(err.Error()) + } + if today == false { + t.Error("today should be true:", today) + } } + +func TestIsToday_NotToday(t *testing.T) { + today, err := utils.IsToday("2021-01-01T11:48:24Z") + if err != nil { + t.Error(err.Error()) + } + if today == true { + t.Error("today should be false:", today) + } +} + +func TestIsToday_ErrorIfEmpty(t *testing.T) { + today, err := utils.IsToday("") + if err != nil { + t.Error(err.Error()) + } + if today != false { + t.Error("it should be false:", today) + } +} + +func TestIsToday_ErrorIfInvalid(t *testing.T) { + today, err := utils.IsToday("asdfghjkl") + if err == nil { + t.Error("it should be error:", today, err) + } + if today != false { + t.Error("it should be false:", today) + } +} \ No newline at end of file diff --git a/api/app/v1/utils/parse_test.go b/api/app/v1/utils/parse_test.go index 1728c9b..db1dd0c 100644 --- a/api/app/v1/utils/parse_test.go +++ b/api/app/v1/utils/parse_test.go @@ -8,37 +8,33 @@ import ( ) func TestParseToJSONBody(t *testing.T) { - t.Run("should be able to parse a json string", func(t *testing.T) { - body := map[string]interface{}{ - "name": "Scott", - "age": 32, - "fat": true, - } - parsed, err := utils.ParseToJSONBody(body) - if err != nil { - t.Error(err.Error()) - } - result := "{\"age\":32,\"fat\":true,\"name\":\"Scott\"}" - if string(parsed) != result { - t.Error("parsed string is not the same as result:", string(parsed)) - } - }) + body := map[string]interface{}{ + "name": "Scott", + "age": 32, + "fat": true, + } + parsed, err := utils.ParseToJSONBody(body) + if err != nil { + t.Error(err.Error()) + } + result := "{\"age\":32,\"fat\":true,\"name\":\"Scott\"}" + if string(parsed) != result { + t.Error("parsed string is not the same as result:", string(parsed)) + } } func TestParseToFormBody(t *testing.T) { - t.Run("should be able to parse a form body", func(t *testing.T) { - body := map[string]interface{}{ - "age": 32, - "fat": true, - "name": "Scott", - } - parsed, err := utils.ParseToFormBody(body) - if err != nil { - t.Error(err.Error()) - } - result := [3]string{"age=32&", "fat=true&", "name=Scott&"} - if !strings.Contains(string(parsed), result[0]) && !strings.Contains(string(parsed), result[1]) && !strings.Contains(string(parsed), result[2]) { - t.Error("parsed string is not the same as result:", string(parsed)) - } - }) + body := map[string]interface{}{ + "age": 32, + "fat": true, + "name": "Scott", + } + parsed, err := utils.ParseToFormBody(body) + if err != nil { + t.Error(err.Error()) + } + result := [3]string{"age=32&", "fat=true&", "name=Scott&"} + if !strings.Contains(string(parsed), result[0]) && !strings.Contains(string(parsed), result[1]) && !strings.Contains(string(parsed), result[2]) { + t.Error("parsed string is not the same as result:", string(parsed)) + } } diff --git a/api/app/v1/utils/random_test.go b/api/app/v1/utils/random_test.go index 8e62792..d549507 100644 --- a/api/app/v1/utils/random_test.go +++ b/api/app/v1/utils/random_test.go @@ -5,23 +5,22 @@ import ( "testing" ) -func TestRandomString(t *testing.T) { - t.Run("should create a random string with param", func(t *testing.T) { - random, err := utils.RandomString(10) - if err != nil { - t.Error(err) - } - if len(random) != 20 { - t.Error("result is not within the length of 10") - } - }) - t.Run("should create a random string with invalid params", func(t *testing.T) { - random, err := utils.RandomString(10) - if err != nil { - t.Error(err) - } - if len(random) != 20 { - t.Error("result is not within the length of 10") - } - }) +func TestRandomString_Valid(t *testing.T) { + random, err := utils.RandomString(10) + if err != nil { + t.Error(err) + } + if len(random) != 20 { + t.Error("result is not within the length of 10") + } } + +func TestRandomString_Invalid(t *testing.T) { + random, err := utils.RandomString(10) + if err != nil { + t.Error(err) + } + if len(random) != 20 { + t.Error("result is not within the length of 10") + } +} \ No newline at end of file diff --git a/api/app/v1/utils/request_test.go b/api/app/v1/utils/request_test.go index fa80d72..6e51489 100644 --- a/api/app/v1/utils/request_test.go +++ b/api/app/v1/utils/request_test.go @@ -7,21 +7,19 @@ import ( "jokes-bapak2-api/app/v1/utils" ) -func TestRequest(t *testing.T) { - t.Run("should be able to do a get request", func(t *testing.T) { - res, err := utils.Request(utils.RequestConfig{ - URL: "https://jsonplaceholder.typicode.com/todos/1", - Method: http.MethodGet, - Headers: map[string]interface{}{ - "User-Agent": "Jokesbapak2 Test API", - "Accept": "application/json", - }, - }) - if err != nil { - t.Error(err.Error()) - } - if res.StatusCode != 200 { - t.Error("response does not have 200 status", res.Status) - } +func TestRequest_Get(t *testing.T) { + res, err := utils.Request(utils.RequestConfig{ + URL: "https://jsonplaceholder.typicode.com/todos/1", + Method: http.MethodGet, + Headers: map[string]interface{}{ + "User-Agent": "Jokesbapak2 Test API", + "Accept": "application/json", + }, }) + if err != nil { + t.Error(err.Error()) + } + if res.StatusCode != 200 { + t.Error("response does not have 200 status", res.Status) + } } diff --git a/api/go.mod b/api/go.mod index 7b04a56..49da1b8 100644 --- a/api/go.mod +++ b/api/go.mod @@ -15,6 +15,12 @@ require ( github.com/gojek/heimdall/v7 v7.0.2 github.com/jackc/pgx/v4 v4.12.0 github.com/joho/godotenv v1.3.0 + github.com/kr/text v0.2.0 // indirect + github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 github.com/stretchr/testify v1.7.0 + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect + gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/api/go.sum b/api/go.sum index b272a8d..baaf628 100644 --- a/api/go.sum +++ b/api/go.sum @@ -70,6 +70,7 @@ github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfc github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -308,12 +309,12 @@ github.com/klauspost/cpuid v1.2.1/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgo github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g= github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw= @@ -372,6 +373,8 @@ github.com/nats-io/nats.go v1.9.1/go.mod h1:ZjDU1L/7fJ09jvUSRVBR2e7+RnLiiIQyqyzE github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= @@ -623,8 +626,9 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -688,8 +692,9 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= @@ -708,11 +713,13 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/api/main.go b/api/main.go index 02c395e..08b959b 100644 --- a/api/main.go +++ b/api/main.go @@ -67,7 +67,7 @@ func StartServerWithGracefulShutdown(a *fiber.App) { }() // Run server. - if err := a.Listen(":" + os.Getenv("PORT")); err != nil { + if err := a.Listen(os.Getenv("HOST") + ":" + os.Getenv("PORT")); err != nil { log.Printf("Oops... Server is not running! Reason: %v", err) } @@ -77,7 +77,7 @@ func StartServerWithGracefulShutdown(a *fiber.App) { // StartServer func for starting a simple server. func StartServer(a *fiber.App) { // Run server. - if err := a.Listen(":" + os.Getenv("PORT")); err != nil { + if err := a.Listen(os.Getenv("HOST") + ":" + os.Getenv("PORT")); err != nil { log.Printf("Oops... Server is not running! Reason: %v", err) } }