MCPcopy
hub / github.com/crowdsecurity/crowdsec / TestBulkCheckAllowlist

Function TestBulkCheckAllowlist

pkg/apiserver/allowlists_test.go:151–190  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

149}
150
151func TestBulkCheckAllowlist(t *testing.T) {
152 ctx := t.Context()
153 lapi := SetupLAPITest(t, ctx)
154
155 // create an allowlist and add one live entry
156 l, err := lapi.DBClient.CreateAllowList(ctx, "test", "test", "", false)
157 require.NoError(t, err)
158
159 added, err := lapi.DBClient.AddToAllowlist(ctx, l, []*models.AllowlistItem{
160 {Value: "1.2.3.4"},
161 })
162 require.NoError(t, err)
163 assert.Equal(t, 1, added)
164
165 // craft a bulk check payload with one matching and one non-matching target
166 reqBody := `{"targets":["1.2.3.4","2.3.4.5"]}`
167 w := lapi.RecordResponse(t, ctx, http.MethodPost, "/v1/allowlists/check", strings.NewReader(reqBody), passwordAuthType)
168 require.Equal(t, http.StatusOK, w.Code)
169
170 // unmarshal and verify
171 resp := models.BulkCheckAllowlistResponse{}
172 require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
173 require.Len(t, resp.Results, 1)
174
175 // expect only "1.2.3.4" in the "test" allowlist, while "2.3.4.5" should not be in the response
176 var match bool
177
178 for _, r := range resp.Results {
179 switch *r.Target {
180 case "1.2.3.4":
181 match = true
182
183 assert.Equal(t, []string{"1.2.3.4 from test"}, r.Allowlists)
184 default:
185 t.Errorf("unexpected target %v", r.Target)
186 }
187 }
188
189 require.True(t, match, "did not see result for 1.2.3.4")
190}
191
192func TestBulkCheckAllowlist_BadRequest(t *testing.T) {
193 ctx := t.Context()

Callers

nothing calls this directly

Calls 6

SetupLAPITestFunction · 0.85
CreateAllowListMethod · 0.80
AddToAllowlistMethod · 0.80
RecordResponseMethod · 0.80
NewReaderMethod · 0.80
LenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…