2020-11-30 09:37:17 +00:00
|
|
|
package apiserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-01-04 16:10:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-30 09:37:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateMachine(t *testing.T) {
|
2024-01-04 16:10:36 +00:00
|
|
|
router, _ := NewAPITest(t)
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Create machine with invalid format
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader("test"))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 400, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, `{"message":"invalid character 'e' in literal true (expecting 'r')"}`, w.Body.String())
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Create machine with invalid input
|
|
|
|
w = httptest.NewRecorder()
|
2024-01-04 16:10:36 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(`{"test": "test"}`))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 500, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, `{"message":"validation failure list:\nmachine_id in body is required\npassword in body is required"}`, w.Body.String())
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
// Create machine
|
|
|
|
b, err := json.Marshal(MachineTest)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
body := string(b)
|
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
2020-11-30 15:15:07 +00:00
|
|
|
assert.Equal(t, 201, w.Code)
|
2020-11-30 09:37:17 +00:00
|
|
|
assert.Equal(t, "", w.Body.String())
|
|
|
|
}
|
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
func TestCreateMachineWithForwardedFor(t *testing.T) {
|
2024-01-04 16:10:36 +00:00
|
|
|
router, config := NewAPITestForwardedFor(t)
|
2022-01-17 16:18:12 +00:00
|
|
|
router.TrustedPlatform = "X-Real-IP"
|
2021-02-09 18:10:14 +00:00
|
|
|
// Create machine
|
|
|
|
b, err := json.Marshal(MachineTest)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
body := string(b)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body))
|
2021-02-09 18:10:14 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
2022-01-17 16:18:12 +00:00
|
|
|
req.Header.Add("X-Real-Ip", "1.1.1.1")
|
2021-02-09 18:10:14 +00:00
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 201, w.Code)
|
|
|
|
assert.Equal(t, "", w.Body.String())
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
ip := GetMachineIP(t, *MachineTest.MachineID, config.API.Server.DbConfig)
|
2023-12-14 13:54:11 +00:00
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
assert.Equal(t, "1.1.1.1", ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateMachineWithForwardedForNoConfig(t *testing.T) {
|
2024-01-04 16:10:36 +00:00
|
|
|
router, config := NewAPITest(t)
|
2021-02-09 18:10:14 +00:00
|
|
|
|
|
|
|
// Create machine
|
|
|
|
b, err := json.Marshal(MachineTest)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
body := string(b)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body))
|
2021-02-09 18:10:14 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
req.Header.Add("X-Real-IP", "1.1.1.1")
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 201, w.Code)
|
|
|
|
assert.Equal(t, "", w.Body.String())
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
ip := GetMachineIP(t, *MachineTest.MachineID, config.API.Server.DbConfig)
|
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
//For some reason, the IP is empty when running tests
|
|
|
|
//if no forwarded-for headers are present
|
|
|
|
assert.Equal(t, "", ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateMachineWithoutForwardedFor(t *testing.T) {
|
2024-01-04 16:10:36 +00:00
|
|
|
router, config := NewAPITestForwardedFor(t)
|
2021-02-09 18:10:14 +00:00
|
|
|
|
|
|
|
// Create machine
|
|
|
|
b, err := json.Marshal(MachineTest)
|
2024-01-04 16:10:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
body := string(b)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body))
|
2021-02-09 18:10:14 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 201, w.Code)
|
|
|
|
assert.Equal(t, "", w.Body.String())
|
|
|
|
|
2024-01-04 16:10:36 +00:00
|
|
|
ip := GetMachineIP(t, *MachineTest.MachineID, config.API.Server.DbConfig)
|
|
|
|
|
2021-02-09 18:10:14 +00:00
|
|
|
//For some reason, the IP is empty when running tests
|
|
|
|
//if no forwarded-for headers are present
|
|
|
|
assert.Equal(t, "", ip)
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func TestCreateMachineAlreadyExist(t *testing.T) {
|
2024-01-04 16:10:36 +00:00
|
|
|
router, _ := 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
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
2022-08-16 07:46:10 +00:00
|
|
|
req, _ = http.NewRequest(http.MethodPost, "/v1/watchers", strings.NewReader(body))
|
2020-11-30 09:37:17 +00:00
|
|
|
req.Header.Add("User-Agent", UserAgent)
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
assert.Equal(t, 403, w.Code)
|
2024-01-04 16:10:36 +00:00
|
|
|
assert.Equal(t, `{"message":"user 'test': user already exist"}`, w.Body.String())
|
2020-11-30 09:37:17 +00:00
|
|
|
}
|