Skip to content
Snippets Groups Projects
Commit 0c05830c authored by Jonah Husson's avatar Jonah Husson
Browse files

Don't add username if it isn't there, add test for username in parsecontact

parent 001f7bec
Branches
Tags
2 merge requests!23Release,!6Bob/contact
......@@ -119,12 +119,15 @@ func (m *Manager) parseContacts(response []*Contact,
if err != nil {
return nil, errors.Errorf("failed to parse Contact user ID: %+v", err)
}
var facts []fact.Fact
if c.Username != "" {
facts = []fact.Fact{{c.Username, fact.Username}}
}
// Create new Contact
contacts[i] = contact.Contact{
ID: uid,
DhPubKey: m.grp.NewIntFromBytes(c.PubKey),
Facts: []fact.Fact{{c.Username, fact.Username}},
Facts: facts,
}
// Assign each Fact with a matching hash to the Contact
......
......@@ -448,6 +448,46 @@ func TestManager_parseContacts(t *testing.T) {
}
}
func TestManager_parseContacts_username(t *testing.T) {
m := &Manager{grp: cyclic.NewGroup(large.NewInt(107), large.NewInt(2))}
// Generate fact list
var factList fact.FactList
for i := 0; i < 10; i++ {
factList = append(factList, fact.Fact{
Fact: fmt.Sprintf("fact %d", i),
T: fact.FactType(rand.Intn(4)),
})
}
factHashes, factMap := hashFactList(factList)
var contacts []*Contact
var expectedContacts []contact.Contact
for i, hash := range factHashes {
contacts = append(contacts, &Contact{
UserID: id.NewIdFromString("user", id.User, t).Marshal(),
Username: "zezima",
PubKey: []byte{byte(i + 1)},
TrigFacts: []*HashFact{hash},
})
expectedContacts = append(expectedContacts, contact.Contact{
ID: id.NewIdFromString("user", id.User, t),
DhPubKey: m.grp.NewIntFromBytes([]byte{byte(i + 1)}),
Facts: fact.FactList{{"zezima", fact.Username}, factMap[string(hash.Hash)]},
})
}
testContacts, err := m.parseContacts(contacts, factMap)
if err != nil {
t.Errorf("parseContacts() returned an error: %+v", err)
}
if !reflect.DeepEqual(expectedContacts, testContacts) {
t.Errorf("parseContacts() did not return the expected contacts."+
"\nexpected: %+v\nreceived: %+v", expectedContacts, testContacts)
}
}
// Error path: provided contact IDs are malformed and cannot be unmarshaled.
func TestManager_parseContacts_IdUnmarshalError(t *testing.T) {
m := &Manager{grp: cyclic.NewGroup(large.NewInt(107), large.NewInt(2))}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment