mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-28 09:41:02 +00:00
152 lines
3.2 KiB
Go
152 lines
3.2 KiB
Go
package internal_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/avast/retry-go"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"go.salty.im/saltyim/internal"
|
|
)
|
|
|
|
var (
|
|
serverBind = ":61234"
|
|
serverBaseURL = "http://localhost:61234"
|
|
serverPrimaryDomain = "localhost"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
data, err := os.MkdirTemp("", "data*")
|
|
if err != nil {
|
|
fmt.Printf("error creating data dir: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer os.RemoveAll(data)
|
|
|
|
svr, err := internal.NewServer(serverBind,
|
|
// Debug mode
|
|
internal.WithDebug(true),
|
|
|
|
// TLS options
|
|
internal.WithTLS(false),
|
|
internal.WithTLSKey(""),
|
|
internal.WithTLSCert(""),
|
|
|
|
// Basic options
|
|
internal.WithData(data),
|
|
internal.WithStore("memory://"),
|
|
internal.WithBaseURL(serverBaseURL),
|
|
internal.WithPrimaryDomain(serverPrimaryDomain),
|
|
|
|
// Oeprator
|
|
internal.WithAdminUser("admin@localhost"),
|
|
internal.WithSupportEmail("support@localhost"),
|
|
)
|
|
if err != nil {
|
|
fmt.Printf("error creating server: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
stop := make(chan struct{})
|
|
|
|
wg, ctx := errgroup.WithContext(context.Background())
|
|
|
|
wg.Go(func() error {
|
|
return svr.Run(ctx)
|
|
})
|
|
|
|
wg.Go(func() error {
|
|
<-stop
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
|
|
return svr.Shutdown(ctx)
|
|
})
|
|
|
|
if err := retry.Do(func() error {
|
|
_, err := http.Get(serverBaseURL + "/api/v1/ping")
|
|
return err
|
|
},
|
|
retry.LastErrorOnly(true),
|
|
retry.OnRetry(func(n uint, err error) {
|
|
fmt.Printf("waiting for server to be ready (try #%d): %s", n, err)
|
|
}),
|
|
); err != nil {
|
|
fmt.Printf("error waiting for server: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
i := m.Run()
|
|
|
|
close(stop)
|
|
|
|
go func() {
|
|
<-time.After(3 * time.Second)
|
|
fmt.Println("KILLING IT ALL!! Service failed to shutdown on time!!")
|
|
os.Exit(0)
|
|
}()
|
|
|
|
err = wg.Wait()
|
|
fmt.Println(err)
|
|
|
|
os.Exit(i)
|
|
}
|
|
|
|
func TestBlobPostNoAuthentication(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, serverBaseURL+"/api/v1/blob/test", nil)
|
|
assert.NoError(err)
|
|
res, err := http.DefaultClient.Do(req)
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(http.StatusUnauthorized, res.StatusCode)
|
|
}
|
|
|
|
func TestPing(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, serverBaseURL+"/api/v1/ping", nil)
|
|
assert.NoError(err)
|
|
res, err := http.DefaultClient.Do(req)
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(http.StatusOK, res.StatusCode)
|
|
b, err := io.ReadAll(res.Body)
|
|
assert.NoError(err)
|
|
assert.Equal("{}", string(b))
|
|
}
|
|
|
|
func TestBlobPut(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
cli := internal.NewTestUser("alice@localhost", serverBaseURL, t)
|
|
|
|
body := []byte("Hello World!")
|
|
_, err := cli.Request(http.MethodPut, serverBaseURL+"/api/v1/blob/test", body)
|
|
require.NoError(err)
|
|
}
|
|
|
|
func TestBlobGet(t *testing.T) {
|
|
assert := assert.New(t)
|
|
require := require.New(t)
|
|
|
|
cli := internal.NewTestUser("alice@localhost", serverBaseURL, t)
|
|
|
|
body := []byte("Hello World!")
|
|
_, err := cli.Request(http.MethodPut, serverBaseURL+"/api/v1/blob/test", body)
|
|
require.NoError(err)
|
|
|
|
data, err := cli.Request(http.MethodGet, serverBaseURL+"/api/v1/blob/test", nil)
|
|
require.NoError(err)
|
|
assert.Equal(body, data)
|
|
}
|