test: refactor & clean up

This commit is contained in:
Reinaldy Rafli 2021-08-04 17:05:58 +07:00
parent 98c15d2e6d
commit 79a8dccd9c
5 changed files with 26 additions and 10 deletions

View File

@ -10,6 +10,7 @@ import (
)
func TestAddNewJoke_201(t *testing.T) {
// TODO: Remove this line below, make this test works
t.SkipNow()
err := setup()
if err != nil {
@ -18,7 +19,7 @@ func TestAddNewJoke_201(t *testing.T) {
defer cleanup()
reqBody := strings.NewReader("{\"link\":\"https://via.placeholder.com/300/07f/ff0000.png\",\"key\":\"very secure\",\"token\":\"password\"}")
reqBody := strings.NewReader("{\"link\":\"https://via.placeholder.com/300/07f/ff0000.png\",\"key\":\"test\",\"token\":\"password\"}")
req, _ := http.NewRequest("PUT", "/", reqBody)
req.Header.Set("content-type", "application/json")
req.Header.Add("accept", "application/json")
@ -33,6 +34,7 @@ func TestAddNewJoke_201(t *testing.T) {
}
func TestAddNewJoke_NotValidImage(t *testing.T) {
// TODO: Remove this line below, make this test works
t.SkipNow()
err := setup()
if err != nil {
@ -41,7 +43,7 @@ func TestAddNewJoke_NotValidImage(t *testing.T) {
defer cleanup()
reqBody := strings.NewReader("{\"link\":\"https://google.com/\",\"key\":\"very secure\",\"token\":\"password\"}")
reqBody := strings.NewReader("{\"link\":\"https://google.com/\",\"key\":\"test\",\"token\":\"password\"}")
req, _ := http.NewRequest("PUT", "/", reqBody)
req.Header.Set("content-type", "application/json")
req.Header.Add("accept", "application/json")

View File

@ -1,6 +1,7 @@
package joke_test
import (
"context"
"io/ioutil"
"net/http"
"strings"
@ -17,10 +18,18 @@ func TestDeleteJoke_200(t *testing.T) {
t.Fatal(err)
}
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()
defer cleanup()
reqBody := strings.NewReader("{\"key\":\"very secure\",\"token\":\"password\"}")
req, _ := http.NewRequest("DELETE", "/id/1", reqBody)
req, _ := http.NewRequest("DELETE", "/id/100", reqBody)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
res, err := app.Test(req, -1)
assert.Equalf(t, false, err != nil, "joke delete")
@ -31,6 +40,7 @@ func TestDeleteJoke_200(t *testing.T) {
assert.Equalf(t, "{\"message\":\"specified joke id has been deleted\"}", string(body), "joke delete")
}
func TestDeleteJoke_NotExists(t *testing.T) {
// TODO: Remove this line below, make this test works
t.SkipNow()
err := setup()
if err != nil {
@ -41,6 +51,8 @@ func TestDeleteJoke_NotExists(t *testing.T) {
reqBody := strings.NewReader("{\"key\":\"very secure\",\"token\":\"password\"}")
req, _ := http.NewRequest("DELETE", "/id/100", reqBody)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
res, err := app.Test(req, -1)
assert.Equalf(t, false, err != nil, "joke delete")

View File

@ -5,7 +5,6 @@ import (
"io/ioutil"
"net/http"
"testing"
"time"
v1 "jokes-bapak2-api/app/v1"
"jokes-bapak2-api/app/v1/platform/database"
@ -40,9 +39,7 @@ func setup() error {
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...)
a, err := db.Query(context.Background(), "INSERT INTO \"administrators\" (\"id\", \"key\", \"token\", \"last_used\") VALUES (1, 'test', '$argon2id$v=19$m=65536,t=16,p=4$3a08c79fbf2222467a623df9a9ebf75802c65a4f9be36eb1df2f5d2052d53cb7$ce434bd38f7ba1fc1f2eb773afb8a1f7f2dad49140803ac6cb9d7256ce9826fb3b4afa1e2488da511c852fc6c33a76d5657eba6298a8e49d617b9972645b7106', '');")
if err != nil {
return err
}

View File

@ -17,8 +17,10 @@ func TestUpdateJoke_200(t *testing.T) {
}
defer cleanup()
reqBody := strings.NewReader("{\"link\":\"https://picsum.photos/id/9/200/300\",\"key\":\"very secure\",\"token\":\"password\"}")
reqBody := strings.NewReader("{\"link\":\"https://picsum.photos/id/9/200/300\",\"key\":\"test\",\"token\":\"password\"}")
req, _ := http.NewRequest("PATCH", "/id/1", reqBody)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
res, err := app.Test(req, -1)
assert.Equalf(t, false, err != nil, "joke update")
@ -30,6 +32,7 @@ func TestUpdateJoke_200(t *testing.T) {
}
func TestUpdateJoke_NotExists(t *testing.T) {
// TODO: Remove this line below, make this test works
t.SkipNow()
err := setup()
if err != nil {
@ -37,8 +40,10 @@ func TestUpdateJoke_NotExists(t *testing.T) {
}
defer cleanup()
reqBody := strings.NewReader("{\"link\":\"https://picsum.photos/id/9/200/300\",\"key\":\"very secure\",\"token\":\"password\"}")
reqBody := strings.NewReader("{\"link\":\"https://picsum.photos/id/9/200/300\",\"key\":\"test\",\"token\":\"password\"}")
req, _ := http.NewRequest("PATCH", "/id/100", reqBody)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
res, err := app.Test(req, -1)
assert.Equalf(t, false, err != nil, "joke update")

View File

@ -16,7 +16,7 @@ func TestRandomString_Valid(t *testing.T) {
}
func TestRandomString_Invalid(t *testing.T) {
random, err := utils.RandomString(10)
random, err := utils.RandomString(-10)
if err != nil {
t.Error(err)
}