github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/process/deprovisioning/avs_evaluations_test.go (about)

     1  package deprovisioning
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"strconv"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/kyma-project/kyma-environment-broker/internal/avs"
    12  	"github.com/kyma-project/kyma-environment-broker/internal/broker"
    13  	"github.com/kyma-project/kyma-environment-broker/internal/storage"
    14  
    15  	"github.com/gorilla/mux"
    16  	"github.com/sirupsen/logrus"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  var (
    21  	evalIdsHolder      []int64
    22  	parentEvalIdHolder = make(map[int64]int64)
    23  )
    24  
    25  const (
    26  	internalEvalId = int64(1234)
    27  	externalEvalId = int64(5678)
    28  	parentEvalId   = int64(91011)
    29  )
    30  
    31  func TestAvsEvaluationsRemovalStep_Run(t *testing.T) {
    32  	// given
    33  	logger := logrus.New()
    34  	memoryStorage := storage.NewMemoryStorage()
    35  
    36  	deProvisioningOperation := fixDeprovisioningOperation().Operation
    37  	deProvisioningOperation.Avs.AvsEvaluationInternalId = internalEvalId
    38  	deProvisioningOperation.Avs.AVSEvaluationExternalId = externalEvalId
    39  	err := memoryStorage.Operations().InsertOperation(deProvisioningOperation)
    40  	assert.NoError(t, err)
    41  	assert.False(t, deProvisioningOperation.Avs.AVSInternalEvaluationDeleted)
    42  	assert.False(t, deProvisioningOperation.Avs.AVSExternalEvaluationDeleted)
    43  
    44  	mockOauthServer := newMockAvsOauthServer()
    45  	defer mockOauthServer.Close()
    46  	mockAvsServer := newMockAvsServer(t)
    47  	defer mockAvsServer.Close()
    48  	avsConfig := avsConfig(mockOauthServer, mockAvsServer)
    49  	avsClient, err := avs.NewClient(context.TODO(), avsConfig, logrus.New())
    50  	assert.NoError(t, err)
    51  	avsDel := avs.NewDelegator(avsClient, avsConfig, memoryStorage.Operations())
    52  	internalEvalAssistant := avs.NewInternalEvalAssistant(avsConfig)
    53  	externalEvalAssistant := avs.NewExternalEvalAssistant(avsConfig)
    54  	step := NewAvsEvaluationsRemovalStep(avsDel, memoryStorage.Operations(), externalEvalAssistant, internalEvalAssistant)
    55  
    56  	assert.Equal(t, 0, len(evalIdsHolder))
    57  	assert.Equal(t, 0, len(parentEvalIdHolder))
    58  	// when
    59  	deProvisioningOperation, repeat, err := step.Run(deProvisioningOperation, logger)
    60  
    61  	// then
    62  	assert.NoError(t, err)
    63  	assert.Equal(t, time.Duration(0), repeat)
    64  
    65  	assert.Equal(t, 2, len(evalIdsHolder))
    66  	assert.Contains(t, evalIdsHolder, internalEvalId)
    67  	assert.Contains(t, evalIdsHolder, externalEvalId)
    68  
    69  	assert.Equal(t, 2, len(parentEvalIdHolder))
    70  	assert.Contains(t, parentEvalIdHolder, internalEvalId)
    71  	assert.Contains(t, parentEvalIdHolder, externalEvalId)
    72  	assert.Equal(t, parentEvalIdHolder[internalEvalId], parentEvalId)
    73  	assert.Equal(t, parentEvalIdHolder[externalEvalId], parentEvalId)
    74  
    75  	inDB, err := memoryStorage.Operations().GetDeprovisioningOperationByID(deProvisioningOperation.ID)
    76  	assert.NoError(t, err)
    77  	assert.True(t, inDB.Avs.AVSInternalEvaluationDeleted)
    78  	assert.True(t, inDB.Avs.AVSExternalEvaluationDeleted)
    79  	assert.Equal(t, internalEvalId, inDB.Avs.AvsEvaluationInternalId)
    80  	assert.Equal(t, externalEvalId, inDB.Avs.AVSEvaluationExternalId)
    81  }
    82  
    83  func TestAvsEvaluationsRemovalWhenAlreadyDeleted_Run(t *testing.T) {
    84  	// given
    85  	logger := logrus.New()
    86  	memoryStorage := storage.NewMemoryStorage()
    87  
    88  	deProvisioningOperation := fixDeprovisioningOperation().Operation
    89  	deProvisioningOperation.Avs.AvsEvaluationInternalId = internalEvalId
    90  	deProvisioningOperation.Avs.AVSEvaluationExternalId = externalEvalId
    91  	deProvisioningOperation.Avs.AVSExternalEvaluationDeleted = true
    92  	deProvisioningOperation.Avs.AVSInternalEvaluationDeleted = true
    93  	err := memoryStorage.Operations().InsertOperation(deProvisioningOperation)
    94  	assert.NoError(t, err)
    95  	assert.True(t, deProvisioningOperation.Avs.AVSInternalEvaluationDeleted)
    96  	assert.True(t, deProvisioningOperation.Avs.AVSExternalEvaluationDeleted)
    97  
    98  	mockOauthServer := newMockAvsOauthServer()
    99  	defer mockOauthServer.Close()
   100  	mockAvsServer := newMockAvsServer(t)
   101  	defer mockAvsServer.Close()
   102  	avsConfig := avsConfig(mockOauthServer, mockAvsServer)
   103  	avsClient, err := avs.NewClient(context.TODO(), avsConfig, logrus.New())
   104  	assert.NoError(t, err)
   105  	avsDel := avs.NewDelegator(avsClient, avsConfig, memoryStorage.Operations())
   106  	internalEvalAssistant := avs.NewInternalEvalAssistant(avsConfig)
   107  	externalEvalAssistant := avs.NewExternalEvalAssistant(avsConfig)
   108  	step := NewAvsEvaluationsRemovalStep(avsDel, memoryStorage.Operations(), externalEvalAssistant, internalEvalAssistant)
   109  
   110  	// when
   111  	deProvisioningOperation, repeat, err := step.Run(deProvisioningOperation, logger)
   112  
   113  	// then
   114  	assert.NoError(t, err)
   115  	assert.Equal(t, time.Duration(0), repeat)
   116  
   117  	inDB, err := memoryStorage.Operations().GetDeprovisioningOperationByID(deProvisioningOperation.ID)
   118  	assert.NoError(t, err)
   119  	assert.True(t, inDB.Avs.AVSInternalEvaluationDeleted)
   120  	assert.True(t, inDB.Avs.AVSExternalEvaluationDeleted)
   121  	assert.Equal(t, internalEvalId, inDB.Avs.AvsEvaluationInternalId)
   122  	assert.Equal(t, externalEvalId, inDB.Avs.AVSEvaluationExternalId)
   123  }
   124  
   125  func TestExternalAvsEvaluationsRemovalSkipForTrialPlan_Run(t *testing.T) {
   126  	// given
   127  	logger := logrus.New()
   128  	memoryStorage := storage.NewMemoryStorage()
   129  
   130  	deProvisioningOperation := fixDeprovisioningOperationWithPlanID(broker.TrialPlanID)
   131  	deProvisioningOperation.Avs.AVSEvaluationExternalId = externalEvalId
   132  	err := memoryStorage.Operations().InsertOperation(deProvisioningOperation)
   133  	assert.NoError(t, err)
   134  	assert.False(t, deProvisioningOperation.Avs.AVSInternalEvaluationDeleted)
   135  	assert.False(t, deProvisioningOperation.Avs.AVSExternalEvaluationDeleted)
   136  
   137  	mockOauthServer := newMockAvsOauthServer()
   138  	defer mockOauthServer.Close()
   139  	mockAvsServer := newMockAvsServer(t)
   140  	defer mockAvsServer.Close()
   141  	avsConfig := avsConfig(mockOauthServer, mockAvsServer)
   142  	avsClient, err := avs.NewClient(context.TODO(), avsConfig, logrus.New())
   143  	assert.NoError(t, err)
   144  	avsDel := avs.NewDelegator(avsClient, avsConfig, memoryStorage.Operations())
   145  	internalEvalAssistant := avs.NewInternalEvalAssistant(avsConfig)
   146  	externalEvalAssistant := avs.NewExternalEvalAssistant(avsConfig)
   147  	step := NewAvsEvaluationsRemovalStep(avsDel, memoryStorage.Operations(), externalEvalAssistant, internalEvalAssistant)
   148  
   149  	// when
   150  	deProvisioningOperation, repeat, err := step.Run(deProvisioningOperation, logger)
   151  
   152  	// then
   153  	assert.NoError(t, err)
   154  	assert.Equal(t, time.Duration(0), repeat)
   155  
   156  	inDB, err := memoryStorage.Operations().GetDeprovisioningOperationByID(deProvisioningOperation.ID)
   157  	assert.NoError(t, err)
   158  	assert.False(t, inDB.Avs.AVSExternalEvaluationDeleted)
   159  	assert.Equal(t, externalEvalId, inDB.Avs.AVSEvaluationExternalId)
   160  }
   161  
   162  func TestExternalAvsEvaluationsRemovalSkipForFreemiumPlan_Run(t *testing.T) {
   163  	// given
   164  	logger := logrus.New()
   165  	memoryStorage := storage.NewMemoryStorage()
   166  
   167  	deProvisioningOperation := fixDeprovisioningOperationWithPlanID(broker.FreemiumPlanID)
   168  	deProvisioningOperation.Avs.AVSEvaluationExternalId = externalEvalId
   169  	err := memoryStorage.Operations().InsertOperation(deProvisioningOperation)
   170  	assert.NoError(t, err)
   171  	assert.False(t, deProvisioningOperation.Avs.AVSInternalEvaluationDeleted)
   172  	assert.False(t, deProvisioningOperation.Avs.AVSExternalEvaluationDeleted)
   173  
   174  	mockOauthServer := newMockAvsOauthServer()
   175  	defer mockOauthServer.Close()
   176  	mockAvsServer := newMockAvsServer(t)
   177  	defer mockAvsServer.Close()
   178  	avsConfig := avsConfig(mockOauthServer, mockAvsServer)
   179  	avsClient, err := avs.NewClient(context.TODO(), avsConfig, logrus.New())
   180  	assert.NoError(t, err)
   181  	avsDel := avs.NewDelegator(avsClient, avsConfig, memoryStorage.Operations())
   182  	internalEvalAssistant := avs.NewInternalEvalAssistant(avsConfig)
   183  	externalEvalAssistant := avs.NewExternalEvalAssistant(avsConfig)
   184  	step := NewAvsEvaluationsRemovalStep(avsDel, memoryStorage.Operations(), externalEvalAssistant, internalEvalAssistant)
   185  
   186  	// when
   187  	deProvisioningOperation, repeat, err := step.Run(deProvisioningOperation, logger)
   188  
   189  	// then
   190  	assert.NoError(t, err)
   191  	assert.Equal(t, time.Duration(0), repeat)
   192  
   193  	inDB, err := memoryStorage.Operations().GetDeprovisioningOperationByID(deProvisioningOperation.ID)
   194  	assert.NoError(t, err)
   195  	assert.False(t, inDB.Avs.AVSExternalEvaluationDeleted)
   196  	assert.Equal(t, externalEvalId, inDB.Avs.AVSEvaluationExternalId)
   197  }
   198  
   199  func newMockAvsOauthServer() *httptest.Server {
   200  	return httptest.NewServer(
   201  		http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
   202  			writer.Header().Set("Content-Type", "application/json")
   203  			_, _ = writer.Write([]byte(`{"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "scope": "user", "token_type": "bearer", "expires_in": 86400}`))
   204  		}))
   205  }
   206  
   207  func newMockAvsServer(t *testing.T) *httptest.Server {
   208  	router := mux.NewRouter()
   209  	router.HandleFunc("/{evalId}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   210  		vars := mux.Vars(r)
   211  		evalIdsHolder = append(evalIdsHolder, extractId(vars, "evalId", t))
   212  		w.WriteHeader(http.StatusOK)
   213  	})).Methods(http.MethodDelete)
   214  
   215  	router.HandleFunc("/{parentId}/child/{evalId}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   216  		vars := mux.Vars(r)
   217  
   218  		parentEval := extractId(vars, "parentId", t)
   219  		evalId := extractId(vars, "evalId", t)
   220  		parentEvalIdHolder[evalId] = parentEval
   221  
   222  		w.WriteHeader(http.StatusOK)
   223  	})).Methods(http.MethodDelete)
   224  	return httptest.NewServer(router)
   225  }
   226  
   227  func extractId(vars map[string]string, key string, t *testing.T) int64 {
   228  	evalIdStr := vars[key]
   229  	evalId, err := strconv.ParseInt(evalIdStr, 10, 64)
   230  	assert.NoError(t, err)
   231  	return evalId
   232  }
   233  
   234  func avsConfig(mockOauthServer *httptest.Server, mockAvsServer *httptest.Server) avs.Config {
   235  	return avs.Config{
   236  		OauthTokenEndpoint:     mockOauthServer.URL,
   237  		OauthUsername:          "dummy",
   238  		OauthPassword:          "dummy",
   239  		OauthClientId:          "dummy",
   240  		ApiEndpoint:            mockAvsServer.URL,
   241  		DefinitionType:         avs.DefinitionType,
   242  		InternalTesterAccessId: 1234,
   243  		InternalTesterService:  "",
   244  		ExternalTesterAccessId: 5678,
   245  		ExternalTesterService:  "dummy",
   246  		GroupId:                5555,
   247  		ParentId:               parentEvalId,
   248  	}
   249  }