github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/javascript_injection_test.go (about) 1 package botman 2 3 import ( 4 "context" 5 "encoding/json" 6 "errors" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 // Test Get JavascriptInjection 17 func TestBotman_GetJavascriptInjection(t *testing.T) { 18 tests := map[string]struct { 19 params GetJavascriptInjectionRequest 20 responseStatus int 21 responseBody string 22 expectedPath string 23 expectedResponse map[string]interface{} 24 withError func(*testing.T, error) 25 }{ 26 "200 OK": { 27 params: GetJavascriptInjectionRequest{ 28 ConfigID: 43253, 29 Version: 15, 30 SecurityPolicyID: "AAAA_81230", 31 }, 32 responseStatus: http.StatusOK, 33 responseBody: `{"testKey":"testValue3"}`, 34 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/javascript-injection", 35 expectedResponse: map[string]interface{}{"testKey": "testValue3"}, 36 }, 37 "500 internal server error": { 38 params: GetJavascriptInjectionRequest{ 39 ConfigID: 43253, 40 Version: 15, 41 SecurityPolicyID: "AAAA_81230", 42 }, 43 responseStatus: http.StatusInternalServerError, 44 responseBody: ` 45 { 46 "type": "internal_error", 47 "title": "Internal Server Error", 48 "detail": "Error fetching match target" 49 }`, 50 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/javascript-injection", 51 withError: func(t *testing.T, err error) { 52 want := &Error{ 53 Type: "internal_error", 54 Title: "Internal Server Error", 55 Detail: "Error fetching match target", 56 StatusCode: http.StatusInternalServerError, 57 } 58 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 59 }, 60 }, 61 "Missing ConfigID": { 62 params: GetJavascriptInjectionRequest{ 63 Version: 15, 64 SecurityPolicyID: "AAAA_81230", 65 }, 66 withError: func(t *testing.T, err error) { 67 want := ErrStructValidation 68 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 69 assert.Contains(t, err.Error(), "ConfigID") 70 }, 71 }, 72 "Missing Version": { 73 params: GetJavascriptInjectionRequest{ 74 ConfigID: 43253, 75 SecurityPolicyID: "AAAA_81230", 76 }, 77 withError: func(t *testing.T, err error) { 78 want := ErrStructValidation 79 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 80 assert.Contains(t, err.Error(), "Version") 81 }, 82 }, 83 "Missing SecurityPolicyID": { 84 params: GetJavascriptInjectionRequest{ 85 ConfigID: 43253, 86 Version: 15, 87 }, 88 withError: func(t *testing.T, err error) { 89 want := ErrStructValidation 90 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 91 assert.Contains(t, err.Error(), "SecurityPolicyID") 92 }, 93 }, 94 } 95 96 for name, test := range tests { 97 t.Run(name, func(t *testing.T) { 98 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 99 assert.Equal(t, test.expectedPath, r.URL.String()) 100 assert.Equal(t, http.MethodGet, r.Method) 101 w.WriteHeader(test.responseStatus) 102 _, err := w.Write([]byte(test.responseBody)) 103 assert.NoError(t, err) 104 })) 105 client := mockAPIClient(t, mockServer) 106 result, err := client.GetJavascriptInjection(context.Background(), test.params) 107 if test.withError != nil { 108 test.withError(t, err) 109 return 110 } 111 require.NoError(t, err) 112 assert.Equal(t, test.expectedResponse, result) 113 }) 114 } 115 } 116 117 // Test Update JavascriptInjection. 118 func TestBotman_UpdateJavascriptInjection(t *testing.T) { 119 tests := map[string]struct { 120 params UpdateJavascriptInjectionRequest 121 responseStatus int 122 responseBody string 123 expectedPath string 124 expectedResponse map[string]interface{} 125 withError func(*testing.T, error) 126 }{ 127 "200 Success": { 128 params: UpdateJavascriptInjectionRequest{ 129 ConfigID: 43253, 130 Version: 15, 131 SecurityPolicyID: "AAAA_81230", 132 JsonPayload: json.RawMessage(`{"testKey":"testValue3"}`), 133 }, 134 responseStatus: http.StatusOK, 135 responseBody: `{"testKey":"testValue3"}`, 136 expectedResponse: map[string]interface{}{"testKey": "testValue3"}, 137 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/javascript-injection", 138 }, 139 "500 internal server error": { 140 params: UpdateJavascriptInjectionRequest{ 141 ConfigID: 43253, 142 Version: 15, 143 SecurityPolicyID: "AAAA_81230", 144 JsonPayload: json.RawMessage(`{"testKey":"testValue3"}`), 145 }, 146 responseStatus: http.StatusInternalServerError, 147 responseBody: ` 148 { 149 "type": "internal_error", 150 "title": "Internal Server Error", 151 "detail": "Error creating zone" 152 }`, 153 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/AAAA_81230/javascript-injection", 154 withError: func(t *testing.T, err error) { 155 want := &Error{ 156 Type: "internal_error", 157 Title: "Internal Server Error", 158 Detail: "Error creating zone", 159 StatusCode: http.StatusInternalServerError, 160 } 161 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 162 }, 163 }, 164 "Missing ConfigID": { 165 params: UpdateJavascriptInjectionRequest{ 166 Version: 15, 167 SecurityPolicyID: "AAAA_81230", 168 JsonPayload: json.RawMessage(`{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 169 }, 170 withError: func(t *testing.T, err error) { 171 want := ErrStructValidation 172 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 173 assert.Contains(t, err.Error(), "ConfigID") 174 }, 175 }, 176 "Missing Version": { 177 params: UpdateJavascriptInjectionRequest{ 178 ConfigID: 43253, 179 SecurityPolicyID: "AAAA_81230", 180 JsonPayload: json.RawMessage(`{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 181 }, 182 withError: func(t *testing.T, err error) { 183 want := ErrStructValidation 184 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 185 assert.Contains(t, err.Error(), "Version") 186 }, 187 }, 188 "Missing SecurityPolicyID": { 189 params: UpdateJavascriptInjectionRequest{ 190 ConfigID: 43253, 191 Version: 15, 192 JsonPayload: json.RawMessage(`{"categoryId":"cc9c3f89-e179-4892-89cf-d5e623ba9dc7", "testKey":"testValue3"}`), 193 }, 194 withError: func(t *testing.T, err error) { 195 want := ErrStructValidation 196 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 197 assert.Contains(t, err.Error(), "SecurityPolicyID") 198 }, 199 }, 200 "Missing JsonPayload": { 201 params: UpdateJavascriptInjectionRequest{ 202 ConfigID: 43253, 203 Version: 15, 204 SecurityPolicyID: "AAAA_81230", 205 }, 206 withError: func(t *testing.T, err error) { 207 want := ErrStructValidation 208 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 209 assert.Contains(t, err.Error(), "JsonPayload") 210 }, 211 }, 212 } 213 214 for name, test := range tests { 215 t.Run(name, func(t *testing.T) { 216 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 217 assert.Equal(t, test.expectedPath, r.URL.String()) 218 assert.Equal(t, http.MethodPut, r.Method) 219 w.WriteHeader(test.responseStatus) 220 if len(test.responseBody) > 0 { 221 _, err := w.Write([]byte(test.responseBody)) 222 assert.NoError(t, err) 223 } 224 })) 225 client := mockAPIClient(t, mockServer) 226 result, err := client.UpdateJavascriptInjection( 227 session.ContextWithOptions( 228 context.Background()), test.params) 229 if test.withError != nil { 230 test.withError(t, err) 231 return 232 } 233 require.NoError(t, err) 234 assert.Equal(t, test.expectedResponse, result) 235 }) 236 } 237 }