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

Function TestCheckInAllowlist

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

Source from the content-addressed store, hash-verified

71}
72
73func TestCheckInAllowlist(t *testing.T) {
74 ctx := t.Context()
75 lapi := SetupLAPITest(t, ctx)
76
77 l, err := lapi.DBClient.CreateAllowList(ctx, "test", "test", "", false)
78
79 require.NoError(t, err)
80
81 added, err := lapi.DBClient.AddToAllowlist(ctx, l, []*models.AllowlistItem{
82 {
83 Value: "1.2.3.4",
84 },
85 {
86 Value: "2.3.4.5",
87 Expiration: strfmt.DateTime(time.Now().Add(-time.Hour)), // expired
88 },
89 })
90
91 require.NoError(t, err)
92 assert.Equal(t, 2, added)
93
94 // GET request, should return 200 and status in body
95 w := lapi.RecordResponse(t, ctx, http.MethodGet, "/v1/allowlists/check/1.2.3.4", emptyBody, passwordAuthType)
96
97 require.Equal(t, http.StatusOK, w.Code)
98
99 resp := models.CheckAllowlistResponse{}
100
101 err = json.Unmarshal(w.Body.Bytes(), &resp)
102 require.NoError(t, err)
103
104 require.True(t, resp.Allowlisted)
105
106 // GET request, should return 200 and status in body
107 w = lapi.RecordResponse(t, ctx, http.MethodGet, "/v1/allowlists/check/2.3.4.5", emptyBody, passwordAuthType)
108
109 require.Equal(t, http.StatusOK, w.Code)
110
111 resp = models.CheckAllowlistResponse{}
112
113 err = json.Unmarshal(w.Body.Bytes(), &resp)
114
115 require.NoError(t, err)
116 require.False(t, resp.Allowlisted)
117
118 // GET request, should return 200 and status in body
119 w = lapi.RecordResponse(t, ctx, http.MethodGet, "/v1/allowlists/check/2.3.4.0%2F24", emptyBody, passwordAuthType)
120
121 require.Equal(t, http.StatusOK, w.Code)
122
123 resp = models.CheckAllowlistResponse{}
124
125 err = json.Unmarshal(w.Body.Bytes(), &resp)
126
127 require.NoError(t, err)
128 require.False(t, resp.Allowlisted)
129
130 // HEAD request, should return 200

Callers

nothing calls this directly

Calls 5

SetupLAPITestFunction · 0.85
CreateAllowListMethod · 0.80
AddToAllowlistMethod · 0.80
RecordResponseMethod · 0.80
AddMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…