2020-07-22 00:14:28 -04:00
package scopes_test
import (
"context"
2020-09-23 08:10:27 -07:00
"errors"
2020-08-13 17:39:48 -04:00
"fmt"
2020-07-22 00:14:28 -04:00
"strings"
"testing"
"github.com/golang/protobuf/ptypes/wrappers"
2020-08-05 10:19:55 -07:00
"github.com/google/go-cmp/cmp"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/db"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/scopes"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/iam"
2021-05-04 19:57:35 -05:00
"github.com/hashicorp/boundary/internal/perms"
2021-07-27 09:59:52 -07:00
"github.com/hashicorp/boundary/internal/servers/controller/auth"
2020-09-23 08:10:27 -07:00
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/servers/controller/handlers/scopes"
"github.com/hashicorp/boundary/internal/types/scope"
2020-07-22 00:14:28 -04:00
"google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/proto"
2020-08-05 10:19:55 -07:00
"google.golang.org/protobuf/testing/protocmp"
2021-01-20 10:53:01 -05:00
"google.golang.org/protobuf/types/known/structpb"
2021-04-14 16:19:02 -05:00
"google.golang.org/protobuf/types/known/timestamppb"
2020-07-22 00:14:28 -04:00
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2021-04-23 09:13:37 -05:00
var testAuthorizedActions = [] string { "no-op" , "read" , "update" , "delete" }
2020-07-22 00:14:28 -04:00
func createDefaultScopesAndRepo ( t * testing . T ) ( * iam . Scope , * iam . Scope , func () ( * iam . Repository , error )) {
t . Helper ()
conn , _ := db . TestSetup ( t , "postgres" )
wrap := db . TestWrapper ( t )
2020-08-19 16:47:18 -04:00
iamRepo := iam . TestRepo ( t , conn , wrap )
2020-07-22 00:14:28 -04:00
repoFn := func () ( * iam . Repository , error ) {
2020-08-19 16:47:18 -04:00
return iamRepo , nil
2020-07-22 00:14:28 -04:00
}
2020-08-19 16:47:18 -04:00
oRes , pRes := iam . TestScopes ( t , iamRepo )
2020-07-22 00:14:28 -04:00
2021-03-25 03:15:43 -04:00
oRes . Name = "defaultOrg"
oRes . Description = "defaultOrg"
2020-07-22 00:14:28 -04:00
repo , err := repoFn ()
require . NoError ( t , err )
2020-08-07 12:55:02 -04:00
oRes , _ , err = repo . UpdateScope ( context . Background (), oRes , 1 , [] string { "Name" , "Description" })
2020-07-22 00:14:28 -04:00
require . NoError ( t , err )
pRes . Name = "defaultProj"
pRes . Description = "defaultProj"
repo , err = repoFn ()
require . NoError ( t , err )
2020-08-07 12:55:02 -04:00
pRes , _ , err = repo . UpdateScope ( context . Background (), pRes , 1 , [] string { "Name" , "Description" })
2020-07-22 00:14:28 -04:00
require . NoError ( t , err )
return oRes , pRes , repoFn
}
2021-04-08 20:54:32 -04:00
var globalAuthorizedCollectionActions = map [ string ] * structpb . ListValue {
"auth-methods" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"auth-tokens" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "list" ),
},
},
"groups" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"roles" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"scopes" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"sessions" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "list" ),
},
},
"users" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
}
2021-01-20 10:53:01 -05:00
var orgAuthorizedCollectionActions = map [ string ] * structpb . ListValue {
"auth-methods" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"auth-tokens" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "list" ),
},
},
"groups" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"roles" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"scopes" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"sessions" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "list" ),
},
},
"users" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
}
var projectAuthorizedCollectionActions = map [ string ] * structpb . ListValue {
"groups" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"host-catalogs" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"roles" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
"targets" : {
Values : [] * structpb . Value {
structpb . NewStringValue ( "create" ),
structpb . NewStringValue ( "list" ),
},
},
}
2020-07-22 00:14:28 -04:00
func TestGet ( t * testing . T ) {
2021-03-25 03:15:43 -04:00
org , proj , repoFn := createDefaultScopesAndRepo ( t )
2020-07-22 00:14:28 -04:00
toMerge := & pbs . GetScopeRequest {
Id : proj . GetPublicId (),
}
oScope := & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : org . GetPublicId (),
ScopeId : org . GetParentId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : "global" , Type : scope . Global . String (), Name : scope . Global . String (), Description : "Global Scope" },
2021-01-20 10:53:01 -05:00
Name : & wrapperspb . StringValue { Value : org . GetName ()},
Description : & wrapperspb . StringValue { Value : org . GetDescription ()},
CreatedTime : org . CreateTime . GetTimestamp (),
UpdatedTime : org . UpdateTime . GetTimestamp (),
Version : 2 ,
Type : scope . Org . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : orgAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
}
pScope := & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : proj . GetPublicId (),
ScopeId : proj . GetParentId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : oScope . Id , Type : scope . Org . String (), ParentScopeId : scope . Global . String (), Name : "defaultOrg" , Description : "defaultOrg" },
2021-01-20 10:53:01 -05:00
Name : & wrapperspb . StringValue { Value : proj . GetName ()},
Description : & wrapperspb . StringValue { Value : proj . GetDescription ()},
CreatedTime : proj . CreateTime . GetTimestamp (),
UpdatedTime : proj . UpdateTime . GetTimestamp (),
Version : 2 ,
Type : scope . Project . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : projectAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
}
cases := [] struct {
name string
scopeId string
req * pbs . GetScopeRequest
res * pbs . GetScopeResponse
2020-09-23 08:10:27 -07:00
err error
2020-07-22 00:14:28 -04:00
}{
{
name : "Get an existing org" ,
scopeId : "global" ,
req : & pbs . GetScopeRequest { Id : org . GetPublicId ()},
res : & pbs . GetScopeResponse { Item : oScope },
},
{
name : "Get a non existing org" ,
scopeId : "global" ,
req : & pbs . GetScopeRequest { Id : "o_DoesntExis" },
res : nil ,
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-07-22 00:14:28 -04:00
},
{
name : "Get an existing project" ,
scopeId : org . GetPublicId (),
req : & pbs . GetScopeRequest { Id : proj . GetPublicId ()},
res : & pbs . GetScopeResponse { Item : pScope },
},
{
name : "Get a non existing project" ,
scopeId : org . GetPublicId (),
req : & pbs . GetScopeRequest { Id : "p_DoesntExis" },
res : nil ,
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-07-22 00:14:28 -04:00
},
{
name : "Wrong id prefix" ,
scopeId : org . GetPublicId (),
req : & pbs . GetScopeRequest { Id : "j_1234567890" },
res : nil ,
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
{
name : "space in id" ,
scopeId : org . GetPublicId (),
req : & pbs . GetScopeRequest { Id : "p_1 23456789" },
res : nil ,
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-09-23 08:10:27 -07:00
assert , require := assert . New ( t ), require . New ( t )
2020-07-22 00:14:28 -04:00
req := proto . Clone ( toMerge ).( * pbs . GetScopeRequest )
proto . Merge ( req , tc . req )
2021-03-25 03:15:43 -04:00
s , err := scopes . NewService ( repoFn )
2020-07-22 00:14:28 -04:00
require . NoError ( err , "Couldn't create new project service." )
2021-03-25 03:15:43 -04:00
got , gErr := s . GetScope ( auth . DisabledAuthTestContext ( repoFn , tc . scopeId ), req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "GetScope(%+v) got error\n%v, wanted\n%v" , req , gErr , tc . err )
}
2020-08-07 12:55:02 -04:00
assert . Empty ( cmp . Diff ( tc . res , got , protocmp . Transform ()), "GetScope(%q) got response\n%q, wanted\n%q" , req , got , tc . res )
2020-07-22 00:14:28 -04:00
})
}
}
func TestList ( t * testing . T ) {
conn , _ := db . TestSetup ( t , "postgres" )
wrap := db . TestWrapper ( t )
2020-08-19 16:47:18 -04:00
iamRepo := iam . TestRepo ( t , conn , wrap )
2020-07-22 00:14:28 -04:00
repoFn := func () ( * iam . Repository , error ) {
2020-08-19 16:47:18 -04:00
return iamRepo , nil
2020-07-22 00:14:28 -04:00
}
repo , err := repoFn ()
require . NoError ( t , err )
2020-08-19 16:47:18 -04:00
oNoProjects , p1 := iam . TestScopes ( t , repo )
2020-07-22 00:14:28 -04:00
_ , err = repo . DeleteScope ( context . Background (), p1 . GetPublicId ())
require . NoError ( t , err )
2020-08-19 16:47:18 -04:00
oWithProjects , p2 := iam . TestScopes ( t , repo )
2020-07-22 00:14:28 -04:00
_ , err = repo . DeleteScope ( context . Background (), p2 . GetPublicId ())
require . NoError ( t , err )
2021-05-04 19:57:35 -05:00
outputFields := perms . OutputFieldsMap ( nil ). SelfOrDefaults ( "u_auth" )
2020-07-22 00:14:28 -04:00
var initialOrgs [] * pb . Scope
2021-03-25 03:15:43 -04:00
globalScope := & pb . ScopeInfo { Id : "global" , Type : scope . Global . String (), Name : scope . Global . String (), Description : "Global Scope" }
2021-05-04 19:57:35 -05:00
oNoProjectsProto , err := scopes . ToProto ( context . Background (), oNoProjects , handlers . WithOutputFields ( & outputFields ))
require . NoError ( t , err )
2020-07-22 00:14:28 -04:00
oNoProjectsProto . Scope = globalScope
2021-04-23 09:13:37 -05:00
oNoProjectsProto . AuthorizedActions = testAuthorizedActions
2021-01-20 10:53:01 -05:00
oNoProjectsProto . AuthorizedCollectionActions = orgAuthorizedCollectionActions
2021-05-04 19:57:35 -05:00
oWithProjectsProto , err := scopes . ToProto ( context . Background (), oWithProjects , handlers . WithOutputFields ( & outputFields ))
require . NoError ( t , err )
2020-07-22 00:14:28 -04:00
oWithProjectsProto . Scope = globalScope
2021-04-23 09:13:37 -05:00
oWithProjectsProto . AuthorizedActions = testAuthorizedActions
2021-01-20 10:53:01 -05:00
oWithProjectsProto . AuthorizedCollectionActions = orgAuthorizedCollectionActions
2020-07-22 00:14:28 -04:00
initialOrgs = append ( initialOrgs , oNoProjectsProto , oWithProjectsProto )
scopes . SortScopes ( initialOrgs )
cases := [] struct {
name string
scopeId string
req * pbs . ListScopesRequest
res * pbs . ListScopesResponse
2020-09-23 08:10:27 -07:00
err error
2020-07-22 00:14:28 -04:00
}{
{
2020-07-22 15:10:44 -04:00
name : "List initial orgs" ,
2020-07-22 00:14:28 -04:00
scopeId : scope . Global . String (),
2020-07-22 15:10:44 -04:00
req : & pbs . ListScopesRequest { ScopeId : "global" },
2020-07-22 00:14:28 -04:00
res : & pbs . ListScopesResponse { Items : initialOrgs },
},
{
name : "List No Projects" ,
scopeId : oNoProjects . GetPublicId (),
2020-07-22 15:10:44 -04:00
req : & pbs . ListScopesRequest { ScopeId : oNoProjects . GetPublicId ()},
2020-07-22 00:14:28 -04:00
res : & pbs . ListScopesResponse {},
},
2020-10-07 17:43:12 -07:00
{
name : "Cant List Project Scopes" ,
scopeId : p1 . GetPublicId (),
req : & pbs . ListScopesRequest { ScopeId : p1 . GetPublicId ()},
2020-10-13 13:56:21 -04:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-10-07 17:43:12 -07:00
},
2021-02-22 10:09:25 -07:00
{
name : "Filter To Single Org" ,
scopeId : scope . Global . String (),
req : & pbs . ListScopesRequest { ScopeId : "global" , Filter : fmt . Sprintf ( `"/item/id"==%q` , initialOrgs [ 1 ]. GetId ())},
res : & pbs . ListScopesResponse { Items : initialOrgs [ 1 : 2 ]},
},
2020-07-22 00:14:28 -04:00
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
s , err := scopes . NewService ( repoFn )
require . NoError ( err , "Couldn't create new role service." )
2021-04-14 16:19:02 -05:00
// Test with non-anonymous listing first
2021-05-04 19:57:35 -05:00
got , gErr := s . ListScopes ( auth . DisabledAuthTestContext ( repoFn , tc . scopeId ), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "ListScopes(%+v) got error\n%v, wanted\n%v" , tc . req , gErr , tc . err )
2021-04-14 16:19:02 -05:00
return
2020-09-23 08:10:27 -07:00
}
2020-08-05 10:19:55 -07:00
assert . Empty ( cmp . Diff ( got , tc . res , protocmp . Transform ()), "ListScopes(%q) got response\n%q\nwanted\n%q" , tc . req , got , tc . res )
2021-04-14 16:19:02 -05:00
// Now test with anonymous listing
2021-05-04 19:57:35 -05:00
got , gErr = s . ListScopes ( auth . DisabledAuthTestContext ( repoFn , tc . scopeId , auth . WithUserId ( auth . AnonymousUserId )), tc . req )
2021-04-14 16:19:02 -05:00
require . NoError ( gErr )
2021-05-04 19:57:35 -05:00
assert . Len ( got . Items , len ( tc . res . Items ))
2021-04-14 16:19:02 -05:00
for _ , item := range got . GetItems () {
assert . Nil ( item . CreatedTime )
assert . Nil ( item . UpdatedTime )
assert . Empty ( item . Version )
}
2020-07-22 00:14:28 -04:00
})
}
var wantOrgs [] * pb . Scope
for i := 0 ; i < 10 ; i ++ {
newO , err := iam . NewOrg ()
require . NoError ( t , err )
2020-08-13 17:39:48 -04:00
o , err := repo . CreateScope ( context . Background (), newO , "" )
2020-07-22 00:14:28 -04:00
require . NoError ( t , err )
wantOrgs = append ( wantOrgs , & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : o . GetPublicId (),
ScopeId : globalScope . GetId (),
Scope : globalScope ,
CreatedTime : o . GetCreateTime (). GetTimestamp (),
UpdatedTime : o . GetUpdateTime (). GetTimestamp (),
Version : 1 ,
Type : scope . Org . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : orgAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
})
}
wantOrgs = append ( wantOrgs , initialOrgs ... )
scopes . SortScopes ( wantOrgs )
var wantProjects [] * pb . Scope
for i := 0 ; i < 10 ; i ++ {
newP , err := iam . NewProject ( oWithProjects . GetPublicId ())
require . NoError ( t , err )
2020-08-13 17:39:48 -04:00
p , err := repo . CreateScope ( context . Background (), newP , "" )
2020-07-22 00:14:28 -04:00
require . NoError ( t , err )
wantProjects = append ( wantProjects , & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : p . GetPublicId (),
ScopeId : oWithProjects . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : oWithProjects . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String ()},
2021-01-20 10:53:01 -05:00
CreatedTime : p . GetCreateTime (). GetTimestamp (),
UpdatedTime : p . GetUpdateTime (). GetTimestamp (),
Version : 1 ,
Type : scope . Project . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : projectAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
})
}
scopes . SortScopes ( wantProjects )
2021-01-25 19:37:40 -05:00
totalScopes := append ( wantOrgs , wantProjects ... )
scopes . SortScopes ( totalScopes )
2020-07-22 00:14:28 -04:00
cases = [] struct {
name string
scopeId string
req * pbs . ListScopesRequest
res * pbs . ListScopesResponse
2020-09-23 08:10:27 -07:00
err error
2020-07-22 00:14:28 -04:00
}{
{
name : "List Many Orgs" ,
scopeId : scope . Global . String (),
2020-07-22 15:10:44 -04:00
req : & pbs . ListScopesRequest { ScopeId : "global" },
2020-07-22 00:14:28 -04:00
res : & pbs . ListScopesResponse { Items : wantOrgs },
},
{
name : "List Many Projects" ,
scopeId : oWithProjects . GetPublicId (),
2020-07-22 15:10:44 -04:00
req : & pbs . ListScopesRequest { ScopeId : oWithProjects . GetPublicId ()},
2020-07-22 00:14:28 -04:00
res : & pbs . ListScopesResponse { Items : wantProjects },
},
2021-01-25 19:37:40 -05:00
{
name : "List Global Recursively" ,
scopeId : scope . Global . String (),
req : & pbs . ListScopesRequest { ScopeId : scope . Global . String (), Recursive : true },
res : & pbs . ListScopesResponse { Items : totalScopes },
},
2021-02-22 10:09:25 -07:00
{
name : "Filter To Orgs" ,
scopeId : scope . Global . String (),
req : & pbs . ListScopesRequest { ScopeId : scope . Global . String (), Recursive : true , Filter : fmt . Sprintf ( `"/item/scope/type"==%q` , scope . Global . String ())},
res : & pbs . ListScopesResponse { Items : wantOrgs },
},
{
name : "Filter To Projects" ,
scopeId : scope . Global . String (),
req : & pbs . ListScopesRequest { ScopeId : scope . Global . String (), Recursive : true , Filter : fmt . Sprintf ( `"/item/scope/type"==%q` , scope . Org . String ())},
res : & pbs . ListScopesResponse { Items : wantProjects },
},
{
name : "Filter Bad Format" ,
req : & pbs . ListScopesRequest { ScopeId : scope . Global . String (), Filter : `"//id/"=="bad"` },
err : handlers . InvalidArgumentErrorf ( "bad format" , nil ),
},
2020-07-22 00:14:28 -04:00
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
s , err := scopes . NewService ( repoFn )
require . NoError ( err , "Couldn't create new role service." )
2021-04-14 16:19:02 -05:00
// Test with non-anonymous listing first
2021-05-04 19:57:35 -05:00
got , gErr := s . ListScopes ( auth . DisabledAuthTestContext ( repoFn , tc . scopeId ), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "ListScopes(%+v) got error\n%v, wanted\n%v" , tc . req , gErr , tc . err )
2021-04-14 16:19:02 -05:00
return
2020-09-23 08:10:27 -07:00
}
2021-04-14 16:19:02 -05:00
require . NoError ( gErr )
2020-08-05 10:19:55 -07:00
assert . Empty ( cmp . Diff ( got , tc . res , protocmp . Transform ()), "ListScopes(%q) got response\n%q, wanted\n%q" , tc . req , got , tc . res )
2021-04-14 16:19:02 -05:00
// Now test with anonymous listing
2021-05-04 19:57:35 -05:00
got , gErr = s . ListScopes ( auth . DisabledAuthTestContext ( repoFn , tc . scopeId , auth . WithUserId ( auth . AnonymousUserId )), tc . req )
2021-04-14 16:19:02 -05:00
require . NoError ( gErr )
2021-05-04 19:57:35 -05:00
assert . Len ( got . Items , len ( tc . res . Items ))
2021-04-14 16:19:02 -05:00
for _ , item := range got . GetItems () {
assert . Nil ( item . CreatedTime )
assert . Nil ( item . UpdatedTime )
assert . Empty ( item . Version )
}
2020-07-22 00:14:28 -04:00
})
}
}
func TestDelete ( t * testing . T ) {
2021-03-25 03:15:43 -04:00
org , proj , repoFn := createDefaultScopesAndRepo ( t )
2020-07-22 00:14:28 -04:00
2021-03-25 03:15:43 -04:00
s , err := scopes . NewService ( repoFn )
2020-09-23 08:10:27 -07:00
require . NoError ( t , err , "Error when getting new project service." )
2020-07-22 00:14:28 -04:00
cases := [] struct {
name string
scopeId string
req * pbs . DeleteScopeRequest
res * pbs . DeleteScopeResponse
2020-09-23 08:10:27 -07:00
err error
2020-07-22 00:14:28 -04:00
}{
{
name : "Delete an Existing Project" ,
scopeId : org . GetPublicId (),
req : & pbs . DeleteScopeRequest {
Id : proj . GetPublicId (),
},
},
{
name : "Delete bad project id Project" ,
scopeId : org . GetPublicId (),
req : & pbs . DeleteScopeRequest {
Id : "p_doesntexis" ,
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-07-22 00:14:28 -04:00
},
{
name : "Bad Project Id formatting" ,
scopeId : org . GetPublicId (),
req : & pbs . DeleteScopeRequest {
Id : "bad_format" ,
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
{
name : "Delete an Existing Org" ,
scopeId : scope . Global . String (),
req : & pbs . DeleteScopeRequest {
Id : org . GetPublicId (),
},
},
{
name : "Delete bad org id Org" ,
scopeId : scope . Global . String (),
req : & pbs . DeleteScopeRequest {
Id : "p_doesntexis" ,
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-07-22 00:14:28 -04:00
},
{
name : "Bad Org Id formatting" ,
scopeId : scope . Global . String (),
req : & pbs . DeleteScopeRequest {
Id : "bad_format" ,
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-09-23 08:10:27 -07:00
assert , require := assert . New ( t ), require . New ( t )
2021-03-25 03:15:43 -04:00
got , gErr := s . DeleteScope ( auth . DisabledAuthTestContext ( repoFn , tc . scopeId ), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "DeleteScope(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
2020-08-05 10:19:55 -07:00
assert . EqualValuesf ( tc . res , got , "DeleteScope(%q) got response %q, wanted %q" , tc . req , got , tc . res )
2020-07-22 00:14:28 -04:00
})
}
}
func TestDelete_twice ( t * testing . T ) {
2020-09-23 08:10:27 -07:00
assert , require := assert . New ( t ), require . New ( t )
2021-03-25 03:15:43 -04:00
org , proj , repoFn := createDefaultScopesAndRepo ( t )
2020-07-22 00:14:28 -04:00
2021-03-25 03:15:43 -04:00
s , err := scopes . NewService ( repoFn )
2020-07-22 00:14:28 -04:00
require . NoError ( err , "Error when getting new scopes service" )
2021-03-25 03:15:43 -04:00
ctx := auth . DisabledAuthTestContext ( repoFn , org . GetPublicId ())
2020-07-22 00:14:28 -04:00
req := & pbs . DeleteScopeRequest {
Id : proj . GetPublicId (),
}
2020-09-08 23:57:26 -04:00
_ , gErr := s . DeleteScope ( ctx , req )
2020-07-22 00:14:28 -04:00
assert . NoError ( gErr , "First attempt" )
2020-09-08 23:57:26 -04:00
_ , gErr = s . DeleteScope ( ctx , req )
2020-09-04 14:48:11 -07:00
assert . Error ( gErr , "Second attempt" )
2020-09-23 08:10:27 -07:00
assert . True ( errors . Is ( gErr , handlers . ApiErrorWithCode ( codes . NotFound )), "Expected not found for the second delete." )
2020-07-22 00:14:28 -04:00
2021-03-25 03:15:43 -04:00
ctx = auth . DisabledAuthTestContext ( repoFn , scope . Global . String ())
2020-07-22 00:14:28 -04:00
req = & pbs . DeleteScopeRequest {
Id : org . GetPublicId (),
}
2020-09-08 23:57:26 -04:00
_ , gErr = s . DeleteScope ( ctx , req )
2020-07-22 00:14:28 -04:00
assert . NoError ( gErr , "First attempt" )
2020-09-08 23:57:26 -04:00
_ , gErr = s . DeleteScope ( ctx , req )
2020-09-04 14:48:11 -07:00
assert . Error ( gErr , "Second attempt" )
2020-09-23 08:10:27 -07:00
assert . True ( errors . Is ( gErr , handlers . ApiErrorWithCode ( codes . NotFound )), "Expected not found for the second delete." )
2020-07-22 00:14:28 -04:00
}
func TestCreate ( t * testing . T ) {
2020-08-13 17:39:48 -04:00
ctx := context . Background ()
defaultOrg , defaultProj , repoFn := createDefaultScopesAndRepo ( t )
2021-04-14 16:19:02 -05:00
defaultProjCreated := defaultProj . GetCreateTime (). GetTimestamp (). AsTime ()
2020-07-22 00:14:28 -04:00
toMerge := & pbs . CreateScopeRequest {}
2020-08-13 17:39:48 -04:00
repo , err := repoFn ()
2020-09-08 16:04:19 -07:00
require . NoError ( t , err )
2020-08-13 17:39:48 -04:00
globalUser , err := iam . NewUser ( scope . Global . String ())
2020-09-08 16:04:19 -07:00
require . NoError ( t , err )
2020-08-13 17:39:48 -04:00
globalUser , err = repo . CreateUser ( ctx , globalUser )
2020-09-08 16:04:19 -07:00
require . NoError ( t , err )
2020-08-13 17:39:48 -04:00
orgUser , err := iam . NewUser ( defaultOrg . GetPublicId ())
2020-09-08 16:04:19 -07:00
require . NoError ( t , err )
2020-08-13 17:39:48 -04:00
orgUser , err = repo . CreateUser ( ctx , orgUser )
2020-09-08 16:04:19 -07:00
require . NoError ( t , err )
2020-08-13 17:39:48 -04:00
2020-07-22 00:14:28 -04:00
cases := [] struct {
name string
scopeId string
req * pbs . CreateScopeRequest
res * pbs . CreateScopeResponse
2020-09-23 08:10:27 -07:00
err error
2020-07-22 00:14:28 -04:00
}{
{
name : "Create a valid Project" ,
scopeId : defaultOrg . GetPublicId (),
2020-07-22 15:10:44 -04:00
req : & pbs . CreateScopeRequest {
Item : & pb . Scope {
2020-09-02 19:43:41 -07:00
ScopeId : defaultOrg . GetPublicId (),
2020-07-22 15:10:44 -04:00
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
},
},
2020-07-22 00:14:28 -04:00
res : & pbs . CreateScopeResponse {
Uri : "scopes/p_" ,
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
ScopeId : defaultOrg . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : defaultOrg . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String (), Name : "defaultOrg" , Description : "defaultOrg" },
2021-01-20 10:53:01 -05:00
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
Version : 1 ,
Type : scope . Project . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : projectAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
},
},
},
{
name : "Create a valid Org" ,
scopeId : scope . Global . String (),
2020-07-22 15:10:44 -04:00
req : & pbs . CreateScopeRequest {
Item : & pb . Scope {
2020-09-02 19:43:41 -07:00
ScopeId : scope . Global . String (),
2020-07-22 15:10:44 -04:00
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
},
},
2020-07-22 00:14:28 -04:00
res : & pbs . CreateScopeResponse {
Uri : "scopes/o_" ,
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
ScopeId : scope . Global . String (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : scope . Global . String (), Type : scope . Global . String (), Name : scope . Global . String (), Description : "Global Scope" },
2021-01-20 10:53:01 -05:00
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
Version : 1 ,
Type : scope . Org . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : orgAuthorizedCollectionActions ,
2020-09-08 16:04:19 -07:00
},
},
},
{
name : "Create a valid Project with type specified" ,
scopeId : defaultOrg . GetPublicId (),
req : & pbs . CreateScopeRequest {
Item : & pb . Scope {
ScopeId : defaultOrg . GetPublicId (),
Description : & wrapperspb . StringValue { Value : "desc" },
Type : scope . Project . String (),
},
},
res : & pbs . CreateScopeResponse {
Uri : "scopes/p_" ,
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
ScopeId : defaultOrg . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : defaultOrg . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String (), Name : "defaultOrg" , Description : "defaultOrg" },
2021-01-20 10:53:01 -05:00
Description : & wrapperspb . StringValue { Value : "desc" },
Version : 1 ,
Type : scope . Project . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : projectAuthorizedCollectionActions ,
2020-09-08 16:04:19 -07:00
},
},
},
{
name : "Create a valid Org with type specified" ,
scopeId : scope . Global . String (),
req : & pbs . CreateScopeRequest {
Item : & pb . Scope {
ScopeId : scope . Global . String (),
Description : & wrapperspb . StringValue { Value : "desc" },
Type : scope . Org . String (),
},
},
res : & pbs . CreateScopeResponse {
Uri : "scopes/o_" ,
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
ScopeId : scope . Global . String (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : scope . Global . String (), Type : scope . Global . String (), Name : scope . Global . String (), Description : "Global Scope" },
2021-01-20 10:53:01 -05:00
Description : & wrapperspb . StringValue { Value : "desc" },
Version : 1 ,
Type : scope . Org . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : orgAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
},
},
},
2020-09-08 16:04:19 -07:00
{
name : "Project with bad type specified" ,
scopeId : defaultOrg . GetPublicId (),
req : & pbs . CreateScopeRequest {
Item : & pb . Scope {
ScopeId : defaultOrg . GetPublicId (),
Description : & wrapperspb . StringValue { Value : "desc" },
Type : scope . Org . String (),
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-09-08 16:04:19 -07:00
},
{
name : "Org with bad type specified" ,
scopeId : scope . Global . String (),
req : & pbs . CreateScopeRequest {
Item : & pb . Scope {
ScopeId : scope . Global . String (),
Description : & wrapperspb . StringValue { Value : "desc" },
Type : scope . Project . String (),
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-09-08 16:04:19 -07:00
},
2020-07-22 00:14:28 -04:00
{
name : "Can't specify Id" ,
scopeId : defaultOrg . GetPublicId (),
req : & pbs . CreateScopeRequest { Item : & pb . Scope {
Id : "not allowed to be set" ,
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
{
name : "Can't specify Created Time" ,
scopeId : defaultOrg . GetPublicId (),
req : & pbs . CreateScopeRequest { Item : & pb . Scope {
2021-04-14 16:19:02 -05:00
CreatedTime : timestamppb . Now (),
2020-07-22 00:14:28 -04:00
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
{
name : "Can't specify Update Time" ,
scopeId : defaultOrg . GetPublicId (),
req : & pbs . CreateScopeRequest { Item : & pb . Scope {
2021-04-14 16:19:02 -05:00
UpdatedTime : timestamppb . Now (),
2020-07-22 00:14:28 -04:00
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
}
for _ , tc := range cases {
2020-08-13 17:39:48 -04:00
for _ , withUserId := range [] bool { false , true } {
t . Run ( fmt . Sprintf ( "%s-userid-%t" , tc . name , withUserId ), func ( t * testing . T ) {
2020-09-08 16:04:19 -07:00
assert , require := assert . New ( t ), require . New ( t )
2020-08-13 17:39:48 -04:00
var name string
if tc . req != nil && tc . req . GetItem () != nil && tc . req . GetItem (). GetName () != nil {
name = tc . req . GetItem (). GetName (). GetValue ()
localName := name
defer func () {
tc . res . GetItem (). GetName (). Value = localName
}()
}
req := proto . Clone ( toMerge ).( * pbs . CreateScopeRequest )
proto . Merge ( req , tc . req )
2020-07-22 00:14:28 -04:00
2020-08-13 17:39:48 -04:00
s , err := scopes . NewService ( repoFn )
require . NoError ( err , "Error when getting new project service." )
if name != "" {
name = fmt . Sprintf ( "%s-%t" , name , withUserId )
req . GetItem (). GetName (). Value = name
2020-07-22 00:14:28 -04:00
}
2020-08-13 17:39:48 -04:00
var userId string
if withUserId {
if tc . scopeId == scope . Global . String () {
userId = globalUser . GetPublicId ()
} else {
userId = orgUser . GetPublicId ()
}
assert . NotEmpty ( userId )
}
2021-03-25 03:15:43 -04:00
got , gErr := s . CreateScope ( auth . DisabledAuthTestContext ( repoFn , tc . scopeId , auth . WithUserId ( userId )), req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "CreateScope(%+v) got error %v, wanted %v" , req , gErr , tc . err )
}
2020-08-13 17:39:48 -04:00
if got != nil {
assert . Contains ( got . GetUri (), tc . res . Uri )
switch tc . scopeId {
case "global" :
assert . True ( strings . HasPrefix ( got . GetItem (). GetId (), "o_" ))
default :
assert . True ( strings . HasPrefix ( got . GetItem (). GetId (), "p_" ))
}
2021-04-14 16:19:02 -05:00
gotCreateTime := got . GetItem (). GetCreatedTime (). AsTime ()
gotUpdateTime := got . GetItem (). GetUpdatedTime (). AsTime ()
2020-08-13 17:39:48 -04:00
// Verify it is a project created after the test setup's default project
assert . True ( gotCreateTime . After ( defaultProjCreated ), "New scope should have been created after default project. Was created %v, which is after %v" , gotCreateTime , defaultProjCreated )
assert . True ( gotUpdateTime . After ( defaultProjCreated ), "New scope should have been updated after default project. Was updated %v, which is after %v" , gotUpdateTime , defaultProjCreated )
if withUserId {
repo , err := repoFn ()
require . NoError ( err )
2021-01-23 13:39:04 -05:00
roles , err := repo . ListRoles ( ctx , [] string { got . GetItem (). GetId ()})
2020-08-13 17:39:48 -04:00
require . NoError ( err )
2020-10-13 13:56:21 -04:00
switch tc . scopeId {
case defaultOrg . PublicId :
2021-02-11 13:14:18 -05:00
require . Len ( roles , 2 )
2020-10-13 13:56:21 -04:00
case "global" :
require . Len ( roles , 2 )
}
2020-09-29 20:38:16 -04:00
for _ , role := range roles {
switch role . GetName () {
case "Administration" :
assert . Equal ( fmt . Sprintf ( "Role created for administration of scope %s by user %s at its creation time" , got . GetItem (). GetId (), userId ), role . GetDescription ())
2020-10-01 10:51:54 -04:00
case "Login and Default Grants" :
2021-02-11 13:14:18 -05:00
assert . Equal ( fmt . Sprintf ( "Role created for login capability, account self-management, and other default grants for users of scope %s at its creation time" , got . GetItem (). GetId ()), role . GetDescription ())
case "Default Grants" :
assert . Equal ( fmt . Sprintf ( "Role created to provide default grants to users of scope %s at its creation time" , got . GetItem (). GetId ()), role . GetDescription ())
2020-09-29 20:38:16 -04:00
default :
t . Fatal ( "unexpected role name" , role . GetName ())
}
}
2020-08-13 17:39:48 -04:00
}
// Clear all values which are hard to compare against.
assert . Equal ( name , got . GetItem (). GetName (). GetValue ())
got . Item . Name = tc . res . GetItem (). GetName ()
got . Uri , tc . res . Uri = "" , ""
got . Item . Id , tc . res . Item . Id = "" , ""
got . Item . CreatedTime , got . Item . UpdatedTime , tc . res . Item . CreatedTime , tc . res . Item . UpdatedTime = nil , nil , nil , nil
}
assert . Empty ( cmp . Diff ( tc . res , got , protocmp . Transform ()), "CreateScope(%q) got response %q, wanted %q" , req , got , tc . res )
})
}
2020-07-22 00:14:28 -04:00
}
}
func TestUpdate ( t * testing . T ) {
org , proj , repoFn := createDefaultScopesAndRepo ( t )
tested , err := scopes . NewService ( repoFn )
2020-09-08 16:04:19 -07:00
require . NoError ( t , err , "Error when getting new project service." )
2020-07-22 00:14:28 -04:00
2021-04-08 20:54:32 -04:00
iamRepo , err := repoFn ()
require . NoError ( t , err )
global , err := iamRepo . LookupScope ( context . Background (), "global" )
require . NoError ( t , err )
2020-08-07 12:55:02 -04:00
var orgVersion uint32 = 2
var projVersion uint32 = 2
2021-04-08 20:54:32 -04:00
var globalVersion uint32 = global . Version
2020-08-07 12:55:02 -04:00
2020-07-22 00:14:28 -04:00
resetOrg := func () {
2020-08-07 12:55:02 -04:00
orgVersion ++
2020-07-22 00:14:28 -04:00
repo , err := repoFn ()
2020-09-08 16:04:19 -07:00
require . NoError ( t , err , "Couldn't get a new repo" )
2020-08-07 12:55:02 -04:00
org , _ , err = repo . UpdateScope ( context . Background (), org , orgVersion , [] string { "Name" , "Description" })
2020-09-08 16:04:19 -07:00
require . NoError ( t , err , "Failed to reset the org" )
2020-08-07 12:55:02 -04:00
orgVersion ++
2020-07-22 00:14:28 -04:00
}
resetProject := func () {
2020-08-07 12:55:02 -04:00
projVersion ++
2020-07-22 00:14:28 -04:00
repo , err := repoFn ()
2020-09-08 16:04:19 -07:00
require . NoError ( t , err , "Couldn't get a new repo" )
2020-08-07 12:55:02 -04:00
proj , _ , err = repo . UpdateScope ( context . Background (), proj , projVersion , [] string { "Name" , "Description" })
2020-09-08 16:04:19 -07:00
require . NoError ( t , err , "Failed to reset the project" )
2020-08-07 12:55:02 -04:00
projVersion ++
2020-07-22 00:14:28 -04:00
}
2021-04-08 20:54:32 -04:00
resetGlobal := func () {
repo , err := repoFn ()
require . NoError ( t , err , "Couldn't get a new repo" )
globalScope := iam . AllocScope ()
globalScope . PublicId = "global"
global , _ , err = repo . UpdateScope ( context . Background (), & globalScope , globalVersion , [] string { "Name" , "Description" })
require . NoError ( t , err , "Failed to reset the global scope" )
globalVersion = global . Version
}
2021-04-14 16:19:02 -05:00
projCreated := proj . GetCreateTime (). GetTimestamp (). AsTime ()
2020-07-22 00:14:28 -04:00
projToMerge := & pbs . UpdateScopeRequest {
Id : proj . GetPublicId (),
}
orgToMerge := & pbs . UpdateScopeRequest {
Id : org . GetPublicId (),
}
2021-04-08 20:54:32 -04:00
globalToMerge := & pbs . UpdateScopeRequest {
Id : "global" ,
}
2020-07-22 00:14:28 -04:00
cases := [] struct {
name string
scopeId string
req * pbs . UpdateScopeRequest
res * pbs . UpdateScopeResponse
2020-09-23 08:10:27 -07:00
err error
2020-07-22 00:14:28 -04:00
}{
{
name : "Update an Existing Project" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" , "description" },
},
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
2020-09-08 16:04:19 -07:00
Type : scope . Project . String (),
2020-07-22 00:14:28 -04:00
},
},
res : & pbs . UpdateScopeResponse {
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : proj . GetPublicId (),
ScopeId : org . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : org . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String (), Name : "defaultOrg" , Description : "defaultOrg" },
2021-01-20 10:53:01 -05:00
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
CreatedTime : proj . GetCreateTime (). GetTimestamp (),
Type : scope . Project . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : projectAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
},
},
},
{
name : "Update an Existing Org" ,
scopeId : scope . Global . String (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" , "description" },
},
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
2020-09-08 16:04:19 -07:00
Type : scope . Org . String (),
2020-07-22 00:14:28 -04:00
},
},
res : & pbs . UpdateScopeResponse {
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : org . GetPublicId (),
ScopeId : scope . Global . String (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : scope . Global . String (), Type : scope . Global . String (), Name : scope . Global . String (), Description : "Global Scope" },
2021-01-20 10:53:01 -05:00
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
CreatedTime : org . GetCreateTime (). GetTimestamp (),
Type : scope . Org . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : orgAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
},
},
},
2021-04-08 20:54:32 -04:00
{
name : "Update global" ,
scopeId : scope . Global . String (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" , "description" },
},
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
Type : scope . Global . String (),
},
},
res : & pbs . UpdateScopeResponse {
Item : & pb . Scope {
Id : scope . Global . String (),
Scope : & pb . ScopeInfo { Id : scope . Global . String (), Type : scope . Global . String (), Name : scope . Global . String (), Description : "Global Scope" },
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
CreatedTime : global . GetCreateTime (). GetTimestamp (),
Type : scope . Global . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-04-08 20:54:32 -04:00
AuthorizedCollectionActions : globalAuthorizedCollectionActions ,
},
},
},
2020-07-22 00:14:28 -04:00
{
name : "Multiple Paths in single string" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name,description" },
},
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
},
},
res : & pbs . UpdateScopeResponse {
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : proj . GetPublicId (),
ScopeId : org . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : org . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String (), Name : "defaultOrg" , Description : "defaultOrg" },
2021-01-20 10:53:01 -05:00
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
CreatedTime : proj . GetCreateTime (). GetTimestamp (),
Type : scope . Project . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : projectAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
},
},
},
{
name : "No Update Mask" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "updated name" },
Description : & wrapperspb . StringValue { Value : "updated desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
2020-09-08 16:04:19 -07:00
{
name : "Cant modify type" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" , "type" },
},
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "updated name" },
Type : scope . Org . String (),
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-09-08 16:04:19 -07:00
},
2020-07-22 00:14:28 -04:00
{
name : "No Paths in Mask" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask { Paths : [] string {}},
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "updated name" },
Description : & wrapperspb . StringValue { Value : "updated desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
{
name : "Only non-existant paths in Mask" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask { Paths : [] string { "nonexistant_field" }},
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "updated name" },
Description : & wrapperspb . StringValue { Value : "updated desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
{
name : "Unset Name" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" },
},
Item : & pb . Scope {
Description : & wrapperspb . StringValue { Value : "ignored" },
},
},
res : & pbs . UpdateScopeResponse {
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : proj . GetPublicId (),
ScopeId : org . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : org . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String (), Name : "defaultOrg" , Description : "defaultOrg" },
2021-01-20 10:53:01 -05:00
Description : & wrapperspb . StringValue { Value : "defaultProj" },
CreatedTime : proj . GetCreateTime (). GetTimestamp (),
Type : scope . Project . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : projectAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
},
},
},
{
name : "Unset Description" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "description" },
},
Item : & pb . Scope {
Name : & wrappers . StringValue { Value : "ignored" },
},
},
res : & pbs . UpdateScopeResponse {
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : proj . GetPublicId (),
ScopeId : org . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : org . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String (), Name : "defaultOrg" , Description : "defaultOrg" },
2021-01-20 10:53:01 -05:00
Name : & wrappers . StringValue { Value : "defaultProj" },
CreatedTime : proj . GetCreateTime (). GetTimestamp (),
Type : scope . Project . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : projectAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
},
},
},
{
name : "Update Only Name" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" },
},
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "updated" },
Description : & wrapperspb . StringValue { Value : "ignored" },
},
},
res : & pbs . UpdateScopeResponse {
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : proj . GetPublicId (),
ScopeId : org . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : org . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String (), Name : "defaultOrg" , Description : "defaultOrg" },
2021-01-20 10:53:01 -05:00
Name : & wrapperspb . StringValue { Value : "updated" },
Description : & wrapperspb . StringValue { Value : "defaultProj" },
CreatedTime : proj . GetCreateTime (). GetTimestamp (),
Type : scope . Project . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : projectAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
},
},
},
{
name : "Update Only Description" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "description" },
},
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "ignored" },
Description : & wrapperspb . StringValue { Value : "notignored" },
},
},
res : & pbs . UpdateScopeResponse {
Item : & pb . Scope {
2021-01-20 10:53:01 -05:00
Id : proj . GetPublicId (),
ScopeId : org . GetPublicId (),
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : org . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String (), Name : "defaultOrg" , Description : "defaultOrg" },
2021-01-20 10:53:01 -05:00
Name : & wrapperspb . StringValue { Value : "defaultProj" },
Description : & wrapperspb . StringValue { Value : "notignored" },
CreatedTime : proj . GetCreateTime (). GetTimestamp (),
Type : scope . Project . String (),
2021-04-23 09:13:37 -05:00
AuthorizedActions : testAuthorizedActions ,
2021-01-20 10:53:01 -05:00
AuthorizedCollectionActions : projectAuthorizedCollectionActions ,
2020-07-22 00:14:28 -04:00
},
},
},
{
name : "Update a Non Existing Project" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
Id : "p_DoesntExis" ,
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "description" },
},
Item : & pb . Scope {
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-07-22 00:14:28 -04:00
},
{
name : "Cant change Id" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
Id : proj . GetPublicId (),
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "id" },
},
Item : & pb . Scope {
Id : "p_somethinge" ,
2021-03-25 03:15:43 -04:00
Scope : & pb . ScopeInfo { Id : org . GetPublicId (), Type : scope . Org . String (), ParentScopeId : scope . Global . String (), Name : "defaultOrg" , Description : "defaultOrg" },
2020-07-22 00:14:28 -04:00
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "new desc" },
2021-01-07 10:02:32 -05:00
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
{
name : "Cant specify Created Time" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "created_time" },
},
Item : & pb . Scope {
2021-04-14 16:19:02 -05:00
CreatedTime : timestamppb . Now (),
2020-07-22 00:14:28 -04:00
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
{
name : "Cant specify Updated Time" ,
scopeId : org . GetPublicId (),
req : & pbs . UpdateScopeRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "updated_time" },
},
Item : & pb . Scope {
2021-04-14 16:19:02 -05:00
UpdatedTime : timestamppb . Now (),
2020-07-22 00:14:28 -04:00
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-07-22 00:14:28 -04:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-08-07 12:55:02 -04:00
ver := orgVersion
if tc . req . Id == proj . PublicId {
ver = projVersion
}
2020-08-18 17:38:38 -07:00
tc . req . Item . Version = ver
2020-08-07 12:55:02 -04:00
2020-09-08 16:04:19 -07:00
assert , require := assert . New ( t ), require . New ( t )
2020-07-22 00:14:28 -04:00
var req * pbs . UpdateScopeRequest
2021-04-08 20:54:32 -04:00
switch {
case tc . scopeId == scope . Global . String () && tc . req . Item . GetType () == scope . Global . String ():
tc . req . Item . Version = globalVersion
ver = globalVersion
req = proto . Clone ( globalToMerge ).( * pbs . UpdateScopeRequest )
if tc . err == nil {
defer resetGlobal ()
}
case tc . scopeId == scope . Global . String ():
2020-07-22 00:14:28 -04:00
req = proto . Clone ( orgToMerge ).( * pbs . UpdateScopeRequest )
2020-09-23 08:10:27 -07:00
if tc . err == nil {
2020-08-07 12:55:02 -04:00
defer resetOrg ()
}
2020-07-22 00:14:28 -04:00
default :
2020-08-07 12:55:02 -04:00
ver = projVersion
2020-08-18 17:38:38 -07:00
tc . req . Item . Version = ver
2020-07-22 00:14:28 -04:00
req = proto . Clone ( projToMerge ).( * pbs . UpdateScopeRequest )
2020-09-23 08:10:27 -07:00
if tc . err == nil {
2020-08-07 12:55:02 -04:00
defer resetProject ()
}
2020-07-22 00:14:28 -04:00
}
proto . Merge ( req , tc . req )
2021-03-25 03:15:43 -04:00
got , gErr := tested . UpdateScope ( auth . DisabledAuthTestContext ( repoFn , tc . scopeId ), req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "UpdateScope(%+v) got error\n%v, wanted\n%v" , req , gErr , tc . err )
}
2020-07-22 00:14:28 -04:00
if got != nil {
assert . NotNilf ( tc . res , "Expected UpdateScope response to be nil, but was %v" , got )
2021-04-14 16:19:02 -05:00
gotUpdateTime := got . GetItem (). GetUpdatedTime (). AsTime ()
2020-07-22 00:14:28 -04:00
// Verify it is a project updated after it was created
assert . True ( gotUpdateTime . After ( projCreated ), "Updated project should have been updated after it's creation. Was updated %v, which is after %v" , gotUpdateTime , projCreated )
// Clear all values which are hard to compare against.
got . Item . UpdatedTime , tc . res . Item . UpdatedTime = nil , nil
2020-08-07 12:55:02 -04:00
assert . Equal ( ver + 1 , got . GetItem (). GetVersion ())
tc . res . Item . Version = ver + 1
2020-07-22 00:14:28 -04:00
}
2020-08-05 10:19:55 -07:00
assert . Empty ( cmp . Diff ( got , tc . res , protocmp . Transform ()), "UpdateScope(%q) got response\n%q, wanted\n%q" , req , got , tc . res )
2020-07-22 00:14:28 -04:00
})
}
}