diff options
| author | ishaan <contact@ishaan.cc> | 2026-07-19 14:21:13 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-07-19 14:21:13 +0530 |
| commit | d6449a232e6bd9eb46132c7403a05ac1a756b619 (patch) | |
| tree | a88c144f54557306caf69ed739de15857fc898d4 /internal/utils/utils_test.go | |
| parent | 8a5de371fccb89635bc1a624d8b1aca52aa18da4 (diff) | |
feat: discover SSH keys from remote services (eg. bob@github fetches bob's keys from GitHub)
Diffstat (limited to 'internal/utils/utils_test.go')
| -rw-r--r-- | internal/utils/utils_test.go | 91 |
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") +} + + |