github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/cd-service/pkg/repository/testutil/testutil.go (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  package testutil
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/freiheit-com/kuberpult/services/cd-service/pkg/config"
    26  	"github.com/onokonem/sillyQueueServer/timeuuid"
    27  
    28  	"github.com/freiheit-com/kuberpult/pkg/auth"
    29  	"github.com/freiheit-com/kuberpult/pkg/uuid"
    30  	"google.golang.org/grpc/metadata"
    31  )
    32  
    33  func MakeTestContext() context.Context {
    34  	u := auth.User{
    35  		DexAuthContext: nil,
    36  		Email:          "testmail@example.com",
    37  		Name:           "test tester",
    38  	}
    39  	ctx := auth.WriteUserToContext(context.Background(), u)
    40  
    41  	// for some specific calls we need to set the *incoming* grpc context
    42  	// this happens when we call a function like `ProcessBatch` directly in the test
    43  	ctx = metadata.NewIncomingContext(ctx, metadata.New(map[string]string{
    44  		auth.HeaderUserEmail: auth.Encode64("myemail@example.com"),
    45  		auth.HeaderUserName:  auth.Encode64("my name"),
    46  	}))
    47  	return ctx
    48  }
    49  
    50  func MakeTestContextDexEnabled() context.Context {
    51  	// Default user role.
    52  	return MakeTestContextDexEnabledUser("developer")
    53  }
    54  
    55  func MakeTestContextDexEnabledUser(role string) context.Context {
    56  	u := auth.User{
    57  		Email:          "testmail@example.com",
    58  		Name:           "test tester",
    59  		DexAuthContext: &auth.DexAuthContext{Role: role},
    60  	}
    61  	ctx := auth.WriteUserToContext(context.Background(), u)
    62  	ctx = metadata.NewIncomingContext(ctx, metadata.New(map[string]string{
    63  		auth.HeaderUserEmail: auth.Encode64("myemail@example.com"),
    64  		auth.HeaderUserName:  auth.Encode64("my name"),
    65  		auth.HeaderUserRole:  auth.Encode64(role),
    66  	}))
    67  	return ctx
    68  }
    69  
    70  func MakeEnvConfigLatest(argoCd *config.EnvironmentConfigArgoCd) config.EnvironmentConfig {
    71  	return MakeEnvConfigLatestWithGroup(argoCd, nil)
    72  }
    73  
    74  func MakeEnvConfigLatestWithGroup(argoCd *config.EnvironmentConfigArgoCd, envGroup *string) config.EnvironmentConfig {
    75  	return config.EnvironmentConfig{
    76  		Upstream: &config.EnvironmentConfigUpstream{
    77  			Environment: "",
    78  			Latest:      true,
    79  		},
    80  		ArgoCd:           argoCd,
    81  		EnvironmentGroup: envGroup,
    82  	}
    83  }
    84  
    85  func MakeEnvConfigUpstream(upstream string, argoCd *config.EnvironmentConfigArgoCd) config.EnvironmentConfig {
    86  	return config.EnvironmentConfig{
    87  		Upstream: &config.EnvironmentConfigUpstream{
    88  			Latest:      false,
    89  			Environment: upstream,
    90  		},
    91  		ArgoCd:           argoCd,
    92  		EnvironmentGroup: nil,
    93  	}
    94  }
    95  
    96  type TestGenerator struct {
    97  	Time time.Time
    98  }
    99  
   100  func (t TestGenerator) Generate() string {
   101  	return timeuuid.UUIDFromTime(t.Time).String()
   102  }
   103  
   104  type IncrementalUUIDBase struct {
   105  	count uint64
   106  }
   107  
   108  func (gen *IncrementalUUIDBase) Generate() string {
   109  	ret := "00000000-0000-0000-0000-" + strings.Repeat("0", (12-len(fmt.Sprint(gen.count)))) + fmt.Sprint(gen.count)
   110  	gen.count++
   111  	return ret
   112  }
   113  
   114  type IncrementalUUID struct {
   115  	gen *IncrementalUUIDBase
   116  }
   117  
   118  func (gen IncrementalUUID) Generate() string {
   119  	return gen.gen.Generate()
   120  }
   121  
   122  func NewIncrementalUUIDGenerator() uuid.GenerateUUIDs {
   123  	fakeGenBase := IncrementalUUIDBase{
   124  		count: 0,
   125  	}
   126  	fakeGen := IncrementalUUID{
   127  		gen: &fakeGenBase,
   128  	}
   129  	return fakeGen
   130  }