|  | 
|  | 1 | +// Copyright Mia srl | 
|  | 2 | +// SPDX-License-Identifier: Apache-2.0 | 
|  | 3 | +// | 
|  | 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | +// you may not use this file except in compliance with the License. | 
|  | 6 | +// You may obtain a copy of the License at | 
|  | 7 | +// | 
|  | 8 | +//     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | +// | 
|  | 10 | +// Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +// distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +// See the License for the specific language governing permissions and | 
|  | 14 | +// limitations under the License. | 
|  | 15 | + | 
|  | 16 | +package itd | 
|  | 17 | + | 
|  | 18 | +import ( | 
|  | 19 | +	"fmt" | 
|  | 20 | +	"net/http" | 
|  | 21 | +	"net/http/httptest" | 
|  | 22 | +	"testing" | 
|  | 23 | + | 
|  | 24 | +	"github.com/mia-platform/miactl/internal/client" | 
|  | 25 | +	"github.com/mia-platform/miactl/internal/clioptions" | 
|  | 26 | +	itd "github.com/mia-platform/miactl/internal/resources/item-type-definition" | 
|  | 27 | +	"github.com/mia-platform/miactl/internal/resources/marketplace" | 
|  | 28 | +	"github.com/stretchr/testify/require" | 
|  | 29 | +) | 
|  | 30 | + | 
|  | 31 | +const ( | 
|  | 32 | +	mockDeleteCompanyID = "company-id" | 
|  | 33 | +) | 
|  | 34 | + | 
|  | 35 | +func TestDeleteResourceCmd(t *testing.T) { | 
|  | 36 | +	t.Run("test command creation", func(t *testing.T) { | 
|  | 37 | +		opts := clioptions.NewCLIOptions() | 
|  | 38 | +		cmd := DeleteCmd(opts) | 
|  | 39 | +		require.NotNil(t, cmd) | 
|  | 40 | +	}) | 
|  | 41 | + | 
|  | 42 | +	t.Run("should not run command when Console version is lower than 14.0.0", func(t *testing.T) { | 
|  | 43 | +		server := httptest.NewServer(unexecutedCmdMockServer(t)) | 
|  | 44 | +		defer server.Close() | 
|  | 45 | + | 
|  | 46 | +		opts := clioptions.NewCLIOptions() | 
|  | 47 | +		opts.CompanyID = mockDeleteCompanyID | 
|  | 48 | +		opts.Endpoint = server.URL | 
|  | 49 | + | 
|  | 50 | +		cmd := DeleteCmd(opts) | 
|  | 51 | +		cmd.SetArgs([]string{"delete", "--item-id", "some-item-id", "--version", "1.0.0"}) | 
|  | 52 | + | 
|  | 53 | +		err := cmd.Execute() | 
|  | 54 | +		require.ErrorIs(t, err, itd.ErrUnsupportedCompanyVersion) | 
|  | 55 | +	}) | 
|  | 56 | +} | 
|  | 57 | + | 
|  | 58 | +func deleteByItemIDAndVersionMockServer(t *testing.T, | 
|  | 59 | +	statusCode int, | 
|  | 60 | +	mockName string, | 
|  | 61 | +	callsCount *int, | 
|  | 62 | +) *httptest.Server { | 
|  | 63 | +	t.Helper() | 
|  | 64 | +	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | 
|  | 65 | +		require.Equal(t, | 
|  | 66 | +			fmt.Sprintf(deleteItdEndpoint, mockDeleteCompanyID, mockName), | 
|  | 67 | +			r.RequestURI, | 
|  | 68 | +		) | 
|  | 69 | +		require.Equal(t, http.MethodDelete, r.Method) | 
|  | 70 | +		w.WriteHeader(statusCode) | 
|  | 71 | +		if statusCode != http.StatusNoContent { | 
|  | 72 | +			w.Write([]byte(` | 
|  | 73 | +			{ | 
|  | 74 | +				"message": "some error message" | 
|  | 75 | +			} | 
|  | 76 | +			`)) | 
|  | 77 | +		} | 
|  | 78 | +		*callsCount++ | 
|  | 79 | +	})) | 
|  | 80 | +} | 
|  | 81 | + | 
|  | 82 | +func TestDeleteItemByItemIDAndVersion(t *testing.T) { | 
|  | 83 | +	mockClientConfig := &client.Config{ | 
|  | 84 | +		Transport: http.DefaultTransport, | 
|  | 85 | +	} | 
|  | 86 | +	testCases := []struct { | 
|  | 87 | +		testName string | 
|  | 88 | + | 
|  | 89 | +		statusCode int | 
|  | 90 | + | 
|  | 91 | +		name string | 
|  | 92 | + | 
|  | 93 | +		expectedErr   error | 
|  | 94 | +		expectedCalls int | 
|  | 95 | +	}{ | 
|  | 96 | +		{ | 
|  | 97 | +			testName:   "should not return error if deletion is successful", | 
|  | 98 | +			statusCode: http.StatusNoContent, | 
|  | 99 | +			name:       "plugin", | 
|  | 100 | + | 
|  | 101 | +			expectedErr:   nil, | 
|  | 102 | +			expectedCalls: 1, | 
|  | 103 | +		}, | 
|  | 104 | +		{ | 
|  | 105 | +			testName: "should return not found error in case the item is not found", | 
|  | 106 | +			name:     "plugin", | 
|  | 107 | + | 
|  | 108 | +			statusCode: http.StatusNotFound, | 
|  | 109 | + | 
|  | 110 | +			expectedErr:   marketplace.ErrItemNotFound, | 
|  | 111 | +			expectedCalls: 1, | 
|  | 112 | +		}, | 
|  | 113 | +		{ | 
|  | 114 | +			testName: "should return generic error in case the server responds 500", | 
|  | 115 | +			name:     "plugin", | 
|  | 116 | + | 
|  | 117 | +			statusCode: http.StatusInternalServerError, | 
|  | 118 | + | 
|  | 119 | +			expectedErr:   ErrServerDeleteItem, | 
|  | 120 | +			expectedCalls: 1, | 
|  | 121 | +		}, | 
|  | 122 | +		{ | 
|  | 123 | +			testName: "should return unexpected response error in case of bad request response", | 
|  | 124 | +			name:     "plugin", | 
|  | 125 | + | 
|  | 126 | +			statusCode: http.StatusBadRequest, | 
|  | 127 | + | 
|  | 128 | +			expectedErr:   ErrUnexpectedDeleteItem, | 
|  | 129 | +			expectedCalls: 1, | 
|  | 130 | +		}, | 
|  | 131 | +	} | 
|  | 132 | + | 
|  | 133 | +	for _, tt := range testCases { | 
|  | 134 | +		t.Run(tt.testName, func(t *testing.T) { | 
|  | 135 | +			callsCount := new(int) | 
|  | 136 | +			*callsCount = 0 | 
|  | 137 | +			testServer := deleteByItemIDAndVersionMockServer( | 
|  | 138 | +				t, | 
|  | 139 | +				tt.statusCode, | 
|  | 140 | +				tt.name, | 
|  | 141 | +				callsCount, | 
|  | 142 | +			) | 
|  | 143 | +			defer testServer.Close() | 
|  | 144 | + | 
|  | 145 | +			mockClientConfig.Host = testServer.URL | 
|  | 146 | +			client, err := client.APIClientForConfig(mockClientConfig) | 
|  | 147 | +			require.NoError(t, err) | 
|  | 148 | + | 
|  | 149 | +			err = deleteITD( | 
|  | 150 | +				t.Context(), | 
|  | 151 | +				client, | 
|  | 152 | +				mockDeleteCompanyID, | 
|  | 153 | +				tt.name, | 
|  | 154 | +			) | 
|  | 155 | + | 
|  | 156 | +			require.Equal(t, tt.expectedCalls, *callsCount, "did not match number of calls") | 
|  | 157 | + | 
|  | 158 | +			if tt.expectedErr != nil { | 
|  | 159 | +				require.ErrorIs(t, err, tt.expectedErr) | 
|  | 160 | +			} else { | 
|  | 161 | +				require.NoError(t, err) | 
|  | 162 | +			} | 
|  | 163 | +		}) | 
|  | 164 | +	} | 
|  | 165 | +} | 
0 commit comments