Don't return nil, nil for API service methods. Add tests for updating w/ wrong version. (#396)

This commit is contained in:
Todd Knight authored and GitHub committed 2020-09-21 15:09:30 -04:00
1 parent 1adbe64160
commit 21f5cc274f
20 files changed
+71 -23

No files matched your search

+6
View File
@@ -174,6 +174,12 @@ func TestErrors(t *testing.T) {
assert.Nil(apiErr)
assert.NotNil(u)
// Updating the wrong version should fail.
_, apiErr, err = accountClient.Update(tc.Context(), u.Item.Id, 73, accounts.WithName("anything"))
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
// Create another resource with the same name.
_, apiErr, err = accountClient.Create(tc.Context(), amId, accounts.WithPasswordAccountLoginName("first"))
require.NoError(err)
+6
View File
@@ -197,6 +197,12 @@ func TestErrors(t *testing.T) {
assert.Nil(apiErr)
assert.NotNil(g)
// Updating the wrong version should fail.
_, apiErr, err = groupClient.Update(tc.Context(), g.Item.Id, 73, groups.WithName("anything"))
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
// Create another resource with the same name.
_, apiErr, err = groupClient.Create(tc.Context(), tt.scopeId, groups.WithName("first"))
require.NoError(err)
+6
View File
@@ -137,6 +137,12 @@ func TestErrors(t *testing.T) {
assert.Nil(apiErr)
assert.NotNil(hc)
// Updating the wrong version should fail.
_, apiErr, err = pc.Update(tc.Context(), hc.Item.Id, 73, hostcatalogs.WithName("anything"))
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = pc.Create(tc.Context(), "static", proj.GetPublicId(), hostcatalogs.WithName("foo"))
require.NoError(err)
assert.NotNil(apiErr)
+6
View File
@@ -155,6 +155,12 @@ func TestErrors(t *testing.T) {
require.Nil(apiErr)
assert.NotNil(h)
// Updating the wrong version should fail.
_, apiErr, err = hClient.Update(tc.Context(), h.Item.Id, 73, hosts.WithName("anything"))
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = hClient.Create(tc.Context(), hc.Item.Id, hosts.WithName("foo"), hosts.WithStaticHostAddress("someaddress"))
require.NoError(err)
assert.NotNil(apiErr)
+6
View File
@@ -200,6 +200,12 @@ func TestErrors(t *testing.T) {
require.Nil(apiErr)
assert.NotNil(h)
// Updating the wrong version should fail.
_, apiErr, err = hClient.Update(tc.Context(), h.Item.Id, 73, hostsets.WithName("anything"))
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
h, apiErr, err = hClient.Create(tc.Context(), hc.Item.Id, hostsets.WithName("foo"))
require.NoError(err)
assert.NotNil(apiErr)
+6
View File
@@ -292,6 +292,12 @@ func TestErrors(t *testing.T) {
assert.Nil(apiErr)
assert.NotNil(u)
// Updating the wrong version should fail.
_, apiErr, err = roleClient.Update(tc.Context(), u.Item.Id, 73, roles.WithName("anything"))
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
// Create another resource with the same name.
_, apiErr, err = roleClient.Create(tc.Context(), tt.scopeId, roles.WithName("first"))
require.NoError(err)
+6
View File
@@ -123,6 +123,12 @@ func TestErrors(t *testing.T) {
assert.NotNil(createdProj)
assert.Nil(apiErr)
// Updating the wrong version should fail.
_, apiErr, err = scps.Update(tc.Context(), createdProj.Item.Id, 73, scopes.WithName("anything"))
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
_, apiErr, err = scps.Read(tc.Context(), "p_doesntexis")
require.NoError(err)
// TODO: Should this be nil instead of just a Project that has no values set
+7 -1
View File
@@ -170,7 +170,6 @@ func TestCrud(t *testing.T) {
assert.EqualValues(http.StatusNotFound, apiErr.Status)
}
// TODO: Get better coverage for expected errors and error formats.
func TestSet_Errors(t *testing.T) {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
@@ -187,6 +186,13 @@ func TestSet_Errors(t *testing.T) {
require.NoError(err)
require.Nil(apiErr)
assert.NotNil(tar)
// Updating the wrong version should fail.
_, apiErr, err = tarClient.Update(tc.Context(), tar.Item.Id, 73, targets.WithName("anything"))
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
tar, apiErr, err = tarClient.Create(tc.Context(), "tcp", proj.GetPublicId(), targets.WithName("foo"))
require.NoError(err)
assert.NotNil(apiErr)
+6
View File
@@ -134,6 +134,12 @@ func TestErrors(t *testing.T) {
assert.Nil(apiErr)
assert.NotNil(u)
// Updating the wrong version should fail.
_, apiErr, err = userClient.Update(tc.Context(), u.Item.Id, 73, users.WithName("anything"))
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
// Create another resource with the same name.
_, apiErr, err = userClient.Create(tc.Context(), org.GetPublicId(), users.WithName("first"))
require.NoError(err)
@@ -129,7 +129,7 @@ func (s Service) DeleteAccount(ctx context.Context, req *pbs.DeleteAccountReques
if err != nil {
return nil, err
}
return nil, nil
return &pbs.DeleteAccountResponse{}, nil
}
// ChangePassword implements the interface pbs.AccountServiceServer.
@@ -134,7 +134,7 @@ func (s Service) DeleteAuthMethod(ctx context.Context, req *pbs.DeleteAuthMethod
if err != nil {
return nil, err
}
return nil, nil
return &pbs.DeleteAuthMethodResponse{}, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.AuthMethod, error) {
@@ -87,7 +87,7 @@ func (s Service) DeleteAuthToken(ctx context.Context, req *pbs.DeleteAuthTokenRe
if err != nil {
return nil, err
}
return nil, nil
return &pbs.DeleteAuthTokenResponse{}, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.AuthToken, error) {
@@ -130,7 +130,7 @@ func (s Service) DeleteGroup(ctx context.Context, req *pbs.DeleteGroupRequest) (
if err != nil {
return nil, err
}
return nil, nil
return &pbs.DeleteGroupResponse{}, nil
}
// AddGroupMembers implements the interface pbs.GroupServiceServer.
@@ -135,7 +135,7 @@ func (s Service) DeleteHostCatalog(ctx context.Context, req *pbs.DeleteHostCatal
if err != nil {
return nil, err
}
return nil, nil
return &pbs.DeleteHostCatalogResponse{}, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.HostCatalog, error) {
@@ -130,7 +130,7 @@ func (s Service) DeleteHostSet(ctx context.Context, req *pbs.DeleteHostSetReques
if err != nil {
return nil, err
}
return nil, nil
return &pbs.DeleteHostSetResponse{}, nil
}
// AddHostSetHosts implements the interface pbs.HostSetServiceServer.
@@ -132,7 +132,7 @@ func (s Service) DeleteHost(ctx context.Context, req *pbs.DeleteHostRequest) (*p
if err != nil {
return nil, err
}
return nil, nil
return &pbs.DeleteHostResponse{}, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Host, error) {
@@ -131,7 +131,7 @@ func (s Service) DeleteRole(ctx context.Context, req *pbs.DeleteRoleRequest) (*p
if err != nil {
return nil, err
}
return nil, nil
return &pbs.DeleteRoleResponse{}, nil
}
// AddRolePrincipals implements the interface pbs.RoleServiceServer.
@@ -136,7 +136,7 @@ func (s Service) DeleteScope(ctx context.Context, req *pbs.DeleteScopeRequest) (
if err != nil {
return nil, err
}
return nil, nil
return &pbs.DeleteScopeResponse{}, nil
}
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Scope, error) {
@@ -120,9 +120,6 @@ func (s Service) GetTarget(ctx context.Context, req *pbs.GetTargetRequest) (*pbs
if err != nil {
return nil, err
}
if u == nil {
return nil, nil
}
u.Scope = authResults.Scope
return &pbs.GetTargetResponse{Item: u}, nil
}
@@ -157,9 +154,6 @@ func (s Service) UpdateTarget(ctx context.Context, req *pbs.UpdateTargetRequest)
if err != nil {
return nil, err
}
if u == nil {
return nil, nil
}
u.Scope = authResults.Scope
return &pbs.UpdateTargetResponse{Item: u}, nil
}
@@ -177,7 +171,7 @@ func (s Service) DeleteTarget(ctx context.Context, req *pbs.DeleteTargetRequest)
if err != nil {
return nil, err
}
return nil, nil
return &pbs.DeleteTargetResponse{}, nil
}
// AddTargetHostSets implements the interface pbs.TargetServiceServer.
@@ -265,12 +259,12 @@ func (s Service) AuthorizeSession(ctx context.Context, req *pbs.AuthorizeSession
t, hostSets, err := repo.LookupTarget(ctx, req.GetId())
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
return nil, nil
return nil, handlers.NotFoundErrorf("Target %q not found.", req.GetId())
}
return nil, err
}
if t == nil {
return nil, nil
return nil, handlers.NotFoundErrorf("Target %q not found.", req.GetId())
}
// Instantiate some repos
@@ -427,12 +421,12 @@ func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Target, error)
u, m, err := repo.LookupTarget(ctx, id)
if err != nil {
if errors.Is(err, db.ErrRecordNotFound) {
return nil, nil
return nil, handlers.NotFoundErrorf("Target %q doesn't exist.", id)
}
return nil, err
}
if u == nil {
return nil, nil
return nil, handlers.NotFoundErrorf("Target %q doesn't exist.", id)
}
return toProto(u, m)
}
@@ -513,7 +507,7 @@ func (s Service) updateInRepo(ctx context.Context, scopeId, id string, mask []st
return nil, status.Errorf(codes.Internal, "Unable to update target: %v.", err)
}
if rowsUpdated == 0 {
return nil, nil
return nil, handlers.NotFoundErrorf("Target %q not found or incorrect version provided.", id)
}
return toProto(out, m)
}
+1 -1
View File
@@ -228,7 +228,7 @@ func (w *Worker) closeConnection(ctx context.Context, req *pbs.CloseConnectionRe
}
func (w *Worker) closeConnections(ctx context.Context, closeMap map[string]string) error {
w.logger.Trace("marking connections as closed", "session_and_connection_ids", fmt.Sprint("%#v", closeMap))
w.logger.Trace("marking connections as closed", "session_and_connection_ids", fmt.Sprintf("%#v", closeMap))
closeData := make([]*pbs.CloseConnectionRequestData, 0, len(closeMap))
for connId := range closeMap {