2020-04-02 19:14:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-08-10 20:39:31 +00:00
|
|
|
"path/filepath"
|
2020-04-02 19:14:18 +00:00
|
|
|
"testing"
|
2020-11-15 15:52:03 +00:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
2021-08-10 20:34:25 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-02 19:14:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func setupFakeDB(t *testing.T) {
|
|
|
|
var err error
|
2021-08-10 20:39:31 +00:00
|
|
|
appDb, err = sql.Open("sqlite3", filepath.Join(t.TempDir(), "data.db")+"?cache=shared&mode=rwc&_journal_mode=WAL&_busy_timeout=100")
|
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()
|
2021-08-10 20:34:25 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-02 19:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_slugExists(t *testing.T) {
|
|
|
|
t.Run("Test slugs", func(t *testing.T) {
|
|
|
|
setupFakeDB(t)
|
2021-08-10 20:34:25 +00:00
|
|
|
|
|
|
|
exists, err := slugExists("source")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, exists)
|
|
|
|
exists, err = slugExists("test")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, exists)
|
|
|
|
|
2020-04-02 19:14:18 +00:00
|
|
|
closeFakeDB(t)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_generateSlug(t *testing.T) {
|
|
|
|
t.Run("Test slug generation", func(t *testing.T) {
|
2021-08-10 20:34:25 +00:00
|
|
|
assert.Len(t, generateSlug(), 6)
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2021-08-10 20:34:25 +00:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, resp.StatusCode)
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
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()
|
2021-08-10 20:34:25 +00:00
|
|
|
|
|
|
|
assert.Equal(t, "https://git.jlel.se/jlelse/GoShort", resp.Header.Get("Location"))
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
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()
|
2021-08-10 20:34:25 +00:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
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()
|
2021-08-10 20:34:25 +00:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, resp.StatusCode)
|
|
|
|
assert.Equal(t, "http://long.example.com", resp.Header.Get("Location"))
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
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)
|
2021-08-10 20:34:25 +00:00
|
|
|
|
|
|
|
assert.False(t, checkPassword(httptest.NewRecorder(), req))
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
t.Run("Password via query", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test?password=abc", nil)
|
2021-08-10 20:34:25 +00:00
|
|
|
|
|
|
|
assert.True(t, checkPassword(httptest.NewRecorder(), req))
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
t.Run("Wrong password via query", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test?password=wrong", nil)
|
2021-08-10 20:34:25 +00:00
|
|
|
|
|
|
|
assert.False(t, checkPassword(httptest.NewRecorder(), req))
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
t.Run("Password via BasicAuth", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test", nil)
|
|
|
|
req.SetBasicAuth("username", "abc")
|
2021-08-10 20:34:25 +00:00
|
|
|
|
|
|
|
assert.True(t, checkPassword(httptest.NewRecorder(), req))
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
t.Run("Wrong password via BasicAuth", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test", nil)
|
|
|
|
req.SetBasicAuth("username", "wrong")
|
2021-08-10 20:34:25 +00:00
|
|
|
|
|
|
|
assert.False(t, checkPassword(httptest.NewRecorder(), req))
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
}
|