catch sql.ErrNoRows in mysql/invites (#667)

This commit is contained in:
Victor Vrantchan 2016-12-20 16:31:09 -05:00 committed by GitHub
parent 6bd4173475
commit 41610c1f66
7 changed files with 67 additions and 22 deletions

View File

@ -1,11 +1,13 @@
package datastore
import (
"errors"
"fmt"
"testing"
"github.com/kolide/kolide-ose/server/kolide"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testCreateInvite(t *testing.T, ds kolide.Datastore) {
@ -127,3 +129,35 @@ func testSaveInvite(t *testing.T, ds kolide.Datastore) {
assert.True(t, invite.Admin)
}
func testInviteByEmail(t *testing.T, ds kolide.Datastore) {
setupTestInvites(t, ds)
var inviteTests = []struct {
email string
wantErr error
}{
{
email: "user0@foo.com",
},
{
email: "nosuchuser@nosuchdomain.com",
wantErr: errors.New("Invite with email nosuchuser@nosuchdomain.com was not found in the datastore"),
},
}
for _, tt := range inviteTests {
t.Run("", func(t *testing.T) {
invite, err := ds.InviteByEmail(tt.email)
if tt.wantErr != nil {
require.NotNil(t, err)
assert.Equal(t, tt.wantErr.Error(), err.Error())
return
} else {
require.Nil(t, err)
}
assert.NotEqual(t, invite.ID, 0)
})
}
}

View File

@ -18,6 +18,7 @@ func functionName(f func(*testing.T, kolide.Datastore)) string {
var testFunctions = [...]func(*testing.T, kolide.Datastore){
testOrgInfo,
testCreateInvite,
testInviteByEmail,
testListInvites,
testDeleteInvite,
testSaveInvite,

View File

@ -15,13 +15,13 @@ func notFound(kind string) *notFoundError {
}
func (e *notFoundError) Error() string {
if e.ID == 0 {
return fmt.Sprintf("%s was not found in the datastore", e.ResourceType)
if e.ID != 0 {
return fmt.Sprintf("%s %d was not found in the datastore", e.ResourceType, e.ID)
}
if e.Message != "" {
return fmt.Sprintf("%s, %s was not found in the datastore", e.ResourceType, e.Message)
return fmt.Sprintf("%s %s was not found in the datastore", e.ResourceType, e.Message)
}
return fmt.Sprintf("%s %d was not found in the datastore", e.ResourceType, e.ID)
return fmt.Sprintf("%s was not found in the datastore", e.ResourceType)
}
func (e *notFoundError) WithID(id uint) error {

View File

@ -90,7 +90,7 @@ func (d *Datastore) InviteByEmail(email string) (*kolide.Invite, error) {
}
}
return nil, notFound("Invite").
WithMessage(fmt.Sprintf("with email address: %s", email))
WithMessage(fmt.Sprintf("with email %s", email))
}
// SaveInvite saves an invitation in the datastore.

View File

@ -15,13 +15,13 @@ func notFound(kind string) *notFoundError {
}
func (e *notFoundError) Error() string {
if e.ID == 0 {
return fmt.Sprintf("%s was not found in the datastore", e.ResourceType)
if e.ID != 0 {
return fmt.Sprintf("%s %d was not found in the datastore", e.ResourceType, e.ID)
}
if e.Message != "" {
return fmt.Sprintf("%s, %s was not found in the datastore", e.ResourceType, e.Message)
return fmt.Sprintf("%s %s was not found in the datastore", e.ResourceType, e.Message)
}
return fmt.Sprintf("%s %d was not found in the datastore", e.ResourceType, e.ID)
return fmt.Sprintf("%s was not found in the datastore", e.ResourceType)
}
func (e *notFoundError) WithID(id uint) error {

View File

@ -1,8 +1,11 @@
package mysql
import (
"github.com/kolide/kolide-ose/server/errors"
"database/sql"
"fmt"
"github.com/kolide/kolide-ose/server/kolide"
"github.com/pkg/errors"
)
// NewInvite generates a new invitation
@ -16,7 +19,7 @@ func (d *Datastore) NewInvite(i *kolide.Invite) (*kolide.Invite, error) {
result, err := d.db.Exec(sql, i.InvitedBy, i.Email, i.Admin,
i.Name, i.Position, i.Token)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "create invite")
}
id, _ := result.LastInsertId()
@ -32,10 +35,12 @@ func (d *Datastore) ListInvites(opt kolide.ListOptions) ([]*kolide.Invite, error
invites := []*kolide.Invite{}
sql := appendListOptionsToSQL("SELECT * FROM invites WHERE NOT deleted", opt)
err := d.db.Select(&invites, sql)
if err != nil {
return nil, errors.DatabaseError(err)
query := appendListOptionsToSQL("SELECT * FROM invites WHERE NOT deleted", opt)
err := d.db.Select(&invites, query)
if err != nil && err == sql.ErrNoRows {
return nil, notFound("Invite")
} else if err != nil {
return nil, errors.Wrap(err, "select invite by ID")
}
return invites, nil
}
@ -44,8 +49,10 @@ func (d *Datastore) ListInvites(opt kolide.ListOptions) ([]*kolide.Invite, error
func (d *Datastore) Invite(id uint) (*kolide.Invite, error) {
invite := &kolide.Invite{}
err := d.db.Get(invite, "SELECT * FROM invites WHERE id = ? AND NOT deleted", id)
if err != nil {
return nil, errors.DatabaseError(err)
if err != nil && err == sql.ErrNoRows {
return nil, notFound("Invite").WithID(id)
} else if err != nil {
return nil, errors.Wrap(err, "select invite by ID")
}
return invite, nil
}
@ -54,8 +61,11 @@ func (d *Datastore) Invite(id uint) (*kolide.Invite, error) {
func (d *Datastore) InviteByEmail(email string) (*kolide.Invite, error) {
invite := &kolide.Invite{}
err := d.db.Get(invite, "SELECT * FROM invites WHERE email = ? AND NOT deleted", email)
if err != nil {
return nil, errors.DatabaseError(err)
if err != nil && err == sql.ErrNoRows {
return nil, notFound("Invite").
WithMessage(fmt.Sprintf("with email %s", email))
} else if err != nil {
return nil, errors.Wrap(err, "sqlx get invite")
}
return invite, nil
}
@ -71,7 +81,7 @@ func (d *Datastore) SaveInvite(i *kolide.Invite) error {
i.Admin, i.Name, i.Position, i.Token, i.ID,
)
if err != nil {
return errors.DatabaseError(err)
return errors.Wrap(err, "save invite")
}
return nil
@ -86,7 +96,7 @@ func (d *Datastore) DeleteInvite(i *kolide.Invite) error {
`
_, err := d.db.Exec(sql, i.DeletedAt, true, i.ID)
if err != nil {
return errors.DatabaseError(err)
return errors.Wrap(err, "delete invite")
}
return nil
}

View File

@ -156,7 +156,7 @@ func TestCreateUser(t *testing.T) {
NeedsPasswordReset: boolPtr(true),
Admin: boolPtr(false),
InviteToken: &invites["admin2@example.com"].Token,
wantErr: errors.New("Invite was not found in the datastore"),
wantErr: errors.New("Invite with email admin2@example.com was not found in the datastore"),
},
{
Username: stringPtr("admin3"),