aboutsummaryrefslogtreecommitdiff
path: root/internal/utils/utils_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/utils/utils_test.go')
-rw-r--r--internal/utils/utils_test.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go
index 6c467b7..34631df 100644
--- a/internal/utils/utils_test.go
+++ b/internal/utils/utils_test.go
@@ -3,6 +3,9 @@ package utils
import (
"crypto/ed25519"
"crypto/rand"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
"os"
"path/filepath"
"strings"
@@ -10,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "gitlab.com/whiteboardguy/eno/internal/config"
"gitlab.com/whiteboardguy/eno/internal/crypt"
"golang.org/x/crypto/ssh"
)
@@ -159,3 +163,90 @@ func TestRandomLettersNumbers_Zero(t *testing.T) {
assert.Empty(t, result)
}
+func TestPubkeyServiceURL_Custom(t *testing.T) {
+ old := config.Conf.PubkeyServices
+ config.Conf.PubkeyServices = map[string]string{"fagi": "https://git.fagi.de/%s.keys"}
+ defer func() { config.Conf.PubkeyServices = old }()
+
+ url, err := pubkeyServiceURL("johndoe", "fagi")
+ require.NoError(t, err)
+ assert.Equal(t, "https://git.fagi.de/johndoe.keys", url)
+}
+
+func TestPubkeyServiceURL_CustomOverridesBuiltin(t *testing.T) {
+ old := config.Conf.PubkeyServices
+ config.Conf.PubkeyServices = map[string]string{"github": "https://custom-gh.example.com/%s.keys"}
+ defer func() { config.Conf.PubkeyServices = old }()
+
+ url, err := pubkeyServiceURL("alice", "github")
+ require.NoError(t, err)
+ assert.Equal(t, "https://custom-gh.example.com/alice.keys", url)
+}
+
+func TestPubkeyServiceURL_Builtin_GitHub(t *testing.T) {
+ url, err := pubkeyServiceURL("alice", "github")
+ require.NoError(t, err)
+ assert.Equal(t, "https://github.com/alice.keys", url)
+}
+
+func TestPubkeyServiceURL_Builtin_GitLab(t *testing.T) {
+ url, err := pubkeyServiceURL("bob", "gitlab")
+ require.NoError(t, err)
+ assert.Equal(t, "https://gitlab.com/bob.keys", url)
+}
+
+func TestPubkeyServiceURL_Builtin_Codeberg(t *testing.T) {
+ url, err := pubkeyServiceURL("charlie", "codeberg")
+ require.NoError(t, err)
+ assert.Equal(t, "https://codeberg.org/charlie.keys", url)
+}
+
+func TestPubkeyServiceURL_Unknown(t *testing.T) {
+ _, err := pubkeyServiceURL("alice", "nonexistent")
+ assert.Error(t, err)
+ assert.Contains(t, err.Error(), "unknown service")
+}
+
+func TestPubkeyServiceURL_EmptyUsername(t *testing.T) {
+ url, err := pubkeyServiceURL("", "github")
+ require.NoError(t, err)
+ assert.Equal(t, "https://github.com/.keys", url)
+}
+
+func TestGetPubKeyFromService_Success(t *testing.T) {
+ pub, _, err := ed25519.GenerateKey(rand.Reader)
+ require.NoError(t, err)
+ sshPub, err := ssh.NewPublicKey(pub)
+ require.NoError(t, err)
+ pubKeyLine := string(ssh.MarshalAuthorizedKey(sshPub))
+
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ fmt.Fprint(w, pubKeyLine)
+ }))
+ defer srv.Close()
+
+ old := config.Conf.PubkeyServices
+ config.Conf.PubkeyServices = map[string]string{"test": srv.URL + "/%s.keys"}
+ defer func() { config.Conf.PubkeyServices = old }()
+
+ body, err := GetPubKeyFromService("user", "test")
+ require.NoError(t, err)
+ assert.Equal(t, pubKeyLine, string(body))
+}
+
+func TestGetPubKeyFromService_404(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusNotFound)
+ }))
+ defer srv.Close()
+
+ old := config.Conf.PubkeyServices
+ config.Conf.PubkeyServices = map[string]string{"test": srv.URL + "/%s.keys"}
+ defer func() { config.Conf.PubkeyServices = old }()
+
+ _, err := GetPubKeyFromService("user", "test")
+ assert.Error(t, err)
+ assert.Contains(t, err.Error(), "returned status 404")
+}
+
+