github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/engine/paperkey_submit_test.go (about)

     1  // Copyright 2016 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package engine
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/keybase/client/go/libkb"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestPaperKeySubmit(t *testing.T) {
    15  	tc := SetupEngineTest(t, "login")
    16  	defer tc.Cleanup()
    17  
    18  	tc.G.SetService()
    19  	listener := &nlistener{}
    20  	tc.G.NotifyRouter.AddListener(listener)
    21  
    22  	// signup and get the paper key
    23  	fu := NewFakeUserOrBust(t, "paper")
    24  	arg := MakeTestSignupEngineRunArg(fu)
    25  	arg.SkipPaper = false
    26  	loginUI := &paperLoginUI{Username: fu.Username}
    27  	uis := libkb.UIs{
    28  		LogUI:    tc.G.UI.GetLogUI(),
    29  		GPGUI:    &gpgtestui{},
    30  		SecretUI: fu.NewSecretUI(),
    31  		LoginUI:  loginUI,
    32  	}
    33  	s := NewSignupEngine(tc.G, &arg)
    34  	m := NewMetaContextForTest(tc).WithUIs(uis)
    35  	if err := RunEngine2(m, s); err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	assertNumDevicesAndKeys(tc, fu, 2, 4)
    40  
    41  	Logout(tc)
    42  
    43  	paperkey := loginUI.PaperPhrase
    44  	if len(paperkey) == 0 {
    45  		t.Fatal("login ui has no paper key phrase")
    46  	}
    47  
    48  	fu.LoginOrBust(tc)
    49  
    50  	assertPaperKeyCached(m, t, false)
    51  
    52  	// submit the paper key
    53  	m = NewMetaContextForTestWithLogUI(tc)
    54  	eng := NewPaperKeySubmit(tc.G, paperkey)
    55  	if err := RunEngine2(m, eng); err != nil {
    56  		t.Fatal(err)
    57  	}
    58  
    59  	assertPaperKeyCached(m, t, true)
    60  
    61  	if len(listener.paperEncKIDs) != 1 {
    62  		t.Fatalf("num paperkey notifications: %d, expected 1", len(listener.paperEncKIDs))
    63  	}
    64  	if listener.paperEncKIDs[0].NotEqual(eng.deviceWithKeys.EncryptionKey().GetKID()) {
    65  		t.Errorf("enc kid from notify: %s, expected %s", listener.paperEncKIDs[0], eng.deviceWithKeys.EncryptionKey().GetKID())
    66  	}
    67  }
    68  
    69  func assertPaperKeyCached(m libkb.MetaContext, t *testing.T, wantCached bool) {
    70  	device := m.ActiveDevice().ProvisioningKey(m)
    71  	var isCached bool
    72  	if device != nil {
    73  		isCached = device.HasBothKeys()
    74  	}
    75  	require.Equal(t, isCached, wantCached)
    76  }
    77  
    78  type nlistener struct {
    79  	libkb.NoopNotifyListener
    80  	paperEncKIDs []keybase1.KID
    81  }
    82  
    83  var _ libkb.NotifyListener = (*nlistener)(nil)
    84  
    85  func (n *nlistener) PaperKeyCached(uid keybase1.UID, encKID, sigKID keybase1.KID) {
    86  	n.paperEncKIDs = append(n.paperEncKIDs, encKID)
    87  }