Files
ziti/tests/router_reenroll_test.go
T
Andrew Martinez 8c0bf8b885 fixes 1691 multiple er re-enrolls creates multiple enrollments
- ers do not support having multiple enrollments, detail returns 1
- re-enrolling now clears existing enrollments
- adds test to re-enroll twice
- encapsulates all er re-enrollment logic as a single atomic command
2024-01-25 15:09:30 -05:00

147 lines
4.7 KiB
Go

//go:build apitests
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tests
import (
"fmt"
"github.com/openziti/edge-api/rest_model"
"net/http"
"testing"
)
func Test_Router_ReEnroll(t *testing.T) {
ctx := NewTestContext(t)
defer ctx.Teardown()
ctx.StartServer()
ctx.RequireAdminManagementApiLogin()
t.Run("unenrolled router", func(t *testing.T) {
ctx.testContextChanged(t)
edgeRouter := ctx.requireCreateEdgeRouter(false)
t.Run("can start re-enrollment", func(t *testing.T) {
ctx.testContextChanged(t)
resp, err := ctx.AdminManagementSession.newAuthenticatedRequest().SetBody("{}").Post("edge-routers/" + edgeRouter.id + "/re-enroll")
ctx.Req.NoError(err)
ctx.Req.Equal(http.StatusOK, resp.StatusCode())
edgeRouterDetail := &rest_model.EdgeRouterDetail{}
envelope := &rest_model.DetailedEdgeRouterEnvelope{
Data: edgeRouterDetail,
Meta: &rest_model.Meta{},
}
resp, err = ctx.AdminManagementSession.newAuthenticatedRequest().SetResult(envelope).Get("edge-routers/" + edgeRouter.id)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(http.StatusOK, resp.StatusCode())
ctx.Req.NotNil(edgeRouterDetail.EnrollmentJWT)
t.Run("router has no current fingerprint", func(t *testing.T) {
ctx.testContextChanged(t)
ctx.Req.Empty(edgeRouterDetail.Fingerprint)
})
t.Run("router has a new enrollment JWT", func(t *testing.T) {
ctx.testContextChanged(t)
ctx.Req.NotNil(edgeRouterDetail.EnrollmentJWT)
t.Run("router can enroll", func(t *testing.T) {
ctx.testContextChanged(t)
ctx.requireEnrollEdgeRouter(false, *edgeRouterDetail.ID)
})
})
})
})
t.Run("enrolled router", func(t *testing.T) {
enrolledRouter := ctx.createAndEnrollEdgeRouter(false)
t.Run("has no current enrollment", func(t *testing.T) {
ctx.testContextChanged(t)
edgeRouterDetail := &rest_model.EdgeRouterDetail{}
resp, err := ctx.AdminManagementSession.newAuthenticatedRequest().SetBody(edgeRouterDetail).Get("edge-routers/" + enrolledRouter.id)
ctx.Req.NoError(err)
ctx.Req.Equal(http.StatusOK, resp.StatusCode())
ctx.Req.Nil(edgeRouterDetail.EnrollmentJWT)
})
t.Run("can start re-enroll", func(t *testing.T) {
ctx.testContextChanged(t)
resp, err := ctx.AdminManagementSession.newAuthenticatedRequest().SetBody("{}").Post("edge-routers/" + enrolledRouter.id + "/re-enroll")
ctx.Req.NoError(err)
ctx.Req.Equal(http.StatusOK, resp.StatusCode())
edgeRouterDetail := &rest_model.EdgeRouterDetail{}
envelope := &rest_model.DetailedEdgeRouterEnvelope{
Data: edgeRouterDetail,
Meta: &rest_model.Meta{},
}
resp, err = ctx.AdminManagementSession.newAuthenticatedRequest().SetResult(envelope).Get("edge-routers/" + enrolledRouter.id)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(http.StatusOK, resp.StatusCode())
ctx.Req.NotNil(edgeRouterDetail.EnrollmentJWT)
t.Run("router has no current fingerprint", func(t *testing.T) {
ctx.testContextChanged(t)
ctx.Req.Empty(edgeRouterDetail.Fingerprint)
})
t.Run("re-enrolling after re-enrolling does not create multiple enrollments", func(t *testing.T) {
ctx.testContextChanged(t)
resp, err = ctx.AdminManagementSession.newAuthenticatedRequest().SetResult(envelope).Get("edge-routers/" + enrolledRouter.id)
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(http.StatusOK, resp.StatusCode())
ctx.Req.NotNil(edgeRouterDetail.EnrollmentJWT)
listEnrollmentsEnv := &rest_model.ListEnrollmentsEnvelope{
Data: rest_model.EnrollmentList{},
Meta: &rest_model.Meta{},
}
resp, err = ctx.AdminManagementSession.newAuthenticatedRequest().SetResult(listEnrollmentsEnv).Get(fmt.Sprintf(`enrollments?filter=edgeRouter="%s"`, enrolledRouter.id))
ctx.Req.NoError(err)
ctx.Req.NotNil(resp)
ctx.Req.Equal(http.StatusOK, resp.StatusCode())
ctx.Req.Len(listEnrollmentsEnv.Data, 1)
})
t.Run("router has a new enrollment JWT", func(t *testing.T) {
ctx.testContextChanged(t)
ctx.Req.NotNil(edgeRouterDetail.EnrollmentJWT)
t.Run("router can enroll with JWT", func(t *testing.T) {
ctx.testContextChanged(t)
ctx.requireEnrollEdgeRouter(false, *edgeRouterDetail.ID)
})
})
})
})
}