|
| 1 | +/* |
| 2 | + * Zed Attack Proxy (ZAP) and its related class files. |
| 3 | + * |
| 4 | + * ZAP is an HTTP/HTTPS proxy for assessing web application security. |
| 5 | + * |
| 6 | + * Copyright 2025 The ZAP Development Team |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +package org.zaproxy.zap.extension.alertFilters; |
| 21 | + |
| 22 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | +import static org.hamcrest.Matchers.empty; |
| 24 | +import static org.hamcrest.Matchers.emptyString; |
| 25 | +import static org.hamcrest.Matchers.equalTo; |
| 26 | +import static org.hamcrest.Matchers.hasSize; |
| 27 | +import static org.hamcrest.Matchers.is; |
| 28 | +import static org.hamcrest.Matchers.not; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import net.sf.json.JSONObject; |
| 34 | +import org.junit.jupiter.api.AfterAll; |
| 35 | +import org.junit.jupiter.api.BeforeEach; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.EmptySource; |
| 39 | +import org.junit.jupiter.params.provider.ValueSource; |
| 40 | +import org.parosproxy.paros.Constant; |
| 41 | +import org.parosproxy.paros.network.HttpMessage; |
| 42 | +import org.zaproxy.zap.extension.api.API; |
| 43 | +import org.zaproxy.zap.extension.api.API.RequestType; |
| 44 | +import org.zaproxy.zap.extension.api.ApiElement; |
| 45 | +import org.zaproxy.zap.extension.api.ApiException; |
| 46 | +import org.zaproxy.zap.extension.api.ApiImplementor; |
| 47 | +import org.zaproxy.zap.extension.api.ApiParameter; |
| 48 | +import org.zaproxy.zap.testutils.TestUtils; |
| 49 | + |
| 50 | +/** Unit test for {@link AlertFilterAPI}. */ |
| 51 | +class AlertFilterApiUnitTest extends TestUtils { |
| 52 | + |
| 53 | + private AlertFilterAPI alertFiltersApi; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + void setUp() { |
| 57 | + mockMessages(new ExtensionAlertFilters()); |
| 58 | + |
| 59 | + alertFiltersApi = new AlertFilterAPI(); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterAll |
| 63 | + static void cleanUp() { |
| 64 | + Constant.messages = null; |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void shouldHavePrefix() { |
| 69 | + // Given / When |
| 70 | + String prefix = alertFiltersApi.getPrefix(); |
| 71 | + // Then |
| 72 | + assertThat(prefix, is(equalTo("alertFilter"))); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void shouldAddApiElements() { |
| 77 | + // Given / When |
| 78 | + alertFiltersApi = new AlertFilterAPI(); |
| 79 | + // Then |
| 80 | + assertThat(alertFiltersApi.getApiActions(), hasSize(10)); |
| 81 | + assertThat(alertFiltersApi.getApiViews(), hasSize(2)); |
| 82 | + assertThat(alertFiltersApi.getApiOthers(), hasSize(0)); |
| 83 | + } |
| 84 | + |
| 85 | + @ParameterizedTest |
| 86 | + @EmptySource |
| 87 | + @ValueSource(strings = {"unknown", "something"}) |
| 88 | + void shouldThrowApiExceptionForUnknownAction(String name) { |
| 89 | + // Given |
| 90 | + JSONObject params = new JSONObject(); |
| 91 | + // When |
| 92 | + ApiException exception = |
| 93 | + assertThrows( |
| 94 | + ApiException.class, () -> alertFiltersApi.handleApiAction(name, params)); |
| 95 | + // Then |
| 96 | + assertThat(exception.getType(), is(equalTo(ApiException.Type.BAD_ACTION))); |
| 97 | + } |
| 98 | + |
| 99 | + @ParameterizedTest |
| 100 | + @EmptySource |
| 101 | + @ValueSource(strings = {"unknown", "something"}) |
| 102 | + void shouldThrowApiExceptionForUnknownOther(String name) { |
| 103 | + // Given |
| 104 | + HttpMessage message = new HttpMessage(); |
| 105 | + JSONObject params = new JSONObject(); |
| 106 | + // When |
| 107 | + ApiException exception = |
| 108 | + assertThrows( |
| 109 | + ApiException.class, |
| 110 | + () -> alertFiltersApi.handleApiOther(message, name, params)); |
| 111 | + // Then |
| 112 | + assertThat(exception.getType(), is(equalTo(ApiException.Type.BAD_OTHER))); |
| 113 | + } |
| 114 | + |
| 115 | + @ParameterizedTest |
| 116 | + @EmptySource |
| 117 | + @ValueSource(strings = {"unknown", "something"}) |
| 118 | + void shouldThrowApiExceptionForUnknownView(String name) { |
| 119 | + // Given |
| 120 | + JSONObject params = new JSONObject(); |
| 121 | + // When |
| 122 | + ApiException exception = |
| 123 | + assertThrows(ApiException.class, () -> alertFiltersApi.handleApiView(name, params)); |
| 124 | + // Then |
| 125 | + assertThat(exception.getType(), is(equalTo(ApiException.Type.BAD_VIEW))); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void shouldHaveDescriptionsForAllApiElements() { |
| 130 | + alertFiltersApi = new AlertFilterAPI(); |
| 131 | + List<String> missingKeys = new ArrayList<>(); |
| 132 | + List<String> missingDescriptions = new ArrayList<>(); |
| 133 | + checkKey(alertFiltersApi.getDescriptionKey(), missingKeys, missingDescriptions); |
| 134 | + checkApiElements( |
| 135 | + alertFiltersApi, |
| 136 | + alertFiltersApi.getApiActions(), |
| 137 | + API.RequestType.action, |
| 138 | + missingKeys, |
| 139 | + missingDescriptions); |
| 140 | + checkApiElements( |
| 141 | + alertFiltersApi, |
| 142 | + alertFiltersApi.getApiOthers(), |
| 143 | + API.RequestType.other, |
| 144 | + missingKeys, |
| 145 | + missingDescriptions); |
| 146 | + checkApiElements( |
| 147 | + alertFiltersApi, |
| 148 | + alertFiltersApi.getApiViews(), |
| 149 | + API.RequestType.view, |
| 150 | + missingKeys, |
| 151 | + missingDescriptions); |
| 152 | + assertThat(missingKeys, is(empty())); |
| 153 | + assertThat(missingDescriptions, is(empty())); |
| 154 | + } |
| 155 | + |
| 156 | + private static void checkKey(String key, List<String> missingKeys, List<String> missingDescs) { |
| 157 | + if (!Constant.messages.containsKey(key)) { |
| 158 | + missingKeys.add(key); |
| 159 | + } else if (Constant.messages.getString(key).isBlank()) { |
| 160 | + missingDescs.add(key); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private static void checkApiElements( |
| 165 | + ApiImplementor api, |
| 166 | + List<? extends ApiElement> elements, |
| 167 | + RequestType type, |
| 168 | + List<String> missingKeys, |
| 169 | + List<String> missingDescriptions) { |
| 170 | + elements.sort((a, b) -> a.getName().compareTo(b.getName())); |
| 171 | + for (ApiElement element : elements) { |
| 172 | + assertThat( |
| 173 | + "API " + type + " element: " + api.getPrefix() + "/" + element.getName(), |
| 174 | + element.getDescriptionTag(), |
| 175 | + is(not(emptyString()))); |
| 176 | + checkKey(element.getDescriptionTag(), missingKeys, missingDescriptions); |
| 177 | + element.getParameters().stream() |
| 178 | + .map(ApiParameter::getDescriptionKey) |
| 179 | + .forEach(key -> checkKey(key, missingKeys, missingDescriptions)); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments