mirror of
https://github.com/gravitational/teleport.git
synced 2026-10-11 22:49:54 +00:00
Align Integration Web Test Client (#56290)
* Align Integration Web Test Client * Rename DoRequest -> DoWebAPIRequest
This commit is contained in:
@@ -224,7 +224,7 @@ func TestDiagnoseConnectionForPostgresDatabases(t *testing.T) {
|
|||||||
DialTimeout: time.Second,
|
DialTimeout: time.Second,
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
}
|
}
|
||||||
respStatusCode, respBody := webPack.DoRequest(t, http.MethodPost, diagnoseConnectionEndpoint, diagnoseReq)
|
respStatusCode, respBody := webPack.DoWebAPIRequest(t, http.MethodPost, diagnoseConnectionEndpoint, diagnoseReq)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
var connectionDiagnostic ui.ConnectionDiagnostic
|
var connectionDiagnostic ui.ConnectionDiagnostic
|
||||||
@@ -310,7 +310,7 @@ func TestDiagnoseConnectionForPostgresDatabases(t *testing.T) {
|
|||||||
TOTPCode: validToken,
|
TOTPCode: validToken,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
respStatusCode, respBody := webPack.DoRequest(t, http.MethodPost, diagnoseConnectionEndpoint, diagnoseReq)
|
respStatusCode, respBody := webPack.DoWebAPIRequest(t, http.MethodPost, diagnoseConnectionEndpoint, diagnoseReq)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
var connectionDiagnostic ui.ConnectionDiagnostic
|
var connectionDiagnostic ui.ConnectionDiagnostic
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ func TestDatabaseServiceHeartbeat(t *testing.T) {
|
|||||||
|
|
||||||
// List Database Services
|
// List Database Services
|
||||||
listDBServicesEndpoint := strings.Join([]string{"sites", "$site", "databaseservices"}, "/")
|
listDBServicesEndpoint := strings.Join([]string{"sites", "$site", "databaseservices"}, "/")
|
||||||
respStatusCode, respBody := webPack.DoRequest(t, http.MethodGet, listDBServicesEndpoint, nil)
|
respStatusCode, respBody := webPack.DoWebAPIRequest(t, http.MethodGet, listDBServicesEndpoint, nil)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
var listResp listDatabaseServicesResp
|
var listResp listDatabaseServicesResp
|
||||||
|
|||||||
+24
-14
@@ -99,7 +99,7 @@ func LoginWebClient(t *testing.T, host, username, password string) *WebClientPac
|
|||||||
bearerToken: csResp.Token,
|
bearerToken: csResp.Token,
|
||||||
}
|
}
|
||||||
|
|
||||||
respStatusCode, bs := webClient.DoRequest(t, http.MethodGet, "sites", nil)
|
respStatusCode, bs := webClient.DoWebAPIRequest(t, http.MethodGet, "sites", nil)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(bs))
|
require.Equal(t, http.StatusOK, respStatusCode, string(bs))
|
||||||
|
|
||||||
var clusters []ui.Cluster
|
var clusters []ui.Cluster
|
||||||
@@ -189,7 +189,7 @@ func LoginMFAWebClient(t *testing.T, host string, passwordlessDevice *mocku2f.Ke
|
|||||||
bearerToken: csResp.Token,
|
bearerToken: csResp.Token,
|
||||||
}
|
}
|
||||||
|
|
||||||
respStatusCode, bs := webClient.DoRequest(t, http.MethodGet, "sites", nil)
|
respStatusCode, bs := webClient.DoWebAPIRequest(t, http.MethodGet, "sites", nil)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(bs))
|
require.Equal(t, http.StatusOK, respStatusCode, string(bs))
|
||||||
|
|
||||||
var clusters []ui.Cluster
|
var clusters []ui.Cluster
|
||||||
@@ -200,14 +200,12 @@ func LoginMFAWebClient(t *testing.T, host string, passwordlessDevice *mocku2f.Ke
|
|||||||
return webClient
|
return webClient
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoRequest receives a method, endpoint and payload and sends an HTTP Request to the Teleport API.
|
// DoWebAPIRequest receives a method, endpoint and payload and sends an HTTP Request to the Teleport API.
|
||||||
// The endpoint must not contain the host neither the base path ('/v1/webapi/').
|
// The endpoint must not contain the host neither the base path ('/v1/webapi/').
|
||||||
// Status Code and Body are returned.
|
// Status Code and Body are returned.
|
||||||
// "$site" in the endpoint is substituted by the current site.
|
// "$site" in the endpoint is substituted by the current site.
|
||||||
func (w *WebClientPack) DoRequest(t *testing.T, method, endpoint string, payload any) (int, []byte) {
|
func (w *WebClientPack) DoWebAPIRequest(t *testing.T, method, endpoint string, payload any) (int, []byte) {
|
||||||
endpoint = fmt.Sprintf("https://%s/v1/webapi/%s", w.host, endpoint)
|
u, err := url.Parse(w.Endpoint("v1", "webapi", endpoint))
|
||||||
endpoint = strings.ReplaceAll(endpoint, "$site", w.clusterName)
|
|
||||||
u, err := url.Parse(endpoint)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
bs, err := json.Marshal(payload)
|
bs, err := json.Marshal(payload)
|
||||||
@@ -216,14 +214,8 @@ func (w *WebClientPack) DoRequest(t *testing.T, method, endpoint string, payload
|
|||||||
req, err := http.NewRequest(method, u.String(), bytes.NewBuffer(bs))
|
req, err := http.NewRequest(method, u.String(), bytes.NewBuffer(bs))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
req.AddCookie(&http.Cookie{
|
|
||||||
Name: websession.CookieName,
|
|
||||||
Value: w.webCookie,
|
|
||||||
})
|
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %v", w.bearerToken))
|
|
||||||
req.Header.Add("Content-Type", "application/json")
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
resp, err := w.Do(req)
|
||||||
resp, err := w.clt.Do(req)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@@ -234,6 +226,24 @@ func (w *WebClientPack) DoRequest(t *testing.T, method, endpoint string, payload
|
|||||||
return resp.StatusCode, body
|
return resp.StatusCode, body
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do sends an HTTP request with the web session cookie and bearer token.
|
||||||
|
func (w *WebClientPack) Do(req *http.Request) (*http.Response, error) {
|
||||||
|
req.AddCookie(&http.Cookie{
|
||||||
|
Name: websession.CookieName,
|
||||||
|
Value: w.webCookie,
|
||||||
|
})
|
||||||
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %v", w.bearerToken))
|
||||||
|
|
||||||
|
resp, err := w.clt.Do(req)
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpoint returns a Teleport API endpoint URL.
|
||||||
|
func (w *WebClientPack) Endpoint(params ...string) string {
|
||||||
|
endpoint := fmt.Sprintf("%s://%s/%s", client.HTTPS, w.host, strings.Join(params, "/"))
|
||||||
|
return strings.ReplaceAll(endpoint, "$site", w.clusterName)
|
||||||
|
}
|
||||||
|
|
||||||
// OpenWebsocket opens a websocket on a given Teleport API endpoint.
|
// OpenWebsocket opens a websocket on a given Teleport API endpoint.
|
||||||
// The endpoint must not contain the host neither the base path ('/v1/webapi/').
|
// The endpoint must not contain the host neither the base path ('/v1/webapi/').
|
||||||
// Raw websocket and HTTP response are returned.
|
// Raw websocket and HTTP response are returned.
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ func TestIntegrationCRUD(t *testing.T) {
|
|||||||
webPack := helpers.LoginWebClient(t, proxyAddr.String(), username, userPassword)
|
webPack := helpers.LoginWebClient(t, proxyAddr.String(), username, userPassword)
|
||||||
|
|
||||||
// List integrations should return empty
|
// List integrations should return empty
|
||||||
respStatusCode, respBody := webPack.DoRequest(t, http.MethodGet, integrationsEndpoint, nil)
|
respStatusCode, respBody := webPack.DoWebAPIRequest(t, http.MethodGet, integrationsEndpoint, nil)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
listResp := ui.IntegrationsListResponse{}
|
listResp := ui.IntegrationsListResponse{}
|
||||||
@@ -103,7 +103,7 @@ func TestIntegrationCRUD(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
respStatusCode, respBody = webPack.DoRequest(t, http.MethodPost, integrationsEndpoint, createIntegrationReq)
|
respStatusCode, respBody = webPack.DoWebAPIRequest(t, http.MethodPost, integrationsEndpoint, createIntegrationReq)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
// Create Integration without S3 location
|
// Create Integration without S3 location
|
||||||
@@ -115,11 +115,11 @@ func TestIntegrationCRUD(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
respStatusCode, respBody = webPack.DoRequest(t, http.MethodPost, integrationsEndpoint, createIntegrationWithoutS3LocationReq)
|
respStatusCode, respBody = webPack.DoWebAPIRequest(t, http.MethodPost, integrationsEndpoint, createIntegrationWithoutS3LocationReq)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
// Get One Integration by name
|
// Get One Integration by name
|
||||||
respStatusCode, respBody = webPack.DoRequest(t, http.MethodGet, integrationsEndpoint+"/my-aws-account", nil)
|
respStatusCode, respBody = webPack.DoWebAPIRequest(t, http.MethodGet, integrationsEndpoint+"/my-aws-account", nil)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
integrationResp := ui.Integration{}
|
integrationResp := ui.Integration{}
|
||||||
@@ -136,7 +136,7 @@ func TestIntegrationCRUD(t *testing.T) {
|
|||||||
}, integrationResp, string(respBody))
|
}, integrationResp, string(respBody))
|
||||||
|
|
||||||
// Update the integration to another RoleARN
|
// Update the integration to another RoleARN
|
||||||
respStatusCode, respBody = webPack.DoRequest(t, http.MethodPut, integrationsEndpoint+"/my-aws-account", ui.UpdateIntegrationRequest{
|
respStatusCode, respBody = webPack.DoWebAPIRequest(t, http.MethodPut, integrationsEndpoint+"/my-aws-account", ui.UpdateIntegrationRequest{
|
||||||
AWSOIDC: &ui.IntegrationAWSOIDCSpec{
|
AWSOIDC: &ui.IntegrationAWSOIDCSpec{
|
||||||
RoleARN: "arn:aws:iam::123456789012:role/OpsTeam",
|
RoleARN: "arn:aws:iam::123456789012:role/OpsTeam",
|
||||||
IssuerS3Bucket: "my-bucket",
|
IssuerS3Bucket: "my-bucket",
|
||||||
@@ -159,7 +159,7 @@ func TestIntegrationCRUD(t *testing.T) {
|
|||||||
}, integrationResp, string(respBody))
|
}, integrationResp, string(respBody))
|
||||||
|
|
||||||
// Update the integration to remove the S3 Location
|
// Update the integration to remove the S3 Location
|
||||||
respStatusCode, respBody = webPack.DoRequest(t, http.MethodPut, integrationsEndpoint+"/my-aws-account", ui.UpdateIntegrationRequest{
|
respStatusCode, respBody = webPack.DoWebAPIRequest(t, http.MethodPut, integrationsEndpoint+"/my-aws-account", ui.UpdateIntegrationRequest{
|
||||||
AWSOIDC: &ui.IntegrationAWSOIDCSpec{
|
AWSOIDC: &ui.IntegrationAWSOIDCSpec{
|
||||||
RoleARN: "arn:aws:iam::123456789012:role/OpsTeam2",
|
RoleARN: "arn:aws:iam::123456789012:role/OpsTeam2",
|
||||||
},
|
},
|
||||||
@@ -178,7 +178,7 @@ func TestIntegrationCRUD(t *testing.T) {
|
|||||||
}, integrationResp, string(respBody))
|
}, integrationResp, string(respBody))
|
||||||
|
|
||||||
// Delete resource
|
// Delete resource
|
||||||
respStatusCode, respBody = webPack.DoRequest(t, http.MethodDelete, integrationsEndpoint+"/my-aws-account", nil)
|
respStatusCode, respBody = webPack.DoWebAPIRequest(t, http.MethodDelete, integrationsEndpoint+"/my-aws-account", nil)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
// Add multiple integrations to test pagination
|
// Add multiple integrations to test pagination
|
||||||
@@ -196,12 +196,12 @@ func TestIntegrationCRUD(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
respStatusCode, respBody := webPack.DoRequest(t, http.MethodPost, integrationsEndpoint, createIntegrationReq)
|
respStatusCode, respBody := webPack.DoWebAPIRequest(t, http.MethodPost, integrationsEndpoint, createIntegrationReq)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
}
|
}
|
||||||
|
|
||||||
// List integrations should return a full page
|
// List integrations should return a full page
|
||||||
respStatusCode, respBody = webPack.DoRequest(t, http.MethodGet, integrationsEndpoint+"?limit=10", nil)
|
respStatusCode, respBody = webPack.DoWebAPIRequest(t, http.MethodGet, integrationsEndpoint+"?limit=10", nil)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
listResp = ui.IntegrationsListResponse{}
|
listResp = ui.IntegrationsListResponse{}
|
||||||
@@ -210,7 +210,7 @@ func TestIntegrationCRUD(t *testing.T) {
|
|||||||
require.Len(t, listResp.Items, pageSize)
|
require.Len(t, listResp.Items, pageSize)
|
||||||
|
|
||||||
// Requesting the 2nd page should return a full page
|
// Requesting the 2nd page should return a full page
|
||||||
respStatusCode, respBody = webPack.DoRequest(t, http.MethodGet, integrationsEndpoint+"?limit=10&startKey="+listResp.NextKey, nil)
|
respStatusCode, respBody = webPack.DoWebAPIRequest(t, http.MethodGet, integrationsEndpoint+"?limit=10&startKey="+listResp.NextKey, nil)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
listResp = ui.IntegrationsListResponse{}
|
listResp = ui.IntegrationsListResponse{}
|
||||||
@@ -219,7 +219,7 @@ func TestIntegrationCRUD(t *testing.T) {
|
|||||||
require.Len(t, listResp.Items, pageSize)
|
require.Len(t, listResp.Items, pageSize)
|
||||||
|
|
||||||
// Requesting the 3rd page should return two items and empty StartKey
|
// Requesting the 3rd page should return two items and empty StartKey
|
||||||
respStatusCode, respBody = webPack.DoRequest(t, http.MethodGet, integrationsEndpoint+"?limit=10&startKey="+listResp.NextKey, nil)
|
respStatusCode, respBody = webPack.DoWebAPIRequest(t, http.MethodGet, integrationsEndpoint+"?limit=10&startKey="+listResp.NextKey, nil)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
listResp = ui.IntegrationsListResponse{}
|
listResp = ui.IntegrationsListResponse{}
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ func TestMFAAuthenticateChallenge_IsMFARequiredApp(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
respStatusCode, respBody := webPack.DoRequest(t, http.MethodPost, endpoint, req)
|
respStatusCode, respBody := webPack.DoWebAPIRequest(t, http.MethodPost, endpoint, req)
|
||||||
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
require.Equal(t, http.StatusOK, respStatusCode, string(respBody))
|
||||||
|
|
||||||
var resp client.MFAAuthenticateChallenge
|
var resp client.MFAAuthenticateChallenge
|
||||||
|
|||||||
Reference in New Issue
Block a user