2020-04-02 19:14:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-11 08:09:22 +00:00
|
|
|
"io"
|
2020-04-02 19:14:18 +00:00
|
|
|
"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
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-08-11 08:09:22 +00:00
|
|
|
func testApp(t *testing.T) *app {
|
|
|
|
app := &app{
|
|
|
|
config: &config{
|
|
|
|
DBPath: filepath.Join(t.TempDir(), "data.db"),
|
|
|
|
},
|
2020-04-02 19:14:18 +00:00
|
|
|
}
|
2021-08-11 08:09:22 +00:00
|
|
|
err := app.openDatabase()
|
|
|
|
require.NoError(t, err)
|
|
|
|
return app
|
2020-04-02 19:14:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 08:09:22 +00:00
|
|
|
func closeTestApp(_ *testing.T, app *app) {
|
|
|
|
app.shutdown.ShutdownAndWait()
|
2020-04-02 19:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_slugExists(t *testing.T) {
|
|
|
|
t.Run("Test slugs", func(t *testing.T) {
|
2021-08-11 08:09:22 +00:00
|
|
|
app := testApp(t)
|
2021-08-10 20:34:25 +00:00
|
|
|
|
2021-08-11 08:09:22 +00:00
|
|
|
exists, err := app.slugExists("source")
|
2021-08-10 20:34:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, exists)
|
2021-08-11 08:09:22 +00:00
|
|
|
exists, err = app.slugExists("test")
|
2021-08-10 20:34:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, exists)
|
|
|
|
|
2021-08-11 08:09:22 +00:00
|
|
|
closeTestApp(t, app)
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
t.Run("Test ShortenedUrlHandler", func(t *testing.T) {
|
2021-08-11 08:09:22 +00:00
|
|
|
app := testApp(t)
|
|
|
|
|
|
|
|
app.config.DefaultUrl = "http://long.example.com"
|
|
|
|
|
|
|
|
router := app.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()
|
2021-08-11 08:09:22 +00:00
|
|
|
router.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
|
|
|
})
|
2021-08-11 08:09:22 +00:00
|
|
|
t.Run("Test default redirect location header", func(t *testing.T) {
|
2020-04-02 19:14:18 +00:00
|
|
|
req := httptest.NewRequest("GET", "http://example.com/source", nil)
|
|
|
|
w := httptest.NewRecorder()
|
2021-08-11 08:09:22 +00:00
|
|
|
router.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()
|
2021-08-11 08:09:22 +00:00
|
|
|
router.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()
|
2021-08-11 08:09:22 +00:00
|
|
|
router.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
|
|
|
})
|
2021-08-11 08:09:22 +00:00
|
|
|
t.Run("Test custom url redirect", func(t *testing.T) {
|
|
|
|
err := app.insertRedirect("customurl", "https://example.net", typUrl)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/customurl", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
resp := w.Result()
|
|
|
|
|
|
|
|
assert.Equal(t, "https://example.net", resp.Header.Get("Location"))
|
|
|
|
})
|
|
|
|
t.Run("Test custom text", func(t *testing.T) {
|
|
|
|
err := app.insertRedirect("customtext", "Hello!", typText)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/customtext", nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
resp := w.Result()
|
|
|
|
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
|
|
|
|
assert.Equal(t, "Hello!", string(respBody))
|
|
|
|
})
|
|
|
|
|
|
|
|
closeTestApp(t, app)
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_checkPassword(t *testing.T) {
|
2021-08-11 08:09:22 +00:00
|
|
|
app := testApp(t)
|
|
|
|
|
|
|
|
app.config.Password = "abc"
|
|
|
|
|
2020-04-02 19:14:18 +00:00
|
|
|
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
|
|
|
|
2021-08-11 08:09:22 +00:00
|
|
|
assert.False(t, app.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
|
|
|
|
2021-08-11 08:09:22 +00:00
|
|
|
assert.True(t, app.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
|
|
|
|
2021-08-11 08:09:22 +00:00
|
|
|
assert.False(t, app.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
|
|
|
|
2021-08-11 08:09:22 +00:00
|
|
|
assert.True(t, app.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
|
|
|
|
2021-08-11 08:09:22 +00:00
|
|
|
assert.False(t, app.checkPassword(httptest.NewRecorder(), req))
|
2020-04-02 19:14:18 +00:00
|
|
|
})
|
2021-08-11 08:09:22 +00:00
|
|
|
|
|
|
|
t.Run("Test login middleware success", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test", nil)
|
|
|
|
req.SetBasicAuth("username", "abc")
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
app.loginMiddleware(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusNotModified)
|
|
|
|
})).ServeHTTP(w, req)
|
|
|
|
resp := w.Result()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotModified, resp.StatusCode)
|
|
|
|
})
|
|
|
|
t.Run("Test login middleware fail", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest("GET", "http://example.com/test", nil)
|
|
|
|
req.SetBasicAuth("username", "xyz")
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
app.loginMiddleware(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusNotModified)
|
|
|
|
})).ServeHTTP(w, req)
|
|
|
|
resp := w.Result()
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
|
|
|
})
|
|
|
|
|
|
|
|
closeTestApp(t, app)
|
2020-04-02 19:14:18 +00:00
|
|
|
}
|