github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/contacts/mocks.go (about) 1 //go:build !production 2 // +build !production 3 4 package contacts 5 6 import ( 7 "errors" 8 "time" 9 10 "github.com/stretchr/testify/require" 11 12 "github.com/keybase/client/go/libkb" 13 "github.com/keybase/client/go/protocol/keybase1" 14 ) 15 16 // Contacts package things that are useful for testing within contacts package 17 // but also in other packages if necessary (mostly service/contacts_test.go). 18 19 type MockLookupUser struct { 20 UID keybase1.UID 21 Username string 22 Fullname string 23 ServiceMap libkb.UserServiceSummary 24 } 25 26 func MakeMockLookupUser(username, fullname string) MockLookupUser { 27 return MockLookupUser{ 28 Username: username, 29 UID: libkb.UsernameToUID(username), 30 Fullname: fullname, 31 } 32 } 33 34 type MockContactsProvider struct { 35 T libkb.TestingTB 36 PhoneNumbers map[keybase1.RawPhoneNumber]MockLookupUser 37 PhoneNumberErrors map[keybase1.RawPhoneNumber]string 38 Emails map[keybase1.EmailAddress]MockLookupUser 39 Following map[keybase1.UID]bool 40 } 41 42 func MakeMockProvider(t libkb.TestingTB) *MockContactsProvider { 43 return &MockContactsProvider{ 44 T: t, 45 PhoneNumbers: make(map[keybase1.RawPhoneNumber]MockLookupUser), 46 PhoneNumberErrors: make(map[keybase1.RawPhoneNumber]string), 47 Emails: make(map[keybase1.EmailAddress]MockLookupUser), 48 Following: make(map[keybase1.UID]bool), 49 } 50 } 51 52 func (c *MockContactsProvider) LookupAllWithToken(mctx libkb.MetaContext, emails []keybase1.EmailAddress, 53 numbers []keybase1.RawPhoneNumber, _ Token) (ContactLookupResults, error) { 54 return c.LookupAll(mctx, emails, numbers) 55 } 56 57 func (c *MockContactsProvider) LookupAll(mctx libkb.MetaContext, emails []keybase1.EmailAddress, 58 numbers []keybase1.RawPhoneNumber) (ContactLookupResults, error) { 59 60 ret := NewContactLookupResults() 61 for _, email := range emails { 62 if user, found := c.Emails[email]; found { 63 ret.Results[MakeEmailLookupKey(email)] = ContactLookupResult{UID: user.UID} 64 } 65 } 66 for _, number := range numbers { 67 if user, found := c.PhoneNumbers[number]; found { 68 ret.Results[MakePhoneLookupKey(number)] = ContactLookupResult{UID: user.UID} 69 } 70 if errStr, found := c.PhoneNumberErrors[number]; found { 71 ret.Results[MakePhoneLookupKey(number)] = ContactLookupResult{Error: errStr} 72 } 73 } 74 ret.ResolvedFreshness = 10 * 24 * time.Hour // approx 10 days 75 ret.UnresolvedFreshness = 1 * 24 * time.Hour // approx one day 76 return ret, nil 77 } 78 79 func (c *MockContactsProvider) FindUsernames(mctx libkb.MetaContext, uids []keybase1.UID) (map[keybase1.UID]ContactUsernameAndFullName, error) { 80 res := make(map[keybase1.UID]ContactUsernameAndFullName) 81 uidSet := make(map[keybase1.UID]struct{}, len(uids)) 82 for _, v := range uids { 83 uidSet[v] = struct{}{} 84 } 85 86 for _, v := range c.PhoneNumbers { 87 if _, found := uidSet[v.UID]; found { 88 res[v.UID] = ContactUsernameAndFullName{ 89 Username: v.Username, 90 Fullname: v.Fullname, 91 } 92 } 93 } 94 for _, v := range c.Emails { 95 if _, found := uidSet[v.UID]; found { 96 res[v.UID] = ContactUsernameAndFullName{ 97 Username: v.Username, 98 Fullname: v.Fullname, 99 } 100 } 101 } 102 return res, nil 103 } 104 105 func (c *MockContactsProvider) FindFollowing(mctx libkb.MetaContext, uids []keybase1.UID) (map[keybase1.UID]bool, error) { 106 res := make(map[keybase1.UID]bool) 107 for _, uid := range uids { 108 res[uid] = c.Following[uid] 109 } 110 return res, nil 111 } 112 113 func (c *MockContactsProvider) FindServiceMaps(mctx libkb.MetaContext, uids []keybase1.UID) (res map[keybase1.UID]libkb.UserServiceSummary, err error) { 114 res = make(map[keybase1.UID]libkb.UserServiceSummary) 115 uidSet := make(map[keybase1.UID]struct{}, len(uids)) 116 for _, v := range uids { 117 uidSet[v] = struct{}{} 118 } 119 120 for _, v := range c.PhoneNumbers { 121 if _, found := uidSet[v.UID]; found && v.ServiceMap != nil { 122 res[v.UID] = v.ServiceMap 123 } 124 } 125 for _, v := range c.Emails { 126 if _, found := uidSet[v.UID]; found && v.ServiceMap != nil { 127 res[v.UID] = v.ServiceMap 128 } 129 } 130 return res, nil 131 } 132 133 type ErrorContactsProvider struct { 134 NoFail bool 135 T libkb.TestingTB 136 } 137 138 func (c *ErrorContactsProvider) LookupAllWithToken(mctx libkb.MetaContext, emails []keybase1.EmailAddress, 139 numbers []keybase1.RawPhoneNumber, _ Token) (ret ContactLookupResults, err error) { 140 141 if !c.NoFail { 142 require.Fail(c.T, "Call to ErrorContactsProvider.LookupAllWithToken") 143 } 144 return ret, errors.New("error contacts provider") 145 } 146 147 func (c *ErrorContactsProvider) LookupAll(mctx libkb.MetaContext, emails []keybase1.EmailAddress, 148 numbers []keybase1.RawPhoneNumber) (ret ContactLookupResults, err error) { 149 if !c.NoFail { 150 require.Fail(c.T, "Call to ErrorContactsProvider.LookupAll") 151 } 152 return ret, errors.New("error contacts provider") 153 } 154 155 func (c *ErrorContactsProvider) FindUsernames(mctx libkb.MetaContext, uids []keybase1.UID) (map[keybase1.UID]ContactUsernameAndFullName, error) { 156 if !c.NoFail { 157 require.Fail(c.T, "Call to ErrorContactsProvider.FindUsernames") 158 } 159 return nil, errors.New("mock error") 160 } 161 162 func (c *ErrorContactsProvider) FindFollowing(mctx libkb.MetaContext, uids []keybase1.UID) (map[keybase1.UID]bool, error) { 163 if !c.NoFail { 164 require.Fail(c.T, "Call to ErrorContactsProvider.FindFollowing") 165 } 166 return nil, errors.New("mock error") 167 } 168 169 func (c *ErrorContactsProvider) FindServiceMaps(mctx libkb.MetaContext, uids []keybase1.UID) (map[keybase1.UID]libkb.UserServiceSummary, error) { 170 if !c.NoFail { 171 require.Fail(c.T, "Call to ErrorContactsProvider.FindServiceMaps") 172 } 173 return nil, errors.New("mock error") 174 } 175 176 func MakePhoneComponent(label string, phone string) keybase1.ContactComponent { 177 num := keybase1.RawPhoneNumber(phone) 178 return keybase1.ContactComponent{ 179 Label: label, 180 PhoneNumber: &num, 181 } 182 } 183 184 func MakeEmailComponent(label string, email string) keybase1.ContactComponent { 185 em := keybase1.EmailAddress(email) 186 return keybase1.ContactComponent{ 187 Label: label, 188 Email: &em, 189 } 190 } 191 192 func MakeContact(name string, args ...keybase1.ContactComponent) keybase1.Contact { 193 return keybase1.Contact{ 194 Name: name, 195 Components: args, 196 } 197 }