WIP
diff --git a/cmd/email-exporter/main.go b/cmd/email-exporter/main.go index 274c79c..219f067 100644 --- a/cmd/email-exporter/main.go +++ b/cmd/email-exporter/main.go
@@ -45,7 +45,7 @@ ClientSecret cmd.PasswordConfig // SalesforceBaseURL is the base URL for the Salesforce API. (e.g., - // "https://login.salesforce.com") + // "https://company.salesforce.com") SalesforceBaseURL string `validate:"required"` // PardotBaseURL is the base URL for the Pardot API. (e.g.,
diff --git a/email/exporter.go b/email/exporter.go index a424fc8..8e5201a 100644 --- a/email/exporter.go +++ b/email/exporter.go
@@ -43,6 +43,7 @@ emailCache *EmailCache emailsHandledCounter prometheus.Counter pardotErrorCounter prometheus.Counter + caseErrorCounter prometheus.Counter log blog.Logger } @@ -70,6 +71,12 @@ }) scope.MustRegister(pardotErrorCounter) + caseErrorCounter := prometheus.NewCounter(prometheus.CounterOpts{ + Name: "email_exporter_case_errors", + Help: "Total number of errors encountered when sending Cases to the Salesforce REST API", + }) + scope.MustRegister(caseErrorCounter) + impl := &ExporterImpl{ maxConcurrentRequests: maxConcurrentRequests, limiter: limiter, @@ -78,6 +85,7 @@ emailCache: cache, emailsHandledCounter: emailsHandledCounter, pardotErrorCounter: pardotErrorCounter, + caseErrorCounter: caseErrorCounter, log: logger, } impl.wake = sync.NewCond(&impl.Mutex) @@ -116,6 +124,32 @@ return &emptypb.Empty{}, nil } +// SendCase immediately submits a new Case to the Salesforce REST API using the +// provided details. Any retries are handled internally by the SalesforceClient. +func (impl *ExporterImpl) SendCase(ctx context.Context, req *emailpb.SendCaseRequest) (*emptypb.Empty, error) { + if core.IsAnyNilOrZero(req, req.Origin) { + return nil, berrors.InternalServerError("Incomplete gRPC request message") + } + + err := impl.client.SendCase(Case{ + Origin: req.Origin, + Subject: req.Subject, + Description: req.Description, + ContactEmail: req.ContactEmail, + Organization: req.Organization, + AccountId: req.AccountId, + RateLimitName: req.RateLimitName, + RateLimitTier: req.RateLimitTier, + UseCase: req.UseCase, + }) + if err != nil { + impl.caseErrorCounter.Inc() + return nil, berrors.InternalServerError("Sending Case to the Salesforce REST API: %s", err) + } + + return &emptypb.Empty{}, nil +} + // Start begins asynchronous processing of the email queue. When the parent // daemonCtx is cancelled the queue will be drained and the workers will exit. func (impl *ExporterImpl) Start(daemonCtx context.Context) {
diff --git a/email/exporter_test.go b/email/exporter_test.go index feaed6d..0843bb4 100644 --- a/email/exporter_test.go +++ b/email/exporter_test.go
@@ -55,6 +55,21 @@ return slices.Clone(m.CreatedContacts) } +func (m *mockSalesforceClientImpl) SendCase(payload Case) error { + m.Lock() + defer m.Unlock() + m.CreatedCases = append(m.CreatedCases, payload) + return nil +} + +func (m *mockSalesforceClientImpl) getCreatedCases() []Case { + m.Lock() + defer m.Unlock() + + // Return a copy to avoid race conditions. + return slices.Clone(m.CreatedCases) +} + // setup creates a new ExporterImpl, a mockSalesForceClientImpl, and the start and // cleanup functions for the ExporterImpl. Call start() to begin processing the // ExporterImpl queue and cleanup() to drain and shutdown. If start() is called, @@ -230,3 +245,63 @@ // Check that the error counter was incremented. test.AssertMetricWithLabelsEquals(t, exporter.pardotErrorCounter, prometheus.Labels{}, 1) } + +func TestSendCase(t *testing.T) { + t.Parallel() + + mockClient, clientImpl := newMockSalesforceClientImpl() + exporter := NewExporterImpl(mockClient, nil, 1000000, 5, metrics.NoopRegisterer, blog.NewMock()) + + _, err := exporter.SendCase(ctx, &emailpb.SendCaseRequest{ + Origin: "Web", + Subject: "Some Override", + Description: "Please review", + ContactEmail: "foo@example.com", + }) + test.AssertNotError(t, err, "SendCase should succeed") + + got := clientImpl.getCreatedCases() + if len(got) != 1 { + t.Fatalf("expected 1 case, got %d", len(got)) + } + test.AssertEquals(t, got[0].Origin, "Web") + test.AssertEquals(t, got[0].Subject, "Some Override") + test.AssertEquals(t, got[0].Description, "Please review") + test.AssertEquals(t, got[0].ContactEmail, "foo@example.com") + test.AssertMetricWithLabelsEquals(t, exporter.caseErrorCounter, prometheus.Labels{}, 0) +} + +type mockAlwaysFailCaseClient struct { + mockSalesforceClientImpl +} + +func (m *mockAlwaysFailCaseClient) SendCase(payload Case) error { + return fmt.Errorf("oops, lol") +} + +func TestSendCaseClientErrorIncrementsMetric(t *testing.T) { + t.Parallel() + + mockClient := &mockAlwaysFailCaseClient{} + exporter := NewExporterImpl(mockClient, nil, 1000000, 5, metrics.NoopRegisterer, blog.NewMock()) + + _, err := exporter.SendCase(ctx, &emailpb.SendCaseRequest{Origin: "Web"}) + test.AssertError(t, err, "SendCase should return error on client failure") + test.AssertMetricWithLabelsEquals(t, exporter.caseErrorCounter, prometheus.Labels{}, 1) +} + +func TestSendCaseMissingOriginValidation(t *testing.T) { + t.Parallel() + + mockClient, clientImpl := newMockSalesforceClientImpl() + exporter := NewExporterImpl(mockClient, nil, 1000000, 5, metrics.NoopRegisterer, blog.NewMock()) + + _, err := exporter.SendCase(ctx, &emailpb.SendCaseRequest{Subject: "No origin in this one, d00d"}) + test.AssertError(t, err, "SendCase should fail validation when Origin is missing") + + got := clientImpl.getCreatedCases() + if len(got) != 0 { + t.Errorf("expected 0 cases due to validation error, got %d", len(got)) + } + test.AssertMetricWithLabelsEquals(t, exporter.caseErrorCounter, prometheus.Labels{}, 0) +}
diff --git a/email/pardot.go b/email/pardot.go index 42c2b65..e4afdc0 100644 --- a/email/pardot.go +++ b/email/pardot.go
@@ -327,7 +327,7 @@ continue } - finalErr = fmt.Errorf("create case request returned status %d: %s", resp.StatusCode, respBody) + finalErr = fmt.Errorf("create case request returned status %d: %s", resp.StatusCode, redactEmail(respBody, payload.ContactEmail)) continue }
diff --git a/email/proto/exporter.pb.go b/email/proto/exporter.pb.go index 41c1674..c3b1323 100644 --- a/email/proto/exporter.pb.go +++ b/email/proto/exporter.pb.go
@@ -66,6 +66,114 @@ return nil } +type SendCaseRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + Origin string `protobuf:"bytes,1,opt,name=origin,proto3" json:"origin,omitempty"` + Subject string `protobuf:"bytes,2,opt,name=subject,proto3" json:"subject,omitempty"` + Description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"` + ContactEmail string `protobuf:"bytes,4,opt,name=contactEmail,proto3" json:"contactEmail,omitempty"` + Organization string `protobuf:"bytes,5,opt,name=organization,proto3" json:"organization,omitempty"` + AccountId string `protobuf:"bytes,6,opt,name=accountId,proto3" json:"accountId,omitempty"` + RateLimitName string `protobuf:"bytes,7,opt,name=rateLimitName,proto3" json:"rateLimitName,omitempty"` + RateLimitTier string `protobuf:"bytes,8,opt,name=rateLimitTier,proto3" json:"rateLimitTier,omitempty"` + UseCase string `protobuf:"bytes,9,opt,name=useCase,proto3" json:"useCase,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *SendCaseRequest) Reset() { + *x = SendCaseRequest{} + mi := &file_exporter_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *SendCaseRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*SendCaseRequest) ProtoMessage() {} + +func (x *SendCaseRequest) ProtoReflect() protoreflect.Message { + mi := &file_exporter_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use SendCaseRequest.ProtoReflect.Descriptor instead. +func (*SendCaseRequest) Descriptor() ([]byte, []int) { + return file_exporter_proto_rawDescGZIP(), []int{1} +} + +func (x *SendCaseRequest) GetOrigin() string { + if x != nil { + return x.Origin + } + return "" +} + +func (x *SendCaseRequest) GetSubject() string { + if x != nil { + return x.Subject + } + return "" +} + +func (x *SendCaseRequest) GetDescription() string { + if x != nil { + return x.Description + } + return "" +} + +func (x *SendCaseRequest) GetContactEmail() string { + if x != nil { + return x.ContactEmail + } + return "" +} + +func (x *SendCaseRequest) GetOrganization() string { + if x != nil { + return x.Organization + } + return "" +} + +func (x *SendCaseRequest) GetAccountId() string { + if x != nil { + return x.AccountId + } + return "" +} + +func (x *SendCaseRequest) GetRateLimitName() string { + if x != nil { + return x.RateLimitName + } + return "" +} + +func (x *SendCaseRequest) GetRateLimitTier() string { + if x != nil { + return x.RateLimitTier + } + return "" +} + +func (x *SendCaseRequest) GetUseCase() string { + if x != nil { + return x.UseCase + } + return "" +} + var File_exporter_proto protoreflect.FileDescriptor var file_exporter_proto_rawDesc = string([]byte{ @@ -75,15 +183,38 @@ 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2d, 0x0a, 0x13, 0x53, 0x65, 0x6e, 0x64, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, 0x65, 0x6d, 0x61, - 0x69, 0x6c, 0x73, 0x32, 0x4e, 0x0a, 0x08, 0x45, 0x78, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x72, 0x12, - 0x42, 0x0a, 0x0c, 0x53, 0x65, 0x6e, 0x64, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x73, 0x12, - 0x1a, 0x2e, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x43, 0x6f, 0x6e, 0x74, - 0x61, 0x63, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, - 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, - 0x70, 0x74, 0x79, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, - 0x6d, 0x2f, 0x6c, 0x65, 0x74, 0x73, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x2f, 0x62, 0x6f, - 0x75, 0x6c, 0x64, 0x65, 0x72, 0x2f, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x2f, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x69, 0x6c, 0x73, 0x22, 0xb1, 0x02, 0x0a, 0x0f, 0x53, 0x65, 0x6e, 0x64, 0x43, 0x61, 0x73, 0x65, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x72, 0x69, 0x67, 0x69, + 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x12, + 0x18, 0x0a, 0x07, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x07, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x12, 0x20, 0x0a, 0x0b, 0x64, 0x65, 0x73, + 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0b, + 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x22, 0x0a, 0x0c, 0x63, + 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x0c, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x45, 0x6d, 0x61, 0x69, 0x6c, 0x12, + 0x22, 0x0a, 0x0c, 0x6f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, + 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x6f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, + 0x69, 0x6f, 0x6e, 0x12, 0x1c, 0x0a, 0x09, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x64, + 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, + 0x64, 0x12, 0x24, 0x0a, 0x0d, 0x72, 0x61, 0x74, 0x65, 0x4c, 0x69, 0x6d, 0x69, 0x74, 0x4e, 0x61, + 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x72, 0x61, 0x74, 0x65, 0x4c, 0x69, + 0x6d, 0x69, 0x74, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x72, 0x61, 0x74, 0x65, 0x4c, + 0x69, 0x6d, 0x69, 0x74, 0x54, 0x69, 0x65, 0x72, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, + 0x72, 0x61, 0x74, 0x65, 0x4c, 0x69, 0x6d, 0x69, 0x74, 0x54, 0x69, 0x65, 0x72, 0x12, 0x18, 0x0a, + 0x07, 0x75, 0x73, 0x65, 0x43, 0x61, 0x73, 0x65, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, + 0x75, 0x73, 0x65, 0x43, 0x61, 0x73, 0x65, 0x32, 0x8a, 0x01, 0x0a, 0x08, 0x45, 0x78, 0x70, 0x6f, + 0x72, 0x74, 0x65, 0x72, 0x12, 0x42, 0x0a, 0x0c, 0x53, 0x65, 0x6e, 0x64, 0x43, 0x6f, 0x6e, 0x74, + 0x61, 0x63, 0x74, 0x73, 0x12, 0x1a, 0x2e, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x2e, 0x53, 0x65, 0x6e, + 0x64, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, + 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x3a, 0x0a, 0x08, 0x53, 0x65, 0x6e, 0x64, + 0x43, 0x61, 0x73, 0x65, 0x12, 0x16, 0x2e, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x2e, 0x53, 0x65, 0x6e, + 0x64, 0x43, 0x61, 0x73, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, + 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, + 0x6d, 0x70, 0x74, 0x79, 0x42, 0x2c, 0x5a, 0x2a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x6c, 0x65, 0x74, 0x73, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x2f, 0x62, + 0x6f, 0x75, 0x6c, 0x64, 0x65, 0x72, 0x2f, 0x65, 0x6d, 0x61, 0x69, 0x6c, 0x2f, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, }) var ( @@ -98,16 +229,19 @@ return file_exporter_proto_rawDescData } -var file_exporter_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_exporter_proto_msgTypes = make([]protoimpl.MessageInfo, 2) var file_exporter_proto_goTypes = []any{ (*SendContactsRequest)(nil), // 0: email.SendContactsRequest - (*emptypb.Empty)(nil), // 1: google.protobuf.Empty + (*SendCaseRequest)(nil), // 1: email.SendCaseRequest + (*emptypb.Empty)(nil), // 2: google.protobuf.Empty } var file_exporter_proto_depIdxs = []int32{ 0, // 0: email.Exporter.SendContacts:input_type -> email.SendContactsRequest - 1, // 1: email.Exporter.SendContacts:output_type -> google.protobuf.Empty - 1, // [1:2] is the sub-list for method output_type - 0, // [0:1] is the sub-list for method input_type + 1, // 1: email.Exporter.SendCase:input_type -> email.SendCaseRequest + 2, // 2: email.Exporter.SendContacts:output_type -> google.protobuf.Empty + 2, // 3: email.Exporter.SendCase:output_type -> google.protobuf.Empty + 2, // [2:4] is the sub-list for method output_type + 0, // [0:2] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name @@ -124,7 +258,7 @@ GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_exporter_proto_rawDesc), len(file_exporter_proto_rawDesc)), NumEnums: 0, - NumMessages: 1, + NumMessages: 2, NumExtensions: 0, NumServices: 1, },
diff --git a/email/proto/exporter.proto b/email/proto/exporter.proto index 93abcec..5fa176d 100644 --- a/email/proto/exporter.proto +++ b/email/proto/exporter.proto
@@ -7,8 +7,22 @@ service Exporter { rpc SendContacts (SendContactsRequest) returns (google.protobuf.Empty); + rpc SendCase (SendCaseRequest) returns (google.protobuf.Empty); } message SendContactsRequest { repeated string emails = 1; } + +message SendCaseRequest { + string origin = 1; + string subject = 2; + string description = 3; + string contactEmail = 4; + string organization = 5; + string accountId = 6; + string rateLimitName = 7; + string rateLimitTier = 8; + string useCase = 9; +} +
diff --git a/email/proto/exporter_grpc.pb.go b/email/proto/exporter_grpc.pb.go index 4660d0b..baad48a 100644 --- a/email/proto/exporter_grpc.pb.go +++ b/email/proto/exporter_grpc.pb.go
@@ -21,6 +21,7 @@ const ( Exporter_SendContacts_FullMethodName = "/email.Exporter/SendContacts" + Exporter_SendCase_FullMethodName = "/email.Exporter/SendCase" ) // ExporterClient is the client API for Exporter service. @@ -28,6 +29,7 @@ // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type ExporterClient interface { SendContacts(ctx context.Context, in *SendContactsRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) + SendCase(ctx context.Context, in *SendCaseRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) } type exporterClient struct { @@ -48,11 +50,22 @@ return out, nil } +func (c *exporterClient) SendCase(ctx context.Context, in *SendCaseRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(emptypb.Empty) + err := c.cc.Invoke(ctx, Exporter_SendCase_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + // ExporterServer is the server API for Exporter service. // All implementations must embed UnimplementedExporterServer // for forward compatibility. type ExporterServer interface { SendContacts(context.Context, *SendContactsRequest) (*emptypb.Empty, error) + SendCase(context.Context, *SendCaseRequest) (*emptypb.Empty, error) mustEmbedUnimplementedExporterServer() } @@ -66,6 +79,9 @@ func (UnimplementedExporterServer) SendContacts(context.Context, *SendContactsRequest) (*emptypb.Empty, error) { return nil, status.Errorf(codes.Unimplemented, "method SendContacts not implemented") } +func (UnimplementedExporterServer) SendCase(context.Context, *SendCaseRequest) (*emptypb.Empty, error) { + return nil, status.Errorf(codes.Unimplemented, "method SendCase not implemented") +} func (UnimplementedExporterServer) mustEmbedUnimplementedExporterServer() {} func (UnimplementedExporterServer) testEmbeddedByValue() {} @@ -105,6 +121,24 @@ return interceptor(ctx, in, info, handler) } +func _Exporter_SendCase_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SendCaseRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(ExporterServer).SendCase(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Exporter_SendCase_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(ExporterServer).SendCase(ctx, req.(*SendCaseRequest)) + } + return interceptor(ctx, in, info, handler) +} + // Exporter_ServiceDesc is the grpc.ServiceDesc for Exporter service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -116,6 +150,10 @@ MethodName: "SendContacts", Handler: _Exporter_SendContacts_Handler, }, + { + MethodName: "SendCase", + Handler: _Exporter_SendCase_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "exporter.proto",
diff --git a/mocks/emailexporter.go b/mocks/emailexporter.go index d595b99..cd23179 100644 --- a/mocks/emailexporter.go +++ b/mocks/emailexporter.go
@@ -26,9 +26,7 @@ // interface for mock interaction and the struct for state inspection and // modification. func NewMockSalesforceClientImpl() (email.SalesforceClient, *MockSalesforceClientImpl) { - mockImpl := &MockSalesforceClientImpl{ - CreatedContacts: []string{}, - } + mockImpl := &MockSalesforceClientImpl{} return mockImpl, mockImpl } @@ -95,3 +93,18 @@ } return &emptypb.Empty{}, nil } + +// SendCase submits a Case using the inner email.SalesforceClient. +func (m *MockExporterClientImpl) SendCase(ctx context.Context, req *emailpb.SendCaseRequest, _ ...grpc.CallOption) (*emptypb.Empty, error) { + return &emptypb.Empty{}, m.SalesforceClient.SendCase(email.Case{ + Origin: req.Origin, + Subject: req.Subject, + Description: req.Description, + ContactEmail: req.ContactEmail, + Organization: req.Organization, + AccountId: req.AccountId, + RateLimitName: req.RateLimitName, + RateLimitTier: req.RateLimitTier, + UseCase: req.UseCase, + }) +}
diff --git a/sfe/overrides.go b/sfe/overrides.go index e48c087..1a06a17 100644 --- a/sfe/overrides.go +++ b/sfe/overrides.go
@@ -760,10 +760,25 @@ return } - if sfe.ee != nil && baseFields[fundraisingFieldName] == fundraisingYesOption { + if sfe.ee != nil && baseFields[mailingListFieldName] == "true" { _, err := sfe.ee.SendContacts(r.Context(), &emailpb.SendContactsRequest{Emails: []string{baseFields[emailAddressFieldName]}}) if err != nil { - sfe.log.Errf("failed to send contact to email service: %s", err) + sfe.log.Errf("failed to send contact to email-exporter: %s", err) + } + } + + if sfe.ee != nil && baseFields[fundraisingFieldName] == fundraisingYesOption { + _, err := sfe.ee.SendCase(r.Context(), &emailpb.SendCaseRequest{ + Origin: "Web", + Subject: fmt.Sprintf("%s rate limit override request for %s", req.RateLimit, baseFields[OrganizationFieldName]), + ContactEmail: baseFields[emailAddressFieldName], + Organization: baseFields[OrganizationFieldName], + RateLimitName: req.RateLimit, + RateLimitTier: baseFields[TierFieldName], + UseCase: baseFields[useCaseFieldName], + }) + if err != nil { + sfe.log.Errf("failed to send case to email-exporter: %s", err) } }
diff --git a/test/integration/email_exporter_test.go b/test/integration/email_exporter_test.go index eb68b48..34dc2af 100644 --- a/test/integration/email_exporter_test.go +++ b/test/integration/email_exporter_test.go
@@ -3,6 +3,7 @@ package integration import ( + "bytes" "crypto/ecdsa" "crypto/elliptic" "crypto/rand" @@ -165,3 +166,93 @@ }) } } + +// getCreatedCases queries the pardot-test-srv for the list of created cases. +// Fails the test on error. +func getCreatedCases(t *testing.T, token string) []map[string]any { + t.Helper() + + req, err := http.NewRequest("GET", "http://localhost:9601/cases", nil) + test.AssertNotError(t, err, "Failed to create cases request") + req.Header.Set("Authorization", "Bearer "+token) + + resp, err := http.DefaultClient.Do(req) + test.AssertNotError(t, err, "Failed to query cases") + test.AssertEquals(t, resp.StatusCode, http.StatusOK) + defer resp.Body.Close() + + var got struct { + Cases []map[string]any `json:"cases"` + } + err = json.NewDecoder(resp.Body).Decode(&got) + test.AssertNotError(t, err, "Failed to decode cases") + return got.Cases +} + +// createCase sends a request to create a new case via pardot-test-srv and +// returns the HTTP status code and response body. Fails the test on error. +func createCase(t *testing.T, token string, payload map[string]any) (int, []byte) { + t.Helper() + + b, err := json.Marshal(payload) + test.AssertNotError(t, err, "Failed to marshal case payload") + + req, err := http.NewRequest( + "POST", + "http://localhost:9601/services/data/v64.0/sobjects/Case", + bytes.NewReader(b), + ) + test.AssertNotError(t, err, "Failed to create case POST request") + req.Header.Set("Authorization", "Bearer "+token) + req.Header.Set("Content-Type", "application/json") + + resp, err := http.DefaultClient.Do(req) + test.AssertNotError(t, err, "Failed to POST case") + defer resp.Body.Close() + + var body bytes.Buffer + _, err = body.ReadFrom(resp.Body) + test.AssertNotError(t, err, "Failed to read case response body") + + return resp.StatusCode, body.Bytes() +} + +func TestCasesAPISuccess(t *testing.T) { + t.Parallel() + + token := getOAuthToken(t) + + status, _ := createCase(t, token, map[string]any{ + "Subject": "Integration Test Case", + "Description": "Created by integration test", + "Origin": "Web", + }) + test.AssertEquals(t, status, http.StatusCreated) + + // Verify it was recorded by the fake server. + cases := getCreatedCases(t, token) + found := false + for _, c := range cases { + if c["Subject"] == "Integration Test Case" && c["Origin"] == "Web" { + found = true + break + } + } + if !found { + t.Fatalf("Expected created case to be present; got cases=%s", cases) + } +} + +func TestCasesAPIMissingOrigin(t *testing.T) { + t.Parallel() + + token := getOAuthToken(t) + + // Missing Origin should be rejected by the fake server. + status, body := createCase(t, token, map[string]any{ + "Subject": "Missing Origin Case", + "Description": "Should fail", + }) + test.AssertEquals(t, status, http.StatusBadRequest) + test.AssertContains(t, string(body), "Missing required field: Origin") +}
diff --git a/test/integration/salesforce_cases_api_fake_test.go b/test/integration/salesforce_cases_api_fake_test.go deleted file mode 100644 index 46bf1e6..0000000 --- a/test/integration/salesforce_cases_api_fake_test.go +++ /dev/null
@@ -1,102 +0,0 @@ -//go:build integration - -package integration - -import ( - "bytes" - "encoding/json" - "net/http" - "testing" - - "github.com/letsencrypt/boulder/test" -) - -// getCreatedCases queries the pardot-test-srv for the list of created cases. -// Fails the test on error. -func getCreatedCases(t *testing.T, token string) []map[string]any { - t.Helper() - - req, err := http.NewRequest("GET", "http://localhost:9601/cases", nil) - test.AssertNotError(t, err, "Failed to create cases request") - req.Header.Set("Authorization", "Bearer "+token) - - resp, err := http.DefaultClient.Do(req) - test.AssertNotError(t, err, "Failed to query cases") - test.AssertEquals(t, resp.StatusCode, http.StatusOK) - defer resp.Body.Close() - - var got struct { - Cases []map[string]any `json:"cases"` - } - err = json.NewDecoder(resp.Body).Decode(&got) - test.AssertNotError(t, err, "Failed to decode cases") - return got.Cases -} - -// createCase sends a request to create a new case via pardot-test-srv and -// returns the HTTP status code and response body. Fails the test on error. -func createCase(t *testing.T, token string, payload map[string]any) (int, []byte) { - t.Helper() - - b, err := json.Marshal(payload) - test.AssertNotError(t, err, "Failed to marshal case payload") - - req, err := http.NewRequest( - "POST", - "http://localhost:9601/services/data/v64.0/sobjects/Case", - bytes.NewReader(b), - ) - test.AssertNotError(t, err, "Failed to create case POST request") - req.Header.Set("Authorization", "Bearer "+token) - req.Header.Set("Content-Type", "application/json") - - resp, err := http.DefaultClient.Do(req) - test.AssertNotError(t, err, "Failed to POST case") - defer resp.Body.Close() - - var body bytes.Buffer - _, err = body.ReadFrom(resp.Body) - test.AssertNotError(t, err, "Failed to read case response body") - - return resp.StatusCode, body.Bytes() -} - -func TestCasesAPISuccess(t *testing.T) { - t.Parallel() - - token := getOAuthToken(t) - - status, _ := createCase(t, token, map[string]any{ - "Subject": "Integration Test Case", - "Description": "Created by integration test", - "Origin": "Web", - }) - test.AssertEquals(t, status, http.StatusCreated) - - // Verify it was recorded by the fake server. - cases := getCreatedCases(t, token) - found := false - for _, c := range cases { - if c["Subject"] == "Integration Test Case" && c["Origin"] == "Web" { - found = true - break - } - } - if !found { - t.Fatalf("Expected created case to be present; got cases=%s", cases) - } -} - -func TestCasesAPIMissingOrigin(t *testing.T) { - t.Parallel() - - token := getOAuthToken(t) - - // Missing Origin should be rejected by the fake server. - status, body := createCase(t, token, map[string]any{ - "Subject": "Missing Origin Case", - "Description": "Should fail", - }) - test.AssertEquals(t, status, http.StatusBadRequest) - test.AssertContains(t, string(body), "Missing required field: Origin") -}