* Add error handler that converts from rpc.Status errors to Watchtower API errors. * Better testing for ErrorHandler. * Rename testcase and remove empty default switch case. * Change the error status back to int64. * Converting everything to int32s. jsonpb wraps int64s as a string which we dont like. We'll figure out how to use values larger than int64s when it comes up. * Remove special casing for TraceId which isn't needed anymore. * Removing wrappers from error details since we never need to know when they are set or unset from the end user. * Using the helper error functions inside the project service. * Correct usage of hclog, replace panic with Error log. * Adding periods to all API returned errors, correct spelling, fix missed invalid argument error not using helper function. * Change our logged errors to Error instead of Warn. * Add TODO for defining and using our own defined API error codes. * Add TODO to remove internal error messages.
76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package scopes
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/watchtower/api"
|
|
"github.com/hashicorp/watchtower/internal/servers/controller"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestProjects_Crud(t *testing.T) {
|
|
tc := controller.NewTestController(t, nil)
|
|
defer tc.Shutdown()
|
|
|
|
client := tc.Client()
|
|
org := &Organization{
|
|
Client: client,
|
|
}
|
|
|
|
checkProject := func(step string, p *Project, apiErr *api.Error, err error, wantedName string) {
|
|
assert := assert.New(t)
|
|
assert.NoError(err, step)
|
|
assert.Nil(apiErr, step)
|
|
assert.NotNil(p, "returned project", step)
|
|
assert.Equal(wantedName, *p.Name, step)
|
|
}
|
|
|
|
p, apiErr, err := org.CreateProject(tc.Context(), &Project{Name: api.String("foo")})
|
|
checkProject("create", p, apiErr, err, "foo")
|
|
|
|
p, apiErr, err = org.ReadProject(tc.Context(), &Project{Id: p.Id})
|
|
checkProject("read", p, apiErr, err, "foo")
|
|
|
|
p = &Project{Id: p.Id}
|
|
p.Name = api.String("bar")
|
|
p, apiErr, err = org.UpdateProject(tc.Context(), p)
|
|
checkProject("update", p, apiErr, err, "bar")
|
|
|
|
existed, apiErr, err := org.DeleteProject(tc.Context(), p)
|
|
assert.NoError(t, err)
|
|
assert.True(t, existed, "Expected existing project when deleted, but it wasn't.")
|
|
|
|
existed, apiErr, err = org.DeleteProject(tc.Context(), p)
|
|
assert.NoError(t, err)
|
|
assert.False(t, existed, "Expected project to not exist when deleted, but it did.")
|
|
}
|
|
|
|
// TODO: Get better coverage for expected errors and error formats.
|
|
func TestProject_Errors(t *testing.T) {
|
|
assert := assert.New(t)
|
|
tc := controller.NewTestController(t, nil)
|
|
defer tc.Shutdown()
|
|
ctx := tc.Context()
|
|
|
|
client := tc.Client()
|
|
org := &Organization{
|
|
Client: client,
|
|
}
|
|
createdProj, apiErr, err := org.CreateProject(ctx, &Project{})
|
|
assert.NoError(err)
|
|
assert.NotNil(createdProj)
|
|
assert.Nil(apiErr)
|
|
|
|
_, apiErr, err = org.ReadProject(ctx, &Project{Id: "p_doesntexis"})
|
|
assert.NoError(err)
|
|
// TODO: Should this be nil instead of just a Project that has no values set
|
|
assert.NotNil(apiErr)
|
|
assert.EqualValues(*apiErr.Status, http.StatusNotFound)
|
|
|
|
_, apiErr, err = org.ReadProject(ctx, &Project{Id: "invalid id"})
|
|
assert.NoError(err)
|
|
assert.NotNil(apiErr)
|
|
assert.EqualValues(*apiErr.Status, http.StatusBadRequest)
|
|
}
|