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

     1  package systests
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/keybase/client/go/client"
     7  
     8  	"github.com/keybase/client/go/kbtest"
     9  	"github.com/keybase/client/go/libkb"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  type mockEmailNotification struct {
    15  	list     []keybase1.Email
    16  	category string
    17  	email    keybase1.EmailAddress
    18  }
    19  
    20  type mockEmailListener struct {
    21  	libkb.NoopNotifyListener
    22  	verifiedEmails      []keybase1.EmailAddress
    23  	changeNotifications []mockEmailNotification
    24  }
    25  
    26  var _ libkb.NotifyListener = (*mockEmailListener)(nil)
    27  
    28  func (n *mockEmailListener) EmailAddressVerified(emailAddress keybase1.EmailAddress) {
    29  	n.verifiedEmails = append(n.verifiedEmails, emailAddress)
    30  }
    31  
    32  func (n *mockEmailListener) EmailsChanged(list []keybase1.Email, category string, email keybase1.EmailAddress) {
    33  	n.changeNotifications = append(n.changeNotifications, mockEmailNotification{
    34  		list:     list,
    35  		category: category,
    36  		email:    email,
    37  	})
    38  }
    39  
    40  func setupUserWithMockEmailListener(user *userPlusDevice) *mockEmailListener {
    41  	userListener := &mockEmailListener{}
    42  	user.tc.G.SetService()
    43  	user.tc.G.NotifyRouter.AddListener(userListener)
    44  	return userListener
    45  }
    46  
    47  func TestEmailVerificationNotifications(t *testing.T) {
    48  	tt := newTeamTester(t)
    49  	defer tt.cleanup()
    50  
    51  	bob := tt.addUser("bob")
    52  	email := bob.userInfo.email
    53  	bobListener := setupUserWithMockEmailListener(bob)
    54  
    55  	// verifying an email address fires off an "email.verified" notification with the email
    56  	err := kbtest.VerifyEmailAuto(bob.MetaContext(), keybase1.EmailAddress(email))
    57  	require.NoError(t, err)
    58  	bob.drainGregor()
    59  	expectedNotification := []keybase1.EmailAddress{keybase1.EmailAddress(email)}
    60  	require.Equal(t, bobListener.verifiedEmails, expectedNotification)
    61  }
    62  
    63  func TestEmailChangeNotifications(t *testing.T) {
    64  	tt := newTeamTester(t)
    65  	defer tt.cleanup()
    66  
    67  	bob := tt.addUser("bob")
    68  	bobListener := setupUserWithMockEmailListener(bob)
    69  
    70  	// Create new random email that we will add
    71  	sinfo := randomUser("")
    72  
    73  	cmd := client.CmdAddEmail{Contextified: libkb.NewContextified(bob.tc.G)}
    74  	cmd.Email = sinfo.email
    75  	cmd.Visibility = keybase1.IdentityVisibility_PRIVATE
    76  	err := cmd.Run()
    77  	require.NoError(t, err)
    78  
    79  	bob.drainGregor()
    80  	require.Len(t, bobListener.changeNotifications, 1)
    81  	change := bobListener.changeNotifications[0]
    82  	require.Equal(t, "email.added", change.category)
    83  	require.EqualValues(t, sinfo.email, change.email)
    84  	require.Len(t, change.list, 2)
    85  }