Skip to content

feat: add server flag to disable user custom quiet hours #11124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Merge branch 'main' into dean/disable-user-custom-quiet-hours
  • Loading branch information
deansheather committed Dec 15, 2023
commit 79574464127bd5da3fdaab1ba6e012f3d3a892ff
2 changes: 1 addition & 1 deletion cli/testdata/coder_server_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ workspaces stopping during the day due to template max TTL.
false, users can't change their quiet hours schedule and the site
default is always used.

--default-quiet-hours-schedule string, $CODER_QUIET_HOURS_DEFAULT_SCHEDULE
--default-quiet-hours-schedule string, $CODER_QUIET_HOURS_DEFAULT_SCHEDULE (default: CRON_TZ=UTC 0 0 * * *)
The default daily cron schedule applied to users that haven't set a
custom quiet hours schedule themselves. The quiet hours schedule
determines when workspaces will be force stopped due to the template's
Expand Down
14 changes: 7 additions & 7 deletions cli/testdata/server-config.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,13 @@ wgtunnelHost: ""
userQuietHoursSchedule:
# The default daily cron schedule applied to users that haven't set a custom quiet
# hours schedule themselves. The quiet hours schedule determines when workspaces
# will be force stopped due to the template's max TTL, and will round the max TTL
# up to be within the user's quiet hours window (or default). The format is the
# same as the standard cron format, but the day-of-month, month and day-of-week
# must be *. Only one hour and minute can be specified (ranges or comma separated
# values are not supported).
# (default: <unset>, type: string)
defaultQuietHoursSchedule: ""
# will be force stopped due to the template's autostop requirement, and will round
# the max deadline up to be within the user's quiet hours window (or default). The
# format is the same as the standard cron format, but the day-of-month, month and
# day-of-week must be *. Only one hour and minute can be specified (ranges or
# comma separated values are not supported).
# (default: CRON_TZ=UTC 0 0 * * *, type: string)
defaultQuietHoursSchedule: CRON_TZ=UTC 0 0 * * *
# Allow users to set their own quiet hours schedule for workspaces to stop in
# (depending on template autostop requirement settings). If false, users can't
# change their quiet hours schedule and the site default is always used.
Expand Down
2 changes: 1 addition & 1 deletion enterprise/cli/testdata/coder_server_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ workspaces stopping during the day due to template max TTL.
false, users can't change their quiet hours schedule and the site
default is always used.

--default-quiet-hours-schedule string, $CODER_QUIET_HOURS_DEFAULT_SCHEDULE
--default-quiet-hours-schedule string, $CODER_QUIET_HOURS_DEFAULT_SCHEDULE (default: CRON_TZ=UTC 0 0 * * *)
The default daily cron schedule applied to users that haven't set a
custom quiet hours schedule themselves. The quiet hours schedule
determines when workspaces will be force stopped due to the template's
Expand Down
2 changes: 0 additions & 2 deletions enterprise/coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,6 @@ func (api *API) updateEntitlements(ctx context.Context) error {
api.Logger.Warn(ctx, "template autostop requirement will default to UTC midnight as the default user quiet hours schedule. Set a custom default quiet hours schedule using CODER_QUIET_HOURS_DEFAULT_SCHEDULE to avoid this warning")
api.DefaultQuietHoursSchedule = "CRON_TZ=UTC 0 0 * * *"
}
enterpriseTemplateStore.UseAutostopRequirement.Store(true)

quietHoursStore, err := schedule.NewEnterpriseUserQuietHoursScheduleStore(api.DefaultQuietHoursSchedule, api.DeploymentValues.UserQuietHoursSchedule.AllowUserCustom.Value())
if err != nil {
api.Logger.Error(ctx, "unable to set up enterprise user quiet hours schedule store, template autostop requirements will not be applied to workspace builds", slog.Error(err))
Expand Down
61 changes: 1 addition & 60 deletions enterprise/coderd/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,68 +177,10 @@ func TestUserQuietHours(t *testing.T) {
require.Equal(t, http.StatusForbidden, sdkErr.StatusCode())
})

t.Run("NotEnabled", func(t *testing.T) {
t.Parallel()

dv := coderdtest.DeploymentValues(t)
dv.UserQuietHoursSchedule.DefaultSchedule.Set("")
dv.Experiments.Set(string(codersdk.ExperimentTemplateAutostopRequirement))

client, user := coderdenttest.New(t, &coderdenttest.Options{
NoDefaultQuietHoursSchedule: true,
Options: &coderdtest.Options{
DeploymentValues: dv,
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAdvancedTemplateScheduling: 1,
codersdk.FeatureTemplateAutostopRequirement: 1,
},
},
})

ctx := testutil.Context(t, testutil.WaitLong)
//nolint:gocritic // We want to test the lack of feature, not RBAC.
_, err := client.UserQuietHoursSchedule(ctx, user.UserID.String())
require.Error(t, err)
var sdkErr *codersdk.Error
require.ErrorAs(t, err, &sdkErr)
require.Equal(t, http.StatusForbidden, sdkErr.StatusCode())
})

t.Run("NoFeatureFlag", func(t *testing.T) {
t.Parallel()

dv := coderdtest.DeploymentValues(t)
dv.UserQuietHoursSchedule.DefaultSchedule.Set("CRON_TZ=America/Chicago 0 0 * * *")
dv.UserQuietHoursSchedule.DefaultSchedule.Set("")

client, user := coderdenttest.New(t, &coderdenttest.Options{
Options: &coderdtest.Options{
DeploymentValues: dv,
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAdvancedTemplateScheduling: 1,
codersdk.FeatureTemplateAutostopRequirement: 1,
},
},
})

ctx := testutil.Context(t, testutil.WaitLong)
//nolint:gocritic // We want to test the lack of feature, not RBAC.
_, err := client.UserQuietHoursSchedule(ctx, user.UserID.String())
require.Error(t, err)
var sdkErr *codersdk.Error
require.ErrorAs(t, err, &sdkErr)
require.Equal(t, http.StatusNotFound, sdkErr.StatusCode())
})

t.Run("UserCannotSet", func(t *testing.T) {
t.Parallel()

dv := coderdtest.DeploymentValues(t)
dv.Experiments.Set(string(codersdk.ExperimentTemplateAutostopRequirement))
dv.UserQuietHoursSchedule.DefaultSchedule.Set("CRON_TZ=America/Chicago 0 0 * * *")
dv.UserQuietHoursSchedule.AllowUserCustom.Set("false")

Expand All @@ -248,8 +190,7 @@ func TestUserQuietHours(t *testing.T) {
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureAdvancedTemplateScheduling: 1,
codersdk.FeatureTemplateAutostopRequirement: 1,
codersdk.FeatureAdvancedTemplateScheduling: 1,
},
},
})
Expand Down
38 changes: 36 additions & 2 deletions site/src/pages/UserSettingsPage/SchedulePage/SchedulePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const submitForm = async () => {
const defaultQuietHoursResponse = {
raw_schedule: "CRON_TZ=America/Chicago 0 0 * * *",
user_set: false,
user_can_set: true,
time: "00:00",
timezone: "America/Chicago",
next: "", // not consumed by the frontend
Expand All @@ -52,7 +53,6 @@ const cronTests = [

describe("SchedulePage", () => {
beforeEach(() => {
// appear logged out
server.use(
rest.get(`/api/v2/users/${MockUser.id}/quiet-hours`, (req, res, ctx) => {
return res(ctx.status(200), ctx.json(defaultQuietHoursResponse));
Expand All @@ -72,7 +72,6 @@ describe("SchedulePage", () => {
return res(
ctx.status(200),
ctx.json({
response: {},
raw_schedule: data.schedule,
user_set: true,
time: `${test.hour.toString().padStart(2, "0")}:${test.minute
Expand Down Expand Up @@ -121,4 +120,39 @@ describe("SchedulePage", () => {
expect(errorMessage).toBeDefined();
});
});

describe("when user custom schedule is disabled", () => {
it("shows a warning and disables the form", async () => {
server.use(
rest.get(
`/api/v2/users/${MockUser.id}/quiet-hours`,
(req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
raw_schedule: "CRON_TZ=America/Chicago 0 0 * * *",
user_can_set: false,
user_set: false,
time: "00:00",
timezone: "America/Chicago",
next: "", // not consumed by the frontend
}),
);
},
),
);

renderWithAuth(<SchedulePage />);
await screen.findByText(
"Your administrator has disabled the ability to set a custom quiet hours schedule.",
);

const timeInput = screen.getByLabelText("Start time");
expect(timeInput).toBeDisabled();
const timezoneDropdown = screen.getByLabelText("Timezone");
expect(timezoneDropdown).toHaveClass("Mui-disabled");
const updateButton = screen.getByText("Update schedule");
expect(updateButton).toBeDisabled();
});
});
});
You are viewing a condensed version of this merge commit. You can view the full changes here.