github.com/mendersoftware/go-lib-micro@v0.0.0-20240304135804-e8e39c59b148/store/utils_test.go (about) 1 // Copyright 2023 Northern.tech AS 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package store 15 16 import ( 17 "context" 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 22 "github.com/mendersoftware/go-lib-micro/identity" 23 ) 24 25 func TestDbFromContextEmptyContext(t *testing.T) { 26 db := DbFromContext(context.Background(), "foo") 27 assert.Equal(t, db, "foo") 28 } 29 30 func TestDbFromContextNoTenant(t *testing.T) { 31 ctx := context.Background() 32 id := identity.Identity{ 33 Subject: "subject", 34 } 35 db := DbFromContext(identity.WithContext(ctx, &id), "foo") 36 assert.Equal(t, db, "foo") 37 } 38 39 func TestDbFromContext(t *testing.T) { 40 ctx := context.Background() 41 id := identity.Identity{ 42 Subject: "subject", 43 Tenant: "bar", 44 } 45 db := DbFromContext(identity.WithContext(ctx, &id), "foo") 46 assert.Equal(t, db, "foo-bar") 47 } 48 49 func TestIsTenantDb(t *testing.T) { 50 matcher := IsTenantDb("servicedb") 51 52 assert.True(t, matcher("servicedb-tenant1")) 53 assert.False(t, matcher("servicedb")) 54 assert.False(t, matcher("servicedbtenant1")) 55 56 } 57 58 func TestTenantFromDbName(t *testing.T) { 59 60 assert.Equal(t, "tenant1", TenantFromDbName("ser-vice_dev-adm-tenant1", "ser-vice_dev-adm")) 61 assert.Equal(t, "", TenantFromDbName("-tenant1", "service_devadm")) 62 assert.Equal(t, "", TenantFromDbName("service_devadm", "service_devadm")) 63 assert.Equal(t, "198273913adsjhakdh", 64 TenantFromDbName("123__--afff-198273913adsjhakdh", "123__--afff")) 65 } 66 67 func TestDbNameForTenant(t *testing.T) { 68 assert.Equal(t, "basedb-tenant1", DbNameForTenant("tenant1", "basedb")) 69 assert.Equal(t, "basedb", DbNameForTenant("", "basedb")) 70 }