2023-05-29 10:58:25 -06:00
|
|
|
//go:build ignore
|
2023-01-25 10:35:09 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ed25519"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/matryer/is"
|
2023-02-26 22:33:01 -07:00
|
|
|
"go.sour.is/ev/app/webfinger"
|
|
|
|
"go.sour.is/ev/pkg/service"
|
2023-01-25 10:35:09 -07:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-03-19 08:31:00 -06:00
|
|
|
os.Setenv("EV_DATA", "mem:")
|
2023-01-25 10:35:09 -07:00
|
|
|
os.Setenv("EV_HTTP", "[::1]:61234")
|
2023-04-02 21:00:22 -06:00
|
|
|
os.Setenv("WEBFINGER_DOMAINS", "sour.is")
|
2023-01-25 10:35:09 -07:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2023-01-28 10:35:46 -07:00
|
|
|
running := make(chan struct{})
|
|
|
|
apps.Register(99, func(ctx context.Context, s *service.Harness) error {
|
|
|
|
go func() {
|
|
|
|
<-s.OnRunning()
|
|
|
|
close(running)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2023-01-25 10:35:09 -07:00
|
|
|
wg, ctx := errgroup.WithContext(ctx)
|
|
|
|
wg.Go(func() error {
|
|
|
|
// Run application
|
2023-01-28 10:35:46 -07:00
|
|
|
if err := run(ctx); err != nil {
|
2023-01-25 10:35:09 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
wg.Go(func() error {
|
2023-01-28 10:35:46 -07:00
|
|
|
<-running
|
2023-01-25 10:35:09 -07:00
|
|
|
m.Run()
|
|
|
|
cancel()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := wg.Wait(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 09:48:20 -06:00
|
|
|
func TestE2EGetHTTP(t *testing.T) {
|
2023-01-25 10:35:09 -07:00
|
|
|
is := is.New(t)
|
|
|
|
res, err := http.DefaultClient.Get("http://[::1]:61234/.well-known/webfinger")
|
|
|
|
is.NoErr(err)
|
|
|
|
is.Equal(res.StatusCode, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
2023-05-29 09:48:20 -06:00
|
|
|
func TestE2ECreateResource(t *testing.T) {
|
2023-01-25 10:35:09 -07:00
|
|
|
is := is.New(t)
|
|
|
|
|
|
|
|
_, priv, err := ed25519.GenerateKey(nil)
|
|
|
|
is.NoErr(err)
|
|
|
|
|
|
|
|
jrd := &webfinger.JRD{
|
2023-04-02 21:00:22 -06:00
|
|
|
Subject: "acct:me@sour.is",
|
2023-01-25 10:35:09 -07:00
|
|
|
Properties: map[string]*string{
|
|
|
|
"foo": ptr("bar"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// create
|
|
|
|
token, err := webfinger.NewSignedRequest(jrd, priv)
|
|
|
|
is.NoErr(err)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPut, "http://[::1]:61234/.well-known/webfinger", strings.NewReader(token))
|
|
|
|
is.NoErr(err)
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
is.NoErr(err)
|
|
|
|
|
|
|
|
is.Equal(res.StatusCode, http.StatusCreated)
|
|
|
|
|
|
|
|
// repeat
|
|
|
|
req, err = http.NewRequest(http.MethodPut, "http://[::1]:61234/.well-known/webfinger", strings.NewReader(token))
|
|
|
|
is.NoErr(err)
|
|
|
|
|
|
|
|
res, err = http.DefaultClient.Do(req)
|
|
|
|
is.NoErr(err)
|
|
|
|
|
|
|
|
is.Equal(res.StatusCode, http.StatusAlreadyReported)
|
|
|
|
|
|
|
|
// fetch
|
2023-04-02 21:00:22 -06:00
|
|
|
req, err = http.NewRequest(http.MethodGet, "http://[::1]:61234/.well-known/webfinger?resource=acct:me@sour.is", nil)
|
2023-01-25 10:35:09 -07:00
|
|
|
is.NoErr(err)
|
|
|
|
|
|
|
|
res, err = http.DefaultClient.Do(req)
|
|
|
|
is.NoErr(err)
|
|
|
|
|
|
|
|
is.Equal(res.StatusCode, http.StatusOK)
|
|
|
|
|
|
|
|
resJRD := &webfinger.JRD{}
|
|
|
|
err = json.NewDecoder(res.Body).Decode(resJRD)
|
|
|
|
is.NoErr(err)
|
|
|
|
is.Equal(jrd.Subject, resJRD.Subject)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ptr[T any](t T) *T { return &t }
|