github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/security_policy_test.go (about) 1 package appsec 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 func TestAppSec_ListSecurityPolicies(t *testing.T) { 17 18 result := GetSecurityPoliciesResponse{} 19 20 respData := compactJSON(loadFixtureBytes("testdata/TestSecurityPolicy/SecurityPolicy.json")) 21 err := json.Unmarshal([]byte(respData), &result) 22 require.NoError(t, err) 23 24 tests := map[string]struct { 25 params GetSecurityPoliciesRequest 26 responseStatus int 27 responseBody string 28 expectedPath string 29 expectedResponse *GetSecurityPoliciesResponse 30 withError error 31 headers http.Header 32 }{ 33 "200 OK": { 34 params: GetSecurityPoliciesRequest{ 35 ConfigID: 43253, 36 Version: 15, 37 }, 38 headers: http.Header{ 39 "Content-Type": []string{"application/json"}, 40 }, 41 responseStatus: http.StatusOK, 42 responseBody: string(respData), 43 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies", 44 expectedResponse: &result, 45 }, 46 "500 internal server error": { 47 params: GetSecurityPoliciesRequest{ 48 ConfigID: 43253, 49 Version: 15, 50 }, 51 headers: http.Header{}, 52 responseStatus: http.StatusInternalServerError, 53 responseBody: ` 54 { 55 "type": "internal_error", 56 "title": "Internal Server Error", 57 "detail": "Error fetching propertys", 58 "status": 500 59 }`, 60 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies", 61 withError: &Error{ 62 Type: "internal_error", 63 Title: "Internal Server Error", 64 Detail: "Error fetching propertys", 65 StatusCode: http.StatusInternalServerError, 66 }, 67 }, 68 } 69 70 for name, test := range tests { 71 t.Run(name, func(t *testing.T) { 72 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 73 assert.Equal(t, test.expectedPath, r.URL.String()) 74 assert.Equal(t, http.MethodGet, r.Method) 75 w.WriteHeader(test.responseStatus) 76 _, err := w.Write([]byte(test.responseBody)) 77 assert.NoError(t, err) 78 })) 79 client := mockAPIClient(t, mockServer) 80 result, err := client.GetSecurityPolicies( 81 session.ContextWithOptions( 82 context.Background(), 83 session.WithContextHeaders(test.headers), 84 ), 85 test.params) 86 if test.withError != nil { 87 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 88 return 89 } 90 require.NoError(t, err) 91 assert.Equal(t, test.expectedResponse, result) 92 }) 93 } 94 } 95 96 func TestAppSec_CreateSecurityPolicyWithDefaultProtectionsSuccess(t *testing.T) { 97 98 response := CreateSecurityPolicyResponse{} 99 respData := compactJSON(loadFixtureBytes("testdata/TestSecurityPolicy/CreateSecurityPolicyWithDefaultProtectionsResponse.json")) 100 err := json.Unmarshal([]byte(respData), &response) 101 require.NoError(t, err) 102 103 requestParams := CreateSecurityPolicyWithDefaultProtectionsRequest{ 104 ConfigVersion: ConfigVersion{ 105 ConfigID: 43253, 106 Version: 15, 107 }, 108 PolicyName: "akamaitools", 109 PolicyPrefix: "AK01", 110 } 111 112 tests := map[string]struct { 113 params CreateSecurityPolicyWithDefaultProtectionsRequest 114 responseStatus int 115 responseBody string 116 expectedMethod string 117 expectedPath string 118 expectedResponse *CreateSecurityPolicyResponse 119 withError error 120 headers http.Header 121 }{ 122 "200 OK": { 123 params: requestParams, 124 headers: http.Header{ 125 "Content-Type": []string{"application/json"}, 126 }, 127 responseStatus: http.StatusOK, 128 responseBody: string(respData), 129 expectedMethod: http.MethodPost, 130 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/protections", 131 expectedResponse: &response, 132 }, 133 "500 internal server error": { 134 params: requestParams, 135 headers: http.Header{}, 136 responseStatus: http.StatusInternalServerError, 137 responseBody: ` 138 { 139 "type": "internal_error", 140 "title": "Internal Server Error", 141 "detail": "Error creating security policy with default protections", 142 "status": 500 143 }`, 144 expectedMethod: http.MethodPost, 145 expectedPath: "/appsec/v1/configs/43253/versions/15/security-policies/protections", 146 withError: &Error{ 147 Type: "internal_error", 148 Title: "Internal Server Error", 149 Detail: "Error creating security policy with default protections", 150 StatusCode: http.StatusInternalServerError, 151 }, 152 }, 153 } 154 155 for name, test := range tests { 156 t.Run(name, func(t *testing.T) { 157 mockServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 158 assert.Equal(t, test.expectedPath, r.URL.String()) 159 assert.Equal(t, test.expectedMethod, r.Method) 160 w.WriteHeader(test.responseStatus) 161 _, err := w.Write([]byte(test.responseBody)) 162 assert.NoError(t, err) 163 })) 164 client := mockAPIClient(t, mockServer) 165 _, err := client.CreateSecurityPolicyWithDefaultProtections( 166 session.ContextWithOptions( 167 context.Background(), 168 session.WithContextHeaders(test.headers), 169 ), 170 test.params) 171 if test.withError != nil { 172 assert.True(t, errors.Is(err, test.withError), "want: %s; got: %s", test.withError, err) 173 return 174 } 175 require.NoError(t, err) 176 }) 177 } 178 }