github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/phonenumbers/user_test.go (about) 1 // Copyright 2018 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 package phonenumbers 4 5 import ( 6 "fmt" 7 "strings" 8 "testing" 9 10 "github.com/keybase/client/go/kbtest" 11 "github.com/keybase/client/go/libkb" 12 "github.com/keybase/client/go/protocol/keybase1" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestSetPhoneNumber(t *testing.T) { 17 tc := libkb.SetupTest(t, "TestPhoneNumbers", 1) 18 defer tc.Cleanup() 19 20 _, err := kbtest.CreateAndSignupFakeUser("phon", tc.G) 21 require.NoError(t, err) 22 23 // Generate a random phone number e.g. "14155552671". 24 randomNumber := kbtest.GenerateTestPhoneNumber() 25 // In strict format: "+14155552671". 26 phoneNumber := keybase1.PhoneNumber("+" + randomNumber) 27 // Create a representation likely to come from phone contact book: "+1-415-555-2671". 28 phoneFormatted := keybase1.RawPhoneNumber(fmt.Sprintf("+%s-%s-%s-%s", randomNumber[0:1], randomNumber[1:4], randomNumber[4:7], randomNumber[7:11])) 29 // Sanity check. 30 require.EqualValues(t, phoneNumber, strings.ReplaceAll(string(phoneFormatted), "-", "")) 31 32 t.Logf("Generated phone number: %q formatted as %q", phoneNumber, phoneFormatted) 33 34 mctx := libkb.NewMetaContextForTest(tc) 35 36 err = AddPhoneNumber(mctx, phoneNumber, keybase1.IdentityVisibility_PRIVATE) 37 require.NoError(t, err) 38 39 code, err := kbtest.GetPhoneVerificationCode(mctx, phoneNumber) 40 require.NoError(t, err) 41 t.Logf("Got verification code: %q", code) 42 43 err = VerifyPhoneNumber(mctx, phoneNumber, code) 44 require.NoError(t, err) 45 err = SetVisibilityPhoneNumber(mctx, phoneNumber, keybase1.IdentityVisibility_PUBLIC) 46 require.NoError(t, err) 47 48 resp, err := GetPhoneNumbers(mctx) 49 require.NoError(t, err) 50 require.Len(t, resp, 1) 51 require.Equal(t, phoneNumber, resp[0].PhoneNumber) 52 require.True(t, resp[0].Verified) 53 54 err = DeletePhoneNumber(mctx, phoneNumber) 55 require.NoError(t, err) 56 57 resp, err = GetPhoneNumbers(mctx) 58 require.NoError(t, err) 59 require.Len(t, resp, 0) 60 } 61 62 func TestDeleteSupersededNumber(t *testing.T) { 63 tc := libkb.SetupTest(t, "TestPhoneNumbers", 1) 64 defer tc.Cleanup() 65 66 mctx := libkb.NewMetaContextForTest(tc) 67 68 user1, err := kbtest.CreateAndSignupFakeUser("user1", tc.G) 69 require.NoError(t, err) 70 71 phoneNumber := keybase1.PhoneNumber("+15550123456") 72 73 err = AddPhoneNumber(mctx, phoneNumber, keybase1.IdentityVisibility_PRIVATE) 74 require.NoError(t, err) 75 76 // Verify phone on another user 77 _, err = kbtest.CreateAndSignupFakeUser("user2", tc.G) 78 require.NoError(t, err) 79 err = AddPhoneNumber(mctx, phoneNumber, keybase1.IdentityVisibility_PRIVATE) 80 require.NoError(t, err) 81 82 code, err := kbtest.GetPhoneVerificationCode(mctx, phoneNumber) 83 require.NoError(t, err) 84 t.Logf("Got verification code: %q", code) 85 err = VerifyPhoneNumber(mctx, phoneNumber, code) 86 require.NoError(t, err) 87 88 // Check it's superseded on user1 89 kbtest.Logout(tc) 90 err = user1.Login(tc.G) 91 require.NoError(t, err) 92 93 numbers, err := GetPhoneNumbers(mctx) 94 require.NoError(t, err) 95 require.Len(t, numbers, 1) 96 require.True(t, numbers[0].Superseded) 97 98 // Try adding again; superseded one should be deleted 99 err = AddPhoneNumber(mctx, phoneNumber, keybase1.IdentityVisibility_PRIVATE) 100 require.NoError(t, err) 101 numbers, err = GetPhoneNumbers(mctx) 102 require.NoError(t, err) 103 require.Len(t, numbers, 1) 104 require.False(t, numbers[0].Superseded) 105 } 106 107 func TestBadPhoneNumbers(t *testing.T) { 108 tc := libkb.SetupTest(t, "TestPhoneNumbers", 1) 109 defer tc.Cleanup() 110 111 _, err := kbtest.CreateAndSignupFakeUser("phon", tc.G) 112 require.NoError(t, err) 113 114 mctx := libkb.NewMetaContextForTest(tc) 115 require.Error(t, AddPhoneNumber(mctx, "14155552671", keybase1.IdentityVisibility_PUBLIC)) 116 require.Error(t, AddPhoneNumber(mctx, "014155552671", keybase1.IdentityVisibility_PUBLIC)) 117 require.Error(t, AddPhoneNumber(mctx, "784111222", keybase1.IdentityVisibility_PUBLIC)) 118 }