github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/emails/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 emails 4 5 import ( 6 "crypto/rand" 7 "encoding/hex" 8 "fmt" 9 "testing" 10 11 "github.com/keybase/client/go/kbtest" 12 "github.com/keybase/client/go/libkb" 13 "github.com/keybase/client/go/protocol/keybase1" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func randomEmailAddress(t *testing.T) keybase1.EmailAddress { 18 buf := make([]byte, 5) 19 _, err := rand.Read(buf) 20 require.NoError(t, err) 21 email := fmt.Sprintf("%s@example.org", hex.EncodeToString(buf)) 22 return keybase1.EmailAddress(email) 23 } 24 25 func TestEmailHappyPath(t *testing.T) { 26 tc := libkb.SetupTest(t, "TestEmailHappyPath", 1) 27 defer tc.Cleanup() 28 29 _, err := kbtest.CreateAndSignupFakeUser("emai", tc.G) 30 require.NoError(t, err) 31 32 email1 := randomEmailAddress(t) 33 email2 := randomEmailAddress(t) 34 require.NotEqual(t, email1, email2) 35 36 mctx := libkb.NewMetaContextForTest(tc) 37 38 err = AddEmail(mctx, email1, keybase1.IdentityVisibility_PUBLIC) 39 require.NoError(t, err) 40 41 err = DeleteEmail(mctx, email1) 42 require.NoError(t, err) 43 err = AddEmail(mctx, email2, keybase1.IdentityVisibility_PUBLIC) 44 require.NoError(t, err) 45 46 emails, err := GetEmails(mctx) 47 require.NoError(t, err) 48 49 var oldPrimary keybase1.EmailAddress 50 51 require.Len(t, emails, 2) 52 found := false 53 for _, email := range emails { 54 require.NotEqual(t, email.Email, email1) 55 if email.Email == email2 { 56 found = true 57 require.False(t, email.IsVerified) 58 require.False(t, email.IsPrimary) 59 } 60 if email.IsPrimary { 61 oldPrimary = email.Email 62 } 63 } 64 require.True(t, found) 65 66 err = SetPrimaryEmail(mctx, email2) 67 require.NoError(t, err) 68 69 emails, err = GetEmails(mctx) 70 require.NoError(t, err) 71 72 found = false 73 for _, email := range emails { 74 if email.Email == email2 { 75 found = true 76 require.True(t, email.IsPrimary) 77 } 78 } 79 require.True(t, found) 80 81 err = SetPrimaryEmail(mctx, oldPrimary) 82 require.NoError(t, err) 83 84 err = DeleteEmail(mctx, email2) 85 require.NoError(t, err) 86 87 emails, err = GetEmails(mctx) 88 require.NoError(t, err) 89 90 found = false 91 for _, email := range emails { 92 if email.Email == email2 { 93 found = true 94 } 95 } 96 require.False(t, found) 97 98 err = SendVerificationEmail(mctx, oldPrimary) 99 require.NoError(t, err) 100 101 err = kbtest.VerifyEmailAuto(mctx, oldPrimary) 102 require.NoError(t, err) 103 104 err = SetVisibilityEmail(mctx, oldPrimary, keybase1.IdentityVisibility_PUBLIC) 105 require.NoError(t, err) 106 }