github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/botman/challenge_interception_rules_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 ChallengeInterceptionRules 17 func TestBotman_GetChallengeInterceptionRules(t *testing.T) { 18 tests := map[string]struct { 19 params GetChallengeInterceptionRulesRequest 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: GetChallengeInterceptionRulesRequest{ 28 ConfigID: 43253, 29 Version: 15, 30 }, 31 responseStatus: http.StatusOK, 32 responseBody: `{"testKey":"testValue3"}`, 33 expectedPath: "/appsec/v1/configs/43253/versions/15/response-actions/challenge-interception-rules", 34 expectedResponse: map[string]interface{}{"testKey": "testValue3"}, 35 }, 36 "500 internal server error": { 37 params: GetChallengeInterceptionRulesRequest{ 38 ConfigID: 43253, 39 Version: 15, 40 }, 41 responseStatus: http.StatusInternalServerError, 42 responseBody: ` 43 { 44 "type": "internal_error", 45 "title": "Internal Server Error", 46 "detail": "Error fetching data" 47 }`, 48 expectedPath: "/appsec/v1/configs/43253/versions/15/response-actions/challenge-interception-rules", 49 withError: func(t *testing.T, err error) { 50 want := &Error{ 51 Type: "internal_error", 52 Title: "Internal Server Error", 53 Detail: "Error fetching data", 54 StatusCode: http.StatusInternalServerError, 55 } 56 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 57 }, 58 }, 59 "Missing ConfigID": { 60 params: GetChallengeInterceptionRulesRequest{ 61 Version: 15, 62 }, 63 withError: func(t *testing.T, err error) { 64 want := ErrStructValidation 65 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 66 assert.Contains(t, err.Error(), "ConfigID") 67 }, 68 }, 69 "Missing Version": { 70 params: GetChallengeInterceptionRulesRequest{ 71 ConfigID: 43253, 72 }, 73 withError: func(t *testing.T, err error) { 74 want := ErrStructValidation 75 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 76 assert.Contains(t, err.Error(), "Version") 77 }, 78 }, 79 } 80 81 for name, test := range tests { 82 t.Run(name, func(t *testing.T) { 83 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 84 assert.Equal(t, test.expectedPath, r.URL.String()) 85 assert.Equal(t, http.MethodGet, r.Method) 86 w.WriteHeader(test.responseStatus) 87 _, err := w.Write([]byte(test.responseBody)) 88 assert.NoError(t, err) 89 })) 90 client := mockAPIClient(t, mockServer) 91 result, err := client.GetChallengeInterceptionRules(context.Background(), test.params) 92 if test.withError != nil { 93 test.withError(t, err) 94 return 95 } 96 require.NoError(t, err) 97 assert.Equal(t, test.expectedResponse, result) 98 }) 99 } 100 } 101 102 // Test Update ChallengeInterceptionRules. 103 func TestBotman_UpdateChallengeInterceptionRules(t *testing.T) { 104 tests := map[string]struct { 105 params UpdateChallengeInterceptionRulesRequest 106 responseStatus int 107 responseBody string 108 expectedPath string 109 expectedResponse map[string]interface{} 110 withError func(*testing.T, error) 111 }{ 112 "200 Success": { 113 params: UpdateChallengeInterceptionRulesRequest{ 114 ConfigID: 43253, 115 Version: 15, 116 JsonPayload: json.RawMessage(`{"testKey":"testValue3"}`), 117 }, 118 responseStatus: http.StatusOK, 119 responseBody: `{"testKey":"testValue3"}`, 120 expectedResponse: map[string]interface{}{"testKey": "testValue3"}, 121 expectedPath: "/appsec/v1/configs/43253/versions/15/response-actions/challenge-interception-rules", 122 }, 123 "500 internal server error": { 124 params: UpdateChallengeInterceptionRulesRequest{ 125 ConfigID: 43253, 126 Version: 15, 127 JsonPayload: json.RawMessage(`{"testKey":"testValue3"}`), 128 }, 129 responseStatus: http.StatusInternalServerError, 130 responseBody: ` 131 { 132 "type": "internal_error", 133 "title": "Internal Server Error", 134 "detail": "Error updating data" 135 }`, 136 expectedPath: "/appsec/v1/configs/43253/versions/15/response-actions/challenge-interception-rules", 137 withError: func(t *testing.T, err error) { 138 want := &Error{ 139 Type: "internal_error", 140 Title: "Internal Server Error", 141 Detail: "Error updating data", 142 StatusCode: http.StatusInternalServerError, 143 } 144 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 145 }, 146 }, 147 "Missing ConfigID": { 148 params: UpdateChallengeInterceptionRulesRequest{ 149 Version: 15, 150 JsonPayload: json.RawMessage(`{"testKey":"testValue3"}`), 151 }, 152 withError: func(t *testing.T, err error) { 153 want := ErrStructValidation 154 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 155 assert.Contains(t, err.Error(), "ConfigID") 156 }, 157 }, 158 "Missing Version": { 159 params: UpdateChallengeInterceptionRulesRequest{ 160 ConfigID: 43253, 161 JsonPayload: json.RawMessage(`{"testKey":"testValue3"}`), 162 }, 163 withError: func(t *testing.T, err error) { 164 want := ErrStructValidation 165 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 166 assert.Contains(t, err.Error(), "Version") 167 }, 168 }, 169 "Missing JsonPayload": { 170 params: UpdateChallengeInterceptionRulesRequest{ 171 ConfigID: 43253, 172 Version: 15, 173 }, 174 withError: func(t *testing.T, err error) { 175 want := ErrStructValidation 176 assert.True(t, errors.Is(err, want), "want: %s; got: %s", want, err) 177 assert.Contains(t, err.Error(), "JsonPayload") 178 }, 179 }, 180 } 181 182 for name, test := range tests { 183 t.Run(name, func(t *testing.T) { 184 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 185 assert.Equal(t, test.expectedPath, r.URL.String()) 186 assert.Equal(t, http.MethodPut, r.Method) 187 w.WriteHeader(test.responseStatus) 188 if len(test.responseBody) > 0 { 189 _, err := w.Write([]byte(test.responseBody)) 190 assert.NoError(t, err) 191 } 192 })) 193 client := mockAPIClient(t, mockServer) 194 result, err := client.UpdateChallengeInterceptionRules( 195 session.ContextWithOptions( 196 context.Background()), test.params) 197 if test.withError != nil { 198 test.withError(t, err) 199 return 200 } 201 require.NoError(t, err) 202 assert.Equal(t, test.expectedResponse, result) 203 }) 204 } 205 }