...

Source file src/github.com/okta/okta-sdk-golang/v2/tests/integration/group_schema_test.go

Documentation: github.com/okta/okta-sdk-golang/v2/tests/integration

     1  /*
     2   * Copyright 2018 - Present Okta, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package integration
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/okta/okta-sdk-golang/v2/okta"
    24  	"github.com/okta/okta-sdk-golang/v2/tests"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestCanGetGroupProperties(t *testing.T) {
    30  	ctx, client, err := tests.NewClient(context.TODO())
    31  	require.NoError(t, err)
    32  
    33  	gc, response, err := client.GroupSchema.GetGroupSchema(ctx)
    34  	require.NoError(t, err, "getting group schema must not error")
    35  	require.NotNil(t, gc, "group schema should not be nil")
    36  	require.IsType(t, &okta.Response{}, response, "did not return `*okta.Response` type as second variable")
    37  	require.IsType(t, &okta.GroupSchema{}, gc, "did not return `*okta.GroupSchema{}` as first variable")
    38  	assert.Equal(t, "GET", response.Response.Request.Method, "did not make a get request")
    39  	assert.Equal(t, "/api/v1/meta/schemas/group/default", response.Response.Request.URL.Path, "path for request was incorrect")
    40  
    41  	assert.Equal(t, "#custom", gc.Definitions.Custom.Id)
    42  	assert.Equal(t, "#base", gc.Definitions.Base.Id)
    43  	assert.Equal(t, "Name", gc.Definitions.Base.Properties["name"].Title)
    44  	assert.Equal(t, "Description", gc.Definitions.Base.Properties["description"].Title)
    45  }
    46  
    47  func TestCanUpdateCustomGroupProperty(t *testing.T) {
    48  	ctx, client, err := tests.NewClient(context.TODO())
    49  	require.NoError(t, err)
    50  
    51  	gc, _, err := client.GroupSchema.GetGroupSchema(ctx)
    52  	require.NoError(t, err, "getting group schema must not error")
    53  	require.NotNil(t, gc, "group schema should not be nil")
    54  
    55  	testProperty1 := randomTestString()
    56  	testProperty2 := randomTestString()
    57  
    58  	gc.Definitions.Custom.Properties[testProperty1] = &okta.GroupSchemaAttribute{
    59  		Description: "testing",
    60  		Items: &okta.UserSchemaAttributeItems{
    61  			Enum: []interface{}{"test", "1", "2"},
    62  			OneOf: []*okta.UserSchemaAttributeEnum{
    63  				{
    64  					Const: "test",
    65  					Title: "test",
    66  				},
    67  				{
    68  					Const: "1",
    69  					Title: "1",
    70  				},
    71  				{
    72  					Const: "2",
    73  					Title: "2",
    74  				},
    75  			},
    76  			Type: "string",
    77  		},
    78  		Master: &okta.UserSchemaAttributeMaster{
    79  			Type: "OKTA",
    80  		},
    81  		Mutability: "READ_WRITE",
    82  		Permissions: []*okta.UserSchemaAttributePermission{
    83  			{
    84  				Action:    "READ_ONLY",
    85  				Principal: "SELF",
    86  			},
    87  		},
    88  		Scope: "NONE",
    89  		Title: "Property Title",
    90  		Type:  "array",
    91  	}
    92  	var max int64 = 20
    93  	var min int64 = 1
    94  	gc.Definitions.Custom.Properties[testProperty2] = &okta.GroupSchemaAttribute{
    95  		Description:  "User's username for twitter.com",
    96  		ExternalName: "External Twitter username",
    97  		Master: &okta.UserSchemaAttributeMaster{
    98  			Type: "PROFILE_MASTER",
    99  		},
   100  		MaxLength:  max,
   101  		MinLength:  min,
   102  		Mutability: "READ_WRITE",
   103  		Permissions: []*okta.UserSchemaAttributePermission{
   104  			{
   105  				Action:    "READ_WRITE",
   106  				Principal: "SELF",
   107  			},
   108  		},
   109  		Scope:  "NONE",
   110  		Title:  "Twitter username",
   111  		Type:   "string",
   112  		Unique: "UNIQUE_VALIDATED",
   113  	}
   114  	updatedGC, _, err := client.GroupSchema.UpdateGroupSchema(ctx, *gc)
   115  	require.NoError(t, err, "updating group schema must not error")
   116  	require.NotNil(t, updatedGC, "updated group schema should not be nil")
   117  
   118  	assert.Equal(t, "Property Title", updatedGC.Definitions.Custom.Properties[testProperty1].Title)
   119  	assert.Equal(t, 3, len(updatedGC.Definitions.Custom.Properties[testProperty1].Items.Enum))
   120  	assert.Equal(t, 3, len(updatedGC.Definitions.Custom.Properties[testProperty1].Items.OneOf))
   121  	assert.Equal(t, "string", updatedGC.Definitions.Custom.Properties[testProperty1].Items.Type)
   122  	// assert.Equal(t, "OKTA", updatedGC.Definitions.Custom.Properties[testProperty1].Master.Type)
   123  
   124  	assert.Equal(t, "Twitter username", updatedGC.Definitions.Custom.Properties[testProperty2].Title)
   125  	assert.Nil(t, updatedGC.Definitions.Custom.Properties[testProperty2].Items)
   126  	// assert.Equal(t, "PROFILE_MASTER", updatedGC.Definitions.Custom.Properties[testProperty2].Master.Type)
   127  	assert.Equal(t, int64(1), updatedGC.Definitions.Custom.Properties[testProperty2].MinLength)
   128  	assert.Equal(t, int64(20), updatedGC.Definitions.Custom.Properties[testProperty2].MaxLength)
   129  	assert.Equal(t, "UNIQUE_VALIDATED", updatedGC.Definitions.Custom.Properties[testProperty2].Unique)
   130  
   131  	updatedGC.Definitions.Custom.Properties[testProperty1] = nil
   132  	updatedGC.Definitions.Custom.Properties[testProperty2] = nil
   133  
   134  	noCustomGC, _, err := client.GroupSchema.UpdateGroupSchema(ctx, *updatedGC)
   135  	require.NoError(t, err, "updating group schema must not error")
   136  	require.NotNil(t, noCustomGC, "updated group schema should not be nil")
   137  
   138  	assert.Nil(t, noCustomGC.Definitions.Custom.Properties[testProperty1], "property should be removed")
   139  	assert.Nil(t, noCustomGC.Definitions.Custom.Properties[testProperty2], "property should be removed")
   140  }
   141  
   142  func TestCanUpdateCustomGroupPropertyAsNumber(t *testing.T) {
   143  	ctx, client, err := tests.NewClient(context.TODO())
   144  	require.NoError(t, err)
   145  
   146  	gc, _, err := client.GroupSchema.GetGroupSchema(ctx)
   147  	require.NoError(t, err, "getting group schema must not error")
   148  	require.NotNil(t, gc, "group schema should not be nil")
   149  
   150  	testProperty1 := randomTestString()
   151  
   152  	gc.Definitions.Custom.Properties[testProperty1] = &okta.GroupSchemaAttribute{
   153  		Description: "testing",
   154  		Items: &okta.UserSchemaAttributeItems{
   155  			Enum: []interface{}{1.0, 2.0, 3.0},
   156  			OneOf: []*okta.UserSchemaAttributeEnum{
   157  				{
   158  					Const: 1.0,
   159  					Title: "one",
   160  				},
   161  				{
   162  					Const: 2.0,
   163  					Title: "two",
   164  				},
   165  				{
   166  					Const: 3.0,
   167  					Title: "three",
   168  				},
   169  			},
   170  			Type: "number",
   171  		},
   172  		Master: &okta.UserSchemaAttributeMaster{
   173  			Type: "OKTA",
   174  		},
   175  		Mutability: "READ_WRITE",
   176  		Permissions: []*okta.UserSchemaAttributePermission{
   177  			{
   178  				Action:    "READ_ONLY",
   179  				Principal: "SELF",
   180  			},
   181  		},
   182  		Scope: "NONE",
   183  		Title: "Property Title",
   184  		Type:  "array",
   185  	}
   186  	updatedGC, _, err := client.GroupSchema.UpdateGroupSchema(ctx, *gc)
   187  	require.NoError(t, err, "updating group schema must not error")
   188  	require.NotNil(t, updatedGC, "updated group schema should not be nil")
   189  
   190  	assert.Equal(t, "Property Title", updatedGC.Definitions.Custom.Properties[testProperty1].Title)
   191  	assert.Equal(t, 3, len(updatedGC.Definitions.Custom.Properties[testProperty1].Items.Enum))
   192  	oneNumber := updatedGC.Definitions.Custom.Properties[testProperty1].Items.Enum[0]
   193  	assert.Equal(t, 1.0, oneNumber)
   194  	assert.Equal(t, 3, len(updatedGC.Definitions.Custom.Properties[testProperty1].Items.OneOf))
   195  	oneConstNumber := updatedGC.Definitions.Custom.Properties[testProperty1].Items.OneOf[0]
   196  	assert.Equal(t, 1.0, oneConstNumber.Const)
   197  	assert.Equal(t, "one", oneConstNumber.Title)
   198  	assert.Equal(t, "number", updatedGC.Definitions.Custom.Properties[testProperty1].Items.Type)
   199  
   200  	updatedGC.Definitions.Custom.Properties[testProperty1] = nil
   201  
   202  	noCustomGC, _, err := client.GroupSchema.UpdateGroupSchema(ctx, *updatedGC)
   203  	require.NoError(t, err, "updating group schema must not error")
   204  	require.NotNil(t, noCustomGC, "updated group schema should not be nil")
   205  
   206  	assert.Nil(t, noCustomGC.Definitions.Custom.Properties[testProperty1], "property should be removed")
   207  }
   208  

View as plain text