github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/tenant/converter_test.go (about)

     1  package tenant_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/internal/repo"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    11  
    12  	"github.com/kyma-incubator/compass/components/director/internal/model"
    13  
    14  	tnt "github.com/kyma-incubator/compass/components/director/pkg/tenant"
    15  
    16  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    17  
    18  	"github.com/kyma-incubator/compass/components/director/internal/domain/tenant"
    19  	"github.com/stretchr/testify/assert"
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  var (
    24  	ids             = []string{"id1", "id2"}
    25  	names           = []string{"name1", "name2"}
    26  	externalTenants = []string{"external1", "external2"}
    27  	parent          = ""
    28  	subdomain       = "subdomain"
    29  	region          = "region"
    30  	licenseType     = "TESTLICENSE"
    31  )
    32  
    33  func TestConverter(t *testing.T) {
    34  	// GIVEN
    35  	id := "foo"
    36  	name := "bar"
    37  
    38  	t.Run("all fields", func(t *testing.T) {
    39  		c := tenant.NewConverter()
    40  
    41  		// When
    42  		input := newModelBusinessTenantMapping(id, name)
    43  		entity := c.ToEntity(input)
    44  		outputModel := c.FromEntity(entity)
    45  
    46  		// THEN
    47  		assert.Equal(t, input, outputModel)
    48  	})
    49  
    50  	t.Run("initialized from entity", func(t *testing.T) {
    51  		c := tenant.NewConverter()
    52  		initialized := true
    53  		input := newEntityBusinessTenantMapping(id, name)
    54  		input.Initialized = &initialized
    55  
    56  		// When
    57  		outputModel := c.FromEntity(input)
    58  
    59  		// Then
    60  		assert.Equal(t, input.Initialized, outputModel.Initialized)
    61  	})
    62  
    63  	t.Run("nil model", func(t *testing.T) {
    64  		c := tenant.NewConverter()
    65  
    66  		// When
    67  		entity := c.ToEntity(nil)
    68  
    69  		// Then
    70  		require.Nil(t, entity)
    71  	})
    72  
    73  	t.Run("nil entity", func(t *testing.T) {
    74  		c := tenant.NewConverter()
    75  
    76  		// When
    77  		tenantModel := c.FromEntity(nil)
    78  
    79  		// Then
    80  		require.Nil(t, tenantModel)
    81  	})
    82  }
    83  
    84  func TestConverter_ToGraphQL(t *testing.T) {
    85  	t.Run("when input is nil", func(t *testing.T) {
    86  		c := tenant.NewConverter()
    87  
    88  		// WHEN
    89  		res := c.ToGraphQL(nil)
    90  
    91  		// THEN
    92  		require.Nil(t, res)
    93  	})
    94  
    95  	t.Run("all fields", func(t *testing.T) {
    96  		c := tenant.NewConverter()
    97  
    98  		// WHEN
    99  		in := newModelBusinessTenantMapping(ids[0], names[0])
   100  		res := c.ToGraphQL(in)
   101  		expected := &graphql.Tenant{
   102  			ID:          testExternal,
   103  			InternalID:  ids[0],
   104  			Name:        &names[0],
   105  			Type:        string(tnt.Account),
   106  			ParentID:    "",
   107  			Initialized: nil,
   108  			Labels:      nil,
   109  			Provider:    testProvider,
   110  		}
   111  
   112  		// THEN
   113  		require.Equal(t, expected, res)
   114  	})
   115  }
   116  
   117  func TestConverter_ToGraphQLInput(t *testing.T) {
   118  	t.Run("all fields", func(t *testing.T) {
   119  		c := tenant.NewConverter()
   120  
   121  		// WHEN
   122  		in := newModelBusinessTenantMappingInput(names[0], subdomain, region, &licenseType)
   123  		res := c.ToGraphQLInput(in)
   124  		expected := graphql.BusinessTenantMappingInput{
   125  			Name:           names[0],
   126  			ExternalTenant: testExternal,
   127  			Parent:         str.Ptr(parent),
   128  			Subdomain:      str.Ptr(subdomain),
   129  			Region:         str.Ptr(region),
   130  			Type:           string(tnt.Account),
   131  			Provider:       testProvider,
   132  			LicenseType:    str.Ptr(licenseType),
   133  		}
   134  
   135  		// THEN
   136  		require.Equal(t, expected, res)
   137  	})
   138  }
   139  
   140  func TestConverter_MultipleInputFromGraphQL(t *testing.T) {
   141  	t.Run("all fields", func(t *testing.T) {
   142  		c := tenant.NewConverter()
   143  
   144  		// WHEN
   145  		in := []*graphql.BusinessTenantMappingInput{
   146  			{
   147  				Name:           names[0],
   148  				ExternalTenant: externalTenants[0],
   149  				Parent:         str.Ptr(parent),
   150  				Subdomain:      str.Ptr(subdomain),
   151  				Region:         str.Ptr(region),
   152  				Type:           string(tnt.Account),
   153  				Provider:       testProvider,
   154  			},
   155  			{
   156  				Name:           names[1],
   157  				ExternalTenant: externalTenants[1],
   158  				Parent:         str.Ptr(parent),
   159  				Subdomain:      str.Ptr(subdomain),
   160  				Region:         str.Ptr(region),
   161  				Type:           string(tnt.Account),
   162  				Provider:       testProvider,
   163  			}}
   164  		res := c.MultipleInputFromGraphQL(in)
   165  		expected := []model.BusinessTenantMappingInput{
   166  			{
   167  				Name:           names[0],
   168  				ExternalTenant: externalTenants[0],
   169  				Parent:         parent,
   170  				Subdomain:      subdomain,
   171  				Region:         region,
   172  				Type:           string(tnt.Account),
   173  				Provider:       testProvider,
   174  			},
   175  			{
   176  				Name:           names[1],
   177  				ExternalTenant: externalTenants[1],
   178  				Parent:         parent,
   179  				Subdomain:      subdomain,
   180  				Region:         region,
   181  				Type:           string(tnt.Account),
   182  				Provider:       testProvider,
   183  			},
   184  		}
   185  
   186  		// THEN
   187  		require.Equal(t, len(expected), len(res))
   188  		require.Equal(t, expected, res)
   189  	})
   190  }
   191  
   192  func TestConverter_MultipleToGraphQL(t *testing.T) {
   193  	t.Run("when one of the tenants is nil", func(t *testing.T) {
   194  		c := tenant.NewConverter()
   195  
   196  		// WHEN
   197  		in := []*model.BusinessTenantMapping{
   198  			{
   199  				ID:             ids[0],
   200  				Name:           names[0],
   201  				ExternalTenant: externalTenants[0],
   202  				Parent:         parent,
   203  				Type:           tnt.Account,
   204  				Provider:       testProvider,
   205  				Status:         "",
   206  				Initialized:    nil,
   207  			},
   208  			nil,
   209  		}
   210  		res := c.MultipleToGraphQL(in)
   211  		expected := []*graphql.Tenant{
   212  			{
   213  				ID:          externalTenants[0],
   214  				InternalID:  ids[0],
   215  				Name:        &names[0],
   216  				Type:        string(tnt.Account),
   217  				ParentID:    "",
   218  				Initialized: nil,
   219  				Labels:      nil,
   220  				Provider:    testProvider,
   221  			},
   222  		}
   223  
   224  		// THEN
   225  		require.Equal(t, len(expected), len(res))
   226  		require.Equal(t, expected, res)
   227  	})
   228  	t.Run("all fields", func(t *testing.T) {
   229  		c := tenant.NewConverter()
   230  
   231  		// WHEN
   232  		in := []*model.BusinessTenantMapping{
   233  			{
   234  				ID:             ids[0],
   235  				Name:           names[0],
   236  				ExternalTenant: externalTenants[0],
   237  				Parent:         parent,
   238  				Type:           tnt.Account,
   239  				Provider:       testProvider,
   240  				Status:         "",
   241  				Initialized:    nil,
   242  			},
   243  			{
   244  				ID:             ids[1],
   245  				Name:           names[1],
   246  				ExternalTenant: externalTenants[1],
   247  				Parent:         parent,
   248  				Type:           tnt.Account,
   249  				Provider:       testProvider,
   250  				Status:         "",
   251  				Initialized:    nil,
   252  			},
   253  		}
   254  		res := c.MultipleToGraphQL(in)
   255  		expected := []*graphql.Tenant{
   256  			{
   257  				ID:          externalTenants[0],
   258  				InternalID:  ids[0],
   259  				Name:        &names[0],
   260  				Type:        string(tnt.Account),
   261  				ParentID:    parent,
   262  				Initialized: nil,
   263  				Labels:      nil,
   264  				Provider:    testProvider,
   265  			},
   266  			{
   267  				ID:          externalTenants[1],
   268  				InternalID:  ids[1],
   269  				Name:        &names[1],
   270  				Type:        string(tnt.Account),
   271  				ParentID:    parent,
   272  				Initialized: nil,
   273  				Labels:      nil,
   274  				Provider:    testProvider,
   275  			},
   276  		}
   277  
   278  		// THEN
   279  		require.Equal(t, len(expected), len(res))
   280  		require.Equal(t, expected, res)
   281  	})
   282  }
   283  
   284  func TestConverter_MultipleInputToGraphQLInputL(t *testing.T) {
   285  	t.Run("all fields", func(t *testing.T) {
   286  		c := tenant.NewConverter()
   287  
   288  		// WHEN
   289  		in := []model.BusinessTenantMappingInput{
   290  			{
   291  				Name:           names[0],
   292  				ExternalTenant: externalTenants[0],
   293  				Parent:         parent,
   294  				Subdomain:      subdomain,
   295  				Region:         region,
   296  				Type:           string(tnt.Account),
   297  				Provider:       testProvider,
   298  				LicenseType:    str.Ptr(testLicenseType),
   299  			},
   300  			{
   301  				Name:           names[1],
   302  				ExternalTenant: externalTenants[1],
   303  				Parent:         parent,
   304  				Subdomain:      subdomain,
   305  				Region:         region,
   306  				Type:           string(tnt.Account),
   307  				Provider:       testProvider,
   308  			},
   309  		}
   310  		res := c.MultipleInputToGraphQLInput(in)
   311  		expected := []graphql.BusinessTenantMappingInput{
   312  			{
   313  				Name:           names[0],
   314  				ExternalTenant: externalTenants[0],
   315  				Parent:         str.Ptr(parent),
   316  				Subdomain:      str.Ptr(subdomain),
   317  				Region:         str.Ptr(region),
   318  				Type:           string(tnt.Account),
   319  				Provider:       testProvider,
   320  				LicenseType:    str.Ptr(testLicenseType),
   321  			},
   322  			{
   323  				Name:           names[1],
   324  				ExternalTenant: externalTenants[1],
   325  				Parent:         str.Ptr(parent),
   326  				Subdomain:      str.Ptr(subdomain),
   327  				Region:         str.Ptr(region),
   328  				Type:           string(tnt.Account),
   329  				Provider:       testProvider,
   330  			},
   331  		}
   332  
   333  		// THEN
   334  		require.Equal(t, len(expected), len(res))
   335  		require.Equal(t, expected, res)
   336  	})
   337  }
   338  
   339  func TestConverter_TenantAccessInputFromGraphQL(t *testing.T) {
   340  	testCases := []struct {
   341  		Name             string
   342  		Input            graphql.TenantAccessInput
   343  		ExpectedErrorMsg string
   344  		ExpectedOutput   *model.TenantAccess
   345  	}{
   346  		{
   347  			Name: "Success for application",
   348  			Input: graphql.TenantAccessInput{
   349  				TenantID:     testExternal,
   350  				ResourceType: graphql.TenantAccessObjectTypeApplication,
   351  				ResourceID:   testID,
   352  				Owner:        false,
   353  			},
   354  			ExpectedOutput: &model.TenantAccess{
   355  				ExternalTenantID: testExternal,
   356  				ResourceType:     resource.Application,
   357  				ResourceID:       testID,
   358  				Owner:            false,
   359  			},
   360  		},
   361  		{
   362  			Name: "Success for runtime",
   363  			Input: graphql.TenantAccessInput{
   364  				TenantID:     testExternal,
   365  				ResourceType: graphql.TenantAccessObjectTypeRuntime,
   366  				ResourceID:   testID,
   367  				Owner:        false,
   368  			},
   369  			ExpectedOutput: &model.TenantAccess{
   370  				ExternalTenantID: testExternal,
   371  				ResourceType:     resource.Runtime,
   372  				ResourceID:       testID,
   373  				Owner:            false,
   374  			},
   375  		},
   376  		{
   377  			Name: "Success for runtime context",
   378  			Input: graphql.TenantAccessInput{
   379  				TenantID:     testExternal,
   380  				ResourceType: graphql.TenantAccessObjectTypeRuntimeContext,
   381  				ResourceID:   testID,
   382  				Owner:        false,
   383  			},
   384  			ExpectedOutput: &model.TenantAccess{
   385  				ExternalTenantID: testExternal,
   386  				ResourceType:     resource.RuntimeContext,
   387  				ResourceID:       testID,
   388  				Owner:            false,
   389  			},
   390  		},
   391  		{
   392  			Name: "Error when converting resource type",
   393  			Input: graphql.TenantAccessInput{
   394  				TenantID:     testExternal,
   395  				ResourceType: graphql.TenantAccessObjectType(resource.FormationConstraint),
   396  				ResourceID:   testID,
   397  				Owner:        false,
   398  			},
   399  			ExpectedErrorMsg: fmt.Sprintf("Unknown tenant access resource type %q", resource.FormationConstraint),
   400  		},
   401  	}
   402  
   403  	for _, testCase := range testCases {
   404  		t.Run(testCase.Name, func(t *testing.T) {
   405  			c := tenant.NewConverter()
   406  			output, err := c.TenantAccessInputFromGraphQL(testCase.Input)
   407  
   408  			if testCase.ExpectedErrorMsg != "" {
   409  				require.Error(t, err)
   410  				require.Equal(t, testCase.ExpectedErrorMsg, err.Error())
   411  				require.Nil(t, output)
   412  			} else {
   413  				require.NoError(t, err)
   414  				require.Equal(t, testCase.ExpectedOutput, output)
   415  			}
   416  		})
   417  	}
   418  }
   419  
   420  func TestConverter_TenantAccessToGraphQL(t *testing.T) {
   421  	testCases := []struct {
   422  		Name             string
   423  		Input            *model.TenantAccess
   424  		ExpectedErrorMsg string
   425  		ExpectedOutput   *graphql.TenantAccess
   426  	}{
   427  		{
   428  			Name:           "Success when nil input",
   429  			Input:          nil,
   430  			ExpectedOutput: nil,
   431  		},
   432  		{
   433  			Name: "Success for application",
   434  			Input: &model.TenantAccess{
   435  				ExternalTenantID: testExternal,
   436  				InternalTenantID: testInternal,
   437  				ResourceType:     resource.Application,
   438  				ResourceID:       testID,
   439  				Owner:            false,
   440  			},
   441  			ExpectedOutput: &graphql.TenantAccess{
   442  				TenantID:     testExternal,
   443  				ResourceType: graphql.TenantAccessObjectTypeApplication,
   444  				ResourceID:   testID,
   445  				Owner:        false,
   446  			},
   447  		},
   448  		{
   449  			Name: "Success for runtime",
   450  			Input: &model.TenantAccess{
   451  				ExternalTenantID: testExternal,
   452  				InternalTenantID: testInternal,
   453  				ResourceType:     resource.Runtime,
   454  				ResourceID:       testID,
   455  				Owner:            false,
   456  			},
   457  			ExpectedOutput: &graphql.TenantAccess{
   458  				TenantID:     testExternal,
   459  				ResourceType: graphql.TenantAccessObjectTypeRuntime,
   460  				ResourceID:   testID,
   461  				Owner:        false,
   462  			},
   463  		},
   464  		{
   465  			Name: "Success for runtime context",
   466  			Input: &model.TenantAccess{
   467  				ExternalTenantID: testExternal,
   468  				InternalTenantID: testInternal,
   469  				ResourceType:     resource.RuntimeContext,
   470  				ResourceID:       testID,
   471  				Owner:            false,
   472  			},
   473  			ExpectedOutput: &graphql.TenantAccess{
   474  				TenantID:     testExternal,
   475  				ResourceType: graphql.TenantAccessObjectTypeRuntimeContext,
   476  				ResourceID:   testID,
   477  				Owner:        false,
   478  			},
   479  		},
   480  		{
   481  			Name: "Error when converting resource type",
   482  			Input: &model.TenantAccess{
   483  				ExternalTenantID: testExternal,
   484  				InternalTenantID: testInternal,
   485  				ResourceType:     resource.FormationConstraint,
   486  				ResourceID:       testID,
   487  				Owner:            false,
   488  			},
   489  			ExpectedErrorMsg: fmt.Sprintf("Unknown tenant access resource type %q", resource.FormationConstraint),
   490  		},
   491  	}
   492  
   493  	for _, testCase := range testCases {
   494  		t.Run(testCase.Name, func(t *testing.T) {
   495  			c := tenant.NewConverter()
   496  			output, err := c.TenantAccessToGraphQL(testCase.Input)
   497  
   498  			if testCase.ExpectedErrorMsg != "" {
   499  				require.Error(t, err)
   500  				require.Equal(t, testCase.ExpectedErrorMsg, err.Error())
   501  				require.Nil(t, output)
   502  			} else {
   503  				require.NoError(t, err)
   504  				require.Equal(t, testCase.ExpectedOutput, output)
   505  			}
   506  		})
   507  	}
   508  }
   509  
   510  func TestConverter_TenantAccessToEntity(t *testing.T) {
   511  	testCases := []struct {
   512  		Name           string
   513  		Input          *model.TenantAccess
   514  		ExpectedOutput *repo.TenantAccess
   515  	}{
   516  		{
   517  			Name:           "when input is nil",
   518  			Input:          nil,
   519  			ExpectedOutput: nil,
   520  		},
   521  		{
   522  			Name: "all fields",
   523  			Input: &model.TenantAccess{
   524  				ExternalTenantID: testExternal,
   525  				InternalTenantID: testInternal,
   526  				ResourceType:     resource.Application,
   527  				ResourceID:       testID,
   528  				Owner:            true,
   529  			},
   530  			ExpectedOutput: &repo.TenantAccess{
   531  				TenantID:   testInternal,
   532  				ResourceID: testID,
   533  				Owner:      true,
   534  			},
   535  		},
   536  	}
   537  
   538  	for _, testCase := range testCases {
   539  		t.Run(testCase.Name, func(t *testing.T) {
   540  			converter := tenant.NewConverter()
   541  			output := converter.TenantAccessToEntity(testCase.Input)
   542  			require.Equal(t, testCase.ExpectedOutput, output)
   543  		})
   544  	}
   545  }
   546  
   547  func TestConverter_TenantAccessFromEntity(t *testing.T) {
   548  	testCases := []struct {
   549  		Name           string
   550  		Input          *repo.TenantAccess
   551  		ExpectedOutput *model.TenantAccess
   552  	}{
   553  		{
   554  			Name:           "when input is nil",
   555  			Input:          nil,
   556  			ExpectedOutput: nil,
   557  		},
   558  		{
   559  			Name: "all fields",
   560  			Input: &repo.TenantAccess{
   561  				TenantID:   testInternal,
   562  				ResourceID: testID,
   563  				Owner:      false,
   564  			},
   565  			ExpectedOutput: &model.TenantAccess{
   566  				InternalTenantID: testInternal,
   567  				ResourceID:       testID,
   568  				Owner:            false,
   569  			},
   570  		},
   571  	}
   572  
   573  	for _, testCase := range testCases {
   574  		t.Run(testCase.Name, func(t *testing.T) {
   575  			converter := tenant.NewConverter()
   576  			output := converter.TenantAccessFromEntity(testCase.Input)
   577  			require.Equal(t, testCase.ExpectedOutput, output)
   578  		})
   579  	}
   580  }