diff --git a/user/echo.go b/user/echo.go index c2c07b3..0be75cf 100644 --- a/user/echo.go +++ b/user/echo.go @@ -8,6 +8,20 @@ import ( "github.com/pkg/errors" ) +// NewEcho creates a signed user@echo (for testing). +func NewEcho(sk *keys.EdX25519Key, name string, seq int) (*User, error) { + usr, err := NewForSigning(sk.ID(), "echo", name) + if err != nil { + return nil, err + } + msg, err := usr.Sign(sk) + if err != nil { + return nil, err + } + urs := "test://echo/alice/" + sk.ID().String() + "/" + url.QueryEscape(strings.ReplaceAll(msg, "\n", " ")) + return New(sk.ID(), "echo", name, urs, seq) +} + func echoRequest(ur *url.URL) ([]byte, error) { if ur.Scheme != "test" { return nil, errors.Errorf("invalid scheme for echo") diff --git a/user/echo_test.go b/user/echo_test.go index 4e125a5..6954f0d 100644 --- a/user/echo_test.go +++ b/user/echo_test.go @@ -110,22 +110,3 @@ func TestRequestVerifyEcho(t *testing.T) { t.Logf("result: %+v", result) require.Equal(t, user.StatusOK, result.Status) } - -func testEchoSigchain(t *testing.T, sk *keys.EdX25519Key, name string, clock tsutil.Clock) *keys.Sigchain { - usr, err := user.NewForSigning(sk.ID(), "echo", name) - require.NoError(t, err) - msg, err := usr.Sign(sk) - require.NoError(t, err) - err = user.Verify(msg, usr) - require.NoError(t, err) - - urs := "test://echo/" + name + "/" + sk.ID().String() + "/" + url.QueryEscape(strings.ReplaceAll(msg, "\n", " ")) - sc := keys.NewSigchain(sk.ID()) - stu, err := user.New(sk.ID(), "echo", "alice", urs, sc.LastSeq()+1) - require.NoError(t, err) - st, err := user.NewSigchainStatement(sc, stu, sk, clock.Now()) - require.NoError(t, err) - err = sc.Add(st) - require.NoError(t, err) - return sc -} diff --git a/user/user.go b/user/user.go index 95ba7a2..75a4c58 100644 --- a/user/user.go +++ b/user/user.go @@ -2,13 +2,18 @@ package user import ( + "fmt" + "net/url" "strconv" + "strings" "time" "github.com/keys-pub/keys" "github.com/keys-pub/keys/encoding" "github.com/keys-pub/keys/json" "github.com/keys-pub/keys/link" + "github.com/keys-pub/keys/request" + "github.com/keys-pub/keys/tsutil" "github.com/pkg/errors" ) @@ -325,3 +330,46 @@ func FindInSigchain(sc *keys.Sigchain) (*User, error) { return &user, nil } + +// MockStatement for testing. +func MockStatement(key *keys.EdX25519Key, sc *keys.Sigchain, name string, service string, req *request.MockRequestor, clock tsutil.Clock) (*keys.Statement, error) { + us, err := NewForSigning(key.ID(), service, name) + if err != nil { + return nil, err + } + msg, err := us.Sign(key) + if err != nil { + return nil, err + } + + urs := "" + switch service { + case "github": + urs = fmt.Sprintf("https://gist.github.com/%s/1", name) + case "twitter": + urs = fmt.Sprintf("https://mobile.twitter.com/%s/status/1", name) + case "echo": + urs = "test://echo/" + name + "/" + key.ID().String() + "/" + url.QueryEscape(strings.ReplaceAll(msg, "\n", " ")) + case "https": + urs = "https://" + name + default: + return nil, errors.Errorf("unsupported service for mock") + } + + usr, err := New(key.ID(), service, name, urs, sc.LastSeq()+1) + if err != nil { + return nil, err + } + st, err := NewSigchainStatement(sc, usr, key, clock.Now()) + if err != nil { + return nil, err + } + + req.SetResponse(urs, []byte(msg)) + + if err := sc.Add(st); err != nil { + return nil, err + } + + return st, nil +} diff --git a/user/users_test.go b/user/users_test.go index ab3224d..02e1df3 100644 --- a/user/users_test.go +++ b/user/users_test.go @@ -159,7 +159,9 @@ func TestCheckForExisting(t *testing.T) { users := user.NewUsers(ds, scs, user.Requestor(req), user.Clock(clock)) sk1 := keys.NewEdX25519KeyFromSeed(testSeed(0x01)) - sc1 := testEchoSigchain(t, sk1, "alice", clock) + sc1 := keys.NewSigchain(sk1.ID()) + _, err = user.MockStatement(sk1, sc1, "alice", "echo", req, clock) + require.NoError(t, err) kid, err := users.CheckForExisting(context.TODO(), sc1) require.NoError(t, err) require.Empty(t, kid) @@ -169,7 +171,9 @@ func TestCheckForExisting(t *testing.T) { require.NoError(t, err) sk2 := keys.NewEdX25519KeyFromSeed(testSeed(0x02)) - sc2 := testEchoSigchain(t, sk2, "alice", clock) + sc2 := keys.NewSigchain(sk2.ID()) + _, err = user.MockStatement(sk2, sc2, "alice", "echo", req, clock) + require.NoError(t, err) kid, err = users.CheckForExisting(context.TODO(), sc2) require.NoError(t, err) require.Equal(t, kid, sk1.ID())