github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/engine/account_delete_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  //go:build !production
     5  // +build !production
     6  
     7  //
     8  // This is a test template for the AccountDelete engine.
     9  
    10  package engine
    11  
    12  import (
    13  	"testing"
    14  
    15  	"github.com/keybase/client/go/libkb"
    16  	"github.com/keybase/client/go/protocol/keybase1"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  func TestAccountDelete(t *testing.T) {
    21  	tc := SetupEngineTest(t, "acct")
    22  	defer tc.Cleanup()
    23  
    24  	fu := CreateAndSignupFakeUser(tc, "acct")
    25  
    26  	uis := libkb.UIs{
    27  		SecretUI: &libkb.TestSecretUI{Passphrase: fu.Passphrase},
    28  	}
    29  	m := NewMetaContextForTest(tc).WithUIs(uis)
    30  	eng := NewAccountDelete(tc.G, nil)
    31  	err := RunEngine2(m, eng)
    32  	require.NoError(t, err)
    33  
    34  	_, res, err := tc.G.Resolver.ResolveUser(m, fu.Username)
    35  	require.NoError(t, err)
    36  
    37  	err = res.GetError()
    38  	require.NoError(t, err)
    39  	require.True(t, res.GetDeleted())
    40  
    41  	tmp := res.FailOnDeleted()
    42  	err = tmp.GetError()
    43  	require.Error(t, err)
    44  
    45  	if _, ok := err.(libkb.UserDeletedError); !ok {
    46  		t.Fatal("expected a libkb.UserDeletedError")
    47  	}
    48  
    49  	_, err = libkb.LoadUser(libkb.NewLoadUserByNameArg(tc.G, fu.Username))
    50  	require.Error(t, err)
    51  
    52  	if _, ok := err.(libkb.UserDeletedError); !ok {
    53  		t.Errorf("loading deleted user error type: %T, expected libkb.UserDeletedError", err)
    54  	}
    55  }
    56  
    57  func TestAccountDeleteBadPassphrase(t *testing.T) {
    58  	tc := SetupEngineTest(t, "acct")
    59  	defer tc.Cleanup()
    60  
    61  	fu := CreateAndSignupFakeUser(tc, "acct")
    62  
    63  	uis := libkb.UIs{
    64  		SecretUI: &libkb.TestSecretUI{Passphrase: fu.Passphrase + "xxx"},
    65  	}
    66  	m := NewMetaContextForTest(tc).WithUIs(uis)
    67  	eng := NewAccountDelete(tc.G, nil)
    68  	err := RunEngine2(m, eng)
    69  	require.Error(t, err)
    70  
    71  	_, res, err := tc.G.Resolver.ResolveUser(m, fu.Username)
    72  	require.NoError(t, err)
    73  	err = res.GetError()
    74  	require.NoError(t, err)
    75  	require.False(t, res.GetDeleted())
    76  
    77  	tmp := res.FailOnDeleted()
    78  	err = tmp.GetError()
    79  	require.NoError(t, err)
    80  }
    81  
    82  func TestAccountDeleteIdentify(t *testing.T) {
    83  	tc := SetupEngineTest(t, "acct")
    84  	defer tc.Cleanup()
    85  
    86  	fu := CreateAndSignupFakeUser(tc, "acct")
    87  	u, err := libkb.LoadUser(libkb.NewLoadUserByNameArg(tc.G, fu.Username))
    88  	require.NoError(t, err)
    89  	t.Logf("created user %v %v", u.GetNormalizedName(), u.GetUID())
    90  
    91  	uis := libkb.UIs{
    92  		SecretUI: &libkb.TestSecretUI{Passphrase: fu.Passphrase},
    93  	}
    94  	m := NewMetaContextForTest(tc).WithUIs(uis)
    95  	eng := NewAccountDelete(tc.G, nil)
    96  	err = RunEngine2(m, eng)
    97  	require.NoError(t, err)
    98  	t.Logf("deleted user")
    99  
   100  	// Punch through the UPAK cache. Not dealing with upak vs delete race right now.
   101  	_, _, err = tc.G.GetUPAKLoader().LoadV2(
   102  		libkb.NewLoadUserArgWithMetaContext(libkb.NewMetaContextForTest(tc)).WithPublicKeyOptional().
   103  			WithUID(u.GetUID()).WithForcePoll(true))
   104  	require.NoError(t, err)
   105  
   106  	i := newIdentify2WithUIDTester(tc.G)
   107  	tc.G.SetProofServices(i)
   108  	arg := &keybase1.Identify2Arg{
   109  		Uid:              u.GetUID(),
   110  		IdentifyBehavior: keybase1.TLFIdentifyBehavior_CLI,
   111  	}
   112  	ieng := NewIdentify2WithUID(tc.G, arg)
   113  	uis = libkb.UIs{IdentifyUI: i}
   114  	m = NewMetaContextForTest(tc).WithUIs(uis)
   115  	t.Logf("identifying...")
   116  	err = RunEngine2(m, ieng)
   117  	require.Error(t, err)
   118  
   119  	if _, ok := err.(libkb.UserDeletedError); !ok {
   120  		t.Errorf("identify2 error: %T, expected libkb.UserDeletedError", err)
   121  	}
   122  }
   123  
   124  func TestAccountDeleteAfterRestart(t *testing.T) {
   125  	tc := SetupEngineTest(t, "acct")
   126  	defer tc.Cleanup()
   127  
   128  	fu := SignupFakeUserStoreSecret(tc, "acct")
   129  
   130  	simulateServiceRestart(t, tc, fu)
   131  	uis := libkb.UIs{
   132  		SecretUI: &libkb.TestSecretUI{Passphrase: fu.Passphrase},
   133  	}
   134  	m := NewMetaContextForTest(tc).WithUIs(uis)
   135  	eng := NewAccountDelete(tc.G, nil)
   136  	err := RunEngine2(m, eng)
   137  	require.NoError(t, err)
   138  
   139  	_, err = libkb.LoadUser(libkb.NewLoadUserByNameArg(tc.G, fu.Username))
   140  	if err == nil {
   141  		t.Fatal("no error loading deleted user")
   142  	}
   143  	if _, ok := err.(libkb.UserDeletedError); !ok {
   144  		t.Errorf("loading deleted user error type: %T, expected libkb.DeletedError", err)
   145  	}
   146  }
   147  
   148  func TestAccountDeleteWithPassphrase(t *testing.T) {
   149  	tc := SetupEngineTest(t, "acct")
   150  	defer tc.Cleanup()
   151  
   152  	fu := CreateAndSignupFakeUser(tc, "acct")
   153  
   154  	m := NewMetaContextForTest(tc)
   155  	eng := NewAccountDelete(tc.G, &fu.Passphrase)
   156  	err := RunEngine2(m, eng)
   157  	require.NoError(t, err)
   158  
   159  	_, res, err := tc.G.Resolver.ResolveUser(m, fu.Username)
   160  	require.NoError(t, err)
   161  
   162  	err = res.GetError()
   163  	require.NoError(t, err)
   164  	require.True(t, res.GetDeleted())
   165  
   166  	tmp := res.FailOnDeleted()
   167  	err = tmp.GetError()
   168  	require.Error(t, err)
   169  
   170  	if _, ok := err.(libkb.UserDeletedError); !ok {
   171  		t.Fatal("expected a libkb.UserDeletedError")
   172  	}
   173  
   174  	_, err = libkb.LoadUser(libkb.NewLoadUserByNameArg(tc.G, fu.Username))
   175  	require.Error(t, err)
   176  
   177  	if _, ok := err.(libkb.UserDeletedError); !ok {
   178  		t.Errorf("loading deleted user error type: %T, expected libkb.UserDeletedError", err)
   179  	}
   180  }