mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 20:36:12 +02:00
apiserver/apiclient: compact tests (#2694)
* apiserver/apiclient: compact tests * update golangci-lint configuration
This commit is contained in:
parent
1c03fbe99e
commit
da746f77d5
15 changed files with 563 additions and 919 deletions
|
@ -6,20 +6,13 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
router, config, err := NewAPITest(t)
|
||||
if err != nil {
|
||||
log.Fatalf("unable to run local API: %s", err)
|
||||
}
|
||||
router, config := NewAPITest(t)
|
||||
|
||||
body, err := CreateTestMachine(router)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
body := CreateTestMachine(t, router)
|
||||
|
||||
// Login with machine not validated yet
|
||||
w := httptest.NewRecorder()
|
||||
|
@ -28,16 +21,16 @@ func TestLogin(t *testing.T) {
|
|||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 401, w.Code)
|
||||
assert.Equal(t, "{\"code\":401,\"message\":\"machine test not validated\"}", w.Body.String())
|
||||
assert.Equal(t, `{"code":401,"message":"machine test not validated"}`, w.Body.String())
|
||||
|
||||
// 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.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1", "password": "test1"}`))
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 401, w.Code)
|
||||
assert.Equal(t, "{\"code\":401,\"message\":\"ent: machine not found\"}", w.Body.String())
|
||||
assert.Equal(t, `{"code":401,"message":"ent: machine not found"}`, w.Body.String())
|
||||
|
||||
// Login with invalid body
|
||||
w = httptest.NewRecorder()
|
||||
|
@ -46,31 +39,28 @@ func TestLogin(t *testing.T) {
|
|||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 401, w.Code)
|
||||
assert.Equal(t, "{\"code\":401,\"message\":\"missing: invalid character 'e' in literal true (expecting 'r')\"}", w.Body.String())
|
||||
assert.Equal(t, `{"code":401,"message":"missing: invalid character 'e' in literal true (expecting 'r')"}`, w.Body.String())
|
||||
|
||||
// Login with invalid format
|
||||
w = httptest.NewRecorder()
|
||||
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader("{\"machine_id\": \"test1\"}"))
|
||||
req, _ = http.NewRequest(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())
|
||||
assert.Equal(t, `{"code":401,"message":"validation failure list:\npassword in body is required"}`, w.Body.String())
|
||||
|
||||
//Validate machine
|
||||
err = ValidateMachine("test", config.API.Server.DbConfig)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
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.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test", "password": "test1"}`))
|
||||
req.Header.Add("User-Agent", UserAgent)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 401, w.Code)
|
||||
assert.Equal(t, "{\"code\":401,\"message\":\"incorrect Username or Password\"}", w.Body.String())
|
||||
assert.Equal(t, `{"code":401,"message":"incorrect Username or Password"}`, w.Body.String())
|
||||
|
||||
// Login with valid machine
|
||||
w = httptest.NewRecorder()
|
||||
|
@ -79,16 +69,16 @@ func TestLogin(t *testing.T) {
|
|||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "\"token\"")
|
||||
assert.Contains(t, w.Body.String(), "\"expire\"")
|
||||
assert.Contains(t, w.Body.String(), `"token"`)
|
||||
assert.Contains(t, w.Body.String(), `"expire"`)
|
||||
|
||||
// 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.NewRequest(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)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "\"token\"")
|
||||
assert.Contains(t, w.Body.String(), "\"expire\"")
|
||||
assert.Contains(t, w.Body.String(), `"token"`)
|
||||
assert.Contains(t, w.Body.String(), `"expire"`)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue