2023-10-09 18:04:35 -05:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
|
|
import (
|
2026-07-07 12:38:37 -07:00
|
|
|
"bytes"
|
2023-10-09 18:04:35 -05:00
|
|
|
"context"
|
2026-07-07 12:38:37 -07:00
|
|
|
"encoding/json"
|
2023-10-09 18:04:35 -05:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
2026-07-07 12:38:37 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-10-09 18:04:35 -05:00
|
|
|
|
|
|
|
|
"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"
|
2026-06-03 15:16:42 -04:00
|
|
|
"github.com/coder/coder/v2/testutil"
|
|
|
|
|
"github.com/coder/coder/v2/testutil/expecter"
|
2023-10-09 18:04:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestExternalAuth(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
t.Run("CanceledWithURL", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2026-06-03 15:16:42 -04:00
|
|
|
ctx := testutil.Context(t, testutil.WaitMedium)
|
2023-10-09 18:04:35 -05:00
|
|
|
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")
|
2026-06-03 15:16:42 -04:00
|
|
|
stdout := expecter.NewAttachedToInvocation(t, inv)
|
2023-10-09 18:04:35 -05:00
|
|
|
waiter := clitest.StartWithWaiter(t, inv)
|
2026-06-03 15:30:37 -04:00
|
|
|
stdout.ExpectMatch(ctx, "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()
|
2026-06-03 15:16:42 -04:00
|
|
|
ctx := testutil.Context(t, testutil.WaitMedium)
|
2023-10-09 18:04:35 -05:00
|
|
|
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")
|
2026-06-03 15:16:42 -04:00
|
|
|
stdout := expecter.NewAttachedToInvocation(t, inv)
|
2023-10-09 18:04:35 -05:00
|
|
|
clitest.Start(t, inv)
|
2026-06-03 15:30:37 -04:00
|
|
|
stdout.ExpectMatch(ctx, "bananas")
|
2023-10-09 18:04:35 -05:00
|
|
|
})
|
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()
|
2026-06-03 15:16:42 -04:00
|
|
|
ctx := testutil.Context(t, testutil.WaitMedium)
|
2023-10-09 23:02:16 -05:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{
|
|
|
|
|
AccessToken: "bananas",
|
2026-07-07 12:38:37 -07:00
|
|
|
TokenExtra: map[string]any{
|
2023-10-09 23:02:16 -05:00
|
|
|
"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")
|
2026-06-03 15:16:42 -04:00
|
|
|
stdout := expecter.NewAttachedToInvocation(t, inv)
|
2023-10-09 23:02:16 -05:00
|
|
|
clitest.Start(t, inv)
|
2026-06-03 15:30:37 -04:00
|
|
|
stdout.ExpectMatch(ctx, "there")
|
2023-10-09 23:02:16 -05:00
|
|
|
})
|
2026-07-07 12:38:37 -07:00
|
|
|
t.Run("JSONOutput", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
expiry := time.Now().Add(8 * time.Hour).UTC().Truncate(time.Second)
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
resp agentsdk.ExternalAuthResponse
|
|
|
|
|
wantErr error
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "WithExpiry",
|
|
|
|
|
resp: agentsdk.ExternalAuthResponse{AccessToken: "bananas", ExpiresAt: expiry},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "WithURL",
|
|
|
|
|
resp: agentsdk.ExternalAuthResponse{URL: "https://github.com/login"},
|
|
|
|
|
wantErr: cliui.ErrCanceled,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, 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, tt.resp)
|
|
|
|
|
}))
|
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
inv, _ := clitest.New(t, "--agent-url", srv.URL, "--agent-token", "foo", "external-auth", "access-token", "github", "--output", "json")
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
inv.Stdout = buf
|
|
|
|
|
waiter := clitest.StartWithWaiter(t, inv)
|
|
|
|
|
if tt.wantErr != nil {
|
|
|
|
|
waiter.RequireIs(tt.wantErr)
|
|
|
|
|
} else {
|
|
|
|
|
waiter.RequireSuccess()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var resp agentsdk.ExternalAuthResponse
|
|
|
|
|
require.NoError(t, json.Unmarshal(buf.Bytes(), &resp))
|
|
|
|
|
require.Equal(t, tt.resp.AccessToken, resp.AccessToken)
|
|
|
|
|
require.Equal(t, tt.resp.URL, resp.URL)
|
|
|
|
|
require.Equal(t, tt.resp.ExpiresAt.UTC(), resp.ExpiresAt.UTC())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-10-09 18:04:35 -05:00
|
|
|
}
|