Skip to content

fix(coderd): improve use case handling in notifier for appearance fetchers #15242

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 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
8 changes: 8 additions & 0 deletions coderd/notifications/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (n *notifier) fetchAppName(ctx context.Context) (string, error) {
}
return "", xerrors.Errorf("get application name: %w", err)
}

if appName == "" {
appName = notificationsDefaultAppName
}
return appName, nil
}

Expand All @@ -49,5 +53,9 @@ func (n *notifier) fetchLogoURL(ctx context.Context) (string, error) {
}
return "", xerrors.Errorf("get logo URL: %w", err)
}

if logoURL == "" {
logoURL = notificationsDefaultLogoURL
}
return logoURL, nil
}
232 changes: 232 additions & 0 deletions coderd/notifications/fetcher_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package notifications

import (
"context"
"database/sql"
"testing"
"text/template"

"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/coderd/database/dbmock"
)

func TestNotifier_FetchHelpers(t *testing.T) {
t.Parallel()

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

ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
helpers: template.FuncMap{},
}

dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("ACME Inc.", nil)
dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("https://example.com/logo.png", nil)

ctx := context.Background()
helpers, err := n.fetchHelpers(ctx)
require.NoError(t, err)

appName, ok := helpers["app_name"].(func() string)
require.True(t, ok)
require.Equal(t, "ACME Inc.", appName())

logoURL, ok := helpers["logo_url"].(func() string)
require.True(t, ok)
require.Equal(t, "https://example.com/logo.png", logoURL())
})

t.Run("failed to fetch app name", func(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
helpers: template.FuncMap{},
}

dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("", xerrors.New("internal error"))

ctx := context.Background()
_, err := n.fetchHelpers(ctx)
require.Error(t, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think you should be able to drop require.Error if you're checking require.ErrorContains:

require.ErrorContains(t, err, "get application name")
})

t.Run("failed to fetch logo URL", func(t *testing.T) {
t.Parallel()

ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
helpers: template.FuncMap{},
}

dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("ACME Inc.", nil)
dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("", xerrors.New("internal error"))

ctx := context.Background()
_, err := n.fetchHelpers(ctx)
require.Error(t, err)
require.ErrorContains(t, err, "get logo URL")
})
}

func TestNotifier_FetchAppName(t *testing.T) {
t.Parallel()

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

ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
}

dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("ACME Inc.", nil)

ctx := context.Background()
appName, err := n.fetchAppName(ctx)
require.NoError(t, err)
require.Equal(t, "ACME Inc.", appName)
})

t.Run("No rows", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
}

dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("", sql.ErrNoRows)

ctx := context.Background()
appName, err := n.fetchAppName(ctx)
require.NoError(t, err)
require.Equal(t, notificationsDefaultAppName, appName)
})

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

ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
}

dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("", nil)

ctx := context.Background()
appName, err := n.fetchAppName(ctx)
require.NoError(t, err)
require.Equal(t, notificationsDefaultAppName, appName)
})

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

ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
}

dbmock.EXPECT().GetApplicationName(gomock.Any()).Return("", xerrors.New("internal error"))

ctx := context.Background()
_, err := n.fetchAppName(ctx)
require.Error(t, err)
})
}

func TestNotifier_FetchLogoURL(t *testing.T) {
t.Parallel()

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

ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
}

dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("https://example.com/logo.png", nil)

ctx := context.Background()
logoURL, err := n.fetchLogoURL(ctx)
require.NoError(t, err)
require.Equal(t, "https://example.com/logo.png", logoURL)
})

t.Run("No rows", func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
}

dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("", sql.ErrNoRows)

ctx := context.Background()
logoURL, err := n.fetchLogoURL(ctx)
require.NoError(t, err)
require.Equal(t, notificationsDefaultLogoURL, logoURL)
})

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

ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
}

dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("", nil)

ctx := context.Background()
logoURL, err := n.fetchLogoURL(ctx)
require.NoError(t, err)
require.Equal(t, notificationsDefaultLogoURL, logoURL)
})

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

ctrl := gomock.NewController(t)
dbmock := dbmock.NewMockStore(ctrl)

n := &notifier{
store: dbmock,
}

dbmock.EXPECT().GetLogoURL(gomock.Any()).Return("", xerrors.New("internal error"))

ctx := context.Background()
_, err := n.fetchLogoURL(ctx)
require.Error(t, err)
})
}
Loading