From fc12d548dcc66c0fdc6ccfe3cd1aa19d03028bdd Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 09:13:50 +0200 Subject: [PATCH 01/13] feat(cli): pause notifications --- cli/notifications.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 cli/notifications.go diff --git a/cli/notifications.go b/cli/notifications.go new file mode 100644 index 0000000000000..8a7c21a88cd93 --- /dev/null +++ b/cli/notifications.go @@ -0,0 +1,60 @@ +package cli + +import "github.com/coder/serpent" + +func (r *RootCmd) notifications() *serpent.Command { + cmd := &serpent.Command{ + Use: "notifications", + Short: "Manage Coder notifications", + Long: "Administrators can use these commands to change notification settings.\n" + FormatExamples( + Example{ + Description: "Pause Coder notifications", + Command: "coder notifications pause", + }, + Example{ + Description: "Unpause Coder notifications", + Command: "coder notifications unpause", + }, + ), + Aliases: []string{"notification"} + Handler: func(inv *serpent.Invocation) error { + return inv.Command.HelpHandler(inv) + }, + Children: []*serpent.Command{ + + }, + } + return cmd +} + +func (r *RootCmd) pauseNotifications() *serpent.Command { + client := new(codersdk.Client) + cmd := &serpent.Command{ + Use: "pause", + Short: "Pause notifications", + Middleware: serpent.Chain( + serpent.RequireNArgs(0), + r.InitClient(client), + ), + Handler: func(inv *serpent.Invocation) error { + return nil + }, + } + return cmd +} + +func (r *RootCmd) unpauseNotifications() *serpent.Command { + client := new(codersdk.Client) + cmd := &serpent.Command{ + Use: "unpause", + Short: "Unpause notifications", + Middleware: serpent.Chain( + serpent.RequireNArgs(0), + r.InitClient(client), + ), + Handler: func(inv *serpent.Invocation) error { + return nil + }, + } + return cmd +} From 3777e94dd275ac2dd45e244242f520259df18bb5 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 11:14:02 +0200 Subject: [PATCH 02/13] stub --- cli/notifications.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cli/notifications.go b/cli/notifications.go index 8a7c21a88cd93..6d093591301cc 100644 --- a/cli/notifications.go +++ b/cli/notifications.go @@ -1,6 +1,10 @@ package cli -import "github.com/coder/serpent" +import ( + "github.com/coder/serpent" + + "github.com/coder/coder/v2/codersdk" +) func (r *RootCmd) notifications() *serpent.Command { cmd := &serpent.Command{ @@ -16,13 +20,11 @@ func (r *RootCmd) notifications() *serpent.Command { Command: "coder notifications unpause", }, ), - Aliases: []string{"notification"} + Aliases: []string{"notification"}, Handler: func(inv *serpent.Invocation) error { return inv.Command.HelpHandler(inv) }, - Children: []*serpent.Command{ - - }, + Children: []*serpent.Command{}, } return cmd } From acab552afcb2a818a624f3e49dc68fb3f1df7c6f Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 12:03:35 +0200 Subject: [PATCH 03/13] cli tests --- cli/notifications.go | 23 ++++++++- cli/notifications_test.go | 104 ++++++++++++++++++++++++++++++++++++++ cli/root.go | 7 +-- 3 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 cli/notifications_test.go diff --git a/cli/notifications.go b/cli/notifications.go index 6d093591301cc..e3b28fd891716 100644 --- a/cli/notifications.go +++ b/cli/notifications.go @@ -1,6 +1,8 @@ package cli import ( + "fmt" + "github.com/coder/serpent" "github.com/coder/coder/v2/codersdk" @@ -24,7 +26,10 @@ func (r *RootCmd) notifications() *serpent.Command { Handler: func(inv *serpent.Invocation) error { return inv.Command.HelpHandler(inv) }, - Children: []*serpent.Command{}, + Children: []*serpent.Command{ + r.pauseNotifications(), + r.unpauseNotifications(), + }, } return cmd } @@ -39,6 +44,14 @@ func (r *RootCmd) pauseNotifications() *serpent.Command { r.InitClient(client), ), Handler: func(inv *serpent.Invocation) error { + err := client.PutNotificationsSettings(inv.Context(), codersdk.NotificationsSettings{ + NotifierPaused: true, + }) + if err != nil { + return err + } + + _, _ = fmt.Fprintln(inv.Stderr, "Notifications are paused now.") return nil }, } @@ -55,6 +68,14 @@ func (r *RootCmd) unpauseNotifications() *serpent.Command { r.InitClient(client), ), Handler: func(inv *serpent.Invocation) error { + err := client.PutNotificationsSettings(inv.Context(), codersdk.NotificationsSettings{ + NotifierPaused: false, + }) + if err != nil { + return err + } + + _, _ = fmt.Fprintln(inv.Stderr, "Notifications are unpaused now.") return nil }, } diff --git a/cli/notifications_test.go b/cli/notifications_test.go new file mode 100644 index 0000000000000..68232fab08126 --- /dev/null +++ b/cli/notifications_test.go @@ -0,0 +1,104 @@ +package cli_test + +import ( + "bytes" + "context" + "encoding/json" + "net/http" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/coder/coder/v2/cli/clitest" + "github.com/coder/coder/v2/coderd/coderdtest" + "github.com/coder/coder/v2/codersdk" + "github.com/coder/coder/v2/testutil" +) + +func TestPauseNotifications(t *testing.T) { + t.Parallel() + + // given + ownerClient, db := coderdtest.NewWithDatabase(t, nil) + _ = coderdtest.CreateFirstUser(t, ownerClient) + + // when + inv, root := clitest.New(t, "notifications", "pause") + clitest.SetupConfig(t, ownerClient, root) + + var buf bytes.Buffer + inv.Stdout = &buf + err := inv.Run() + require.NoError(t, err) + + // then + ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) + t.Cleanup(cancel) + settingsJSON, err := db.GetNotificationsSettings(ctx) + require.NoError(t, err) + + var settings codersdk.NotificationsSettings + err = json.Unmarshal([]byte(settingsJSON), &settings) + require.NoError(t, err) + require.True(t, settings.NotifierPaused) +} + +func TestPauseNotifications_RegularUser(t *testing.T) { + t.Parallel() + + // given + ownerClient, db := coderdtest.NewWithDatabase(t, nil) + owner := coderdtest.CreateFirstUser(t, ownerClient) + anotherClient, _ := coderdtest.CreateAnotherUser(t, ownerClient, owner.OrganizationID) + + // when + inv, root := clitest.New(t, "notifications", "pause") + clitest.SetupConfig(t, anotherClient, root) + + var buf bytes.Buffer + inv.Stdout = &buf + err := inv.Run() + var sdkError *codersdk.Error + require.Error(t, err) + require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error") + require.Equal(t, http.StatusForbidden, sdkError.StatusCode()) + + // then + ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) + t.Cleanup(cancel) + settingsJSON, err := db.GetNotificationsSettings(ctx) + require.NoError(t, err) + + var settings codersdk.NotificationsSettings + err = json.Unmarshal([]byte(settingsJSON), &settings) + require.NoError(t, err) + require.False(t, settings.NotifierPaused) // still unpaused +} + +func TestUnpauseNotifications(t *testing.T) { + t.Parallel() + + // given + ownerClient, db := coderdtest.NewWithDatabase(t, nil) + _ = coderdtest.CreateFirstUser(t, ownerClient) + + // when + inv, root := clitest.New(t, "notifications", "unpause") + clitest.SetupConfig(t, ownerClient, root) + + var buf bytes.Buffer + inv.Stdout = &buf + err := inv.Run() + require.NoError(t, err) + + // then + ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) + t.Cleanup(cancel) + settingsJSON, err := db.GetNotificationsSettings(ctx) + require.NoError(t, err) + + var settings codersdk.NotificationsSettings + err = json.Unmarshal([]byte(settingsJSON), &settings) + require.NoError(t, err) + require.False(t, settings.NotifierPaused) +} diff --git a/cli/root.go b/cli/root.go index 4e615eaa6e7af..579f3c4c29202 100644 --- a/cli/root.go +++ b/cli/root.go @@ -87,6 +87,8 @@ func (r *RootCmd) CoreSubcommands() []*serpent.Command { r.login(), r.logout(), r.netcheck(), + r.notifications(), + r.organizations(), r.portForward(), r.publickey(), r.resetPassword(), @@ -95,7 +97,6 @@ func (r *RootCmd) CoreSubcommands() []*serpent.Command { r.tokens(), r.users(), r.version(defaultVersionInfo), - r.organizations(), // Workspace Commands r.autoupdate(), @@ -120,11 +121,11 @@ func (r *RootCmd) CoreSubcommands() []*serpent.Command { r.whoami(), // Hidden + r.expCmd(), r.gitssh(), + r.support(), r.vscodeSSH(), r.workspaceAgent(), - r.expCmd(), - r.support(), } } From 4cfdc08a18a7655296e2409703cd359f9a7b3547 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 12:12:30 +0200 Subject: [PATCH 04/13] make gen --- coderd/database/models.go | 2 +- coderd/database/querier.go | 2 +- coderd/database/queries.sql.go | 2 +- docs/cli.md | 1 + docs/cli/notifications.md | 35 +++++++++++++++++++++++++++++++ docs/cli/notifications_pause.md | 11 ++++++++++ docs/cli/notifications_unpause.md | 11 ++++++++++ docs/manifest.json | 15 +++++++++++++ 8 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 docs/cli/notifications.md create mode 100644 docs/cli/notifications_pause.md create mode 100644 docs/cli/notifications_unpause.md diff --git a/coderd/database/models.go b/coderd/database/models.go index c92d51b4366d3..33f8ec939da28 100644 --- a/coderd/database/models.go +++ b/coderd/database/models.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.25.0 +// sqlc v1.26.0 package database diff --git a/coderd/database/querier.go b/coderd/database/querier.go index c4ce70cea28fe..55284295bfa02 100644 --- a/coderd/database/querier.go +++ b/coderd/database/querier.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.25.0 +// sqlc v1.26.0 package database diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index 5e85fd10d9838..ceeb50cd350dc 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.25.0 +// sqlc v1.26.0 package database diff --git a/docs/cli.md b/docs/cli.md index f38bc0e3e133a..ab97ca9cc4d10 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -30,6 +30,7 @@ Coder — A tool for provisioning self-hosted development environments with Terr | [login](./cli/login.md) | Authenticate with Coder deployment | | [logout](./cli/logout.md) | Unauthenticate your local session | | [netcheck](./cli/netcheck.md) | Print network debug information for DERP and STUN | +| [notifications](./cli/notifications.md) | Manage Coder notifications | | [port-forward](./cli/port-forward.md) | Forward ports from a workspace to the local machine. For reverse port forwarding, use "coder ssh -R". | | [publickey](./cli/publickey.md) | Output your Coder public key used for Git operations | | [reset-password](./cli/reset-password.md) | Directly connect to the database to reset a user's password | diff --git a/docs/cli/notifications.md b/docs/cli/notifications.md new file mode 100644 index 0000000000000..eb9fe4d67138f --- /dev/null +++ b/docs/cli/notifications.md @@ -0,0 +1,35 @@ + + +# notifications + +Manage Coder notifications + +Aliases: + +- notification + +## Usage + +```console +coder notifications +``` + +## Description + +```console +Administrators can use these commands to change notification settings. + - Pause Coder notifications: + + $ coder notifications pause + + - Unpause Coder notifications: + + $ coder notifications unpause +``` + +## Subcommands + +| Name | Purpose | +| -------------------------------------------------- | --------------------- | +| [pause](./notifications_pause.md) | Pause notifications | +| [unpause](./notifications_unpause.md) | Unpause notifications | diff --git a/docs/cli/notifications_pause.md b/docs/cli/notifications_pause.md new file mode 100644 index 0000000000000..0cb2b101d474c --- /dev/null +++ b/docs/cli/notifications_pause.md @@ -0,0 +1,11 @@ + + +# notifications pause + +Pause notifications + +## Usage + +```console +coder notifications pause +``` diff --git a/docs/cli/notifications_unpause.md b/docs/cli/notifications_unpause.md new file mode 100644 index 0000000000000..0114f19ccb163 --- /dev/null +++ b/docs/cli/notifications_unpause.md @@ -0,0 +1,11 @@ + + +# notifications unpause + +Unpause notifications + +## Usage + +```console +coder notifications unpause +``` diff --git a/docs/manifest.json b/docs/manifest.json index dc887921b2b20..13508c4fff03a 100644 --- a/docs/manifest.json +++ b/docs/manifest.json @@ -755,6 +755,21 @@ "description": "Print network debug information for DERP and STUN", "path": "cli/netcheck.md" }, + { + "title": "notifications", + "description": "Manage Coder notifications", + "path": "cli/notifications.md" + }, + { + "title": "notifications pause", + "description": "Pause notifications", + "path": "cli/notifications_pause.md" + }, + { + "title": "notifications unpause", + "description": "Unpause notifications", + "path": "cli/notifications_unpause.md" + }, { "title": "open", "description": "Open a workspace", From 85179b36e776b4d0f341437fc8ea91a84f6c9a51 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 12:13:39 +0200 Subject: [PATCH 05/13] golden --- cli/testdata/coder_--help.golden | 1 + .../coder_notifications_--help.golden | 24 +++++++++++++++++++ .../coder_notifications_pause_--help.golden | 9 +++++++ .../coder_notifications_unpause_--help.golden | 9 +++++++ 4 files changed, 43 insertions(+) create mode 100644 cli/testdata/coder_notifications_--help.golden create mode 100644 cli/testdata/coder_notifications_pause_--help.golden create mode 100644 cli/testdata/coder_notifications_unpause_--help.golden diff --git a/cli/testdata/coder_--help.golden b/cli/testdata/coder_--help.golden index a576797d8a48d..494ed7decb492 100644 --- a/cli/testdata/coder_--help.golden +++ b/cli/testdata/coder_--help.golden @@ -27,6 +27,7 @@ SUBCOMMANDS: login Authenticate with Coder deployment logout Unauthenticate your local session netcheck Print network debug information for DERP and STUN + notifications Manage Coder notifications open Open a workspace ping Ping a workspace port-forward Forward ports from a workspace to the local machine. For diff --git a/cli/testdata/coder_notifications_--help.golden b/cli/testdata/coder_notifications_--help.golden new file mode 100644 index 0000000000000..a1c2c81170e6e --- /dev/null +++ b/cli/testdata/coder_notifications_--help.golden @@ -0,0 +1,24 @@ +coder v0.0.0-devel + +USAGE: + coder notifications + + Manage Coder notifications + + Aliases: notification + + Administrators can use these commands to change notification settings. + - Pause Coder notifications: + + $ coder notifications pause + + - Unpause Coder notifications: + + $ coder notifications unpause + +SUBCOMMANDS: + pause Pause notifications + unpause Unpause notifications + +——— +Run `coder --help` for a list of global options. diff --git a/cli/testdata/coder_notifications_pause_--help.golden b/cli/testdata/coder_notifications_pause_--help.golden new file mode 100644 index 0000000000000..fc3f2621ad788 --- /dev/null +++ b/cli/testdata/coder_notifications_pause_--help.golden @@ -0,0 +1,9 @@ +coder v0.0.0-devel + +USAGE: + coder notifications pause + + Pause notifications + +——— +Run `coder --help` for a list of global options. diff --git a/cli/testdata/coder_notifications_unpause_--help.golden b/cli/testdata/coder_notifications_unpause_--help.golden new file mode 100644 index 0000000000000..25515dba13bf3 --- /dev/null +++ b/cli/testdata/coder_notifications_unpause_--help.golden @@ -0,0 +1,9 @@ +coder v0.0.0-devel + +USAGE: + coder notifications unpause + + Unpause notifications + +——— +Run `coder --help` for a list of global options. From 9e94fef53a62f1fd731f490c3341f364e1adbe4a Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 12:19:47 +0200 Subject: [PATCH 06/13] 1.25 --- coderd/database/models.go | 2 +- coderd/database/querier.go | 2 +- coderd/database/queries.sql.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/coderd/database/models.go b/coderd/database/models.go index 33f8ec939da28..c92d51b4366d3 100644 --- a/coderd/database/models.go +++ b/coderd/database/models.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.26.0 +// sqlc v1.25.0 package database diff --git a/coderd/database/querier.go b/coderd/database/querier.go index 55284295bfa02..c4ce70cea28fe 100644 --- a/coderd/database/querier.go +++ b/coderd/database/querier.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.26.0 +// sqlc v1.25.0 package database diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index ceeb50cd350dc..5e85fd10d9838 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.26.0 +// sqlc v1.25.0 package database From 3694faee78405ed1a147f0028b8fa0a0116d10a1 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 14:05:32 +0200 Subject: [PATCH 07/13] unpause -> resume --- cli/notifications.go | 24 ++++++++++--------- cli/notifications_test.go | 6 ++--- .../coder_notifications_--help.golden | 8 +++---- .../coder_notifications_resume_--help.golden | 9 +++++++ docs/cli/notifications.md | 16 +++++++------ docs/cli/notifications_resume.md | 11 +++++++++ docs/cli/notifications_unpause.md | 11 --------- docs/manifest.json | 6 ++--- 8 files changed, 52 insertions(+), 39 deletions(-) create mode 100644 cli/testdata/coder_notifications_resume_--help.golden create mode 100644 docs/cli/notifications_resume.md delete mode 100644 docs/cli/notifications_unpause.md diff --git a/cli/notifications.go b/cli/notifications.go index e3b28fd891716..b2e41a5720505 100644 --- a/cli/notifications.go +++ b/cli/notifications.go @@ -3,6 +3,8 @@ package cli import ( "fmt" + "golang.org/x/xerrors" + "github.com/coder/serpent" "github.com/coder/coder/v2/codersdk" @@ -14,12 +16,12 @@ func (r *RootCmd) notifications() *serpent.Command { Short: "Manage Coder notifications", Long: "Administrators can use these commands to change notification settings.\n" + FormatExamples( Example{ - Description: "Pause Coder notifications", + Description: "Pause Coder notifications. Administrators can temporarily stop notifiers from dispatching messages in case of the target outage (for example: unavailable SMTP server or Webhook not responding).", Command: "coder notifications pause", }, Example{ - Description: "Unpause Coder notifications", - Command: "coder notifications unpause", + Description: "Resume Coder notifications", + Command: "coder notifications resume", }, ), Aliases: []string{"notification"}, @@ -28,7 +30,7 @@ func (r *RootCmd) notifications() *serpent.Command { }, Children: []*serpent.Command{ r.pauseNotifications(), - r.unpauseNotifications(), + r.resumeNotifications(), }, } return cmd @@ -48,21 +50,21 @@ func (r *RootCmd) pauseNotifications() *serpent.Command { NotifierPaused: true, }) if err != nil { - return err + return xerrors.Errorf("unable to pause notifications %w", err) } - _, _ = fmt.Fprintln(inv.Stderr, "Notifications are paused now.") + _, _ = fmt.Fprintln(inv.Stderr, "Notifications are now paused.") return nil }, } return cmd } -func (r *RootCmd) unpauseNotifications() *serpent.Command { +func (r *RootCmd) resumeNotifications() *serpent.Command { client := new(codersdk.Client) cmd := &serpent.Command{ - Use: "unpause", - Short: "Unpause notifications", + Use: "resume", + Short: "Resume notifications", Middleware: serpent.Chain( serpent.RequireNArgs(0), r.InitClient(client), @@ -72,10 +74,10 @@ func (r *RootCmd) unpauseNotifications() *serpent.Command { NotifierPaused: false, }) if err != nil { - return err + return xerrors.Errorf("unable to resume notifications %w", err) } - _, _ = fmt.Fprintln(inv.Stderr, "Notifications are unpaused now.") + _, _ = fmt.Fprintln(inv.Stderr, "Notifications are now resumed.") return nil }, } diff --git a/cli/notifications_test.go b/cli/notifications_test.go index 68232fab08126..29617448c281e 100644 --- a/cli/notifications_test.go +++ b/cli/notifications_test.go @@ -72,10 +72,10 @@ func TestPauseNotifications_RegularUser(t *testing.T) { var settings codersdk.NotificationsSettings err = json.Unmarshal([]byte(settingsJSON), &settings) require.NoError(t, err) - require.False(t, settings.NotifierPaused) // still unpaused + require.False(t, settings.NotifierPaused) // still running } -func TestUnpauseNotifications(t *testing.T) { +func TestResumeNotifications(t *testing.T) { t.Parallel() // given @@ -83,7 +83,7 @@ func TestUnpauseNotifications(t *testing.T) { _ = coderdtest.CreateFirstUser(t, ownerClient) // when - inv, root := clitest.New(t, "notifications", "unpause") + inv, root := clitest.New(t, "notifications", "resume") clitest.SetupConfig(t, ownerClient, root) var buf bytes.Buffer diff --git a/cli/testdata/coder_notifications_--help.golden b/cli/testdata/coder_notifications_--help.golden index a1c2c81170e6e..f613a89455791 100644 --- a/cli/testdata/coder_notifications_--help.golden +++ b/cli/testdata/coder_notifications_--help.golden @@ -12,13 +12,13 @@ USAGE: $ coder notifications pause - - Unpause Coder notifications: + - Resume Coder notifications: - $ coder notifications unpause + $ coder notifications resume SUBCOMMANDS: - pause Pause notifications - unpause Unpause notifications + pause Pause notifications + resume Resume notifications ——— Run `coder --help` for a list of global options. diff --git a/cli/testdata/coder_notifications_resume_--help.golden b/cli/testdata/coder_notifications_resume_--help.golden new file mode 100644 index 0000000000000..ea69e1e789a2e --- /dev/null +++ b/cli/testdata/coder_notifications_resume_--help.golden @@ -0,0 +1,9 @@ +coder v0.0.0-devel + +USAGE: + coder notifications resume + + Resume notifications + +——— +Run `coder --help` for a list of global options. diff --git a/docs/cli/notifications.md b/docs/cli/notifications.md index eb9fe4d67138f..59e74b4324357 100644 --- a/docs/cli/notifications.md +++ b/docs/cli/notifications.md @@ -18,18 +18,20 @@ coder notifications ```console Administrators can use these commands to change notification settings. - - Pause Coder notifications: + - Pause Coder notifications. Administrators can temporarily stop notifiers from +dispatching messages in case of the target outage (for example: unavailable SMTP +server or Webhook not responding).: $ coder notifications pause - - Unpause Coder notifications: + - Resume Coder notifications: - $ coder notifications unpause + $ coder notifications resume ``` ## Subcommands -| Name | Purpose | -| -------------------------------------------------- | --------------------- | -| [pause](./notifications_pause.md) | Pause notifications | -| [unpause](./notifications_unpause.md) | Unpause notifications | +| Name | Purpose | +| ------------------------------------------------ | -------------------- | +| [pause](./notifications_pause.md) | Pause notifications | +| [resume](./notifications_resume.md) | Resume notifications | diff --git a/docs/cli/notifications_resume.md b/docs/cli/notifications_resume.md new file mode 100644 index 0000000000000..a8dc17453a383 --- /dev/null +++ b/docs/cli/notifications_resume.md @@ -0,0 +1,11 @@ + + +# notifications resume + +Resume notifications + +## Usage + +```console +coder notifications resume +``` diff --git a/docs/cli/notifications_unpause.md b/docs/cli/notifications_unpause.md deleted file mode 100644 index 0114f19ccb163..0000000000000 --- a/docs/cli/notifications_unpause.md +++ /dev/null @@ -1,11 +0,0 @@ - - -# notifications unpause - -Unpause notifications - -## Usage - -```console -coder notifications unpause -``` diff --git a/docs/manifest.json b/docs/manifest.json index 13508c4fff03a..82dd73ada47c8 100644 --- a/docs/manifest.json +++ b/docs/manifest.json @@ -766,9 +766,9 @@ "path": "cli/notifications_pause.md" }, { - "title": "notifications unpause", - "description": "Unpause notifications", - "path": "cli/notifications_unpause.md" + "title": "notifications resume", + "description": "Resume notifications", + "path": "cli/notifications_resume.md" }, { "title": "open", From d907ef24cb8eacfcabc8cd97d508fcbf337b18d0 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 14:18:49 +0200 Subject: [PATCH 08/13] check error message --- cli/notifications_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cli/notifications_test.go b/cli/notifications_test.go index 29617448c281e..c7475791ec610 100644 --- a/cli/notifications_test.go +++ b/cli/notifications_test.go @@ -7,6 +7,7 @@ import ( "net/http" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/coder/coder/v2/cli/clitest" @@ -61,7 +62,8 @@ func TestPauseNotifications_RegularUser(t *testing.T) { var sdkError *codersdk.Error require.Error(t, err) require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error") - require.Equal(t, http.StatusForbidden, sdkError.StatusCode()) + assert.Equal(t, http.StatusForbidden, sdkError.StatusCode()) + assert.Contains(t, sdkError.Message, "Insufficient permissions to update notifications settings.") // then ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) From 94987d2800e8497e1cf2ae93141a43cebf19d7a9 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 14:23:54 +0200 Subject: [PATCH 09/13] table tests --- cli/notifications_test.go | 99 +++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 52 deletions(-) diff --git a/cli/notifications_test.go b/cli/notifications_test.go index c7475791ec610..d22584b19bc4f 100644 --- a/cli/notifications_test.go +++ b/cli/notifications_test.go @@ -16,32 +16,55 @@ import ( "github.com/coder/coder/v2/testutil" ) -func TestPauseNotifications(t *testing.T) { +func TestNotifications(t *testing.T) { t.Parallel() - // given - ownerClient, db := coderdtest.NewWithDatabase(t, nil) - _ = coderdtest.CreateFirstUser(t, ownerClient) - - // when - inv, root := clitest.New(t, "notifications", "pause") - clitest.SetupConfig(t, ownerClient, root) - - var buf bytes.Buffer - inv.Stdout = &buf - err := inv.Run() - require.NoError(t, err) - - // then - ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) - t.Cleanup(cancel) - settingsJSON, err := db.GetNotificationsSettings(ctx) - require.NoError(t, err) - - var settings codersdk.NotificationsSettings - err = json.Unmarshal([]byte(settingsJSON), &settings) - require.NoError(t, err) - require.True(t, settings.NotifierPaused) + tests := []struct { + name string + command string + expectPaused bool + }{ + { + name: "PauseNotifications", + command: "pause", + expectPaused: true, + }, + { + name: "ResumeNotifications", + command: "resume", + expectPaused: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + // given + ownerClient, db := coderdtest.NewWithDatabase(t, nil) + _ = coderdtest.CreateFirstUser(t, ownerClient) + + // when + inv, root := clitest.New(t, "notifications", tt.command) + clitest.SetupConfig(t, ownerClient, root) + + var buf bytes.Buffer + inv.Stdout = &buf + err := inv.Run() + require.NoError(t, err) + + // then + ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) + t.Cleanup(cancel) + settingsJSON, err := db.GetNotificationsSettings(ctx) + require.NoError(t, err) + + var settings codersdk.NotificationsSettings + err = json.Unmarshal([]byte(settingsJSON), &settings) + require.NoError(t, err) + require.Equal(t, tt.expectPaused, settings.NotifierPaused) + }) + } } func TestPauseNotifications_RegularUser(t *testing.T) { @@ -76,31 +99,3 @@ func TestPauseNotifications_RegularUser(t *testing.T) { require.NoError(t, err) require.False(t, settings.NotifierPaused) // still running } - -func TestResumeNotifications(t *testing.T) { - t.Parallel() - - // given - ownerClient, db := coderdtest.NewWithDatabase(t, nil) - _ = coderdtest.CreateFirstUser(t, ownerClient) - - // when - inv, root := clitest.New(t, "notifications", "resume") - clitest.SetupConfig(t, ownerClient, root) - - var buf bytes.Buffer - inv.Stdout = &buf - err := inv.Run() - require.NoError(t, err) - - // then - ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) - t.Cleanup(cancel) - settingsJSON, err := db.GetNotificationsSettings(ctx) - require.NoError(t, err) - - var settings codersdk.NotificationsSettings - err = json.Unmarshal([]byte(settingsJSON), &settings) - require.NoError(t, err) - require.False(t, settings.NotifierPaused) -} From c8d208afb57416fa06c55d4e2c02e1e582d96795 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 14:24:45 +0200 Subject: [PATCH 10/13] rm golden --- cli/testdata/coder_notifications_unpause_--help.golden | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 cli/testdata/coder_notifications_unpause_--help.golden diff --git a/cli/testdata/coder_notifications_unpause_--help.golden b/cli/testdata/coder_notifications_unpause_--help.golden deleted file mode 100644 index 25515dba13bf3..0000000000000 --- a/cli/testdata/coder_notifications_unpause_--help.golden +++ /dev/null @@ -1,9 +0,0 @@ -coder v0.0.0-devel - -USAGE: - coder notifications unpause - - Unpause notifications - -——— -Run `coder --help` for a list of global options. From 727620d9024cdec4625aaed7a5cea6f33eb99776 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 14:25:27 +0200 Subject: [PATCH 11/13] missing colon --- cli/notifications.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/notifications.go b/cli/notifications.go index b2e41a5720505..055a4bfa65e3b 100644 --- a/cli/notifications.go +++ b/cli/notifications.go @@ -50,7 +50,7 @@ func (r *RootCmd) pauseNotifications() *serpent.Command { NotifierPaused: true, }) if err != nil { - return xerrors.Errorf("unable to pause notifications %w", err) + return xerrors.Errorf("unable to pause notifications: %w", err) } _, _ = fmt.Fprintln(inv.Stderr, "Notifications are now paused.") @@ -74,7 +74,7 @@ func (r *RootCmd) resumeNotifications() *serpent.Command { NotifierPaused: false, }) if err != nil { - return xerrors.Errorf("unable to resume notifications %w", err) + return xerrors.Errorf("unable to resume notifications: %w", err) } _, _ = fmt.Fprintln(inv.Stderr, "Notifications are now resumed.") From 54404fa07ab9dd3e7df28d0252cc594b65f6ec9f Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 14:36:24 +0200 Subject: [PATCH 12/13] linter complains --- cli/notifications_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/notifications_test.go b/cli/notifications_test.go index d22584b19bc4f..9ea4d7072e4c3 100644 --- a/cli/notifications_test.go +++ b/cli/notifications_test.go @@ -37,6 +37,7 @@ func TestNotifications(t *testing.T) { } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() From 6c1e2d8013c769e96dfc13f5fe8e1c63d0b00d92 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 11 Jul 2024 14:41:43 +0200 Subject: [PATCH 13/13] fix golden --- cli/testdata/coder_notifications_--help.golden | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/testdata/coder_notifications_--help.golden b/cli/testdata/coder_notifications_--help.golden index f613a89455791..b54e98543da7b 100644 --- a/cli/testdata/coder_notifications_--help.golden +++ b/cli/testdata/coder_notifications_--help.golden @@ -8,7 +8,11 @@ USAGE: Aliases: notification Administrators can use these commands to change notification settings. - - Pause Coder notifications: + - Pause Coder notifications. Administrators can temporarily stop notifiers + from + dispatching messages in case of the target outage (for example: unavailable + SMTP + server or Webhook not responding).: $ coder notifications pause