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

     1  package service
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/keybase/client/go/engine"
     7  	"github.com/keybase/client/go/kbtest"
     8  	"github.com/keybase/client/go/libkb"
     9  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
    10  	"github.com/keybase/client/go/teams"
    11  	"github.com/stretchr/testify/require"
    12  	"golang.org/x/net/context"
    13  )
    14  
    15  func TestCheckPassphrase(t *testing.T) {
    16  	tc := libkb.SetupTest(t, "pche", 3)
    17  	defer tc.Cleanup()
    18  
    19  	fu, err := kbtest.CreateAndSignupFakeUser("lmu", tc.G)
    20  	require.NoError(t, err)
    21  
    22  	handler := NewAccountHandler(nil, tc.G)
    23  	ctx := context.Background()
    24  	ret, err := handler.PassphraseCheck(ctx, keybase1.PassphraseCheckArg{
    25  		Passphrase: fu.Passphrase,
    26  	})
    27  	require.NoError(t, err)
    28  	require.True(t, ret)
    29  
    30  	// Bad passphrase should come back as ret=false and no error.
    31  	ret, err = handler.PassphraseCheck(ctx, keybase1.PassphraseCheckArg{
    32  		Passphrase: fu.Passphrase + " ",
    33  	})
    34  	require.NoError(t, err)
    35  	require.False(t, ret)
    36  
    37  	// Other errors should come back as errors.
    38  	kbtest.Logout(tc)
    39  	_, err = handler.PassphraseCheck(ctx, keybase1.PassphraseCheckArg{
    40  		Passphrase: fu.Passphrase + " ",
    41  	})
    42  	require.Error(t, err)
    43  }
    44  
    45  func TestRecoverUsernameWithEmail(t *testing.T) {
    46  	tc := libkb.SetupTest(t, "recu", 3)
    47  	defer tc.Cleanup()
    48  
    49  	fu, err := kbtest.CreateAndSignupFakeUser("rcu", tc.G)
    50  	require.NoError(t, err)
    51  
    52  	handler := NewAccountHandler(nil, tc.G)
    53  	ctx := context.Background()
    54  
    55  	// We are not going to check if the email was sent, but we
    56  	// are testing if we can call this RPC on both logged and
    57  	// unlogged session.
    58  
    59  	err = handler.RecoverUsernameWithEmail(ctx, keybase1.RecoverUsernameWithEmailArg{
    60  		Email: fu.Email,
    61  	})
    62  	require.NoError(t, err)
    63  
    64  	kbtest.Logout(tc)
    65  
    66  	err = handler.RecoverUsernameWithEmail(ctx, keybase1.RecoverUsernameWithEmailArg{
    67  		Email: fu.Email,
    68  	})
    69  	require.NoError(t, err)
    70  
    71  	// `"bad"+fu.Email will receive an email saying a "username was requested
    72  	// but no user exists in keybase" rather than returning an error so we don't
    73  	// expose to the caller if an email exists in the system or not.
    74  	err = handler.RecoverUsernameWithEmail(ctx, keybase1.RecoverUsernameWithEmailArg{
    75  		Email: "bad+" + fu.Email,
    76  	})
    77  	require.NoError(t, err)
    78  }
    79  
    80  func TestContactSettingsAPI(t *testing.T) {
    81  	tc := libkb.SetupTest(t, "cset", 3)
    82  	defer tc.Cleanup()
    83  
    84  	// setup
    85  	user, err := kbtest.CreateAndSignupFakeUser("cset", tc.G)
    86  	require.NoError(t, err)
    87  
    88  	handler := NewAccountHandler(nil, tc.G)
    89  	ctx := context.Background()
    90  
    91  	teamName := user.Username + "t"
    92  	teamID, err := teams.CreateRootTeam(ctx, tc.G, teamName, keybase1.TeamSettings{})
    93  	require.NoError(t, err)
    94  	require.NotNil(t, teamID)
    95  
    96  	// get
    97  	_, err = handler.UserGetContactSettings(ctx)
    98  	require.NoError(t, err)
    99  
   100  	// set
   101  	settings := keybase1.ContactSettings{
   102  		Enabled:              true,
   103  		AllowGoodTeams:       true,
   104  		AllowFolloweeDegrees: 2,
   105  		Teams: []keybase1.TeamContactSettings{
   106  			{TeamID: *teamID,
   107  				Enabled: true,
   108  			}},
   109  	}
   110  	expectedSettings := settings
   111  	err = handler.UserSetContactSettings(ctx, settings)
   112  	require.NoError(t, err)
   113  
   114  	// get
   115  	res, err := handler.UserGetContactSettings(ctx)
   116  	require.NoError(t, err)
   117  	res.Version = nil
   118  	require.Equal(t, expectedSettings, res)
   119  }
   120  
   121  func TestContactSettingsAPIBadInputs(t *testing.T) {
   122  	tc := libkb.SetupTest(t, "cset", 3)
   123  	defer tc.Cleanup()
   124  
   125  	// setup
   126  	user, err := kbtest.CreateAndSignupFakeUser("cset", tc.G)
   127  	require.NoError(t, err)
   128  
   129  	handler := NewAccountHandler(nil, tc.G)
   130  	ctx := context.Background()
   131  
   132  	teamName := user.Username + "t"
   133  	teamID, err := teams.CreateRootTeam(ctx, tc.G, teamName, keybase1.TeamSettings{})
   134  	require.NoError(t, err)
   135  	require.NotNil(t, teamID)
   136  
   137  	// set enabled=true, allowFolloweeDegrees>2
   138  	settings := keybase1.ContactSettings{
   139  		Enabled:              true,
   140  		AllowGoodTeams:       true,
   141  		AllowFolloweeDegrees: 3,
   142  		Teams:                nil,
   143  	}
   144  	err = handler.UserSetContactSettings(ctx, settings)
   145  	require.Error(t, err)
   146  }
   147  
   148  func TestCancelReset(t *testing.T) {
   149  	tc := libkb.SetupTest(t, "arst", 3)
   150  	defer tc.Cleanup()
   151  
   152  	user, err := kbtest.CreateAndSignupFakeUser("arst", tc.G)
   153  	require.NoError(t, err)
   154  
   155  	handler := NewAccountHandler(nil, tc.G)
   156  
   157  	// Can't reset if we are not in autoreset, but it shouldn't log us out or
   158  	// anything like that.
   159  	err = handler.CancelReset(context.Background(), 0)
   160  	require.Error(t, err)
   161  	_, isLoggedOutErr := err.(UserWasLoggedOutError)
   162  	require.False(t, isLoggedOutErr)
   163  	require.True(t, tc.G.ActiveDevice.UID().Exists())
   164  
   165  	{
   166  		// Make special tc to start resetting our user, because we can't
   167  		// enroll to reset from a logged-in device.
   168  		tc2 := libkb.SetupTest(t, "arst", 3)
   169  		defer tc2.Cleanup()
   170  
   171  		uis := libkb.UIs{
   172  			SecretUI: &libkb.TestSecretUI{},
   173  			LogUI:    tc.G.UI.GetLogUI(),
   174  			LoginUI:  &libkb.TestLoginUI{},
   175  		}
   176  		eng := engine.NewAccountReset(tc2.G, user.Username)
   177  		eng.SetPassphrase(user.Passphrase)
   178  		err = engine.RunEngine2(libkb.NewMetaContextForTest(tc2).WithUIs(uis), eng)
   179  		require.NoError(t, err)
   180  	}
   181  
   182  	err = handler.CancelReset(context.Background(), 0)
   183  	require.NoError(t, err)
   184  }
   185  
   186  func TestCancelAutoresetWhenRevoked(t *testing.T) {
   187  	// `CancelReset` RPC is special in a way that it calls
   188  	// `LogoutAndDeprovisionIfRevoked` to log out if user is revoked (because
   189  	// the reset has already been completed for example). This is needed
   190  	// because GUI can be stuck in "Autoreset modal" mode, and `CancelReset` is
   191  	// the only RPC that user can trigger from that state.
   192  
   193  	tc1 := libkb.SetupTest(t, "arst", 3)
   194  	defer tc1.Cleanup()
   195  
   196  	user, err := kbtest.CreateAndSignupFakeUser("arst", tc1.G)
   197  	require.NoError(t, err)
   198  
   199  	tc2 := libkb.SetupTest(t, "lockdown_second", 3)
   200  	defer tc2.Cleanup()
   201  	kbtest.ProvisionNewDeviceKex(&tc1, &tc2, user, keybase1.DeviceTypeV2_DESKTOP)
   202  
   203  	// Reset account using tc1.
   204  	kbtest.ResetAccount(tc1, user)
   205  
   206  	// tc2 does not know that account has been reset (no service tasks / gregor
   207  	// notifications in this test). Assume user clicks "Cancel reset" in the
   208  	// modal window.
   209  
   210  	handler := NewAccountHandler(nil, tc2.G)
   211  	ctx := context.Background()
   212  
   213  	// We should be logged in now.
   214  	require.True(t, tc2.G.ActiveDevice.UID().Exists())
   215  
   216  	err = handler.CancelReset(ctx, 0)
   217  	require.Error(t, err)
   218  	require.IsType(t, UserWasLoggedOutError{}, err)
   219  
   220  	// `CancelReset` should have logged us out.
   221  	require.True(t, tc2.G.ActiveDevice.UID().IsNil())
   222  }