Files
threshold/internal/tests/api/scopes/scope_test.go
T

145 lines
4.2 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"
2020-08-17 15:38:41 -04:00
"github.com/hashicorp/boundary/internal/iam"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/servers/controller"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestList(t *testing.T) {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
2020-07-02 10:26:31 -07:00
defer tc.Shutdown()
client := tc.Client()
token := tc.Token()
client.SetToken(token.Token)
org := iam.TestOrg(t, tc.IamRepo(), iam.WithUserId(token.UserId))
2020-08-17 15:38:41 -04:00
scps := scopes.NewClient(client)
2020-07-02 10:26:31 -07:00
2020-09-24 14:34:58 -07:00
pl, err := scps.List(tc.Context(), org.GetPublicId())
require.NoError(err)
assert.Empty(pl.Items)
2020-07-02 10:26:31 -07:00
expected := make([]*scopes.Scope, 10)
2020-07-02 10:26:31 -07:00
for i := 0; i < 10; i++ {
2020-09-24 14:34:58 -07:00
scr, err := scps.Create(tc.Context(), org.GetPublicId(), scopes.WithName(fmt.Sprintf("%d", i)))
require.NoError(err)
expected[i] = scr.Item
2020-07-02 10:26:31 -07:00
}
2020-09-24 14:34:58 -07:00
pl, err = scps.List(tc.Context(), org.GetPublicId())
require.NoError(err)
assert.ElementsMatch(comparableSlice(expected), comparableSlice(pl.Items))
2021-02-22 14:37:14 -07:00
filterItem := pl.Items[3]
pl, err = scps.List(tc.Context(), org.GetPublicId(),
scopes.WithFilter(fmt.Sprintf(`"/item/id"==%q`, filterItem.Id)))
require.NoError(err)
assert.Len(pl.Items, 1)
assert.Equal(filterItem.Id, pl.Items[0].Id)
2020-07-02 10:26:31 -07:00
}
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,
ScopeId: i.ScopeId,
2020-07-02 10:26:31 -07:00
Name: i.Name,
Description: i.Description,
CreatedTime: i.CreatedTime,
UpdatedTime: i.UpdatedTime,
}
filtered = append(filtered, p)
}
return filtered
}
func TestCrud(t *testing.T) {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
2020-05-15 10:19:55 -04:00
defer tc.Shutdown()
client := tc.Client()
token := tc.Token()
client.SetToken(token.Token)
org, _ := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
2020-08-17 15:38:41 -04:00
scps := scopes.NewClient(client)
2020-05-15 10:19:55 -04:00
2020-09-24 14:34:58 -07:00
checkProject := func(step string, s *scopes.Scope, err error, wantedName string, wantedVersion uint32) {
require.NoError(err, 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
}
2020-09-24 14:34:58 -07:00
s, err := scps.Create(tc.Context(), org.GetPublicId(), scopes.WithName("foo"))
checkProject("create", s.Item, err, "foo", 1)
2020-05-15 10:19:55 -04:00
2020-09-24 14:34:58 -07:00
s, err = scps.Read(tc.Context(), s.Item.Id)
checkProject("read", s.Item, err, "foo", 1)
2020-05-19 12:10:39 -07:00
2020-09-24 14:34:58 -07:00
s, err = scps.Update(tc.Context(), s.Item.Id, s.Item.Version, scopes.WithName("bar"))
checkProject("update", s.Item, err, "bar", 2)
2020-05-15 10:19:55 -04:00
2020-09-24 14:34:58 -07:00
s, err = scps.Update(tc.Context(), s.Item.Id, s.Item.Version, scopes.DefaultName())
checkProject("update, unset name", s.Item, err, "", 3)
2020-06-05 13:02:58 -07:00
2020-09-24 14:34:58 -07:00
_, err = scps.Delete(tc.Context(), s.Item.Id)
require.NoError(err)
2020-05-20 08:04:23 -07:00
2020-09-24 14:34:58 -07:00
_, err = scps.Delete(tc.Context(), s.Item.Id)
require.Error(err)
apiErr := api.AsServerError(err)
assert.NotNil(apiErr)
2021-02-25 12:43:17 -05:00
assert.EqualValues(http.StatusNotFound, apiErr.Response().StatusCode())
2020-05-26 13:46:21 -07:00
}
func TestErrors(t *testing.T) {
assert, require := assert.New(t), require.New(t)
tc := controller.NewTestController(t, nil)
2020-05-26 13:46:21 -07:00
defer tc.Shutdown()
client := tc.Client()
token := tc.Token()
client.SetToken(token.Token)
org, _ := iam.TestScopes(t, tc.IamRepo(), iam.WithUserId(token.UserId))
2020-08-17 15:38:41 -04:00
scps := scopes.NewClient(client)
2020-09-24 14:34:58 -07:00
createdProj, err := scps.Create(tc.Context(), org.GetPublicId())
require.NoError(err)
2020-05-26 13:46:21 -07:00
assert.NotNil(createdProj)
// Updating the wrong version should fail.
2020-09-24 14:34:58 -07:00
_, err = scps.Update(tc.Context(), createdProj.Item.Id, 73, scopes.WithName("anything"))
require.Error(err)
apiErr := api.AsServerError(err)
assert.NotNil(apiErr)
2021-02-25 12:43:17 -05:00
assert.EqualValues(http.StatusNotFound, apiErr.Response().StatusCode())
2020-09-24 14:34:58 -07:00
_, err = scps.Read(tc.Context(), "p_doesntexis")
require.Error(err)
apiErr = api.AsServerError(err)
2020-05-26 13:46:21 -07:00
assert.NotNil(apiErr)
2021-02-25 12:43:17 -05:00
assert.EqualValues(http.StatusNotFound, apiErr.Response().StatusCode())
2020-05-15 10:19:55 -04:00
2020-09-24 14:34:58 -07:00
_, err = scps.Read(tc.Context(), "invalid id")
assert.Error(err)
apiErr = api.AsServerError(err)
2020-05-26 13:46:21 -07:00
assert.NotNil(apiErr)
2021-02-25 12:43:17 -05:00
assert.EqualValues(http.StatusBadRequest, apiErr.Response().StatusCode())
2020-05-15 10:19:55 -04:00
}