Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from mimotej/add-tests-for-requests
Browse files Browse the repository at this point in the history
feat(tests): Create tests for webterminal-proxy requests
  • Loading branch information
tumido authored Apr 25, 2023
2 parents 1187ec2 + bd750d2 commit 057b28f
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 1 deletion.
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ module github.com/janus-idp/webterminal-proxy

go 1.19

require github.com/gorilla/websocket v1.5.0
require (
github.com/gorilla/websocket v1.5.0
github.com/stretchr/testify v1.8.2
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
151 changes: 151 additions & 0 deletions utils/requests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package utils

import (
"bytes"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

const (
USERS_ENDPOINT = "/apis/user.openshift.io/v1/users/~"
WORKSPACE_ENDPOINT = "/apis/workspace.devfile.io/v1alpha2/namespaces/test/devworkspaces/"
WORKSPACE_SERVICE_ENDPOINT = "/api/v1/namespaces/test/services/https:test-workspace-service:4444/proxy/"
TOKEN = "test-token"
NAMESPACE = "test"
WORKSPACE_ID = "test-workspace"
TERMINAL_ID = "test-terminal"
CONTAINER_NAME = "test-container"
)

func TestSetupPod(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, WORKSPACE_SERVICE_ENDPOINT+"exec/init", r.URL.Path)
assert.Equal(t, "Bearer "+TOKEN, r.Header.Get("Authorization"))
assert.Equal(t, TOKEN, r.Header.Get("X-Forwarded-Access-Token"))

w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"pod": "test-pod"}`))
}))
defer server.Close()

connectionData := ConnectionData{
Link: strings.ReplaceAll(server.URL, "https://", ""),
Namespace: NAMESPACE,
WorkspaceID: WORKSPACE_ID,
Token: TOKEN,
}

config := Config{
Container: CONTAINER_NAME,
}

pod, err := SetupUserPod(connectionData, &config)
assert.NoError(t, err)
assert.Equal(t, "test-pod", pod)
}

func TestGetUserName(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
assert.Equal(t, USERS_ENDPOINT, r.URL.Path)
assert.Equal(t, "Bearer "+TOKEN, r.Header.Get("Authorization"))

w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"metadata": {"name": "test-user"}}`))
}))
defer server.Close()

connectionData := ConnectionData{
Link: strings.ReplaceAll(server.URL, "https://", ""),
Token: TOKEN,
}

name, err := GetUserName(connectionData)
assert.NoError(t, err)
assert.Equal(t, "test-user", name)
}

func TestCleanAfterDisconnect(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodDelete, r.Method)
assert.Equal(t, WORKSPACE_ENDPOINT+TERMINAL_ID, r.URL.Path)
assert.Equal(t, "Bearer "+TOKEN, r.Header.Get("Authorization"))

w.WriteHeader(http.StatusOK)
}))
defer server.Close()

connectionData := ConnectionData{
Link: strings.ReplaceAll(server.URL, "https://", ""),
TerminalID: TERMINAL_ID,
Namespace: NAMESPACE,
Token: TOKEN,
}
var buffer bytes.Buffer
log.SetOutput(&buffer)
CleanAfterDisconnect(connectionData)
assert.Empty(t, buffer.String())
}

func TestSendActivityTick(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, WORKSPACE_SERVICE_ENDPOINT+"activity/tick", r.URL.Path)
assert.Equal(t, "Bearer "+TOKEN, r.Header.Get("Authorization"))
assert.Equal(t, TOKEN, r.Header.Get("X-Forwarded-Access-Token"))

w.WriteHeader(http.StatusOK)
}))
defer server.Close()

connectionData := ConnectionData{
Link: strings.ReplaceAll(server.URL, "https://", ""),
Namespace: NAMESPACE,
WorkspaceID: WORKSPACE_ID,
Token: TOKEN,
}

err := SendActivityTick(connectionData)
assert.NoError(t, err)
}

func TestSendPostRequest(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, "/api/test", r.URL.Path)
assert.Equal(t, "test-token", r.Header.Get("Authorization"))
w.Write([]byte(`{"output": "ok"}`))
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
url := server.URL + "/api/test"
payload := []byte(`{"test": "test"}`)
authorizationToken := "test-token"
body, statusCode, err := SendPostRequest(url, payload, authorizationToken)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, statusCode)
assert.Equal(t, `{"output": "ok"}`, string(body))
}

func TestSendGetRequest(t *testing.T) {
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
assert.Equal(t, "/api/test", r.URL.Path)
assert.Equal(t, "test-token", r.Header.Get("Authorization"))
w.Write([]byte(`{"output": "ok"}`))
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
url := server.URL + "/api/test"
authorizationToken := "test-token"
body, statusCode, err := SendGetRequest(url, authorizationToken)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, statusCode)
assert.Equal(t, `{"output": "ok"}`, string(body))
}

0 comments on commit 057b28f

Please sign in to comment.