diff --git a/go.mod b/go.mod index 5312724..ac08fa1 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/go-chi/chi/v5 v5.0.7 + github.com/google/go-cmp v0.5.6 github.com/google/uuid v1.3.0 github.com/pelletier/go-toml v1.9.4 github.com/urfave/cli/v2 v2.3.0 @@ -19,5 +20,6 @@ require ( golang.org/x/net v0.0.0-20211203184738-4852103109b8 // indirect golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect golang.org/x/text v0.3.7 // indirect + golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect google.golang.org/genproto v0.0.0-20211203200212-54befc351ae9 // indirect ) diff --git a/go.sum b/go.sum index ae37d31..d486309 100644 --- a/go.sum +++ b/go.sum @@ -49,8 +49,9 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/pb/ezshare.pb.go b/pb/ezshare.pb.go index 6aef848..edf5cd9 100644 --- a/pb/ezshare.pb.go +++ b/pb/ezshare.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.26.0 +// protoc-gen-go v1.27.1 // protoc v3.19.1 // source: protos/ezshare.proto diff --git a/store/bolt.go b/store/bolt.go index 7003dce..6d25dfb 100644 --- a/store/bolt.go +++ b/store/bolt.go @@ -202,6 +202,8 @@ func (s *BoltStore) ListCertificates() ([]string, error) { return ids, nil } +var _ UserStore = &BoltStore{} + func (s *BoltStore) StoreUser(user *pb.User) error { return s.db.Update(func(tx *bolt.Tx) error { bkt := tx.Bucket(bktKeyUsers) @@ -241,3 +243,31 @@ func (s *BoltStore) ListUsers() ([]string, error) { return ids, err } + +func (s *BoltStore) GetUserByUsername(username string) (*pb.User, error) { + var user pb.User + err := s.db.View(func(tx *bolt.Tx) error { + bkt := tx.Bucket(bktKeyUsers) + c := bkt.Cursor() + + for k, v := c.First(); k != nil; c.Next() { + err := proto.Unmarshal(v, &user) + if err != nil { + // TODO: Log that db has invalid user + continue + } + if user.Username == username { + return nil + } + } + return nil + }) + if err != nil { + return nil, err + } + + if user.Username == username { + return &user, nil + } + return nil, ErrNoSuchFile +} diff --git a/store/bolt_test.go b/store/bolt_test.go index c34048f..b969bce 100644 --- a/store/bolt_test.go +++ b/store/bolt_test.go @@ -1,3 +1,4 @@ +//nolint:staticcheck package store_test import ( diff --git a/store/memory.go b/store/memory.go index 005e63b..0283875 100644 --- a/store/memory.go +++ b/store/memory.go @@ -182,3 +182,14 @@ func (s *MemoryStore) ListUsers() ([]string, error) { } return ids, nil } + +func (s *MemoryStore) GetUserByUsername(username string) (*pb.User, error) { + s.usersLock.RLock() + defer s.usersLock.RUnlock() + for _, user := range s.users { + if user.Username == username { + return user, nil + } + } + return nil, ErrNoSuchFile +} diff --git a/store/store.go b/store/store.go index a439b3c..7bddcf7 100644 --- a/store/store.go +++ b/store/store.go @@ -28,5 +28,6 @@ type CertificateStore interface { type UserStore interface { StoreUser(user *pb.User) error GetUser(id string) (*pb.User, error) + GetUserByUsername(username string) (*pb.User, error) ListUsers() ([]string, error) } diff --git a/store/store_test.go b/store/store_test.go index b778150..52b68b9 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -6,12 +6,14 @@ import ( "crypto/rand" "crypto/x509" "crypto/x509/pkix" - "github.com/google/uuid" - "google.golang.org/protobuf/proto" "math/big" "testing" "time" + "github.com/google/go-cmp/cmp" + "github.com/google/uuid" + "google.golang.org/protobuf/testing/protocmp" + "gitea.benny.dog/torjus/ezshare/pb" "gitea.benny.dog/torjus/ezshare/store" "google.golang.org/protobuf/types/known/timestamppb" @@ -162,8 +164,17 @@ func doUserStoreTests(s store.UserStore, t *testing.T) { t.Fatalf("Retriving user returned error: %s", err) } - if !proto.Equal(user, retrieved) { - t.Fatalf("Retrieved user does not match original") + if diff := cmp.Diff(user, retrieved, protocmp.Transform()); diff != "" { + t.Errorf("User retrieved by name difference:\n%v", diff) + } + + named, err := s.GetUserByUsername(user.Username) + if err != nil { + t.Fatalf("Retrieving user by username returned error: %s", err) + } + + if diff := cmp.Diff(user, named, protocmp.Transform()); diff != "" { + t.Errorf("User retrieved by name difference:\n%v", diff) } list, err := s.ListUsers()