Invites Service (#44)

This commit is contained in:
ben-toogood
2021-01-21 12:52:06 +00:00
committed by GitHub
parent 2f2658da9a
commit e6495ff6d7
13 changed files with 1491 additions and 1 deletions

165
invites/handler/invites.go Normal file
View File

@@ -0,0 +1,165 @@
package handler
import (
"context"
"math/rand"
"regexp"
"strconv"
"strings"
"github.com/google/uuid"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
pb "github.com/micro/services/invites/proto"
"gorm.io/gorm"
)
var (
ErrMissingID = errors.BadRequest("MISSING_ID", "Missing ID")
ErrMissingGroupID = errors.BadRequest("MISSING_GROUP_ID", "Missing GroupID")
ErrInvalidEmail = errors.BadRequest("INVALID_EMAIL", "The email provided was invalid")
ErrMissingEmail = errors.BadRequest("MISSING_EMAIL", "Missing Email")
ErrMissingIDAndCode = errors.BadRequest("ID_OR_CODE_REQUIRED", "An email address code is required to read an invite")
ErrMissingGroupIDAndEmail = errors.BadRequest("GROUP_ID_OR_EMAIL_REQUIRED", "An email address or group id is needed to list invites")
ErrInviteNotFound = errors.NotFound("NOT_FOUND", "Invite not found")
emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
)
type Invite struct {
ID string
Email string `gorm:"uniqueIndex:group_email"`
GroupID string `gorm:"uniqueIndex:group_email"`
Code string `gorm:"uniqueIndex"`
}
func (i *Invite) Serialize() *pb.Invite {
return &pb.Invite{
Id: i.ID,
Email: i.Email,
GroupId: i.GroupID,
Code: i.Code,
}
}
type Invites struct {
DB *gorm.DB
}
// Create an invite
func (i *Invites) Create(ctx context.Context, req *pb.CreateRequest, rsp *pb.CreateResponse) error {
// validate the request
if len(req.GroupId) == 0 {
return ErrMissingGroupID
}
if len(req.Email) == 0 {
return ErrMissingEmail
}
if !isEmailValid(req.Email) {
return ErrInvalidEmail
}
// construct the invite and write to the db
invite := &Invite{
ID: uuid.New().String(),
Code: generateCode(),
GroupID: req.GroupId,
Email: req.Email,
}
if err := i.DB.Create(invite).Error; err != nil && strings.Contains(err.Error(), "group_email") {
} else if err != nil {
logger.Errorf("Error writing to the store: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to the database")
}
// serialize the response
rsp.Invite = invite.Serialize()
return nil
}
// Read an invite using ID or code
func (i *Invites) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
// validate the request
var query Invite
if req.Id != nil {
query.ID = req.Id.Value
} else if req.Code != nil {
query.Code = req.Code.Value
} else {
return ErrMissingIDAndCode
}
// query the database
var invite Invite
if err := i.DB.Where(&query).First(&invite).Error; err == gorm.ErrRecordNotFound {
return ErrInviteNotFound
} else if err != nil {
logger.Errorf("Error reading from the store: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to the database")
}
// serialize the response
rsp.Invite = invite.Serialize()
return nil
}
// List invited for a group or specific email
func (i *Invites) List(ctx context.Context, req *pb.ListRequest, rsp *pb.ListResponse) error {
// validate the request
if req.Email == nil && req.GroupId == nil {
return ErrMissingGroupIDAndEmail
}
// construct the query
var query Invite
if req.GroupId != nil {
query.GroupID = req.GroupId.Value
}
if req.Email != nil {
query.Email = req.Email.Value
}
// query the database
var invites []Invite
if err := i.DB.Where(&query).Find(&invites).Error; err != nil {
logger.Errorf("Error reading from the store: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to the database")
}
// serialize the response
rsp.Invites = make([]*pb.Invite, len(invites))
for i, inv := range invites {
rsp.Invites[i] = inv.Serialize()
}
return nil
}
// Delete an invite
func (i *Invites) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
// validate the request
if len(req.Id) == 0 {
return ErrMissingID
}
// delete from the database
if err := i.DB.Where(&Invite{ID: req.Id}).Delete(&Invite{}).Error; err != nil {
logger.Errorf("Error deleting from the store: %v", err)
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to the database")
}
return nil
}
// isEmailValid checks if the email provided passes the required structure and length.
func isEmailValid(e string) bool {
if len(e) < 3 && len(e) > 254 {
return false
}
return emailRegex.MatchString(e)
}
// generateCode generates a random 8 digit code
func generateCode() string {
v := rand.Intn(89999999) + 10000000
return strconv.Itoa(v)
}

View File

@@ -0,0 +1,267 @@
package handler_test
import (
"context"
"testing"
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/micro/services/invites/handler"
pb "github.com/micro/services/invites/proto"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
func testHandler(t *testing.T) *handler.Invites {
// connect to the database
db, err := gorm.Open(postgres.Open("postgresql://postgres@localhost:5432/invites?sslmode=disable"), &gorm.Config{})
if err != nil {
t.Fatalf("Error connecting to database: %v", err)
}
// migrate the database
if err := db.AutoMigrate(&handler.Invite{}); err != nil {
t.Fatalf("Error migrating database: %v", err)
}
// clean any data from a previous run
if err := db.Exec("TRUNCATE TABLE invites CASCADE").Error; err != nil {
t.Fatalf("Error cleaning database: %v", err)
}
return &handler.Invites{DB: db}
}
func TestCreate(t *testing.T) {
tt := []struct {
Name string
GroupID string
Email string
Error error
}{
{
Name: "MissingGroupID",
Email: "john@doe.com",
Error: handler.ErrMissingGroupID,
},
{
Name: "MissingEmail",
GroupID: uuid.New().String(),
Error: handler.ErrMissingEmail,
},
{
Name: "InvalidEmail",
GroupID: uuid.New().String(),
Email: "foo.foo.foo",
Error: handler.ErrInvalidEmail,
},
{
Name: "Valid",
GroupID: "thisisavalidgroupid",
Email: "john@doe.com",
},
{
Name: "Repeat",
GroupID: "thisisavalidgroupid",
Email: "john@doe.com",
},
}
h := testHandler(t)
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
var rsp pb.CreateResponse
err := h.Create(context.TODO(), &pb.CreateRequest{
GroupId: tc.GroupID, Email: tc.Email,
}, &rsp)
assert.Equal(t, tc.Error, err)
if tc.Error != nil {
assert.Nil(t, rsp.Invite)
return
}
if rsp.Invite == nil {
t.Fatalf("Invite was not returned")
return
}
assert.NotEmpty(t, rsp.Invite.Id)
assert.NotEmpty(t, rsp.Invite.Code)
assert.Equal(t, tc.GroupID, rsp.Invite.GroupId)
assert.Equal(t, tc.Email, rsp.Invite.Email)
})
}
}
func TestRead(t *testing.T) {
h := testHandler(t)
// seed some data
var cRsp pb.CreateResponse
err := h.Create(context.TODO(), &pb.CreateRequest{Email: "john@doe.com", GroupId: uuid.New().String()}, &cRsp)
assert.NoError(t, err)
if cRsp.Invite == nil {
t.Fatal("No invite returned on create")
return
}
tt := []struct {
Name string
ID *wrapperspb.StringValue
Code *wrapperspb.StringValue
Error error
Invite *pb.Invite
}{
{
Name: "MissingIDAndCode",
Error: handler.ErrMissingIDAndCode,
},
{
Name: "NotFoundByID",
ID: &wrapperspb.StringValue{Value: uuid.New().String()},
Error: handler.ErrInviteNotFound,
},
{
Name: "NotFoundByCode",
Code: &wrapperspb.StringValue{Value: "12345678"},
Error: handler.ErrInviteNotFound,
},
{
Name: "ValidID",
ID: &wrapperspb.StringValue{Value: cRsp.Invite.Id},
Invite: cRsp.Invite,
},
{
Name: "ValidCode",
Code: &wrapperspb.StringValue{Value: cRsp.Invite.Code},
Invite: cRsp.Invite,
},
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
var rsp pb.ReadResponse
err := h.Read(context.TODO(), &pb.ReadRequest{Id: tc.ID, Code: tc.Code}, &rsp)
assert.Equal(t, tc.Error, err)
if tc.Invite == nil {
assert.Nil(t, rsp.Invite)
} else {
assertInvitesMatch(t, tc.Invite, rsp.Invite)
}
})
}
}
func TestList(t *testing.T) {
h := testHandler(t)
// seed some data
var cRsp pb.CreateResponse
err := h.Create(context.TODO(), &pb.CreateRequest{Email: "john@doe.com", GroupId: uuid.New().String()}, &cRsp)
assert.NoError(t, err)
if cRsp.Invite == nil {
t.Fatal("No invite returned on create")
return
}
tt := []struct {
Name string
GroupID *wrapperspb.StringValue
Email *wrapperspb.StringValue
Error error
Invite *pb.Invite
}{
{
Name: "MissingIDAndEmail",
Error: handler.ErrMissingGroupIDAndEmail,
},
{
Name: "NoResultsForEmail",
Email: &wrapperspb.StringValue{Value: "foo@bar.com"},
},
{
Name: "NoResultsForGroupID",
GroupID: &wrapperspb.StringValue{Value: uuid.New().String()},
},
{
Name: "ValidGroupID",
GroupID: &wrapperspb.StringValue{Value: cRsp.Invite.GroupId},
Invite: cRsp.Invite,
},
{
Name: "ValidEmail",
Email: &wrapperspb.StringValue{Value: cRsp.Invite.Email},
Invite: cRsp.Invite,
},
{
Name: "EmailAndGroupID",
Email: &wrapperspb.StringValue{Value: cRsp.Invite.Email},
GroupID: &wrapperspb.StringValue{Value: uuid.New().String()},
},
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
var rsp pb.ListResponse
err := h.List(context.TODO(), &pb.ListRequest{Email: tc.Email, GroupId: tc.GroupID}, &rsp)
assert.Equal(t, tc.Error, err)
if tc.Invite == nil {
assert.Empty(t, rsp.Invites)
} else {
if len(rsp.Invites) != 1 {
t.Errorf("Incorrect number of invites returned, expected 1 but got %v", len(rsp.Invites))
return
}
assertInvitesMatch(t, tc.Invite, rsp.Invites[0])
}
})
}
}
func TestDelete(t *testing.T) {
h := testHandler(t)
t.Run("MissingID", func(t *testing.T) {
err := h.Delete(context.TODO(), &pb.DeleteRequest{}, &pb.DeleteResponse{})
assert.Equal(t, handler.ErrMissingID, err)
})
// seed some data
var cRsp pb.CreateResponse
err := h.Create(context.TODO(), &pb.CreateRequest{Email: "john@doe.com", GroupId: uuid.New().String()}, &cRsp)
assert.NoError(t, err)
if cRsp.Invite == nil {
t.Fatal("No invite returned on create")
return
}
t.Run("Valid", func(t *testing.T) {
err := h.Delete(context.TODO(), &pb.DeleteRequest{Id: cRsp.Invite.Id}, &pb.DeleteResponse{})
assert.NoError(t, err)
err = h.Read(context.TODO(), &pb.ReadRequest{Id: &wrapperspb.StringValue{Value: cRsp.Invite.Id}}, &pb.ReadResponse{})
assert.Equal(t, handler.ErrInviteNotFound, err)
})
t.Run("Repeat", func(t *testing.T) {
err := h.Delete(context.TODO(), &pb.DeleteRequest{Id: cRsp.Invite.Id}, &pb.DeleteResponse{})
assert.NoError(t, err)
})
}
func assertInvitesMatch(t *testing.T, exp, act *pb.Invite) {
if act == nil {
t.Errorf("No invite returned")
return
}
assert.Equal(t, exp.Id, act.Id)
assert.Equal(t, exp.Code, act.Code)
assert.Equal(t, exp.Email, act.Email)
assert.Equal(t, exp.GroupId, act.GroupId)
}