(t *testing.T)
| 1470 | } |
| 1471 | |
| 1472 | func TestCreateWorkspaceExternalAuth(t *testing.T) { |
| 1473 | t.Parallel() |
| 1474 | |
| 1475 | // The expected 403 message returned by createWorkspace when the workspace |
| 1476 | // owner is missing required external auth. |
| 1477 | const externalAuthRequiredMessage = "External authentication is required to create a workspace with this template." |
| 1478 | |
| 1479 | // externalAuthVersion returns echo responses for a template version whose |
| 1480 | // graph references the given external auth providers. |
| 1481 | externalAuthVersion := func(providers ...*proto.ExternalAuthProviderResource) *echo.Responses { |
| 1482 | return &echo.Responses{ |
| 1483 | Parse: echo.ParseComplete, |
| 1484 | ProvisionGraph: []*proto.Response{{ |
| 1485 | Type: &proto.Response_Graph{ |
| 1486 | Graph: &proto.GraphComplete{ |
| 1487 | ExternalAuthProviders: providers, |
| 1488 | }, |
| 1489 | }, |
| 1490 | }}, |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | t.Run("RequiredAuthMissing", func(t *testing.T) { |
| 1495 | t.Parallel() |
| 1496 | client := coderdtest.New(t, &coderdtest.Options{ |
| 1497 | IncludeProvisionerDaemon: true, |
| 1498 | ExternalAuthConfigs: []*externalauth.Config{{ |
| 1499 | InstrumentedOAuth2Config: &testutil.OAuth2Config{}, |
| 1500 | ID: "github", |
| 1501 | Regex: regexp.MustCompile(`github\.com`), |
| 1502 | Type: codersdk.EnhancedExternalAuthProviderGitHub.String(), |
| 1503 | DisplayName: "GitHub", |
| 1504 | }}, |
| 1505 | }) |
| 1506 | first := coderdtest.CreateFirstUser(t, client) |
| 1507 | version := coderdtest.CreateTemplateVersion(t, client, first.OrganizationID, |
| 1508 | externalAuthVersion(&proto.ExternalAuthProviderResource{Id: "github"})) |
| 1509 | coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) |
| 1510 | template := coderdtest.CreateTemplate(t, client, first.OrganizationID, version.ID) |
| 1511 | memberClient, member := coderdtest.CreateAnotherUser(t, client, first.OrganizationID) |
| 1512 | |
| 1513 | ctx := testutil.Context(t, testutil.WaitLong) |
| 1514 | |
| 1515 | req := codersdk.CreateWorkspaceRequest{ |
| 1516 | TemplateID: template.ID, |
| 1517 | Name: coderdtest.RandomUsername(t), |
| 1518 | } |
| 1519 | _, err := memberClient.CreateUserWorkspace(ctx, codersdk.Me, req) |
| 1520 | var apiErr *codersdk.Error |
| 1521 | require.ErrorAs(t, err, &apiErr) |
| 1522 | require.Equal(t, http.StatusForbidden, apiErr.StatusCode()) |
| 1523 | require.Equal(t, externalAuthRequiredMessage, apiErr.Message) |
| 1524 | require.Equal(t, "The workspace owner must authenticate with the following external auth providers: GitHub.", apiErr.Detail) |
| 1525 | require.Equal(t, []codersdk.ValidationError{{ |
| 1526 | Field: "external_auth", |
| 1527 | Detail: "github", |
| 1528 | }}, apiErr.Validations) |
| 1529 |
nothing calls this directly
no test coverage detected