Files
threshold/api/scopes/project_test.go
T

146 lines
4.4 KiB
Go
Raw Normal View History

2020-06-05 13:02:58 -07:00
package scopes_test
2020-05-15 10:19:55 -04:00
import (
"fmt"
"net/http"
"testing"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/scopes"
"github.com/hashicorp/boundary/internal/servers/controller"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2020-07-02 10:26:31 -07:00
func TestProjects_List(t *testing.T) {
assert, require := assert.New(t), require.New(t)
orgId := "o_1234567890"
amId := "paum_1234567890"
tc := controller.NewTestController(t, &controller.TestControllerOpts{
DisableAuthorizationFailures: true,
DefaultOrgId: orgId,
DefaultAuthMethodId: amId,
2020-08-11 17:25:00 -04:00
DefaultLoginName: "user",
DefaultPassword: "passpass",
})
2020-07-02 10:26:31 -07:00
defer tc.Shutdown()
client := tc.Client()
scps := scopes.NewScopesClient(client)
2020-07-02 10:26:31 -07:00
pl, apiErr, err := scps.List(tc.Context(), orgId)
require.NoError(err)
2020-07-02 10:26:31 -07:00
assert.Nil(apiErr)
assert.Empty(pl)
expected := make([]*scopes.Scope, 10)
2020-07-02 10:26:31 -07:00
for i := 0; i < 10; i++ {
expected[i], apiErr, err = scps.Create(tc.Context(), orgId, scopes.WithName(fmt.Sprintf("%d", i)))
require.NoError(err)
2020-07-02 10:26:31 -07:00
assert.Nil(apiErr)
}
pl, apiErr, err = scps.List(tc.Context(), orgId)
require.NoError(err)
assert.Nil(apiErr)
2020-07-02 10:26:31 -07:00
assert.ElementsMatch(comparableSlice(expected), comparableSlice(pl))
}
func comparableSlice(in []*scopes.Scope) []scopes.Scope {
var filtered []scopes.Scope
2020-07-02 10:26:31 -07:00
for _, i := range in {
p := scopes.Scope{
2020-07-02 10:26:31 -07:00
Id: i.Id,
Name: i.Name,
Description: i.Description,
CreatedTime: i.CreatedTime,
UpdatedTime: i.UpdatedTime,
}
filtered = append(filtered, p)
}
return filtered
}
2020-05-15 10:19:55 -04:00
func TestProjects_Crud(t *testing.T) {
assert, require := assert.New(t), require.New(t)
orgId := "o_1234567890"
amId := "paum_1234567890"
tc := controller.NewTestController(t, &controller.TestControllerOpts{
DisableAuthorizationFailures: true,
DefaultOrgId: orgId,
DefaultAuthMethodId: amId,
2020-08-11 17:25:00 -04:00
DefaultLoginName: "user",
DefaultPassword: "passpass",
})
2020-05-15 10:19:55 -04:00
defer tc.Shutdown()
client := tc.Client()
scps := scopes.NewScopesClient(client)
2020-05-15 10:19:55 -04:00
checkProject := func(step string, s *scopes.Scope, apiErr *api.Error, err error, wantedName string, wantedVersion uint32) {
require.NoError(err, step)
2020-05-15 10:19:55 -04:00
assert.Nil(apiErr, step)
assert.NotNil(s, "returned project", step)
2020-06-05 13:02:58 -07:00
gotName := ""
if s.Name != "" {
gotName = s.Name
2020-06-05 13:02:58 -07:00
}
assert.Equal(wantedName, gotName, step)
assert.Equal(wantedVersion, s.Version)
2020-05-15 10:19:55 -04:00
}
s, apiErr, err := scps.Create(tc.Context(), orgId, scopes.WithName("foo"))
checkProject("create", s, apiErr, err, "foo", 1)
2020-05-15 10:19:55 -04:00
s, apiErr, err = scps.Read(tc.Context(), s.Id)
checkProject("read", s, apiErr, err, "foo", 1)
2020-05-19 12:10:39 -07:00
s, apiErr, err = scps.Update(tc.Context(), s.Id, s.Version, scopes.WithName("bar"))
checkProject("update", s, apiErr, err, "bar", 2)
2020-05-15 10:19:55 -04:00
s, apiErr, err = scps.Update(tc.Context(), s.Id, s.Version, scopes.DefaultName())
checkProject("update, unset name", s, apiErr, err, "", 3)
2020-06-05 13:02:58 -07:00
existed, apiErr, err := scps.Delete(tc.Context(), s.Id)
require.NoError(err)
assert.Nil(apiErr)
assert.True(existed, "Expected existing project when deleted, but it wasn't.")
2020-05-20 08:04:23 -07:00
_, apiErr, err = scps.Delete(tc.Context(), s.Id)
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status, "Expected project to not exist when deleted, but it did.")
2020-05-26 13:46:21 -07:00
}
// TODO: Get better coverage for expected errors and error formats.
func TestProject_Errors(t *testing.T) {
assert, require := assert.New(t), require.New(t)
orgId := "o_1234567890"
amId := "paum_1234567890"
tc := controller.NewTestController(t, &controller.TestControllerOpts{
DisableAuthorizationFailures: true,
DefaultOrgId: orgId,
DefaultAuthMethodId: amId,
2020-08-11 17:25:00 -04:00
DefaultLoginName: "user",
DefaultPassword: "passpass",
})
2020-05-26 13:46:21 -07:00
defer tc.Shutdown()
client := tc.Client()
scps := scopes.NewScopesClient(client)
createdProj, apiErr, err := scps.Create(tc.Context(), orgId)
require.NoError(err)
2020-05-26 13:46:21 -07:00
assert.NotNil(createdProj)
assert.Nil(apiErr)
_, apiErr, err = scps.Read(tc.Context(), "p_doesntexis")
require.NoError(err)
2020-05-26 13:46:21 -07:00
// TODO: Should this be nil instead of just a Project that has no values set
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
2020-05-15 10:19:55 -04:00
_, apiErr, err = scps.Read(tc.Context(), "invalid id")
2020-05-26 13:46:21 -07:00
assert.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusForbidden, apiErr.Status)
2020-05-15 10:19:55 -04:00
}