Skip to content

Commit 8c13399

Browse files
committed
system call
1 parent e14a89d commit 8c13399

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

internal/service/email_service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,9 @@ func (s *EmailService) SendEmailForTemplate(ctx context.Context, request domain.
245245
"template_id": request.TemplateConfig.TemplateID,
246246
}).Debug("Preparing to send email notification")
247247

248-
// Get the template
249-
template, err := s.templateService.GetTemplateByID(ctx, request.WorkspaceID, request.TemplateConfig.TemplateID, int64(0))
248+
// Get the template (mark as system call to bypass authentication)
249+
systemCtx := context.WithValue(ctx, "system_call", true)
250+
template, err := s.templateService.GetTemplateByID(systemCtx, request.WorkspaceID, request.TemplateConfig.TemplateID, int64(0))
250251
if err != nil {
251252
s.logger.WithFields(map[string]interface{}{
252253
"error": err.Error(),

internal/service/template_service.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,14 @@ func (s *TemplateService) CreateTemplate(ctx context.Context, workspaceID string
9999
}
100100

101101
func (s *TemplateService) GetTemplateByID(ctx context.Context, workspaceID string, id string, version int64) (*domain.Template, error) {
102-
// Authenticate user for workspace
103-
var err error
104-
ctx, _, err = s.authService.AuthenticateUserForWorkspace(ctx, workspaceID)
105-
if err != nil {
106-
return nil, fmt.Errorf("failed to authenticate user: %w", err)
102+
// Check if this is a system call that should bypass authentication
103+
if ctx.Value("system_call") == nil {
104+
// Authenticate user for workspace for regular calls
105+
var err error
106+
ctx, _, err = s.authService.AuthenticateUserForWorkspace(ctx, workspaceID)
107+
if err != nil {
108+
return nil, fmt.Errorf("failed to authenticate user: %w", err)
109+
}
107110
}
108111

109112
// Get template by ID

internal/service/template_service_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,23 @@ func TestTemplateService_GetTemplateByID(t *testing.T) {
227227
assert.ErrorIs(t, err, authErr)
228228
})
229229

230+
t.Run("System Call Bypasses Authentication", func(t *testing.T) {
231+
ctrl := gomock.NewController(t)
232+
defer ctrl.Finish()
233+
templateService, mockRepo, _, _ := setupTemplateServiceTest(ctrl)
234+
235+
// Create a system context that should bypass authentication
236+
systemCtx := context.WithValue(ctx, "system_call", true)
237+
238+
// No auth service call expected since this is a system call
239+
mockRepo.EXPECT().GetTemplateByID(systemCtx, workspaceID, templateID, version).Return(expectedTemplate, nil)
240+
241+
template, err := templateService.GetTemplateByID(systemCtx, workspaceID, templateID, version)
242+
243+
assert.NoError(t, err)
244+
assert.Equal(t, expectedTemplate, template)
245+
})
246+
230247
t.Run("Not Found", func(t *testing.T) {
231248
ctrl := gomock.NewController(t)
232249
defer ctrl.Finish()

0 commit comments

Comments
 (0)