github.com/mendersoftware/go-lib-micro@v0.0.0-20240304135804-e8e39c59b148/store/v2/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  	"errors"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"go.mongodb.org/mongo-driver/bson"
    23  
    24  	"github.com/mendersoftware/go-lib-micro/identity"
    25  )
    26  
    27  type SampleObject struct {
    28  	Attribute string `json:"attribute" bson:"attribute"`
    29  }
    30  
    31  type SampleMarshalerObject struct {
    32  	Attribute string
    33  }
    34  
    35  func (s SampleMarshalerObject) MarshalBSON() ([]byte, error) {
    36  	m := map[string]string{}
    37  	m["attribute"] = s.Attribute
    38  	return bson.Marshal(m)
    39  }
    40  
    41  type SampleBadMarshalerObject struct{ Foo bool }
    42  
    43  func (SampleBadMarshalerObject) MarshalBSON() ([]byte, error) {
    44  	return nil, errors.New("dunno")
    45  }
    46  
    47  type SampleBadMarshalerObject2 struct{ Foo bool }
    48  
    49  func (SampleBadMarshalerObject2) MarshalBSON() ([]byte, error) {
    50  	return []byte("this is an invalid BSON type"), nil
    51  }
    52  
    53  func TestWithTenantID(t *testing.T) {
    54  	ctx := context.Background()
    55  
    56  	sample := &SampleObject{Attribute: "value"}
    57  	sample2 := SampleMarshalerObject{Attribute: "val"}
    58  	sampleBad := SampleBadMarshalerObject{}
    59  	sampleBad2 := SampleBadMarshalerObject2{}
    60  
    61  	// without tenant ID
    62  	res := WithTenantID(ctx, map[string]interface{}{"key": "value"})
    63  	assert.Equal(t, bson.D{{Key: "key", Value: "value"}, {Key: FieldTenantID, Value: ""}}, res)
    64  
    65  	res = WithTenantID(ctx, bson.M{"key": "value"})
    66  	assert.Equal(t, bson.D{{Key: "key", Value: "value"}, {Key: FieldTenantID, Value: ""}}, res)
    67  
    68  	res = WithTenantID(ctx, bson.D{{Key: "key", Value: "value"}})
    69  	assert.Equal(t, bson.D{{Key: "key", Value: "value"}, {Key: FieldTenantID, Value: ""}}, res)
    70  
    71  	res = WithTenantID(ctx, sample)
    72  	assert.Equal(t, bson.D{{Key: "attribute", Value: "value"}, {Key: FieldTenantID, Value: ""}}, res)
    73  
    74  	res = WithTenantID(ctx, sample2)
    75  	assert.Equal(t, bson.D{{Key: "attribute", Value: "val"}, {Key: FieldTenantID, Value: ""}}, res)
    76  
    77  	res = WithTenantID(ctx, sampleBad)
    78  	assert.Nil(t, res)
    79  
    80  	res = WithTenantID(ctx, sampleBad2)
    81  	assert.Nil(t, res)
    82  
    83  	res = WithTenantID(ctx, "dummy-value")
    84  	assert.Nil(t, res)
    85  
    86  	// with tenant ID
    87  	const tenantID = "bar"
    88  	id := &identity.Identity{
    89  		Subject: "subject",
    90  		Tenant:  tenantID,
    91  	}
    92  	ctx = identity.WithContext(ctx, id)
    93  
    94  	res = WithTenantID(ctx, map[string]interface{}{"key": "value"})
    95  	assert.Equal(t, bson.D{{Key: "key", Value: "value"}, {Key: FieldTenantID, Value: tenantID}}, res)
    96  
    97  	res = WithTenantID(ctx, bson.M{"key": "value"})
    98  	assert.Equal(t, bson.D{{Key: "key", Value: "value"}, {Key: FieldTenantID, Value: tenantID}}, res)
    99  
   100  	res = WithTenantID(ctx, bson.D{{Key: "key", Value: "value"}})
   101  	assert.Equal(t, bson.D{{Key: "key", Value: "value"}, {Key: FieldTenantID, Value: tenantID}}, res)
   102  
   103  	res = WithTenantID(ctx, sample)
   104  	assert.Equal(t, bson.D{{Key: "attribute", Value: "value"}, {Key: FieldTenantID, Value: tenantID}}, res)
   105  
   106  	res = WithTenantID(ctx, "dummy-value")
   107  	assert.Nil(t, res)
   108  }
   109  
   110  func TestArrayWithTenantID(t *testing.T) {
   111  	ctx := context.Background()
   112  
   113  	// without tenant ID
   114  	res := ArrayWithTenantID(ctx, bson.A{bson.M{"key": "value"}})
   115  	assert.Equal(t, bson.A{bson.D{{Key: "key", Value: "value"}, {Key: FieldTenantID, Value: ""}}}, res)
   116  
   117  	// with tenant ID
   118  	const tenantID = "bar"
   119  	id := &identity.Identity{
   120  		Subject: "subject",
   121  		Tenant:  tenantID,
   122  	}
   123  	ctx = identity.WithContext(ctx, id)
   124  
   125  	res = ArrayWithTenantID(ctx, bson.A{bson.M{"key": "value"}})
   126  	assert.Equal(t, bson.A{bson.D{{Key: "key", Value: "value"}, {Key: FieldTenantID, Value: tenantID}}}, res)
   127  }
   128  
   129  func TestDbFromContextEmptyContext(t *testing.T) {
   130  	db := DbFromContext(context.Background(), "foo")
   131  	assert.Equal(t, db, "foo")
   132  }
   133  
   134  func TestDbFromContextNoTenant(t *testing.T) {
   135  	ctx := context.Background()
   136  	id := identity.Identity{
   137  		Subject: "subject",
   138  	}
   139  	db := DbFromContext(identity.WithContext(ctx, &id), "foo")
   140  	assert.Equal(t, db, "foo")
   141  }
   142  
   143  func TestDbFromContext(t *testing.T) {
   144  	ctx := context.Background()
   145  	id := identity.Identity{
   146  		Subject: "subject",
   147  		Tenant:  "bar",
   148  	}
   149  	db := DbFromContext(identity.WithContext(ctx, &id), "foo")
   150  	assert.Equal(t, db, "foo")
   151  }
   152  
   153  func TestIsTenantDb(t *testing.T) {
   154  	matcher := IsTenantDb("servicedb")
   155  
   156  	assert.True(t, matcher("servicedb-tenant1"))
   157  	assert.False(t, matcher("servicedb"))
   158  	assert.False(t, matcher("servicedbtenant1"))
   159  
   160  }
   161  
   162  func TestTenantFromDbName(t *testing.T) {
   163  	assert.Equal(t, "tenant1", TenantFromDbName("ser-vice_dev-adm-tenant1", "ser-vice_dev-adm"))
   164  	assert.Equal(t, "", TenantFromDbName("-tenant1", "service_devadm"))
   165  	assert.Equal(t, "", TenantFromDbName("service_devadm", "service_devadm"))
   166  	assert.Equal(t, "198273913adsjhakdh",
   167  		TenantFromDbName("123__--afff-198273913adsjhakdh", "123__--afff"))
   168  }
   169  
   170  func TestDbNameForTenant(t *testing.T) {
   171  	assert.Equal(t, "basedb", DbNameForTenant("tenant1", "basedb"))
   172  	assert.Equal(t, "basedb", DbNameForTenant("", "basedb"))
   173  }