github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/avs/model_test.go (about) 1 package avs 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 "strconv" 8 "testing" 9 10 "github.com/gorilla/mux" 11 "github.com/kyma-project/kyma-environment-broker/internal" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 var evalIdsHolder []int64 16 var parentEvalIdHolder map[int64]int64 = make(map[int64]int64) 17 18 const ( 19 internalEvalId = int64(1234) 20 externalEvalId = int64(5678) 21 internalEvalUpdatedId = int64(12340) 22 externalEvalUpdatedId = int64(56780) 23 ) 24 25 func TestAvsEvaluationConfigs(t *testing.T) { 26 // given 27 assert := assert.New(t) 28 29 mockOauthServer := newMockAvsOauthServer() 30 defer mockOauthServer.Close() 31 mockAvsServer := newMockAvsServer(t) 32 defer mockAvsServer.Close() 33 avsConfig := avsConfig(mockOauthServer, mockAvsServer) 34 internalEvalAssistant := NewInternalEvalAssistant(avsConfig) 35 externalEvalAssistant := NewExternalEvalAssistant(avsConfig) 36 37 // verify assistant configs 38 assert.Equal(internalEvalId, internalEvalAssistant.ProvideTesterAccessId(internal.ProvisioningParameters{})) 39 assert.Equal(externalEvalId, externalEvalAssistant.ProvideTesterAccessId(internal.ProvisioningParameters{})) 40 41 assert.Equal("int", internalEvalAssistant.ProvideSuffix()) 42 assert.Equal("ext", externalEvalAssistant.ProvideSuffix()) 43 44 assert.Equal("", internalEvalAssistant.ProvideCheckType()) 45 assert.Equal("HTTPSGET", externalEvalAssistant.ProvideCheckType()) 46 47 assert.Equal("dummy", internalEvalAssistant.ProvideNewOrDefaultServiceName("dummy")) 48 assert.Equal("external-dummy", externalEvalAssistant.ProvideNewOrDefaultServiceName("dummy")) 49 50 params := internal.Operation{} 51 assert.Equal(7, len(internalEvalAssistant.ProvideTags(internal.Operation{}))) 52 53 // verify confg as json 54 tags, testTag := externalEvalAssistant.ProvideTags(params), Tag{} 55 json.Unmarshal([]byte(`{"content":"","tag_class_id":0,"tag_class_name":""}`), &testTag) 56 assert.Equal(testTag, *tags[0]) 57 58 } 59 60 func newMockAvsOauthServer() *httptest.Server { 61 return httptest.NewServer( 62 http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { 63 writer.Header().Set("Content-Type", "application/json") 64 _, _ = writer.Write([]byte(`{"access_token": "90d64460d14870c08c81352a05dedd3465940a7c", "scope": "user", "token_type": "bearer", "expires_in": 86400}`)) 65 })) 66 } 67 68 func newMockAvsServer(t *testing.T) *httptest.Server { 69 router := mux.NewRouter() 70 router.HandleFunc("/{evalId}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 71 vars := mux.Vars(r) 72 evalIdsHolder = append(evalIdsHolder, extractId(vars, "evalId", t)) 73 w.WriteHeader(http.StatusOK) 74 })).Methods(http.MethodDelete) 75 76 router.HandleFunc("/{parentId}/child/{evalId}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 77 vars := mux.Vars(r) 78 79 parentEval := extractId(vars, "parentId", t) 80 evalId := extractId(vars, "evalId", t) 81 parentEvalIdHolder[evalId] = parentEval 82 83 w.WriteHeader(http.StatusOK) 84 })).Methods(http.MethodDelete) 85 return httptest.NewServer(router) 86 } 87 88 func extractId(vars map[string]string, key string, t *testing.T) int64 { 89 evalIdStr := vars[key] 90 evalId, err := strconv.ParseInt(evalIdStr, 10, 64) 91 assert.NoError(t, err) 92 return evalId 93 } 94 95 func avsConfig(mockOauthServer *httptest.Server, mockAvsServer *httptest.Server) Config { 96 return Config{ 97 OauthTokenEndpoint: mockOauthServer.URL, 98 OauthUsername: "dummy", 99 OauthPassword: "dummy", 100 OauthClientId: "dummy", 101 ApiEndpoint: mockAvsServer.URL, 102 DefinitionType: DefinitionType, 103 InternalTesterAccessId: internalEvalId, 104 InternalTesterService: "", 105 ExternalTesterAccessId: externalEvalId, 106 ExternalTesterService: "external-dummy", 107 GroupId: 5555, 108 ParentId: 91011, 109 } 110 }