2020-04-02 19:14:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2020-11-15 15:52:03 +00:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2020-04-02 19:14:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupFakeDB(t *testing.T) {
|
|
|
|
var err error
|
2020-11-15 15:52:03 +00:00
|
|
|
appDb, err = sql.Open("sqlite3", "file::memory:")
|
2020-04-02 19:14:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
migrateDatabase()
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeFakeDB(t *testing.T) {
|
2020-11-15 15:52:03 +00:00
|
|
|
err := appDb.Close()
|
2020-04-02 19:14:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_slugExists(t *testing.T) {
|
|
|
|
t.Run("Test slugs", func(t *testing.T) {
|
|
|
|
setupFakeDB(t)
|
2020-11-15 15:52:03 +00:00
|
|
|
if exists, err := slugExists("source"); err != nil || exists == false {
|
2020-04-02 19:14:18 +00:00
|
|
|
t.Error("Wrong slug existence")
|
|
|
|
}
|
2020-11-15 15:52:03 +00:00
|
|
|
if exists, err := slugExists("test"); err != nil || exists == true {
|
2020-04-02 19:14:18 +00:00
|
|
|
t.Error("Wrong slug existence")
|
|
|
|
}
|
|
|
|
closeFakeDB(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_generateSlug(t *testing.T) {
|
|
|
|
t.Run("Test slug generation", func(t *testing.T) {
|
|
|
|
if slug := generateSlug(); len(slug) != 6 {
|
|
|
|
t.Error("Wrong slug length")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShortenedUrlHandler(t *testing.T) {
|
|
|
|
viper.Set("defaultUrl", "http://long.example.com")
|
|
|
|
t.Run("Test ShortenedUrlHandler", func(t *testing.T) {
|
|
|
|
setupFakeDB(t)
|
2020-11-15 15:52:03 +00:00
|
|
|
initRouter()
|
2020-04-02 19:14:18 +00:00
|
|
|
t.Run("Test redirect code", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/source", nil)
|
|
|
|
w := httptest.NewRecorder()
|
2020-11-15 15:52:03 +00:00
|
|
|
appRouter.ServeHTTP(w, req)
|
2020-04-02 19:14:18 +00:00
|
|
|
resp := w.Result()
|
|
|
|
if resp.StatusCode != http.StatusTemporaryRedirect {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Test redirect location header", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/source", nil)
|
|
|
|
w := httptest.NewRecorder()
|
2020-11-15 15:52:03 +00:00
|
|
|
appRouter.ServeHTTP(w, req)
|
2020-04-02 19:14:18 +00:00
|
|
|
resp := w.Result()
|
2020-04-16 08:55:30 +00:00
|
|
|
if resp.Header.Get("Location") != "https://git.jlel.se/jlelse/GoShort" {
|
2020-04-02 19:14:18 +00:00
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Test missing slug redirect code", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test", nil)
|
|
|
|
w := httptest.NewRecorder()
|
2020-11-15 15:52:03 +00:00
|
|
|
appRouter.ServeHTTP(w, req)
|
2020-04-02 19:14:18 +00:00
|
|
|
resp := w.Result()
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Test no slug mux var", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/", nil)
|
|
|
|
w := httptest.NewRecorder()
|
2020-11-15 15:52:03 +00:00
|
|
|
appRouter.ServeHTTP(w, req)
|
2020-04-02 19:14:18 +00:00
|
|
|
resp := w.Result()
|
|
|
|
if resp.StatusCode != http.StatusTemporaryRedirect {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
if resp.Header.Get("Location") != "http://long.example.com" {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
closeFakeDB(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_checkPassword(t *testing.T) {
|
|
|
|
viper.Set("password", "abc")
|
|
|
|
t.Run("No password", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test", nil)
|
|
|
|
_ = req.ParseForm()
|
|
|
|
if checkPassword(httptest.NewRecorder(), req) != false {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Password via query", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test?password=abc", nil)
|
|
|
|
_ = req.ParseForm()
|
|
|
|
if checkPassword(httptest.NewRecorder(), req) != true {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Wrong password via query", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test?password=wrong", nil)
|
|
|
|
_ = req.ParseForm()
|
|
|
|
if checkPassword(httptest.NewRecorder(), req) != false {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Password via BasicAuth", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test", nil)
|
|
|
|
req.SetBasicAuth("username", "abc")
|
|
|
|
if checkPassword(httptest.NewRecorder(), req) != true {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("Wrong password via BasicAuth", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test", nil)
|
|
|
|
req.SetBasicAuth("username", "wrong")
|
|
|
|
if checkPassword(httptest.NewRecorder(), req) != false {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|