2022-06-04 20:13:37 +00:00
|
|
|
package coderd_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2022-09-13 17:31:33 +00:00
|
|
|
"net/url"
|
2022-06-04 20:13:37 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-03-07 21:10:01 +00:00
|
|
|
"github.com/coder/coder/cli/clibase"
|
2022-06-04 20:13:37 +00:00
|
|
|
"github.com/coder/coder/coderd/coderdtest"
|
2022-10-23 18:21:49 +00:00
|
|
|
"github.com/coder/coder/coderd/httpmw"
|
2023-04-06 21:35:27 +00:00
|
|
|
"github.com/coder/coder/coderd/workspaceapps/apptest"
|
2022-08-09 17:17:00 +00:00
|
|
|
"github.com/coder/coder/testutil"
|
2022-06-04 20:13:37 +00:00
|
|
|
)
|
|
|
|
|
2022-09-22 22:30:32 +00:00
|
|
|
func TestGetAppHost(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-12-15 18:43:00 +00:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
accessURL string
|
|
|
|
appHostname string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OK",
|
|
|
|
accessURL: "https://test.coder.com",
|
|
|
|
appHostname: "*.test.coder.com",
|
|
|
|
expected: "*.test.coder.com",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "None",
|
|
|
|
accessURL: "https://test.coder.com",
|
|
|
|
appHostname: "",
|
|
|
|
expected: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OKWithPort",
|
|
|
|
accessURL: "https://test.coder.com:8443",
|
|
|
|
appHostname: "*.test.coder.com",
|
|
|
|
expected: "*.test.coder.com:8443",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OKWithSuffix",
|
|
|
|
accessURL: "https://test.coder.com:8443",
|
|
|
|
appHostname: "*--suffix.test.coder.com",
|
|
|
|
expected: "*--suffix.test.coder.com:8443",
|
|
|
|
},
|
|
|
|
}
|
2022-09-22 22:30:32 +00:00
|
|
|
for _, c := range cases {
|
|
|
|
c := c
|
2022-12-15 18:43:00 +00:00
|
|
|
t.Run(c.name, func(t *testing.T) {
|
2022-09-22 22:30:32 +00:00
|
|
|
t.Parallel()
|
2022-12-15 18:43:00 +00:00
|
|
|
|
|
|
|
accessURL, err := url.Parse(c.accessURL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-22 22:30:32 +00:00
|
|
|
client := coderdtest.New(t, &coderdtest.Options{
|
2022-12-15 18:43:00 +00:00
|
|
|
AccessURL: accessURL,
|
|
|
|
AppHostname: c.appHostname,
|
2022-09-22 22:30:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Should not leak to unauthenticated users.
|
2023-01-29 21:47:24 +00:00
|
|
|
host, err := client.AppHost(ctx)
|
2022-09-22 22:30:32 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "", host.Host)
|
|
|
|
|
|
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
2023-01-29 21:47:24 +00:00
|
|
|
host, err = client.AppHost(ctx)
|
2022-09-22 22:30:32 +00:00
|
|
|
require.NoError(t, err)
|
2022-12-15 18:43:00 +00:00
|
|
|
require.Equal(t, c.expected, host.Host)
|
2022-09-22 22:30:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:35:27 +00:00
|
|
|
func TestWorkspaceApps(t *testing.T) {
|
2022-09-13 17:31:33 +00:00
|
|
|
t.Parallel()
|
2023-01-18 22:56:14 +00:00
|
|
|
|
2023-04-06 21:35:27 +00:00
|
|
|
apptest.Run(t, func(t *testing.T, opts *apptest.DeploymentOptions) *apptest.Deployment {
|
2023-03-07 21:10:01 +00:00
|
|
|
deploymentValues := coderdtest.DeploymentValues(t)
|
2023-04-06 21:35:27 +00:00
|
|
|
deploymentValues.DisablePathApps = clibase.Bool(opts.DisablePathApps)
|
|
|
|
deploymentValues.Dangerous.AllowPathAppSharing = clibase.Bool(opts.DangerousAllowPathAppSharing)
|
|
|
|
deploymentValues.Dangerous.AllowPathAppSiteOwnerAccess = clibase.Bool(opts.DangerousAllowPathAppSiteOwnerAccess)
|
2022-12-07 12:55:02 +00:00
|
|
|
|
|
|
|
client := coderdtest.New(t, &coderdtest.Options{
|
2023-04-06 21:35:27 +00:00
|
|
|
DeploymentValues: deploymentValues,
|
|
|
|
AppHostname: opts.AppHost,
|
|
|
|
IncludeProvisionerDaemon: true,
|
2022-12-07 12:55:02 +00:00
|
|
|
RealIPConfig: &httpmw.RealIPConfig{
|
|
|
|
TrustedOrigins: []*net.IPNet{{
|
|
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
|
|
Mask: net.CIDRMask(8, 32),
|
|
|
|
}},
|
|
|
|
TrustedHeaders: []string{
|
|
|
|
"CF-Connecting-IP",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
user := coderdtest.CreateFirstUser(t, client)
|
|
|
|
|
2023-04-06 21:35:27 +00:00
|
|
|
return &apptest.Deployment{
|
|
|
|
Options: opts,
|
|
|
|
Client: client,
|
|
|
|
FirstUser: user,
|
|
|
|
PathAppBaseURL: client.URL,
|
2022-12-07 12:55:02 +00:00
|
|
|
}
|
2023-01-18 22:56:14 +00:00
|
|
|
})
|
|
|
|
}
|