2022-01-02 05:48:20 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go.jolheiser.com/cabinet"
|
2022-01-02 21:26:00 +00:00
|
|
|
"go.jolheiser.com/cabinet/internal/workspace"
|
2022-01-02 05:48:20 +00:00
|
|
|
"go.jolheiser.com/cabinet/internal/workspace/mock"
|
2022-01-02 21:26:00 +00:00
|
|
|
|
|
|
|
"github.com/matryer/is"
|
|
|
|
"github.com/rs/zerolog"
|
2022-01-02 05:48:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRouter(t *testing.T) {
|
2022-01-02 21:26:00 +00:00
|
|
|
zerolog.SetGlobalLevel(zerolog.Disabled)
|
2022-01-02 05:48:20 +00:00
|
|
|
|
|
|
|
m := mock.New()
|
|
|
|
server := httptest.NewUnstartedServer(nil)
|
|
|
|
server.Start()
|
|
|
|
defer server.Close()
|
|
|
|
r := New(server.URL, m, NewLimit(12, 5*1024*1024, 100*1024*1024, 1024*1024))
|
|
|
|
server.Config.Handler = r
|
|
|
|
|
|
|
|
c := cabinet.New(server.URL, cabinet.WithHTTPClient(server.Client()))
|
|
|
|
|
2022-01-02 21:26:00 +00:00
|
|
|
testRouter(t, "No Token", true, true, c)
|
|
|
|
|
|
|
|
// All token
|
|
|
|
aToken := workspace.Token{Key: "all", Permission: workspace.TokenFile | workspace.TokenRedirect}
|
|
|
|
_ = m.AddToken(aToken)
|
|
|
|
testRouter(t, "Incorrect Token", false, false, c)
|
|
|
|
c.Token = aToken.Key
|
|
|
|
testRouter(t, "All Token", true, true, c)
|
|
|
|
|
|
|
|
// Redirect token
|
|
|
|
rToken := workspace.Token{Key: "redirect", Permission: workspace.TokenRedirect}
|
|
|
|
_ = m.AddToken(rToken)
|
|
|
|
c.Token = rToken.Key
|
|
|
|
testRouter(t, "Redirect Token", true, false, c)
|
|
|
|
|
|
|
|
// File token
|
|
|
|
fToken := workspace.Token{Key: "file", Permission: workspace.TokenFile}
|
|
|
|
_ = m.AddToken(fToken)
|
|
|
|
c.Token = fToken.Key
|
|
|
|
testRouter(t, "File Token", false, true, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRouter(t *testing.T, name string, rSucceed, fSucceed bool, c *cabinet.Client) {
|
2022-01-02 05:48:20 +00:00
|
|
|
// Redirect
|
2022-01-02 21:26:00 +00:00
|
|
|
t.Run(name+" Redirect", func(t *testing.T) {
|
|
|
|
assert := is.New(t)
|
|
|
|
redir := "https://duckduckgo.com"
|
|
|
|
u, res, err := c.Redirect(redir)
|
|
|
|
switch {
|
|
|
|
case rSucceed:
|
|
|
|
assert.NoErr(err) // Creating a redirect should succeed
|
|
|
|
resp, err := http.Get(u)
|
|
|
|
assert.NoErr(err)
|
|
|
|
assert.Equal(redir, resp.Request.URL.String()) // The redirect should match what was given
|
|
|
|
case c.Token == "":
|
|
|
|
assert.Equal(res.StatusCode, http.StatusUnauthorized) // Creating a redirect should be unauthorized
|
|
|
|
case c.Token != "":
|
|
|
|
assert.Equal(res.StatusCode, http.StatusForbidden) // Creating a redirect should be forbidden
|
|
|
|
default:
|
|
|
|
assert.Fail() // Unknown case in test
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// File
|
|
|
|
t.Run(name+" File", func(t *testing.T) {
|
|
|
|
assert := is.New(t)
|
|
|
|
file := "foobar"
|
|
|
|
f, res, err := c.File("test.txt", strings.NewReader(file))
|
|
|
|
switch {
|
|
|
|
case fSucceed:
|
|
|
|
assert.NoErr(err) // Creating a file should succeed
|
|
|
|
resp, err := http.Get(f)
|
|
|
|
assert.NoErr(err)
|
|
|
|
b, err := io.ReadAll(resp.Body)
|
|
|
|
assert.NoErr(err)
|
|
|
|
assert.Equal(file, string(b)) // The file should match what was given
|
|
|
|
case c.Token == "":
|
|
|
|
assert.Equal(res.StatusCode, http.StatusUnauthorized) // Creating a redirect should be unauthorized
|
|
|
|
case c.Token != "":
|
|
|
|
assert.Equal(res.StatusCode, http.StatusForbidden) // Creating a redirect should be forbidden
|
|
|
|
default:
|
|
|
|
assert.Fail() // Unknown case in test
|
|
|
|
}
|
|
|
|
})
|
2022-01-02 05:48:20 +00:00
|
|
|
}
|