Skip to content

Commit

Permalink
user: MockStatement (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel authored Sep 2, 2020
1 parent d523bb1 commit dbce0fb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
14 changes: 14 additions & 0 deletions user/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
19 changes: 0 additions & 19 deletions user/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
48 changes: 48 additions & 0 deletions user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
8 changes: 6 additions & 2 deletions user/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())
Expand Down

0 comments on commit dbce0fb

Please sign in to comment.