github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/stellar/stellarsvc/rooter_test.go (about)

     1  // Stuff copied from engine tests
     2  package stellarsvc
     3  
     4  import (
     5  	"context"
     6  	"fmt"
     7  
     8  	"github.com/keybase/client/go/engine"
     9  	"github.com/keybase/client/go/kbtest"
    10  	"github.com/keybase/client/go/libkb"
    11  	"github.com/keybase/client/go/protocol/keybase1"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func proveRooter(tc *TestContext) (postID string, sigID keybase1.SigID) {
    16  	ui, sigID, err := proveRooterWithSecretUI(tc.G, tc.Fu, &libkb.TestSecretUI{}, libkb.GetDefaultSigVersion(tc.G))
    17  	require.NoError(tc.T, err)
    18  	return ui.postID, sigID
    19  }
    20  
    21  func proveRooterWithSecretUI(g *libkb.GlobalContext, fu *kbtest.FakeUser, secretUI libkb.SecretUI, sigVersion libkb.SigVersion) (*ProveUIMock, keybase1.SigID, error) {
    22  	sv := keybase1.SigVersion(sigVersion)
    23  	arg := keybase1.StartProofArg{
    24  		Service:      "rooter",
    25  		Username:     fu.Username,
    26  		Force:        false,
    27  		PromptPosted: true,
    28  		SigVersion:   &sv,
    29  	}
    30  
    31  	eng := engine.NewProve(g, &arg)
    32  
    33  	hook := func(arg keybase1.OkToCheckArg) (bool, string, error) {
    34  		sigID := eng.SigID()
    35  		if sigID.IsNil() {
    36  			return false, "", fmt.Errorf("empty sigID; can't make a post")
    37  		}
    38  		apiArg := libkb.APIArg{
    39  			Endpoint:    "rooter",
    40  			SessionType: libkb.APISessionTypeREQUIRED,
    41  			Args: libkb.HTTPArgs{
    42  				"post": libkb.S{Val: sigID.ToMediumID()},
    43  			},
    44  		}
    45  		mctx := libkb.NewMetaContextTODO(g)
    46  		res, err := g.API.Post(mctx, apiArg)
    47  		ok := err == nil
    48  		var postID string
    49  		if ok {
    50  			pid, err := res.Body.AtKey("post_id").GetString()
    51  			if err == nil {
    52  				postID = pid
    53  			}
    54  		}
    55  		return ok, postID, err
    56  	}
    57  
    58  	proveUI := &ProveUIMock{hook: hook}
    59  
    60  	uis := libkb.UIs{
    61  		LogUI:    g.UI.GetLogUI(),
    62  		SecretUI: secretUI,
    63  		ProveUI:  proveUI,
    64  	}
    65  	m := libkb.NewMetaContextTODO(g).WithUIs(uis)
    66  	err := engine.RunEngine2(m, eng)
    67  	return proveUI, eng.SigID(), err
    68  }
    69  
    70  type ProveUIMock struct {
    71  	username, recheck, overwrite, warning, checked bool
    72  	postID                                         string
    73  	hook                                           func(arg keybase1.OkToCheckArg) (bool, string, error)
    74  }
    75  
    76  func (p *ProveUIMock) PromptOverwrite(_ context.Context, arg keybase1.PromptOverwriteArg) (bool, error) {
    77  	p.overwrite = true
    78  	return true, nil
    79  }
    80  
    81  func (p *ProveUIMock) PromptUsername(_ context.Context, arg keybase1.PromptUsernameArg) (string, error) {
    82  	p.username = true
    83  	return "", nil
    84  }
    85  
    86  func (p *ProveUIMock) OutputPrechecks(_ context.Context, arg keybase1.OutputPrechecksArg) error {
    87  	return nil
    88  }
    89  
    90  func (p *ProveUIMock) PreProofWarning(_ context.Context, arg keybase1.PreProofWarningArg) (bool, error) {
    91  	p.warning = true
    92  	return true, nil
    93  }
    94  
    95  func (p *ProveUIMock) OutputInstructions(_ context.Context, arg keybase1.OutputInstructionsArg) error {
    96  	return nil
    97  }
    98  
    99  func (p *ProveUIMock) OkToCheck(_ context.Context, arg keybase1.OkToCheckArg) (bool, error) {
   100  	if !p.checked {
   101  		p.checked = true
   102  		ok, postID, err := p.hook(arg)
   103  		p.postID = postID
   104  		return ok, err
   105  	}
   106  	return false, fmt.Errorf("Check should have worked the first time!")
   107  }
   108  
   109  func (p *ProveUIMock) Checking(_ context.Context, arg keybase1.CheckingArg) error {
   110  	p.checked = true
   111  	return nil
   112  }
   113  
   114  func (p *ProveUIMock) ContinueChecking(_ context.Context, _ int) (bool, error) {
   115  	return true, nil
   116  }
   117  
   118  func (p *ProveUIMock) DisplayRecheckWarning(_ context.Context, arg keybase1.DisplayRecheckWarningArg) error {
   119  	p.recheck = true
   120  	return nil
   121  }