Skip to content

Commit e665228

Browse files
committed
system
1 parent 8c13399 commit e665228

3 files changed

Lines changed: 48 additions & 14 deletions

File tree

internal/service/email_service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ func (s *EmailService) SendEmailForTemplate(ctx context.Context, request domain.
295295
TrackingSettings: trackingSettings,
296296
}
297297

298-
// Compile the template with the message data
299-
compiledTemplate, err := s.templateService.CompileTemplate(ctx, compileTemplateRequest)
298+
// Compile the template with the message data (use system context to bypass authentication)
299+
compiledTemplate, err := s.templateService.CompileTemplate(systemCtx, compileTemplateRequest)
300300
if err != nil {
301301
s.logger.WithFields(map[string]interface{}{
302302
"error": err.Error(),

internal/service/template_service.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,22 @@ func (s *TemplateService) DeleteTemplate(ctx context.Context, workspaceID string
203203
}
204204

205205
func (s *TemplateService) CompileTemplate(ctx context.Context, payload domain.CompileTemplateRequest) (*domain.CompileTemplateResponse, error) {
206-
// Check if user is already authenticated in context
207-
if user := ctx.Value("authenticated_user"); user == nil {
208-
// Authenticate user for workspace
209-
var user *domain.User
210-
var err error
211-
ctx, user, err = s.authService.AuthenticateUserForWorkspace(ctx, payload.WorkspaceID)
212-
if err != nil {
213-
// Return standard Go error for non-compilation issues
214-
return nil, fmt.Errorf("failed to authenticate user: %w", err)
206+
// Check if this is a system call that should bypass authentication
207+
if ctx.Value("system_call") == nil {
208+
// Check if user is already authenticated in context
209+
if user := ctx.Value("authenticated_user"); user == nil {
210+
// Authenticate user for workspace
211+
var user *domain.User
212+
var err error
213+
ctx, user, err = s.authService.AuthenticateUserForWorkspace(ctx, payload.WorkspaceID)
214+
if err != nil {
215+
// Return standard Go error for non-compilation issues
216+
return nil, fmt.Errorf("failed to authenticate user: %w", err)
217+
}
218+
219+
// Store user in context for future use
220+
ctx = context.WithValue(ctx, "authenticated_user", user)
215221
}
216-
217-
// Store user in context for future use
218-
ctx = context.WithValue(ctx, "authenticated_user", user)
219222
}
220223

221224
payload.TrackingSettings.Endpoint = s.apiEndpoint

internal/service/template_service_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,37 @@ func TestCompileTemplate_AuthError(t *testing.T) {
780780
assert.ErrorIs(t, err, authErr, "Original auth error should be wrapped")
781781
}
782782

783+
func TestCompileTemplate_SystemCallBypassesAuth(t *testing.T) {
784+
ctrl := gomock.NewController(t)
785+
defer ctrl.Finish()
786+
787+
mockAuthService := domainmocks.NewMockAuthService(ctrl)
788+
mockRepo := domainmocks.NewMockTemplateRepository(ctrl)
789+
mockLogger := &MockLogger{}
790+
791+
svc := service.NewTemplateService(mockRepo, mockAuthService, mockLogger, "https://api.example.com")
792+
793+
// Create a system context that should bypass authentication
794+
ctx := context.WithValue(context.Background(), "system_call", true)
795+
workspaceID := "ws_123"
796+
testTree := createValidTestTree(createTestTextBlock("txt1", "Test"))
797+
798+
// No auth service call expected since this is a system call
799+
800+
// --- Act ---
801+
resp, err := svc.CompileTemplate(ctx, domain.CompileTemplateRequest{
802+
WorkspaceID: workspaceID,
803+
VisualEditorTree: testTree,
804+
TemplateData: nil,
805+
TrackingSettings: notifuse_mjml.TrackingSettings{},
806+
})
807+
808+
// --- Assert ---
809+
require.NoError(t, err)
810+
require.NotNil(t, resp)
811+
assert.True(t, resp.Success)
812+
}
813+
783814
func TestCompileTemplate_InvalidTreeData(t *testing.T) {
784815
ctrl := gomock.NewController(t)
785816
defer ctrl.Finish()

0 commit comments

Comments
 (0)