Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions datastores/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (ds Mysql) FindProfileByKeycloakID(keycloakId string) (*proto.Profile, erro

profiles, err := ds.processProfiles([]milpacs.Profile{profile})
if err != nil {
return nil, fmt.Errorf("error generating profile")
return nil, fmt.Errorf("error generating profile: %w", err)
}

return profiles[profile.RelationId], nil
Expand All @@ -134,7 +134,7 @@ func (ds Mysql) FindProfileByDiscordID(discordId string) (*proto.Profile, error)

profiles, err := ds.processProfiles([]milpacs.Profile{profile})
if err != nil {
return nil, fmt.Errorf("error generating profile")
return nil, fmt.Errorf("error generating profile: %w", err)
}

return profiles[profile.RelationId], nil
Expand Down Expand Up @@ -356,7 +356,7 @@ func (ds Mysql) FindS1UniformsRosterByType(rosterType proto.RosterType) (*proto.
milpac, err := ds.generateS1UniformsProtoProfile(profile)

if err != nil {
return nil, fmt.Errorf("error generating profile")
return nil, fmt.Errorf("error generating profile: %w", err)
}
profiles[profile.RelationId] = milpac
}
Expand Down
10 changes: 5 additions & 5 deletions servers/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (server *MilpacsService) GetRoster(ctx context.Context, request *proto.Rost
return nil, err
}
if request.Roster == proto.RosterType_ROSTER_TYPE_UNSPECIFIED {
return nil, errors.New("cannot request null roster type")
return nil, status.Errorf(codes.InvalidArgument, "cannot request null roster type")
}

roster, err := server.Datastore.FindRosterByType(request.Roster)
Expand All @@ -98,7 +98,7 @@ func (server *MilpacsService) GetUserViaKeycloakId(ctx context.Context, request
}

if request.GetKeycloakId() == "" {
Warn.Println("Empty Keycloak ID provided, cannot return profile")
return nil, status.Errorf(codes.InvalidArgument, "keycloak id cannot be empty")
}

profile, err := server.Datastore.FindProfileByKeycloakID(request.GetKeycloakId())
Expand All @@ -119,7 +119,7 @@ func (server *MilpacsService) GetUserViaDiscordId(ctx context.Context, request *
}

if request.GetDiscordId() == "" {
Warn.Println("Empty Discord ID provided, cannot return profile")
return nil, status.Errorf(codes.InvalidArgument, "discord id cannot be empty")
}

profile, err := server.Datastore.FindProfileByDiscordID(request.GetDiscordId())
Expand All @@ -138,7 +138,7 @@ func (server *MilpacsService) GetLiteRoster(ctx context.Context, request *proto.
return nil, err
}
if request.Roster == proto.RosterType_ROSTER_TYPE_UNSPECIFIED {
return nil, errors.New("cannot request null roster type")
return nil, status.Errorf(codes.InvalidArgument, "cannot request null roster type")
}

roster, err := server.Datastore.FindLiteRosterByType(request.Roster)
Expand Down Expand Up @@ -170,7 +170,7 @@ func (server *MilpacsService) GetS1UniformsRoster(ctx context.Context, request *
return nil, err
}
if request.Roster == proto.RosterType_ROSTER_TYPE_UNSPECIFIED {
return nil, errors.New("cannot request null roster type")
return nil, status.Errorf(codes.InvalidArgument, "cannot request null roster type")
}

roster, err := server.Datastore.FindS1UniformsRosterByType(request.Roster)
Expand Down
56 changes: 56 additions & 0 deletions servers/grpc/milpacs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,62 @@ func TestGetGamertagProfile_DatastoreError(t *testing.T) {
assert.Equal(t, codes.Internal, st.Code())
}

func TestGetUserViaKeycloakId_EmptyID_InvalidArgument(t *testing.T) {
// findProfileByKeycloakID left unset: a fall-through to the datastore
// nil-derefs and panics, proving the handler early-returns instead.
svc := &MilpacsService{Datastore: &fakeDatastore{}}
_, err := svc.GetUserViaKeycloakId(withMilpacsKey("read"), &proto.KeycloakIdRequest{KeycloakId: ""})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.InvalidArgument, st.Code())
}

func TestGetUserViaDiscordId_EmptyID_InvalidArgument(t *testing.T) {
svc := &MilpacsService{Datastore: &fakeDatastore{}}
_, err := svc.GetUserViaDiscordId(withMilpacsKey("read"), &proto.DiscordIdRequest{DiscordId: ""})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.InvalidArgument, st.Code())
}

func TestGetGamertagProfile_Empty_InvalidArgument(t *testing.T) {
svc := &MilpacsService{Datastore: &fakeDatastore{}}
_, err := svc.GetGamertagProfile(withMilpacsKey("read"), &proto.GamertagRequest{Gamertag: ""})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.InvalidArgument, st.Code())
}

func TestGetRoster_NullType_InvalidArgument(t *testing.T) {
svc := &MilpacsService{Datastore: &fakeDatastore{}}
_, err := svc.GetRoster(withMilpacsKey("read"), &proto.RosterRequest{Roster: proto.RosterType_ROSTER_TYPE_UNSPECIFIED})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.InvalidArgument, st.Code())
}

func TestGetLiteRoster_NullType_InvalidArgument(t *testing.T) {
svc := &MilpacsService{Datastore: &fakeDatastore{}}
_, err := svc.GetLiteRoster(withMilpacsKey("read"), &proto.RosterRequest{Roster: proto.RosterType_ROSTER_TYPE_UNSPECIFIED})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.InvalidArgument, st.Code())
}

func TestGetS1UniformsRoster_NullType_InvalidArgument(t *testing.T) {
svc := &MilpacsService{Datastore: &fakeDatastore{}}
_, err := svc.GetS1UniformsRoster(withMilpacsKey("read"), &proto.RosterRequest{Roster: proto.RosterType_ROSTER_TYPE_UNSPECIFIED})
require.Error(t, err)
st, ok := status.FromError(err)
require.True(t, ok)
assert.Equal(t, codes.InvalidArgument, st.Code())
}

func TestGetRoster_DatastoreError(t *testing.T) {
svc := &MilpacsService{Datastore: &fakeDatastore{
findRosterByType: func(proto.RosterType) (*proto.Roster, error) {
Expand Down