2023-10-09 18:04:35 -05:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/coder/coder/v2/cli/clitest"
|
|
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
|
|
|
"github.com/coder/coder/v2/codersdk/agentsdk"
|
|
|
|
|
"github.com/coder/coder/v2/pty/ptytest"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestExternalAuth(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
t.Run("CanceledWithURL", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{
|
|
|
|
|
URL: "https://github.com",
|
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
url := srv.URL
|
2024-03-28 13:59:03 -05:00
|
|
|
inv, _ := clitest.New(t, "--agent-url", url, "--agent-token", "foo", "external-auth", "access-token", "github")
|
2023-10-09 18:04:35 -05:00
|
|
|
pty := ptytest.New(t)
|
|
|
|
|
inv.Stdout = pty.Output()
|
|
|
|
|
waiter := clitest.StartWithWaiter(t, inv)
|
|
|
|
|
pty.ExpectMatch("https://github.com")
|
2025-03-26 01:56:39 -05:00
|
|
|
waiter.RequireIs(cliui.ErrCanceled)
|
2023-10-09 18:04:35 -05:00
|
|
|
})
|
|
|
|
|
t.Run("SuccessWithToken", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{
|
|
|
|
|
AccessToken: "bananas",
|
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
url := srv.URL
|
2024-03-28 13:59:03 -05:00
|
|
|
inv, _ := clitest.New(t, "--agent-url", url, "--agent-token", "foo", "external-auth", "access-token", "github")
|
2023-10-09 18:04:35 -05:00
|
|
|
pty := ptytest.New(t)
|
|
|
|
|
inv.Stdout = pty.Output()
|
|
|
|
|
clitest.Start(t, inv)
|
|
|
|
|
pty.ExpectMatch("bananas")
|
|
|
|
|
})
|
2024-02-15 15:17:16 -06:00
|
|
|
t.Run("NoArgs", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{
|
|
|
|
|
AccessToken: "bananas",
|
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
url := srv.URL
|
2024-03-28 13:59:03 -05:00
|
|
|
inv, _ := clitest.New(t, "--agent-url", url, "--agent-token", "foo", "external-auth", "access-token")
|
2024-02-15 15:17:16 -06:00
|
|
|
watier := clitest.StartWithWaiter(t, inv)
|
|
|
|
|
watier.RequireContains("wanted 1 args but got 0")
|
|
|
|
|
})
|
2023-10-09 23:02:16 -05:00
|
|
|
t.Run("SuccessWithExtra", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{
|
|
|
|
|
AccessToken: "bananas",
|
|
|
|
|
TokenExtra: map[string]interface{}{
|
|
|
|
|
"hey": "there",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
url := srv.URL
|
2024-03-28 13:59:03 -05:00
|
|
|
inv, _ := clitest.New(t, "--agent-url", url, "--agent-token", "foo", "external-auth", "access-token", "github", "--extra", "hey")
|
2023-10-09 23:02:16 -05:00
|
|
|
pty := ptytest.New(t)
|
|
|
|
|
inv.Stdout = pty.Output()
|
|
|
|
|
clitest.Start(t, inv)
|
|
|
|
|
pty.ExpectMatch("there")
|
|
|
|
|
})
|
2023-10-09 18:04:35 -05:00
|
|
|
}
|