github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/systests/account_reset_test.go (about)

     1  package systests
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/keybase/client/go/engine"
     8  	"github.com/keybase/client/go/libkb"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func assertResetBadgeState(t *testing.T, user *userPlusDevice, expectedDaysLeft int) {
    13  	g := user.tc.G
    14  	user.kickAutoresetd()
    15  	pollForTrue(t, g, func(i int) bool {
    16  		badges := getBadgeState(t, user)
    17  		t.Logf("Iter loop %d badge state: %+v", i, badges)
    18  		if expectedDaysLeft > 0 {
    19  			daysLeft := int(time.Until(badges.ResetState.EndTime.Time()) / (time.Hour * 24))
    20  			t.Logf("Iter loop %d cmp: %v vs %v", i, daysLeft, expectedDaysLeft)
    21  			return expectedDaysLeft == daysLeft
    22  		}
    23  		return badges.ResetState.EndTime == 0
    24  	})
    25  }
    26  
    27  func processReset(tc libkb.TestContext) error {
    28  	mctx := libkb.NewMetaContextForTest(tc)
    29  	_, err := tc.G.API.Post(mctx, libkb.APIArg{
    30  		Endpoint:    "autoreset/process_dev",
    31  		SessionType: libkb.APISessionTypeNONE,
    32  		RetryCount:  5,
    33  	})
    34  	return err
    35  }
    36  
    37  func TestCancelResetPipeline(t *testing.T) {
    38  	tt := newTeamTester(t)
    39  	defer tt.cleanup()
    40  
    41  	ann := tt.addUser("ann")
    42  	tc := ann.tc
    43  	uis := libkb.UIs{
    44  		LoginUI:  &libkb.TestLoginUI{Username: ann.username},
    45  		SecretUI: ann.newSecretUI(),
    46  	}
    47  	mctx := libkb.NewMetaContextForTest(*tc).WithUIs(uis)
    48  	assertResetBadgeState(t, ann, 0)
    49  
    50  	// fails if the user is not in the pipeline
    51  	err := libkb.CancelResetPipeline(mctx)
    52  	require.Error(t, err)
    53  	serr, ok := err.(libkb.AppStatusError)
    54  	require.True(t, ok)
    55  	require.Equal(t, libkb.SCNotFound, serr.Code)
    56  	assertResetBadgeState(t, ann, 0)
    57  
    58  	// fails since we are logged in with a provisioned device
    59  	eng := engine.NewAccountReset(tc.G, ann.username)
    60  	err = engine.RunEngine2(mctx, eng)
    61  	require.Error(t, err)
    62  	require.IsType(t, libkb.ResetWithActiveDeviceError{}, err)
    63  
    64  	// succeeds and is then canceled
    65  	ann.logout()
    66  	err = engine.RunEngine2(mctx, eng)
    67  	require.NoError(t, err)
    68  
    69  	ann.login()
    70  	assertResetBadgeState(t, ann, 4) // 1 device = 5 days
    71  	err = libkb.CancelResetPipeline(mctx)
    72  	require.NoError(t, err)
    73  	require.NoError(t, processReset(*tc))
    74  	assertResetBadgeState(t, ann, 0)
    75  
    76  	// succeed without a session
    77  	ann.logout()
    78  	uis = libkb.UIs{
    79  		LoginUI:  &libkb.TestLoginUI{Username: ann.username},
    80  		SecretUI: &libkb.TestSecretUI{},
    81  	}
    82  	mctx = libkb.NewMetaContextForTest(*tc).WithUIs(uis)
    83  	eng = engine.NewAccountReset(tc.G, ann.username)
    84  	err = engine.RunEngine2(mctx, eng)
    85  	require.NoError(t, err)
    86  
    87  	ann.login()
    88  	// We don't have any badges since the countdown does not start until the
    89  	// user verifies via email.
    90  	assertResetBadgeState(t, ann, 0)
    91  
    92  	err = libkb.CancelResetPipeline(mctx)
    93  	require.NoError(t, err)
    94  	assertResetBadgeState(t, ann, 0)
    95  
    96  	// fail without a session
    97  	ann.logout()
    98  	uis = libkb.UIs{
    99  		LoginUI:  &libkb.TestLoginUI{Username: ann.username},
   100  		SecretUI: &libkb.TestSecretUI{},
   101  	}
   102  	mctx = libkb.NewMetaContextForTest(*tc).WithUIs(uis)
   103  	eng = engine.NewAccountReset(tc.G, "")
   104  	err = engine.RunEngine2(mctx, eng)
   105  	require.Error(t, err)
   106  	t.Logf("err: %v", err)
   107  	require.IsType(t, libkb.ResetMissingParamsError{}, err)
   108  }