github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/cis/client_ver_1_test.go (about)

     1  package cis
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/kyma-project/kyma-environment-broker/internal/logger"
    11  
    12  	"github.com/gorilla/mux"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  const (
    17  	subAccountTest4 = "c3db1e8e-1b7b-4122-8966-86f7eb3e3b6f"
    18  	subAccountTest5 = "5d57aa1c-00a4-45f6-a56e-9e51ce0c5c2f"
    19  	subAccountTest6 = "9f5e340f-c7f7-4df6-bb55-c3ed6b80945a"
    20  )
    21  
    22  func TestClientVer1_FetchSubaccountsToDelete(t *testing.T) {
    23  	t.Run("client fetched all subaccount IDs to delete", func(t *testing.T) {
    24  		// Given
    25  		testServer := fixHTTPServerVer1(newServerVer1(t))
    26  		defer testServer.Close()
    27  
    28  		client := NewClientVer1(context.TODO(), Config{
    29  			EventServiceURL: testServer.URL,
    30  			PageSize:        "3",
    31  		}, logger.NewLogDummy())
    32  		client.SetHttpClient(testServer.Client())
    33  
    34  		// When
    35  		saList, err := client.FetchSubaccountsToDelete()
    36  
    37  		// Then
    38  		require.NoError(t, err)
    39  		require.Len(t, saList, 3)
    40  		require.ElementsMatch(t, saList, []string{subAccountTest4, subAccountTest5, subAccountTest6})
    41  	})
    42  
    43  	t.Run("error occur during fetch subaccount IDs", func(t *testing.T) {
    44  		// Given
    45  		srv := newServerVer1(t)
    46  		srv.serverErr = true
    47  		testServer := fixHTTPServerVer1(srv)
    48  		defer testServer.Close()
    49  
    50  		client := NewClientVer1(context.TODO(), Config{
    51  			EventServiceURL: testServer.URL,
    52  			PageSize:        "3",
    53  		}, logger.NewLogDummy())
    54  		client.SetHttpClient(testServer.Client())
    55  
    56  		// When
    57  		saList, err := client.FetchSubaccountsToDelete()
    58  
    59  		// Then
    60  		require.Error(t, err)
    61  		require.Len(t, saList, 0)
    62  	})
    63  }
    64  
    65  type serverVer1 struct {
    66  	serverErr bool
    67  	t         *testing.T
    68  }
    69  
    70  func newServerVer1(t *testing.T) *serverVer1 {
    71  	return &serverVer1{
    72  		t: t,
    73  	}
    74  }
    75  
    76  func fixHTTPServerVer1(srv *serverVer1) *httptest.Server {
    77  	r := mux.NewRouter()
    78  
    79  	r.HandleFunc("/public/rest/v2/events", srv.returnCISEvents).Methods(http.MethodGet)
    80  
    81  	return httptest.NewServer(r)
    82  }
    83  
    84  func (s *serverVer1) returnCISEvents(w http.ResponseWriter, r *http.Request) {
    85  	eventType := r.URL.Query().Get("type")
    86  	if eventType != "MASTER_SUBACCOUNT_DELETION" {
    87  		w.WriteHeader(http.StatusNotFound)
    88  		return
    89  	}
    90  
    91  	if s.serverErr {
    92  		s.writeResponse(w, []byte(`{bad}`))
    93  		return
    94  	}
    95  
    96  	pageNum := r.URL.Query().Get("page")
    97  	var response string
    98  	if pageNum != "1" {
    99  		response = `{}`
   100  	} else {
   101  		response = fmt.Sprintf(`{
   102  			"events":[
   103  				{"id":145366,"type":"MASTER_SUBACCOUNT_DELETION","timestamp":"1597906863271","eventData":{"globalAccountGuid":"22cb7ffd-6f53-4b94-9915-b8a6dc6038f3","subaccountGuid":"%s","platformID":"8e8b4231-0224-441e-a017-234cdccd816e","subdomain":"procurepartobedeleted","region":"eu10-canary"}},
   104  				{"id":145365,"type":"MASTER_SUBACCOUNT_DELETION","timestamp":"1597906758247","eventData":{"globalAccountGuid":"22cb7ffd-6f53-4b94-9915-b8a6dc6038f3","subaccountGuid":"%s","platformID":"064adad0-6d0c-4afb-8439-6095d86d4dfd","subdomain":"procurepardevc4","region":"eu10-canary"}},
   105  				{"id":145364,"type":"MASTER_SUBACCOUNT_DELETION","timestamp":"1597906403195","eventData":{"globalAccountGuid":"423e5e64-1b67-474b-87ae-8c84070cdcaa","subaccountGuid":"%s","platformID":"4ae3bbb3-dc96-47b5-81f9-66b5fa030b11","subdomain":"iotae-hotfixtmtest01","region":"eu10-canary"}}
   106  			],
   107  			"totalResults":3,
   108  			"totalPages":1
   109  		}`, subAccountTest4, subAccountTest5, subAccountTest6)
   110  	}
   111  
   112  	s.writeResponse(w, []byte(response))
   113  }
   114  
   115  func (s *serverVer1) writeResponse(w http.ResponseWriter, response []byte) {
   116  	_, err := w.Write(response)
   117  	if err != nil {
   118  		s.t.Errorf("fakeCisServer cannot write response: %s", err)
   119  		w.WriteHeader(http.StatusInternalServerError)
   120  		return
   121  	}
   122  	w.WriteHeader(http.StatusOK)
   123  }