|
| 1 | +package cli_test |
| 2 | + |
| 3 | +import ( |
| 4 | +"bytes" |
| 5 | +"context" |
| 6 | +"encoding/json" |
| 7 | +"net/http" |
| 8 | +"testing" |
| 9 | + |
| 10 | +"github.com/stretchr/testify/assert" |
| 11 | +"github.com/stretchr/testify/require" |
| 12 | + |
| 13 | +"github.com/coder/coder/v2/cli/clitest" |
| 14 | +"github.com/coder/coder/v2/coderd/coderdtest" |
| 15 | +"github.com/coder/coder/v2/codersdk" |
| 16 | +"github.com/coder/coder/v2/testutil" |
| 17 | +) |
| 18 | + |
| 19 | +funcTestNotifications(t*testing.T) { |
| 20 | +t.Parallel() |
| 21 | + |
| 22 | +tests:= []struct { |
| 23 | +namestring |
| 24 | +commandstring |
| 25 | +expectPausedbool |
| 26 | +}{ |
| 27 | +{ |
| 28 | +name:"PauseNotifications", |
| 29 | +command:"pause", |
| 30 | +expectPaused:true, |
| 31 | +}, |
| 32 | +{ |
| 33 | +name:"ResumeNotifications", |
| 34 | +command:"resume", |
| 35 | +expectPaused:false, |
| 36 | +}, |
| 37 | +} |
| 38 | + |
| 39 | +for_,tt:=rangetests { |
| 40 | +tt:=tt |
| 41 | +t.Run(tt.name,func(t*testing.T) { |
| 42 | +t.Parallel() |
| 43 | + |
| 44 | +// given |
| 45 | +ownerClient,db:=coderdtest.NewWithDatabase(t,nil) |
| 46 | +_=coderdtest.CreateFirstUser(t,ownerClient) |
| 47 | + |
| 48 | +// when |
| 49 | +inv,root:=clitest.New(t,"notifications",tt.command) |
| 50 | +clitest.SetupConfig(t,ownerClient,root) |
| 51 | + |
| 52 | +varbuf bytes.Buffer |
| 53 | +inv.Stdout=&buf |
| 54 | +err:=inv.Run() |
| 55 | +require.NoError(t,err) |
| 56 | + |
| 57 | +// then |
| 58 | +ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitShort) |
| 59 | +t.Cleanup(cancel) |
| 60 | +settingsJSON,err:=db.GetNotificationsSettings(ctx) |
| 61 | +require.NoError(t,err) |
| 62 | + |
| 63 | +varsettings codersdk.NotificationsSettings |
| 64 | +err=json.Unmarshal([]byte(settingsJSON),&settings) |
| 65 | +require.NoError(t,err) |
| 66 | +require.Equal(t,tt.expectPaused,settings.NotifierPaused) |
| 67 | +}) |
| 68 | +} |
| 69 | +} |
| 70 | + |
| 71 | +funcTestPauseNotifications_RegularUser(t*testing.T) { |
| 72 | +t.Parallel() |
| 73 | + |
| 74 | +// given |
| 75 | +ownerClient,db:=coderdtest.NewWithDatabase(t,nil) |
| 76 | +owner:=coderdtest.CreateFirstUser(t,ownerClient) |
| 77 | +anotherClient,_:=coderdtest.CreateAnotherUser(t,ownerClient,owner.OrganizationID) |
| 78 | + |
| 79 | +// when |
| 80 | +inv,root:=clitest.New(t,"notifications","pause") |
| 81 | +clitest.SetupConfig(t,anotherClient,root) |
| 82 | + |
| 83 | +varbuf bytes.Buffer |
| 84 | +inv.Stdout=&buf |
| 85 | +err:=inv.Run() |
| 86 | +varsdkError*codersdk.Error |
| 87 | +require.Error(t,err) |
| 88 | +require.ErrorAsf(t,err,&sdkError,"error should be of type *codersdk.Error") |
| 89 | +assert.Equal(t,http.StatusForbidden,sdkError.StatusCode()) |
| 90 | +assert.Contains(t,sdkError.Message,"Insufficient permissions to update notifications settings.") |
| 91 | + |
| 92 | +// then |
| 93 | +ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitShort) |
| 94 | +t.Cleanup(cancel) |
| 95 | +settingsJSON,err:=db.GetNotificationsSettings(ctx) |
| 96 | +require.NoError(t,err) |
| 97 | + |
| 98 | +varsettings codersdk.NotificationsSettings |
| 99 | +err=json.Unmarshal([]byte(settingsJSON),&settings) |
| 100 | +require.NoError(t,err) |
| 101 | +require.False(t,settings.NotifierPaused)// still running |
| 102 | +} |