From 60cf841a187d9c7dc2f768a6639e8f9b08223133 Mon Sep 17 00:00:00 2001
From: Spencer Brown <spencer@privategrity.com>
Date: Wed, 7 Mar 2018 16:58:26 -0800
Subject: [PATCH] Fix implementation of nick list lookup for user map

---
 configure_postgres.sh                         |  0
 globals/user.go                               |  7 ++-
 globals/userDatabase.go                       | 10 ++---
 globals/user_test.go                          |  9 +++-
 ...etContactList.go => requestContactList.go} |  6 +--
 io/requestContactList_test.go                 | 43 +++++++++++++++++++
 6 files changed, 63 insertions(+), 12 deletions(-)
 mode change 100644 => 100755 configure_postgres.sh
 rename io/{clientGetContactList.go => requestContactList.go} (86%)
 create mode 100644 io/requestContactList_test.go

diff --git a/configure_postgres.sh b/configure_postgres.sh
old mode 100644
new mode 100755
diff --git a/globals/user.go b/globals/user.go
index 90ea4919..dc78f21c 100644
--- a/globals/user.go
+++ b/globals/user.go
@@ -9,6 +9,7 @@ package globals
 import (
 	pb "gitlab.com/privategrity/comms/mixmessages"
 	"gitlab.com/privategrity/crypto/cyclic"
+	"github.com/spf13/jwalterweatherman"
 )
 
 // Globally initiated UserRegistry
@@ -78,6 +79,7 @@ func (u *User) DeepCopy() *User {
 
 	nu.Id = u.Id
 	nu.Address = u.Address
+	nu.Nick = u.Nick
 
 	nu.Transmission = *u.Transmission.DeepCopy()
 
@@ -93,7 +95,7 @@ func (u *User) DeepCopy() *User {
 // NewUser creates a new User object with default fields and given address.
 func (m *UserMap) NewUser(address string) *User {
 	idCounter++
-	return &User{Id: idCounter - 1, Address: address,
+	return &User{Id: idCounter - 1, Address: address, Nick: "",
 		// TODO: each user should have unique base and secret keys
 		Transmission: ForwardKey{BaseKey: cyclic.NewIntFromString(
 			"c1248f42f8127999e07c657896a26b56fd9a499c6199e1265053132451128f52", 16),
@@ -135,6 +137,7 @@ func (m *UserMap) CountUsers() int {
 }
 
 func (m *UserMap) GetNickList() (ids []uint64, nicks []string) {
+
 	userCount := m.CountUsers()
 
 	nicks = make([]string, 0, userCount)
@@ -143,6 +146,8 @@ func (m *UserMap) GetNickList() (ids []uint64, nicks []string) {
 		if user != nil {
 			nicks = append(nicks, user.Nick)
 			ids = append(ids, user.Id)
+		} else {
+			jwalterweatherman.FATAL.Panicf("A user was nil.")
 		}
 	}
 
diff --git a/globals/userDatabase.go b/globals/userDatabase.go
index 5cba1fd3..36ae007b 100644
--- a/globals/userDatabase.go
+++ b/globals/userDatabase.go
@@ -136,6 +136,7 @@ func (m *UserDatabase) UpsertUser(user *User) {
 		// On conflict, update the user's fields
 		OnConflict("(id) DO UPDATE").
 		Set("address = EXCLUDED.address," +
+			"nick = EXCLUDED.nick," +
 			"transmission_base_key = EXCLUDED.transmission_base_key," +
 			"transmission_recursive_key = EXCLUDED.transmission_recursive_key," +
 			"reception_base_key = EXCLUDED.reception_base_key," +
@@ -144,8 +145,7 @@ func (m *UserDatabase) UpsertUser(user *User) {
 		// Otherwise, insert the new user
 		Insert()
 	if err != nil {
-		jww.FATAL.Printf("Unable to upsert user %d!", user.Id)
-		panic(err)
+		jww.FATAL.Panicf("Unable to upsert user %d! %s", user.Id, err.Error())
 	}
 }
 
@@ -153,8 +153,7 @@ func (m *UserDatabase) UpsertUser(user *User) {
 func (m *UserDatabase) CountUsers() int {
 	count, err := m.db.Model(&UserDB{}).Count()
 	if err != nil {
-		jww.FATAL.Println("Unable to count users!")
-		panic(err)
+		jww.FATAL.Panicf("Unable to count users! %s", err.Error())
 	}
 	return count
 }
@@ -167,8 +166,7 @@ func (m *UserDatabase) GetNickList() (ids []uint64, nicks []string) {
 	err := m.db.Model(&model).Column("id", "nick").Select(&ids, &nicks)
 
 	if err != nil {
-		jww.FATAL.Println("Unable to get contact list!")
-		panic(err)
+		jww.FATAL.Panicf("Unable to get contact list! %s", err.Error())
 	}
 
 	return ids, nicks
diff --git a/globals/user_test.go b/globals/user_test.go
index 11e76970..0f329934 100644
--- a/globals/user_test.go
+++ b/globals/user_test.go
@@ -15,6 +15,7 @@ import (
 func TestUserRegistry(t *testing.T) {
 
 	testUser := Users.NewUser("Someplace")
+	testUser.Nick = "Me"
 	numUsers := Users.CountUsers()
 	Users.DeleteUser(testUser.Id)
 	Users.UpsertUser(testUser)
@@ -25,13 +26,17 @@ func TestUserRegistry(t *testing.T) {
 	}
 
 	getUser.Transmission.RecursiveKey.SetInt64(5)
+	getUser.Nick = "Michael"
 
 	Users.UpsertUser(getUser)
 
 	getUser2, _ := Users.GetUser(testUser.Id)
 
-	if getUser2.Transmission.RecursiveKey.Int64() != 5 {
-		t.Errorf("UpsertUser: User did not save!")
+	if getUser2.Transmission.RecursiveKey.Int64() != 5 || getUser2.
+		Nick != "Michael" {
+		t.Errorf("UpsertUser: User did not save! Got: %v, %v; expected: %v, " +
+			"%v", getUser2.Transmission.RecursiveKey.Int64(), getUser2.Nick,
+				5, "Michael")
 	}
 
 	Users.DeleteUser(testUser.Id)
diff --git a/io/clientGetContactList.go b/io/requestContactList.go
similarity index 86%
rename from io/clientGetContactList.go
rename to io/requestContactList.go
index f2c60083..e228a5f5 100644
--- a/io/clientGetContactList.go
+++ b/io/requestContactList.go
@@ -12,7 +12,7 @@ import (
 )
 
 func (s ServerImpl) RequestContactList(inputMsg *pb.
-	ContactPoll) *pb.ContactMessage {
+ContactPoll) *pb.ContactMessage {
 	userCount := globals.Users.CountUsers()
 
 	contactList := pb.ContactMessage{
@@ -22,8 +22,8 @@ func (s ServerImpl) RequestContactList(inputMsg *pb.
 	idList, nickList := globals.Users.GetNickList()
 
 	for i := 0; i < userCount; i++ {
-		contactList.Contacts[i].Nick = nickList[i]
-		contactList.Contacts[i].UserID = idList[i]
+		contactList.Contacts[i] = &pb.Contact{Nick: nickList[i],
+			UserID: idList[i]}
 	}
 
 	return &contactList
diff --git a/io/requestContactList_test.go b/io/requestContactList_test.go
new file mode 100644
index 00000000..51f6b515
--- /dev/null
+++ b/io/requestContactList_test.go
@@ -0,0 +1,43 @@
+////////////////////////////////////////////////////////////////////////////////
+// Copyright © 2018 Privategrity Corporation                                   /
+//                                                                             /
+// All rights reserved.                                                        /
+////////////////////////////////////////////////////////////////////////////////
+
+package io
+
+import (
+	"testing"
+	"gitlab.com/privategrity/server/globals"
+	pb "gitlab.com/privategrity/comms/mixmessages"
+	"gitlab.com/privategrity/comms/mixclient"
+)
+
+// MARK
+func TestRequestContactList(t *testing.T) {
+	// empty user registry from previous tests so we can make sure that our
+	// user's first in the map
+	globals.Users.DeleteUser(1)
+	user := globals.Users.NewUser("test") // Create user
+	user.Nick = "Michael"
+	globals.Users.UpsertUser(user)        // Insert user into registry
+	user = globals.Users.NewUser("test")
+	user.Nick = "Me"
+	globals.Users.UpsertUser(user)
+
+	// Currently we just return all the nicks
+	contacts, err := mixclient.RequestContactList(NextServer, &pb.ContactPoll{})
+	if err != nil {
+		t.Errorf("RequestContactList() returned an error: %v", err.Error())
+	}
+
+	expectedNicks := []string{"Michael", "Me"}
+	for i := 0; i < len(expectedNicks); i++ {
+		if contacts.Contacts[i] == nil {
+			t.Errorf("Got a nil nick at index %v.", i)
+		} else if expectedNicks[i] != contacts.Contacts[i].Nick {
+			t.Errorf("Got an unexpected nick at index %v. Got: %v, " +
+				"expected: %v", i, contacts.Contacts[i].Nick, expectedNicks[i])
+		}
+	}
+}
\ No newline at end of file
-- 
GitLab