Skip to content

Commit a5f4625

Browse files
committed
test(notifications): add test for fetchers
1 parent dabeac9 commit a5f4625

File tree

1 file changed

+232
-0
lines changed

1 file changed

+232
-0
lines changed

coderd/notifications/fetcher_test.go

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
package notifications
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"testing"
7+
"text/template"
8+
9+
"github.com/stretchr/testify/require"
10+
"go.uber.org/mock/gomock"
11+
"golang.org/x/xerrors"
12+
13+
"github.com/coder/coder/v2/coderd/database/dbmock"
14+
)
15+
16+
func TestNotifier_FetchHelpers(t *testing.T) {
17+
t.Parallel()
18+
19+
t.Run("ok", func(t *testing.T) {
20+
t.Parallel()
21+
22+
ctrl := gomock.NewController(t)
23+
dbmock := dbmock.NewMockStore(ctrl)
24+
25+
n := &notifier{
26+
store: dbmock,
27+
helpers: template.FuncMap{},
28+
}
29+
30+
dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("ACME Inc.", nil)
31+
dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("https://example.com/logo.png", nil)
32+
33+
ctx := context.Background()
34+
helpers, err := n.fetchHelpers(ctx)
35+
require.NoError(t, err)
36+
37+
appName, ok := helpers["app_name"].(func() string)
38+
require.True(t, ok)
39+
require.Equal(t, "ACME Inc.", appName())
40+
41+
logoURL, ok := helpers["logo_url"].(func() string)
42+
require.True(t, ok)
43+
require.Equal(t, "https://example.com/logo.png", logoURL())
44+
})
45+
46+
t.Run("failed to fetch app name", func(t *testing.T) {
47+
t.Parallel()
48+
49+
ctrl := gomock.NewController(t)
50+
dbmock := dbmock.NewMockStore(ctrl)
51+
52+
n := &notifier{
53+
store: dbmock,
54+
helpers: template.FuncMap{},
55+
}
56+
57+
dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("", xerrors.New("internal error"))
58+
59+
ctx := context.Background()
60+
_, err := n.fetchHelpers(ctx)
61+
require.Error(t, err)
62+
require.ErrorContains(t, err, "get application name")
63+
})
64+
65+
t.Run("failed to fetch logo URL", func(t *testing.T) {
66+
t.Parallel()
67+
68+
ctrl := gomock.NewController(t)
69+
dbmock := dbmock.NewMockStore(ctrl)
70+
71+
n := &notifier{
72+
store: dbmock,
73+
helpers: template.FuncMap{},
74+
}
75+
76+
dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("ACME Inc.", nil)
77+
dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("", xerrors.New("internal error"))
78+
79+
ctx := context.Background()
80+
_, err := n.fetchHelpers(ctx)
81+
require.Error(t, err)
82+
require.ErrorContains(t, err, "get logo URL")
83+
})
84+
}
85+
86+
func TestNotifier_FetchAppName(t *testing.T) {
87+
t.Parallel()
88+
89+
t.Run("ok", func(t *testing.T) {
90+
t.Parallel()
91+
92+
ctrl := gomock.NewController(t)
93+
dbmock := dbmock.NewMockStore(ctrl)
94+
95+
n := &notifier{
96+
store: dbmock,
97+
}
98+
99+
dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("ACME Inc.", nil)
100+
101+
ctx := context.Background()
102+
appName, err := n.fetchAppName(ctx)
103+
require.NoError(t, err)
104+
require.Equal(t, "ACME Inc.", appName)
105+
})
106+
107+
t.Run("No rows", func(t *testing.T) {
108+
t.Parallel()
109+
ctrl := gomock.NewController(t)
110+
dbmock := dbmock.NewMockStore(ctrl)
111+
112+
n := &notifier{
113+
store: dbmock,
114+
}
115+
116+
dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("", sql.ErrNoRows)
117+
118+
ctx := context.Background()
119+
appName, err := n.fetchAppName(ctx)
120+
require.NoError(t, err)
121+
require.Equal(t, notificationsDefaultAppName, appName)
122+
})
123+
124+
t.Run("Empty string", func(t *testing.T) {
125+
t.Parallel()
126+
127+
ctrl := gomock.NewController(t)
128+
dbmock := dbmock.NewMockStore(ctrl)
129+
130+
n := &notifier{
131+
store: dbmock,
132+
}
133+
134+
dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("", nil)
135+
136+
ctx := context.Background()
137+
appName, err := n.fetchAppName(ctx)
138+
require.NoError(t, err)
139+
require.Equal(t, notificationsDefaultAppName, appName)
140+
})
141+
142+
t.Run("internal error", func(t *testing.T) {
143+
t.Parallel()
144+
145+
ctrl := gomock.NewController(t)
146+
dbmock := dbmock.NewMockStore(ctrl)
147+
148+
n := &notifier{
149+
store: dbmock,
150+
}
151+
152+
dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("", xerrors.New("internal error"))
153+
154+
ctx := context.Background()
155+
_, err := n.fetchAppName(ctx)
156+
require.Error(t, err)
157+
})
158+
}
159+
160+
func TestNotifier_FetchLogoURL(t *testing.T) {
161+
t.Parallel()
162+
163+
t.Run("ok", func(t *testing.T) {
164+
t.Parallel()
165+
166+
ctrl := gomock.NewController(t)
167+
dbmock := dbmock.NewMockStore(ctrl)
168+
169+
n := &notifier{
170+
store: dbmock,
171+
}
172+
173+
dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("https://example.com/logo.png", nil)
174+
175+
ctx := context.Background()
176+
logoURL, err := n.fetchLogoURL(ctx)
177+
require.NoError(t, err)
178+
require.Equal(t, "https://example.com/logo.png", logoURL)
179+
})
180+
181+
t.Run("No rows", func(t *testing.T) {
182+
t.Parallel()
183+
ctrl := gomock.NewController(t)
184+
dbmock := dbmock.NewMockStore(ctrl)
185+
186+
n := &notifier{
187+
store: dbmock,
188+
}
189+
190+
dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("", sql.ErrNoRows)
191+
192+
ctx := context.Background()
193+
logoURL, err := n.fetchLogoURL(ctx)
194+
require.NoError(t, err)
195+
require.Equal(t, notificationsDefaultLogoURL, logoURL)
196+
})
197+
198+
t.Run("Empty string", func(t *testing.T) {
199+
t.Parallel()
200+
201+
ctrl := gomock.NewController(t)
202+
dbmock := dbmock.NewMockStore(ctrl)
203+
204+
n := &notifier{
205+
store: dbmock,
206+
}
207+
208+
dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("", nil)
209+
210+
ctx := context.Background()
211+
logoURL, err := n.fetchLogoURL(ctx)
212+
require.NoError(t, err)
213+
require.Equal(t, notificationsDefaultLogoURL, logoURL)
214+
})
215+
216+
t.Run("internal error", func(t *testing.T) {
217+
t.Parallel()
218+
219+
ctrl := gomock.NewController(t)
220+
dbmock := dbmock.NewMockStore(ctrl)
221+
222+
n := &notifier{
223+
store: dbmock,
224+
}
225+
226+
dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("", xerrors.New("internal error"))
227+
228+
ctx := context.Background()
229+
_, err := n.fetchLogoURL(ctx)
230+
require.Error(t, err)
231+
})
232+
}

0 commit comments

Comments
 (0)