@ -130,72 +130,6 @@ func Test_ValidateType(t *testing.T) {
}
}
func Test_ValidateProject ( t * testing . T ) {
t . Parallel ( )
type input struct {
name string
input Grant
output Grant
errResult string
}
tests := [ ] input {
{
name : "no project" ,
input : Grant {
scope : Scope {
Type : iam . OrganizationScope ,
} ,
} ,
output : Grant {
scope : Scope {
Type : iam . OrganizationScope ,
} ,
} ,
} ,
{
name : "project, organization scope" ,
input : Grant {
project : "foobar" ,
scope : Scope {
Type : iam . OrganizationScope ,
} ,
} ,
output : Grant {
project : "foobar" ,
scope : Scope {
Type : iam . ProjectScope ,
Id : "foobar" ,
} ,
} ,
} ,
{
name : "project, non-organization scope" ,
input : Grant {
project : "foobar" ,
scope : Scope {
Type : iam . ProjectScope ,
} ,
} ,
errResult : "cannot specify a project in the grant when the scope is not an organization" ,
} ,
}
for _ , test := range tests {
t . Run ( test . name , func ( t * testing . T ) {
err := test . input . validateAndModifyProject ( )
if test . errResult == "" {
require . NoError ( t , err )
assert . Equal ( t , test . output , test . input )
} else {
require . Error ( t , err )
assert . Equal ( t , test . errResult , err . Error ( ) )
}
} )
}
}
func Test_MarshallingAndCloning ( t * testing . T ) {
t . Parallel ( )
@ -218,46 +152,21 @@ func Test_MarshallingAndCloning(t *testing.T) {
canonicalString : ` ` ,
} ,
{
name : "project" ,
input : Grant {
project : "foobar" ,
scope : Scope {
Type : iam . OrganizationScope ,
} ,
} ,
jsonOutput : ` { "project":"foobar"} ` ,
canonicalString : ` project=foobar ` ,
} ,
{
name : "project and type" ,
input : Grant {
project : "foobar" ,
scope : Scope {
Type : iam . ProjectScope ,
} ,
typ : TypeGroup ,
} ,
jsonOutput : ` { "project":"foobar","type":"group"} ` ,
canonicalString : ` project=foobar;type=group ` ,
} ,
{
name : "project, type, and id" ,
name : "type and id" ,
input : Grant {
id : "baz" ,
project : "foobar" ,
id : "baz" ,
scope : Scope {
Type : iam . ProjectScope ,
} ,
typ : TypeGroup ,
} ,
jsonOutput : ` { "id":"baz"," project":"foobar"," type":"group"}` ,
canonicalString : ` project=foobar; id=baz;type=group` ,
jsonOutput : ` { "id":"baz","type":"group"} ` ,
canonicalString : ` id=baz;type=group ` ,
} ,
{
name : "everything" ,
input : Grant {
id : "baz" ,
project : "foobar" ,
id : "baz" ,
scope : Scope {
Type : iam . ProjectScope ,
} ,
@ -268,8 +177,8 @@ func Test_MarshallingAndCloning(t *testing.T) {
} ,
actionsBeingParsed : [ ] string { "create" , "read" } ,
} ,
jsonOutput : ` { "actions":["create","read"],"id":"baz"," project":"foobar"," type":"group"}` ,
canonicalString : ` project=foobar; id=baz;type=group;actions=create,read` ,
jsonOutput : ` { "actions":["create","read"],"id":"baz"," type":"group"}` ,
canonicalString : ` id=baz;type=group;actions=create,read` ,
} ,
}
@ -310,19 +219,11 @@ func Test_Unmarshaling(t *testing.T) {
jsonErr : "invalid character 'w' looking for beginning of value" ,
} ,
{
name : "good project" ,
expected : Grant {
project : "foobar" ,
} ,
jsonInput : ` { "project":"foobar"} ` ,
textInput : ` project=foobar ` ,
} ,
{
name : "bad project" ,
jsonInput : ` { "project":true} ` ,
jsonErr : ` unable to interpret "project" as string ` ,
textInput : ` project= ` ,
textErr : ` segment "project=" not formatted correctly, missing value ` ,
name : "bad segment" ,
jsonInput : ` { "id":true} ` ,
jsonErr : ` unable to interpret "id" as string ` ,
textInput : ` id= ` ,
textErr : ` segment "id=" not formatted correctly, missing value ` ,
} ,
{
name : "good id" ,
@ -450,17 +351,21 @@ func Test_Parse(t *testing.T) {
input : "id=foobar;type=host-catalog;actions=createread" ,
err : ` unknown action "createread" ` ,
} ,
{
name : "empty id and type" ,
input : "actions=create" ,
err : ` "id" and "type" cannot both be empty ` ,
} ,
{
name : "good json" ,
input : ` { "project":"proj","id":"foobar","type":"host-catalog","actions":["create","read"]} ` ,
input : ` { " id":"foobar","type":"host-catalog","actions":["create","read"]}` ,
expected : Grant {
scope : Scope {
Id : " proj ",
Type : iam . Project Scope,
Id : " scope ",
Type : iam . Organization Scope,
} ,
project : "proj" ,
id : "foobar" ,
typ : "host-catalog" ,
id : "foobar" ,
typ : "host-catalog" ,
actions : map [ iam . Action ] bool {
iam . ActionCreate : true ,
iam . ActionRead : true ,
@ -469,15 +374,14 @@ func Test_Parse(t *testing.T) {
} ,
{
name : "good text" ,
input : ` project=proj; id=foobar;type=host-catalog;actions=create,read` ,
input : ` id=foobar;type=host-catalog;actions=create,read` ,
expected : Grant {
scope : Scope {
Id : " proj ",
Type : iam . Project Scope,
Id : " scope ",
Type : iam . Organization Scope,
} ,
project : "proj" ,
id : "foobar" ,
typ : "host-catalog" ,
id : "foobar" ,
typ : "host-catalog" ,
actions : map [ iam . Action ] bool {
iam . ActionCreate : true ,
iam . ActionRead : true ,
@ -523,10 +427,6 @@ func Test_Parse(t *testing.T) {
require . Error ( err )
assert . Equal ( "no scope ID provided" , err . Error ( ) )
_ , err = Parse ( Scope { Id : "foobar" , Type : iam . ProjectScope } , "" , ` project=foobar ` )
require . Error ( err )
assert . Equal ( "cannot specify a project in the grant when the scope is not an organization" , err . Error ( ) )
for _ , test := range tests {
t . Run ( test . name , func ( t * testing . T ) {
grant , err := Parse ( Scope { Id : "scope" , Type : iam . OrganizationScope } , test . userId , test . input )