Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions test/suites/connector/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package connector

import (
"net/http"
"testing"

adminapi "github.com/trebent/kerberos/test/client/admin"
lib "github.com/trebent/kerberos/test/lib"
)

func loginAndGetSessionCookie(t *testing.T) *http.Cookie {
t.Helper()
loginResponse, err := lib.AdminClient.LoginWithResponse(
t.Context(),
adminapi.LoginJSONRequestBody{Username: adminUser, Password: adminUserPassword},
)
lib.CheckErr(err, t)
lib.VerifyStatusCode(loginResponse.StatusCode(), http.StatusNoContent, t)
sessionCookie, err := lib.ExtractSessionCookie(loginResponse.HTTPResponse)
lib.CheckErr(err, t)
return sessionCookie
}
6 changes: 6 additions & 0 deletions test/suites/connector/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package connector

const (
adminUser = "connector-admin"
adminUserPassword = "connector-admin-password"
)
25 changes: 13 additions & 12 deletions test/suites/connector/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connector

import (
"fmt"
lib "github.com/trebent/kerberos/test/lib"
"net/http"
"net/url"
"testing"
Expand All @@ -18,12 +19,12 @@ func TestCORS(t *testing.T) {

t.Run("allowed origin, OPTIONS preflight", func(t *testing.T) {
t.Parallel()
url := fmt.Sprintf("http://%s:%d", getHost(), getConnectorPort())
resp := options(url, t, http.Header{"Origin": []string{"https://admin.trebent.test:30001"}})
url := fmt.Sprintf("http://%s:%d", lib.GetHost(), lib.GetConnectorPort())
resp := lib.Options(url, t, http.Header{"Origin": []string{"https://admin.trebent.test:30001"}})
defer resp.Body.Close()
verifyStatusCode(resp.StatusCode, http.StatusNoContent, t)
verifyHeader(resp.Header, "Access-Control-Allow-Origin", "https://admin.trebent.test:30001", t)
verifyHeader(resp.Header, "Access-Control-Allow-Credentials", "true", t)
lib.VerifyStatusCode(resp.StatusCode, http.StatusNoContent, t)
lib.VerifyHeader(resp.Header, "Access-Control-Allow-Origin", "https://admin.trebent.test:30001", t)
lib.VerifyHeader(resp.Header, "Access-Control-Allow-Credentials", "true", t)
if resp.Header.Get("Access-Control-Allow-Methods") == "" {
t.Fatal("Expected Access-Control-Allow-Methods to be set")
}
Expand All @@ -38,7 +39,7 @@ func testCORS(t *testing.T, origin string, expectCORSHeaders bool) {
Method: "GET",
URL: &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", getHost(), getConnectorPort()),
Host: fmt.Sprintf("%s:%d", lib.GetHost(), lib.GetConnectorPort()),
},
Header: make(http.Header),
}
Expand All @@ -48,15 +49,15 @@ func testCORS(t *testing.T, origin string, expectCORSHeaders bool) {
}

response, err := httpClient.Do(req)
checkErr(err, t)
lib.CheckErr(err, t)
defer response.Body.Close()

if expectCORSHeaders {
verifyStatusCode(response.StatusCode, http.StatusOK, t)
verifyHeader(response.Header, "Access-Control-Allow-Origin", origin, t)
verifyHeader(response.Header, "Access-Control-Allow-Credentials", "true", t)
lib.VerifyStatusCode(response.StatusCode, http.StatusOK, t)
lib.VerifyHeader(response.Header, "Access-Control-Allow-Origin", origin, t)
lib.VerifyHeader(response.Header, "Access-Control-Allow-Credentials", "true", t)
} else {
verifyStatusCode(response.StatusCode, http.StatusOK, t)
verifyHeaderMissing(response.Header, "Access-Control-Allow-Origin", t)
lib.VerifyStatusCode(response.StatusCode, http.StatusOK, t)
lib.VerifyHeaderMissing(response.Header, "Access-Control-Allow-Origin", t)
}
}
157 changes: 0 additions & 157 deletions test/suites/connector/lib.go

This file was deleted.

13 changes: 7 additions & 6 deletions test/suites/connector/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connector

import (
"context"
lib "github.com/trebent/kerberos/test/lib"
"net/http"
"os"
"testing"
Expand All @@ -12,10 +13,10 @@ import (
func TestMain(m *testing.M) {
println("Running TestMain, setting up test foundation...")

loginResp, err := adminClient.LoginSuperuserWithResponse(
loginResp, err := lib.AdminClient.LoginSuperuserWithResponse(
context.Background(), adminapi.LoginSuperuserJSONRequestBody{
ClientId: superUserClientID,
ClientSecret: superUserClientSecret,
ClientId: lib.SuperUserClientID,
ClientSecret: lib.SuperUserClientSecret,
},
)
if err != nil {
Expand All @@ -24,13 +25,13 @@ func TestMain(m *testing.M) {
if loginResp.StatusCode() != http.StatusNoContent {
panic("superuser login response did not indicate success: " + loginResp.Status())
}
cookie, err := extractSessionCookie(loginResp.HTTPResponse)
cookie, err := lib.ExtractSessionCookie(loginResp.HTTPResponse)
if err != nil {
panic(err)
}
requestEditorSuper := makeRequestEditorFromCookie(cookie)
requestEditorSuper := lib.MakeRequestEditorFromCookie(cookie)

createAdminUserResp, err := adminClient.CreateUserWithResponse(
createAdminUserResp, err := lib.AdminClient.CreateUserWithResponse(
context.Background(),
adminapi.CreateUserJSONRequestBody{
Username: adminUser,
Expand Down
19 changes: 10 additions & 9 deletions test/suites/connector/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connector

import (
"fmt"
lib "github.com/trebent/kerberos/test/lib"
"net/http"
"net/url"
"testing"
Expand All @@ -15,16 +16,16 @@ func TestSession(t *testing.T) {
Method: "GET",
URL: &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", getHost(), getConnectorPort()),
Host: fmt.Sprintf("%s:%d", lib.GetHost(), lib.GetConnectorPort()),
},
Header: make(http.Header),
}

response, err := httpClient.Do(req)
checkErr(err, t)
lib.CheckErr(err, t)
defer response.Body.Close()

verifyStatusCode(response.StatusCode, http.StatusUnauthorized, t)
lib.VerifyStatusCode(response.StatusCode, http.StatusUnauthorized, t)
})

t.Run("invalid session cookie, unauthorized", func(t *testing.T) {
Expand All @@ -34,7 +35,7 @@ func TestSession(t *testing.T) {
Method: "GET",
URL: &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", getHost(), getConnectorPort()),
Host: fmt.Sprintf("%s:%d", lib.GetHost(), lib.GetConnectorPort()),
},
Header: make(http.Header),
}
Expand All @@ -44,10 +45,10 @@ func TestSession(t *testing.T) {
})

response, err := httpClient.Do(req)
checkErr(err, t)
lib.CheckErr(err, t)
defer response.Body.Close()

verifyStatusCode(response.StatusCode, http.StatusUnauthorized, t)
lib.VerifyStatusCode(response.StatusCode, http.StatusUnauthorized, t)
})

t.Run("session cookie is valid", func(t *testing.T) {
Expand All @@ -62,16 +63,16 @@ func TestSession(t *testing.T) {
Method: "GET",
URL: &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", getHost(), getConnectorPort()),
Host: fmt.Sprintf("%s:%d", lib.GetHost(), lib.GetConnectorPort()),
},
Header: make(http.Header),
}
req.AddCookie(cookie)

response, err := httpClient.Do(req)
checkErr(err, t)
lib.CheckErr(err, t)
defer response.Body.Close()

verifyStatusCode(response.StatusCode, http.StatusOK, t)
lib.VerifyStatusCode(response.StatusCode, http.StatusOK, t)
})
}
9 changes: 5 additions & 4 deletions test/suites/connector/whitelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connector

import (
"fmt"
lib "github.com/trebent/kerberos/test/lib"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -33,7 +34,7 @@ func testWhitelist(t *testing.T, origin string, expectAllowed bool) {
Method: http.MethodGet,
URL: &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", getHost(), getConnectorPort()),
Host: fmt.Sprintf("%s:%d", lib.GetHost(), lib.GetConnectorPort()),
},
Header: make(http.Header),
}
Expand All @@ -43,12 +44,12 @@ func testWhitelist(t *testing.T, origin string, expectAllowed bool) {
}

response, err := httpClient.Do(req)
checkErr(err, t)
lib.CheckErr(err, t)
_ = response.Body.Close()

if expectAllowed {
verifyStatusCode(response.StatusCode, http.StatusOK, t)
lib.VerifyStatusCode(response.StatusCode, http.StatusOK, t)
} else {
verifyStatusCode(response.StatusCode, http.StatusForbidden, t)
lib.VerifyStatusCode(response.StatusCode, http.StatusForbidden, t)
}
}
2 changes: 1 addition & 1 deletion test/suites/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/trebent/kerberos/test

go 1.26.5
go 1.26.6

require (
github.com/jaegertracing/jaeger-idl v0.9.0
Expand Down
Loading