github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/tenant/fixtures_test.go (about) 1 package tenant_test 2 3 import ( 4 "database/sql" 5 "database/sql/driver" 6 "errors" 7 "testing" 8 9 "github.com/jmoiron/sqlx" 10 "github.com/kyma-incubator/compass/components/director/internal/domain/tenant/automock" 11 "github.com/kyma-incubator/compass/components/director/internal/repo/testdb" 12 "github.com/kyma-incubator/compass/components/director/pkg/resource" 13 14 "github.com/kyma-incubator/compass/components/director/internal/repo" 15 16 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 17 "github.com/kyma-incubator/compass/components/director/pkg/str" 18 "github.com/kyma-incubator/compass/components/director/pkg/tenant" 19 20 "github.com/DATA-DOG/go-sqlmock" 21 "github.com/kyma-incubator/compass/components/director/internal/model" 22 ) 23 24 const ( 25 testExternal = "external" 26 testInternal = "internalID" 27 testID = "foo" 28 testName = "bar" 29 testParentID = "parent" 30 testParentID2 = "parent2" 31 testInternalParentID = "internal-parent" 32 testTemporaryInternalParentID = "internal-parent-temp" 33 testSubdomain = "subdomain" 34 testRegion = "eu-1" 35 testProvider = "Compass" 36 testLicenseType = "TESTLICENSE" 37 initializedColumn = "initialized" 38 invalidResourceType = "INVALID" 39 ) 40 41 var ( 42 testError = errors.New("test error") 43 testTableColumns = []string{"id", "external_name", "external_tenant", "parent", "type", "provider_name", "status"} 44 tenantAccessTestTableColumns = []string{"tenant_id", "id", "owner"} 45 tenantAccessInput = graphql.TenantAccessInput{ 46 TenantID: testExternal, 47 ResourceType: graphql.TenantAccessObjectTypeApplication, 48 ResourceID: testID, 49 Owner: true, 50 } 51 tenantAccessInputWithInvalidResourceType = graphql.TenantAccessInput{ 52 TenantID: testExternal, 53 ResourceType: graphql.TenantAccessObjectType(invalidResourceType), 54 ResourceID: testID, 55 Owner: true, 56 } 57 tenantAccessGQL = &graphql.TenantAccess{ 58 TenantID: testExternal, 59 ResourceType: graphql.TenantAccessObjectTypeApplication, 60 ResourceID: testID, 61 Owner: true, 62 } 63 tenantAccessModel = &model.TenantAccess{ 64 ExternalTenantID: testExternal, 65 InternalTenantID: testInternal, 66 ResourceType: resource.Application, 67 ResourceID: testID, 68 Owner: true, 69 } 70 tenantAccessModelWithoutExternalTenant = &model.TenantAccess{ 71 InternalTenantID: testInternal, 72 ResourceType: resource.Application, 73 ResourceID: testID, 74 Owner: true, 75 } 76 tenantAccessWithoutInternalTenantModel = &model.TenantAccess{ 77 ExternalTenantID: testExternal, 78 ResourceType: resource.Application, 79 ResourceID: testID, 80 Owner: true, 81 } 82 tenantAccessEntity = &repo.TenantAccess{ 83 TenantID: testInternal, 84 ResourceID: testID, 85 Owner: true, 86 } 87 invalidTenantAccessModel = &model.TenantAccess{ 88 ResourceType: invalidResourceType, 89 } 90 ) 91 92 func newModelBusinessTenantMapping(id, name string) *model.BusinessTenantMapping { 93 return newModelBusinessTenantMappingWithType(id, name, "", nil, tenant.Account) 94 } 95 96 func newModelBusinessTenantMappingWithLicense(id, name string, licenseType *string) *model.BusinessTenantMapping { 97 return newModelBusinessTenantMappingWithType(id, name, "", licenseType, tenant.Account) 98 } 99 100 func newModelBusinessTenantMappingWithType(id, name, parent string, licenseType *string, tenantType tenant.Type) *model.BusinessTenantMapping { 101 return &model.BusinessTenantMapping{ 102 ID: id, 103 Name: name, 104 ExternalTenant: testExternal, 105 Parent: parent, 106 Type: tenantType, 107 Provider: testProvider, 108 Status: tenant.Active, 109 LicenseType: licenseType, 110 } 111 } 112 113 func newModelBusinessTenantMappingWithComputedValues(id, name string, initialized *bool) *model.BusinessTenantMapping { 114 tenantModel := newModelBusinessTenantMapping(id, name) 115 tenantModel.Initialized = initialized 116 return tenantModel 117 } 118 119 func newModelBusinessTenantMappingWithParentAndType(id, name, parent string, licenseType *string, tntType tenant.Type) *model.BusinessTenantMapping { 120 return &model.BusinessTenantMapping{ 121 ID: id, 122 Name: name, 123 ExternalTenant: testExternal, 124 Parent: parent, 125 Type: tntType, 126 Provider: testProvider, 127 Status: tenant.Active, 128 Initialized: boolToPtr(true), 129 LicenseType: licenseType, 130 } 131 } 132 133 func newEntityBusinessTenantMapping(id, name string) *tenant.Entity { 134 return newEntityBusinessTenantMappingWithParent(id, name, "") 135 } 136 137 func newEntityBusinessTenantMappingWithParent(id, name, parent string) *tenant.Entity { 138 return &tenant.Entity{ 139 ID: id, 140 Name: name, 141 ExternalTenant: testExternal, 142 Parent: repo.NewValidNullableString(parent), 143 Type: tenant.Account, 144 ProviderName: testProvider, 145 Status: tenant.Active, 146 } 147 } 148 149 func newEntityBusinessTenantMappingWithParentAndAccount(id, name, parent string, tntType tenant.Type) *tenant.Entity { 150 tnt := newEntityBusinessTenantMappingWithParent(id, name, parent) 151 tnt.Type = tntType 152 153 return tnt 154 } 155 156 func newEntityBusinessTenantMappingWithComputedValues(id, name string, initialized *bool) *tenant.Entity { 157 tenantEntity := newEntityBusinessTenantMapping(id, name) 158 tenantEntity.Initialized = initialized 159 return tenantEntity 160 } 161 162 type sqlRow struct { 163 id string 164 name string 165 externalTenant string 166 parent sql.NullString 167 typeRow string 168 provider string 169 status tenant.Status 170 } 171 172 type sqlRowWithComputedValues struct { 173 sqlRow 174 initialized *bool 175 } 176 177 func fixSQLRowsWithComputedValues(rows []sqlRowWithComputedValues) *sqlmock.Rows { 178 columns := append(testTableColumns, initializedColumn) 179 out := sqlmock.NewRows(columns) 180 for _, row := range rows { 181 out.AddRow(row.id, row.name, row.externalTenant, row.parent, row.typeRow, row.provider, row.status, row.initialized) 182 } 183 return out 184 } 185 186 func fixSQLRows(rows []sqlRow) *sqlmock.Rows { 187 out := sqlmock.NewRows(testTableColumns) 188 for _, row := range rows { 189 out.AddRow(row.id, row.name, row.externalTenant, row.parent, row.typeRow, row.provider, row.status) 190 } 191 return out 192 } 193 194 func fixTenantMappingCreateArgs(ent tenant.Entity) []driver.Value { 195 return []driver.Value{ent.ID, ent.Name, ent.ExternalTenant, ent.Parent, ent.Type, ent.ProviderName, ent.Status} 196 } 197 198 func newModelBusinessTenantMappingInput(name, subdomain, region string, licenseType *string) model.BusinessTenantMappingInput { 199 return newModelBusinessTenantMappingInputWithType(testExternal, name, "", subdomain, region, licenseType, tenant.Account) 200 } 201 202 func newModelBusinessTenantMappingInputWithType(tenantID, name, parent, subdomain, region string, licenseType *string, tenantType tenant.Type) model.BusinessTenantMappingInput { 203 return model.BusinessTenantMappingInput{ 204 Name: name, 205 ExternalTenant: tenantID, 206 Subdomain: subdomain, 207 Region: region, 208 Parent: parent, 209 Type: tenant.TypeToStr(tenantType), 210 Provider: testProvider, 211 LicenseType: licenseType, 212 } 213 } 214 215 func newGraphQLTenant(id, internalID, name string) *graphql.Tenant { 216 return &graphql.Tenant{ 217 ID: id, 218 InternalID: internalID, 219 Name: str.Ptr(name), 220 } 221 } 222 223 func fixTenantAccesses() []repo.TenantAccess { 224 return []repo.TenantAccess{ 225 { 226 TenantID: testID, 227 ResourceID: "resourceID", 228 Owner: true, 229 }, 230 } 231 } 232 233 func fixTenantAccessesRow() []driver.Value { 234 return []driver.Value{testID, "resourceID", true} 235 } 236 237 func boolToPtr(in bool) *bool { 238 return &in 239 } 240 241 func unusedConverter() *automock.BusinessTenantMappingConverter { 242 return &automock.BusinessTenantMappingConverter{} 243 } 244 245 func unusedDBMock(t *testing.T) (*sqlx.DB, testdb.DBMock) { 246 return testdb.MockDatabase(t) 247 } 248 249 func unusedTenantConverter() *automock.BusinessTenantMappingConverter { 250 return &automock.BusinessTenantMappingConverter{} 251 } 252 253 func unusedTenantService() *automock.BusinessTenantMappingService { 254 return &automock.BusinessTenantMappingService{} 255 } 256 257 func unusedFetcherService() *automock.Fetcher { 258 return &automock.Fetcher{} 259 }