@@ -19,24 +19,14 @@ import (
19
19
"github.com/coder/coder/v2/testutil"
20
20
)
21
21
22
- func createOpts (t * testing.T , usePostgres bool )* coderdenttest.Options {
22
+ func createOpts (t * testing.T )* coderdenttest.Options {
23
23
t .Helper ()
24
24
25
- if usePostgres {
26
- if ! dbtestutil .WillUsePostgres () {
27
- t .Skip ("This test requires postgres; it relies on read from and writing to the notification_templates table" )
28
- }
29
- }
30
-
31
- db ,ps := dbtestutil .NewDB (t )
32
-
33
25
dt := coderdtest .DeploymentValues (t )
34
26
dt .Experiments = []string {string (codersdk .ExperimentNotifications )}
35
27
return & coderdenttest.Options {
36
28
Options :& coderdtest.Options {
37
29
DeploymentValues :dt ,
38
- Database :db ,
39
- Pubsub :ps ,
40
30
},
41
31
}
42
32
}
@@ -47,16 +37,20 @@ func TestUpdateNotificationTemplateMethod(t *testing.T) {
47
37
t .Run ("Happy path" ,func (t * testing.T ) {
48
38
t .Parallel ()
49
39
40
+ if ! dbtestutil .WillUsePostgres () {
41
+ t .Skip ("This test requires postgres; it relies on read from and writing to the notification_templates table" )
42
+ }
43
+
50
44
ctx := testutil .Context (t ,testutil .WaitSuperLong )
51
- api ,_ := coderdenttest .New (t ,createOpts (t , true ))
45
+ api ,_ := coderdenttest .New (t ,createOpts (t ))
52
46
53
47
var (
54
48
method = string (database .NotificationMethodSmtp )
55
49
templateID = notifications .TemplateWorkspaceDeleted
56
50
)
57
51
58
52
// Given: a template whose method is initially empty (i.e. deferring to the global method value).
59
- template ,err := getTemplateById (t ,ctx ,api ,templateID )
53
+ template ,err := getTemplateByID (t ,ctx ,api ,templateID )
60
54
require .NoError (t ,err )
61
55
require .NotNil (t ,template )
62
56
require .Empty (t ,template .Method )
@@ -65,7 +59,7 @@ func TestUpdateNotificationTemplateMethod(t *testing.T) {
65
59
require .NoError (t ,api .UpdateNotificationTemplateMethod (ctx ,notifications .TemplateWorkspaceDeleted ,method ),"initial request to set the method failed" )
66
60
67
61
// Then: the method should be set.
68
- template ,err = getTemplateById (t ,ctx ,api ,templateID )
62
+ template ,err = getTemplateByID (t ,ctx ,api ,templateID )
69
63
require .NoError (t ,err )
70
64
require .NotNil (t ,template )
71
65
require .Equal (t ,method ,template .Method )
@@ -74,10 +68,14 @@ func TestUpdateNotificationTemplateMethod(t *testing.T) {
74
68
t .Run ("Insufficient permissions" ,func (t * testing.T ) {
75
69
t .Parallel ()
76
70
71
+ if ! dbtestutil .WillUsePostgres () {
72
+ t .Skip ("This test requires postgres; it relies on read from and writing to the notification_templates table" )
73
+ }
74
+
77
75
ctx := testutil .Context (t ,testutil .WaitSuperLong )
78
76
79
77
// Given: the first user which has an "owner" role, and another user which does not.
80
- api ,firstUser := coderdenttest .New (t ,createOpts (t , false ))
78
+ api ,firstUser := coderdenttest .New (t ,createOpts (t ))
81
79
anotherClient ,_ := coderdtest .CreateAnotherUser (t ,api ,firstUser .OrganizationID )
82
80
83
81
// When: calling the API as an unprivileged user.
@@ -94,13 +92,19 @@ func TestUpdateNotificationTemplateMethod(t *testing.T) {
94
92
t .Run ("Invalid notification method" ,func (t * testing.T ) {
95
93
t .Parallel ()
96
94
95
+ if ! dbtestutil .WillUsePostgres () {
96
+ t .Skip ("This test requires postgres; it relies on read from and writing to the notification_templates table" )
97
+ }
98
+
97
99
ctx := testutil .Context (t ,testutil .WaitSuperLong )
98
100
99
101
// Given: the first user which has an "owner" role
100
- api ,_ := coderdenttest .New (t ,createOpts (t , true ))
102
+ api ,_ := coderdenttest .New (t ,createOpts (t ))
101
103
102
104
// When: calling the API with an invalid method.
103
105
const method = "nope"
106
+
107
+ // nolint:gocritic // Using an owner-scope user is kinda the point.
104
108
err := api .UpdateNotificationTemplateMethod (ctx ,notifications .TemplateWorkspaceDeleted ,method )
105
109
106
110
// Then: the request is invalid because of the unacceptable method.
@@ -117,15 +121,19 @@ func TestUpdateNotificationTemplateMethod(t *testing.T) {
117
121
t .Run ("Not modified" ,func (t * testing.T ) {
118
122
t .Parallel ()
119
123
124
+ if ! dbtestutil .WillUsePostgres () {
125
+ t .Skip ("This test requires postgres; it relies on read from and writing to the notification_templates table" )
126
+ }
127
+
120
128
ctx := testutil .Context (t ,testutil .WaitSuperLong )
121
- api ,_ := coderdenttest .New (t ,createOpts (t , true ))
129
+ api ,_ := coderdenttest .New (t ,createOpts (t ))
122
130
123
131
var (
124
132
method = string (database .NotificationMethodSmtp )
125
133
templateID = notifications .TemplateWorkspaceDeleted
126
134
)
127
135
128
- template ,err := getTemplateById (t ,ctx ,api ,templateID )
136
+ template ,err := getTemplateByID (t ,ctx ,api ,templateID )
129
137
require .NoError (t ,err )
130
138
require .NotNil (t ,template )
131
139
@@ -134,38 +142,39 @@ func TestUpdateNotificationTemplateMethod(t *testing.T) {
134
142
135
143
// When: calling the API to update the method, it should set it.
136
144
require .NoError (t ,api .UpdateNotificationTemplateMethod (ctx ,notifications .TemplateWorkspaceDeleted ,method ),"initial request to set the method failed" )
137
- template ,err = getTemplateById (t ,ctx ,api ,templateID )
145
+ template ,err = getTemplateByID (t ,ctx ,api ,templateID )
138
146
require .NoError (t ,err )
139
147
require .NotNil (t ,template )
140
148
require .Equal (t ,method ,template .Method )
141
149
142
150
// Then: when calling the API again with the same method, the method will remain unchanged.
143
151
require .NoError (t ,api .UpdateNotificationTemplateMethod (ctx ,notifications .TemplateWorkspaceDeleted ,method ),"second request to set the method failed" )
144
- template ,err = getTemplateById (t ,ctx ,api ,templateID )
152
+ template ,err = getTemplateByID (t ,ctx ,api ,templateID )
145
153
require .NoError (t ,err )
146
154
require .NotNil (t ,template )
147
155
require .Equal (t ,method ,template .Method )
148
156
})
149
157
}
150
158
151
- func getTemplateById (t * testing.T ,ctx context.Context ,api * codersdk.Client ,id uuid.UUID ) (* codersdk.NotificationTemplate ,error ) {
159
+ // nolint:revive // t takes precedence.
160
+ func getTemplateByID (t * testing.T ,ctx context.Context ,api * codersdk.Client ,id uuid.UUID ) (* codersdk.NotificationTemplate ,error ) {
152
161
t .Helper ()
153
162
154
- var template * codersdk.NotificationTemplate
163
+ var template codersdk.NotificationTemplate
155
164
templates ,err := api .GetSystemNotificationTemplates (ctx )
156
165
if err != nil {
157
166
return nil ,err
158
167
}
159
168
160
169
for _ ,tmpl := range templates {
161
170
if tmpl .ID == id {
162
- template = & tmpl
171
+ template = tmpl
163
172
}
164
173
}
165
174
166
- if template == nil {
175
+ if template . ID == uuid . Nil {
167
176
return nil ,xerrors .Errorf ("template not found: %q" ,id .String ())
168
177
}
169
178
170
- return template ,nil
179
+ return & template ,nil
171
180
}