mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-04 22:10:56 +01:00
add all setup keys tests
This commit is contained in:
parent
bcec9b44ef
commit
b3f09598ac
@ -9,6 +9,8 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -26,14 +28,19 @@ const (
|
||||
testAccountId = "testUserId"
|
||||
testUserId = "testAccountId"
|
||||
testPeerId = "testPeerId"
|
||||
testGroupId = "testGroupId"
|
||||
testKeyId = "testKeyId"
|
||||
|
||||
newKeyName = "newKey"
|
||||
expiresIn = 3600
|
||||
newKeyName = "newKey"
|
||||
newGroupId = "newGroupId"
|
||||
expiresIn = 3600
|
||||
revokedKeyId = "revokedKeyId"
|
||||
expiredKeyId = "expiredKeyId"
|
||||
|
||||
existingKeyName = "existingKey"
|
||||
)
|
||||
|
||||
func Test_SetupKeys(t *testing.T) {
|
||||
func Test_SetupKeys_Create(t *testing.T) {
|
||||
truePointer := true
|
||||
tt := []struct {
|
||||
name string
|
||||
@ -149,15 +156,15 @@ func Test_SetupKeys(t *testing.T) {
|
||||
requestType: http.MethodPost,
|
||||
requestPath: "/api/setup-keys",
|
||||
requestBody: &api.CreateSetupKeyRequest{
|
||||
AutoGroups: []string{"testGroup"},
|
||||
AutoGroups: []string{testGroupId},
|
||||
ExpiresIn: expiresIn,
|
||||
Name: newKeyName,
|
||||
Type: "reusable",
|
||||
UsageLimit: 0,
|
||||
UsageLimit: 1,
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{"testGroup"},
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
@ -217,6 +224,35 @@ func Test_SetupKeys(t *testing.T) {
|
||||
expectedStatus: http.StatusUnprocessableEntity,
|
||||
expectedResponse: nil,
|
||||
},
|
||||
{
|
||||
name: "Create Setup Key",
|
||||
requestType: http.MethodPost,
|
||||
requestPath: "/api/setup-keys",
|
||||
requestBody: &api.CreateSetupKeyRequest{
|
||||
AutoGroups: nil,
|
||||
ExpiresIn: expiresIn,
|
||||
Name: newKeyName,
|
||||
Type: "reusable",
|
||||
UsageLimit: 0,
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: newKeyName,
|
||||
Revoked: false,
|
||||
State: "valid",
|
||||
Type: "reusable",
|
||||
UpdatedAt: time.Now(),
|
||||
UsageLimit: 0,
|
||||
UsedTimes: 0,
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
@ -260,6 +296,600 @@ func Test_SetupKeys(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_SetupKeys_Update(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
expectedStatus int
|
||||
expectedResponse *api.SetupKey
|
||||
requestBody *api.SetupKeyRequest
|
||||
requestType string
|
||||
requestPath string
|
||||
requestId string
|
||||
}{
|
||||
{
|
||||
name: "Add existing Group to existing Setup Key",
|
||||
requestType: http.MethodPut,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: testKeyId,
|
||||
requestBody: &api.SetupKeyRequest{
|
||||
AutoGroups: []string{testGroupId, newGroupId},
|
||||
Revoked: false,
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{testGroupId, newGroupId},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: false,
|
||||
State: "valid",
|
||||
Type: "one-off",
|
||||
UpdatedAt: time.Now(),
|
||||
UsageLimit: 1,
|
||||
UsedTimes: 0,
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Add non-existing Group to existing Setup Key",
|
||||
requestType: http.MethodPut,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: testKeyId,
|
||||
requestBody: &api.SetupKeyRequest{
|
||||
AutoGroups: []string{testGroupId, "someGroupId"},
|
||||
Revoked: false,
|
||||
},
|
||||
expectedStatus: http.StatusUnprocessableEntity,
|
||||
expectedResponse: nil,
|
||||
},
|
||||
{
|
||||
name: "Add existing Group to non-existing Setup Key",
|
||||
requestType: http.MethodPut,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: "someId",
|
||||
requestBody: &api.SetupKeyRequest{
|
||||
AutoGroups: []string{testGroupId, newGroupId},
|
||||
Revoked: false,
|
||||
},
|
||||
expectedStatus: http.StatusNotFound,
|
||||
expectedResponse: nil,
|
||||
},
|
||||
{
|
||||
name: "Remove existing Group from existing Setup Key",
|
||||
requestType: http.MethodPut,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: testKeyId,
|
||||
requestBody: &api.SetupKeyRequest{
|
||||
AutoGroups: []string{},
|
||||
Revoked: false,
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: false,
|
||||
State: "valid",
|
||||
Type: "one-off",
|
||||
UpdatedAt: time.Now(),
|
||||
UsageLimit: 1,
|
||||
UsedTimes: 0,
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Remove existing Group to non-existing Setup Key",
|
||||
requestType: http.MethodPut,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: "someID",
|
||||
requestBody: &api.SetupKeyRequest{
|
||||
AutoGroups: []string{},
|
||||
Revoked: false,
|
||||
},
|
||||
expectedStatus: http.StatusNotFound,
|
||||
expectedResponse: nil,
|
||||
},
|
||||
{
|
||||
name: "Revoke existing valid Setup Key",
|
||||
requestType: http.MethodPut,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: testKeyId,
|
||||
requestBody: &api.SetupKeyRequest{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Revoked: true,
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: true,
|
||||
State: "revoked",
|
||||
Type: "one-off",
|
||||
UpdatedAt: time.Now(),
|
||||
UsageLimit: 1,
|
||||
UsedTimes: 0,
|
||||
Valid: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Revoke existing revoked Setup Key",
|
||||
requestType: http.MethodPut,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: revokedKeyId,
|
||||
requestBody: &api.SetupKeyRequest{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Revoked: true,
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: true,
|
||||
State: "revoked",
|
||||
Type: "reusable",
|
||||
UpdatedAt: time.Now(),
|
||||
UsageLimit: 3,
|
||||
UsedTimes: 0,
|
||||
Valid: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Un-Revoke existing revoked Setup Key",
|
||||
requestType: http.MethodPut,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: revokedKeyId,
|
||||
requestBody: &api.SetupKeyRequest{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Revoked: false,
|
||||
},
|
||||
expectedStatus: http.StatusUnprocessableEntity,
|
||||
expectedResponse: nil,
|
||||
},
|
||||
{
|
||||
name: "Revoke existing expired Setup Key",
|
||||
requestType: http.MethodPut,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: expiredKeyId,
|
||||
requestBody: &api.SetupKeyRequest{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Revoked: true,
|
||||
},
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: true,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: true,
|
||||
State: "expired",
|
||||
Type: "reusable",
|
||||
UpdatedAt: time.Now(),
|
||||
UsageLimit: 5,
|
||||
UsedTimes: 1,
|
||||
Valid: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
apiHandler, am, done := buildApiBlackBoxWithDBState(t, "testdata/setup_keys.sql", nil)
|
||||
|
||||
body, err := json.Marshal(tc.requestBody)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to marshal request body: %v", err)
|
||||
}
|
||||
|
||||
req := buildRequest(t, body, tc.requestType, strings.Replace(tc.requestPath, "{id}", tc.requestId, 1))
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
apiHandler.ServeHTTP(recorder, req)
|
||||
|
||||
content, noResponseExpected := readResponse(t, recorder, tc.expectedStatus)
|
||||
if noResponseExpected {
|
||||
return
|
||||
}
|
||||
got := &api.SetupKey{}
|
||||
if err := json.Unmarshal(content, &got); err != nil {
|
||||
t.Fatalf("Sent content is not in correct json format; %v", err)
|
||||
}
|
||||
|
||||
validateCreatedKey(t, tc.expectedResponse, got)
|
||||
|
||||
key, err := am.GetSetupKey(context.Background(), testAccountId, testUserId, got.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
validateCreatedKey(t, tc.expectedResponse, toResponseBody(key))
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_SetupKeys_Get(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
expectedStatus int
|
||||
expectedResponse *api.SetupKey
|
||||
requestType string
|
||||
requestPath string
|
||||
requestId string
|
||||
}{
|
||||
{
|
||||
name: "Get existing valid Setup Key",
|
||||
requestType: http.MethodGet,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: testKeyId,
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: false,
|
||||
State: "valid",
|
||||
Type: "one-off",
|
||||
UpdatedAt: time.Date(2021, time.August, 19, 20, 46, 20, 5936822, time.Local),
|
||||
UsageLimit: 1,
|
||||
UsedTimes: 0,
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Get existing expired Setup Key",
|
||||
requestType: http.MethodGet,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: expiredKeyId,
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: true,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: false,
|
||||
State: "expired",
|
||||
Type: "reusable",
|
||||
UpdatedAt: time.Date(2021, time.August, 19, 20, 46, 20, 5936822, time.Local),
|
||||
UsageLimit: 5,
|
||||
UsedTimes: 1,
|
||||
Valid: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Get existing revoked Setup Key",
|
||||
requestType: http.MethodGet,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: revokedKeyId,
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: true,
|
||||
State: "revoked",
|
||||
Type: "reusable",
|
||||
UpdatedAt: time.Date(2021, time.August, 19, 20, 46, 20, 5936822, time.Local),
|
||||
UsageLimit: 3,
|
||||
UsedTimes: 0,
|
||||
Valid: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Get non-existing Setup Key",
|
||||
requestType: http.MethodGet,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: "someId",
|
||||
expectedStatus: http.StatusNotFound,
|
||||
expectedResponse: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
apiHandler, am, done := buildApiBlackBoxWithDBState(t, "testdata/setup_keys.sql", nil)
|
||||
|
||||
req := buildRequest(t, []byte{}, tc.requestType, strings.Replace(tc.requestPath, "{id}", tc.requestId, 1))
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
apiHandler.ServeHTTP(recorder, req)
|
||||
|
||||
content, noResponseExpected := readResponse(t, recorder, tc.expectedStatus)
|
||||
if noResponseExpected {
|
||||
return
|
||||
}
|
||||
got := &api.SetupKey{}
|
||||
if err := json.Unmarshal(content, &got); err != nil {
|
||||
t.Fatalf("Sent content is not in correct json format; %v", err)
|
||||
}
|
||||
|
||||
validateCreatedKey(t, tc.expectedResponse, got)
|
||||
|
||||
key, err := am.GetSetupKey(context.Background(), testAccountId, testUserId, got.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
validateCreatedKey(t, tc.expectedResponse, toResponseBody(key))
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_SetupKeys_GetAll(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
expectedStatus int
|
||||
expectedResponse []*api.SetupKey
|
||||
requestType string
|
||||
requestPath string
|
||||
}{
|
||||
{
|
||||
name: "Get all Setup Keys",
|
||||
requestType: http.MethodGet,
|
||||
requestPath: "/api/setup-keys",
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: []*api.SetupKey{
|
||||
{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: false,
|
||||
State: "valid",
|
||||
Type: "one-off",
|
||||
UpdatedAt: time.Date(2021, time.August, 19, 20, 46, 20, 5936822, time.Local),
|
||||
UsageLimit: 1,
|
||||
UsedTimes: 0,
|
||||
Valid: true,
|
||||
},
|
||||
{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: true,
|
||||
State: "revoked",
|
||||
Type: "reusable",
|
||||
UpdatedAt: time.Date(2021, time.August, 19, 20, 46, 20, 5936822, time.Local),
|
||||
UsageLimit: 3,
|
||||
UsedTimes: 0,
|
||||
Valid: false,
|
||||
},
|
||||
{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: true,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: false,
|
||||
State: "expired",
|
||||
Type: "reusable",
|
||||
UpdatedAt: time.Date(2021, time.August, 19, 20, 46, 20, 5936822, time.Local),
|
||||
UsageLimit: 5,
|
||||
UsedTimes: 1,
|
||||
Valid: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
apiHandler, am, done := buildApiBlackBoxWithDBState(t, "testdata/setup_keys.sql", nil)
|
||||
|
||||
req := buildRequest(t, []byte{}, tc.requestType, tc.requestPath)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
apiHandler.ServeHTTP(recorder, req)
|
||||
|
||||
content, noResponseExpected := readResponse(t, recorder, tc.expectedStatus)
|
||||
if noResponseExpected {
|
||||
return
|
||||
}
|
||||
got := []api.SetupKey{}
|
||||
if err := json.Unmarshal(content, &got); err != nil {
|
||||
t.Fatalf("Sent content is not in correct json format; %v", err)
|
||||
}
|
||||
|
||||
sort.Slice(got, func(i, j int) bool {
|
||||
return got[i].UsageLimit < got[j].UsageLimit
|
||||
})
|
||||
|
||||
sort.Slice(tc.expectedResponse, func(i, j int) bool {
|
||||
return tc.expectedResponse[i].UsageLimit < tc.expectedResponse[j].UsageLimit
|
||||
})
|
||||
|
||||
for i, _ := range tc.expectedResponse {
|
||||
validateCreatedKey(t, tc.expectedResponse[i], &got[i])
|
||||
|
||||
key, err := am.GetSetupKey(context.Background(), testAccountId, testUserId, got[i].Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
validateCreatedKey(t, tc.expectedResponse[i], toResponseBody(key))
|
||||
}
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_SetupKeys_Delete(t *testing.T) {
|
||||
tt := []struct {
|
||||
name string
|
||||
expectedStatus int
|
||||
expectedResponse *api.SetupKey
|
||||
requestType string
|
||||
requestPath string
|
||||
requestId string
|
||||
}{
|
||||
{
|
||||
name: "Delete existing valid Setup Key",
|
||||
requestType: http.MethodDelete,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: testKeyId,
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: false,
|
||||
State: "valid",
|
||||
Type: "one-off",
|
||||
UpdatedAt: time.Date(2021, time.August, 19, 20, 46, 20, 5936822, time.Local),
|
||||
UsageLimit: 1,
|
||||
UsedTimes: 0,
|
||||
Valid: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Delete existing expired Setup Key",
|
||||
requestType: http.MethodDelete,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: expiredKeyId,
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: true,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: false,
|
||||
State: "expired",
|
||||
Type: "reusable",
|
||||
UpdatedAt: time.Date(2021, time.August, 19, 20, 46, 20, 5936822, time.Local),
|
||||
UsageLimit: 5,
|
||||
UsedTimes: 1,
|
||||
Valid: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Delete existing revoked Setup Key",
|
||||
requestType: http.MethodDelete,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: revokedKeyId,
|
||||
expectedStatus: http.StatusOK,
|
||||
expectedResponse: &api.SetupKey{
|
||||
AutoGroups: []string{testGroupId},
|
||||
Ephemeral: false,
|
||||
Expires: time.Time{},
|
||||
Id: "",
|
||||
Key: "",
|
||||
LastUsed: time.Time{},
|
||||
Name: existingKeyName,
|
||||
Revoked: true,
|
||||
State: "revoked",
|
||||
Type: "reusable",
|
||||
UpdatedAt: time.Date(2021, time.August, 19, 20, 46, 20, 5936822, time.Local),
|
||||
UsageLimit: 3,
|
||||
UsedTimes: 0,
|
||||
Valid: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Delete non-existing Setup Key",
|
||||
requestType: http.MethodDelete,
|
||||
requestPath: "/api/setup-keys/{id}",
|
||||
requestId: "someId",
|
||||
expectedStatus: http.StatusNotFound,
|
||||
expectedResponse: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
apiHandler, am, done := buildApiBlackBoxWithDBState(t, "testdata/setup_keys.sql", nil)
|
||||
|
||||
req := buildRequest(t, []byte{}, tc.requestType, strings.Replace(tc.requestPath, "{id}", tc.requestId, 1))
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
apiHandler.ServeHTTP(recorder, req)
|
||||
|
||||
content, noResponseExpected := readResponse(t, recorder, tc.expectedStatus)
|
||||
if noResponseExpected {
|
||||
return
|
||||
}
|
||||
got := &api.SetupKey{}
|
||||
if err := json.Unmarshal(content, &got); err != nil {
|
||||
t.Fatalf("Sent content is not in correct json format; %v", err)
|
||||
}
|
||||
|
||||
_, err := am.GetSetupKey(context.Background(), testAccountId, testUserId, got.Id)
|
||||
assert.Errorf(t, err, "Expected error when trying to get deleted key")
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Error("timeout waiting for peerShouldNotReceiveUpdate")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func buildApiBlackBoxWithDBState(t *testing.T, sqlFile string, expectedPeerUpdate *server.UpdateMessage) (http.Handler, server.AccountManager, chan struct{}) {
|
||||
store, cleanup, err := server.NewTestStoreFromSQL(context.Background(), sqlFile, t.TempDir())
|
||||
if err != nil {
|
||||
@ -327,18 +957,20 @@ func readResponse(t *testing.T, recorder *httptest.ResponseRecorder, expectedSta
|
||||
func validateCreatedKey(t *testing.T, expectedKey *api.SetupKey, got *api.SetupKey) {
|
||||
t.Helper()
|
||||
|
||||
if got.Expires.After(time.Now().Add(-1*time.Minute)) && got.Expires.Before(time.Now().Add(expiresIn*time.Second)) {
|
||||
if got.Expires.After(time.Now().Add(-1*time.Minute)) && got.Expires.Before(time.Now().Add(expiresIn*time.Second)) ||
|
||||
got.Expires.After(time.Date(2300, 01, 01, 0, 0, 0, 0, time.Local)) ||
|
||||
got.Expires.Before(time.Date(1950, 01, 01, 0, 0, 0, 0, time.Local)) {
|
||||
got.Expires = time.Time{}
|
||||
expectedKey.Expires = time.Time{}
|
||||
}
|
||||
|
||||
if got.Id == "" {
|
||||
t.Error("Expected key to have an ID")
|
||||
t.Fatalf("Expected key to have an ID")
|
||||
}
|
||||
got.Id = ""
|
||||
|
||||
if got.Key == "" {
|
||||
t.Error("Expected key to have a key")
|
||||
t.Fatalf("Expected key to have a key")
|
||||
}
|
||||
got.Key = ""
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
CREATE TABLE `accounts` (`id` text,`created_by` text,`created_at` datetime,`domain` text,`domain_category` text,`is_domain_primary_account` numeric,`network_identifier` text,`network_net` text,`network_dns` text,`network_serial` integer,`dns_settings_disabled_management_groups` text,`settings_peer_login_expiration_enabled` numeric,`settings_peer_login_expiration` integer,`settings_regular_users_view_blocked` numeric,`settings_groups_propagation_enabled` numeric,`settings_jwt_groups_enabled` numeric,`settings_jwt_groups_claim_name` text,`settings_jwt_allow_groups` text,`settings_extra_peer_approval_enabled` numeric,`settings_extra_integrated_validator_groups` text,PRIMARY KEY (`id`));
|
||||
CREATE TABLE `setup_keys` (`id` text,`account_id` text,`key` text,`name` text,`type` text,`created_at` datetime,`expires_at` datetime,`updated_at` datetime,`revoked` numeric,`used_times` integer,`last_used` datetime,`auto_groups` text,`usage_limit` integer,`ephemeral` numeric,PRIMARY KEY (`id`),CONSTRAINT `fk_accounts_setup_keys_g` FOREIGN KEY (`account_id`) REFERENCES `accounts`(`id`));
|
||||
CREATE TABLE `setup_keys` (`id` text,`account_id` text,`key` text,`key_secret` text,`name` text,`type` text,`created_at` datetime,`expires_at` datetime,`updated_at` datetime,`revoked` numeric,`used_times` integer,`last_used` datetime,`auto_groups` text,`usage_limit` integer,`ephemeral` numeric,PRIMARY KEY (`id`),CONSTRAINT `fk_accounts_setup_keys_g` FOREIGN KEY (`account_id`) REFERENCES `accounts`(`id`));
|
||||
CREATE TABLE `users` (`id` text,`account_id` text,`role` text,`is_service_user` numeric,`non_deletable` numeric,`service_user_name` text,`auto_groups` text,`blocked` numeric,`last_login` datetime,`created_at` datetime,`issued` text DEFAULT "api",`integration_ref_id` integer,`integration_ref_integration_type` text,PRIMARY KEY (`id`),CONSTRAINT `fk_accounts_users_g` FOREIGN KEY (`account_id`) REFERENCES `accounts`(`id`));
|
||||
CREATE TABLE `peers` (`id` text,`account_id` text,`key` text,`setup_key` text,`ip` text,`meta_hostname` text,`meta_go_os` text,`meta_kernel` text,`meta_core` text,`meta_platform` text,`meta_os` text,`meta_os_version` text,`meta_wt_version` text,`meta_ui_version` text,`meta_kernel_version` text,`meta_network_addresses` text,`meta_system_serial_number` text,`meta_system_product_name` text,`meta_system_manufacturer` text,`meta_environment` text,`meta_files` text,`name` text,`dns_label` text,`peer_status_last_seen` datetime,`peer_status_connected` numeric,`peer_status_login_expired` numeric,`peer_status_requires_approval` numeric,`user_id` text,`ssh_key` text,`ssh_enabled` numeric,`login_expiration_enabled` numeric,`last_login` datetime,`created_at` datetime,`ephemeral` numeric,`location_connection_ip` text,`location_country_code` text,`location_city_name` text,`location_geo_name_id` integer,PRIMARY KEY (`id`),CONSTRAINT `fk_accounts_peers_g` FOREIGN KEY (`account_id`) REFERENCES `accounts`(`id`));
|
||||
CREATE TABLE `groups` (`id` text,`account_id` text,`name` text,`issued` text,`peers` text,`integration_ref_id` integer,`integration_ref_integration_type` text,PRIMARY KEY (`id`),CONSTRAINT `fk_accounts_groups_g` FOREIGN KEY (`account_id`) REFERENCES `accounts`(`id`));
|
||||
@ -7,7 +7,10 @@ CREATE TABLE `groups` (`id` text,`account_id` text,`name` text,`issued` text,`pe
|
||||
INSERT INTO accounts VALUES('testAccountId','','2024-10-02 16:01:38.210014+02:00','test.com','private',1,'testNetworkIdentifier','{"IP":"100.64.0.0","Mask":"//8AAA=="}','',0,'[]',0,86400000000000,0,0,0,'',NULL,NULL,NULL);
|
||||
INSERT INTO users VALUES('testUserId','testAccountId','admin',0,0,'','[]',0,'0001-01-01 00:00:00+00:00','2024-10-02 16:01:38.210678+02:00','api',0,'');
|
||||
INSERT INTO peers VALUES('testPeerId','testAccountId','5rvhvriKJZ3S9oxYToVj5TzDM9u9y8cxg7htIMWlYAg=','72546A29-6BC8-4311-BCFC-9CDBF33F1A48','"100.64.114.31"','f2a34f6a4731','linux','Linux','11','unknown','Debian GNU/Linux','','0.12.0','','',NULL,'','','','{"Cloud":"","Platform":""}',NULL,'f2a34f6a4731','f2a34f6a4731','2023-03-02 09:21:02.189035775+01:00',0,0,0,'','ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILzUUSYG/LGnV8zarb2SGN+tib/PZ+M7cL4WtTzUrTpk',0,1,'2023-03-01 19:48:19.817799698+01:00','2024-10-02 17:00:32.527947+02:00',0,'""','','',0);
|
||||
INSERT INTO "groups" VALUES('testGroup','testAccountId','testGroupName','api','[]',0,'');
|
||||
INSERT INTO "groups" VALUES('testGroupId','testAccountId','testGroupName','api','[]',0,'');
|
||||
INSERT INTO "groups" VALUES('newGroupId','testAccountId','newGroupName','api','[]',0,'');
|
||||
|
||||
INSERT INTO setup_keys VALUES('testKey','testAccountId','testKey','existingKey','reusable','2021-08-19 20:46:20.005936822+02:00','2321-09-18 20:46:20.005936822+02:00','2021-08-19 20:46:20.005936822+02:00',0,0,'0001-01-01 00:00:00+00:00','[]',0,0);
|
||||
INSERT INTO setup_keys VALUES('testKeyId','testAccountId','testKey','testK****','existingKey','one-off','2021-08-19 20:46:20.005936822+02:00','2321-09-18 20:46:20.005936822+02:00','2021-08-19 20:46:20.005936822+02:00',0,0,'0001-01-01 00:00:00+00:00','["testGroupId"]',1,0);
|
||||
INSERT INTO setup_keys VALUES('revokedKeyId','testAccountId','revokedKey','testK****','existingKey','reusable','2021-08-19 20:46:20.005936822+02:00','2321-09-18 20:46:20.005936822+02:00','2021-08-19 20:46:20.005936822+02:00',1,0,'0001-01-01 00:00:00+00:00','["testGroupId"]',3,0);
|
||||
INSERT INTO setup_keys VALUES('expiredKeyId','testAccountId','expiredKey','testK****','existingKey','reusable','2021-08-19 20:46:20.005936822+02:00','1921-09-18 20:46:20.005936822+02:00','2021-08-19 20:46:20.005936822+02:00',0,1,'0001-01-01 00:00:00+00:00','["testGroupId"]',5,1);
|
||||
|
||||
|
@ -305,7 +305,7 @@ func (am *DefaultAccountManager) SaveSetupKey(ctx context.Context, accountID str
|
||||
|
||||
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
||||
if err = validateSetupKeyAutoGroups(ctx, transaction, accountID, keyToSave.AutoGroups); err != nil {
|
||||
return err
|
||||
return status.Errorf(status.InvalidArgument, "invalid auto groups: %v", err)
|
||||
}
|
||||
|
||||
oldKey, err = transaction.GetSetupKeyByID(ctx, LockingStrengthShare, accountID, keyToSave.Id)
|
||||
|
Loading…
Reference in New Issue
Block a user