mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 20:36:12 +02:00
context propagation: explicit ctx parameter in unit tests (#3229)
This commit is contained in:
parent
b14201aa84
commit
7c5d4d8b3d
6 changed files with 131 additions and 87 deletions
|
@ -1,6 +1,7 @@
|
|||
package apiserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
|
@ -12,11 +13,13 @@ import (
|
|||
func TestLogin(t *testing.T) {
|
||||
router, config := NewAPITest(t)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
body := CreateTestMachine(t, router, "")
|
||||
|
||||
// Login with machine not validated yet
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
|
@ -25,7 +28,7 @@ func TestLogin(t *testing.T) {
|
|||
|
||||
// Login with machine not exist
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1", "password": "test1"}`))
|
||||
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1", "password": "test1"}`))
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
|
@ -34,7 +37,7 @@ func TestLogin(t *testing.T) {
|
|||
|
||||
// Login with invalid body
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader("test"))
|
||||
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader("test"))
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
|
@ -43,19 +46,19 @@ func TestLogin(t *testing.T) {
|
|||
|
||||
// Login with invalid format
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1"}`))
|
||||
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1"}`))
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 401, w.Code)
|
||||
assert.Equal(t, `{"code":401,"message":"validation failure list:\npassword in body is required"}`, w.Body.String())
|
||||
|
||||
//Validate machine
|
||||
// Validate machine
|
||||
ValidateMachine(t, "test", config.API.Server.DbConfig)
|
||||
|
||||
// Login with invalid password
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test", "password": "test1"}`))
|
||||
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test", "password": "test1"}`))
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
|
@ -64,7 +67,7 @@ func TestLogin(t *testing.T) {
|
|||
|
||||
// Login with valid machine
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
|
||||
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
|
@ -74,7 +77,7 @@ func TestLogin(t *testing.T) {
|
|||
|
||||
// Login with valid machine + scenarios
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test", "password": "test", "scenarios": ["crowdsecurity/test", "crowdsecurity/test2"]}`))
|
||||
req, _ = http.NewRequestWithContext(ctx, http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test", "password": "test", "scenarios": ["crowdsecurity/test", "crowdsecurity/test2"]}`))
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue