2020-11-30 09:37:17 +00:00
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogin(t *testing.T) {
|
2024-01-04 16:10:36 +00:00
|
|
|
router, config := NewAPITest(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
body := CreateTestMachine(t, router)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Login with machine not validated yet
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 401, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, `{"code":401,"message":"machine test not validated"}`, w.Body.String())
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Login with machine not exist
|
|
|
|
w = httptest.NewRecorder()
|
2024-01-04 16:10:36 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1", "password": "test1"}`))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 401, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, `{"code":401,"message":"ent: machine not found"}`, w.Body.String())
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Login with invalid body
|
|
|
|
w = httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader("test"))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 401, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, `{"code":401,"message":"missing: invalid character 'e' in literal true (expecting 'r')"}`, w.Body.String())
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Login with invalid format
|
|
|
|
w = httptest.NewRecorder()
|
2024-01-04 16:10:36 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test1"}`))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 401, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, `{"code":401,"message":"validation failure list:\npassword in body is required"}`, w.Body.String())
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
//Validate machine
|
2024-01-04 16:10:36 +00:00
|
|
|
ValidateMachine(t, "test", config.API.Server.DbConfig)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Login with invalid password
|
|
|
|
w = httptest.NewRecorder()
|
2024-01-04 16:10:36 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test", "password": "test1"}`))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 401, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, `{"code":401,"message":"incorrect Username or Password"}`, w.Body.String())
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Login with valid machine
|
|
|
|
w = httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(body))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 200, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Contains(t, w.Body.String(), `"token"`)
|
|
|
|
assert.Contains(t, w.Body.String(), `"expire"`)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Login with valid machine + scenarios
|
|
|
|
w = httptest.NewRecorder()
|
2024-01-04 16:10:36 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers/login", strings.NewReader(`{"machine_id": "test", "password": "test", "scenarios": ["crowdsecurity/test", "crowdsecurity/test2"]}`))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 200, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Contains(t, w.Body.String(), `"token"`)
|
|
|
|
assert.Contains(t, w.Body.String(), `"expire"`)
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|