Add GetByUsername to user store

This commit is contained in:
Torjus Håkestad 2021-12-05 12:39:28 +01:00
parent be230233dc
commit 6d6d307a91
8 changed files with 63 additions and 6 deletions

2
go.mod
View File

@ -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
)

3
go.sum
View File

@ -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=

View File

@ -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

View File

@ -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
}

View File

@ -1,3 +1,4 @@
//nolint:staticcheck
package store_test
import (

View File

@ -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
}

View File

@ -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)
}

View File

@ -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()