github.com/letsencrypt/boulder@v0.20251208.0/mocks/emailexporter.go (about) 1 package mocks 2 3 import ( 4 "context" 5 "slices" 6 "sync" 7 8 "google.golang.org/grpc" 9 "google.golang.org/protobuf/types/known/emptypb" 10 11 "github.com/letsencrypt/boulder/email" 12 emailpb "github.com/letsencrypt/boulder/email/proto" 13 ) 14 15 var _ email.SalesforceClient = (*MockSalesforceClientImpl)(nil) 16 17 // MockSalesforceClientImpl is a mock implementation of email.SalesforceClient. 18 type MockSalesforceClientImpl struct { 19 sync.Mutex 20 CreatedContacts []string 21 CreatedCases []email.Case 22 } 23 24 // NewMockSalesforceClientImpl returns a MockSalesforceClientImpl, which implements 25 // the PardotClient interface. It returns the underlying concrete type, so callers 26 // have access to its struct members and helper methods. 27 func NewMockSalesforceClientImpl() *MockSalesforceClientImpl { 28 return &MockSalesforceClientImpl{} 29 } 30 31 // SendContact adds an email to CreatedContacts. 32 func (m *MockSalesforceClientImpl) SendContact(email string) error { 33 m.Lock() 34 defer m.Unlock() 35 36 m.CreatedContacts = append(m.CreatedContacts, email) 37 return nil 38 } 39 40 // GetCreatedContacts is used for testing to retrieve the list of created 41 // contacts in a thread-safe manner. 42 func (m *MockSalesforceClientImpl) GetCreatedContacts() []string { 43 m.Lock() 44 defer m.Unlock() 45 46 // Return a copy to avoid race conditions. 47 return slices.Clone(m.CreatedContacts) 48 } 49 50 // SendCase adds a case payload to CreatedCases. 51 func (m *MockSalesforceClientImpl) SendCase(payload email.Case) error { 52 m.Lock() 53 defer m.Unlock() 54 55 m.CreatedCases = append(m.CreatedCases, payload) 56 return nil 57 } 58 59 // GetCreatedCases is used for testing to retrieve the list of created cases in 60 // a thread-safe manner. 61 func (m *MockSalesforceClientImpl) GetCreatedCases() []email.Case { 62 m.Lock() 63 defer m.Unlock() 64 65 // Return a copy to avoid race conditions. 66 return slices.Clone(m.CreatedCases) 67 } 68 69 var _ emailpb.ExporterClient = (*MockExporterClientImpl)(nil) 70 71 // MockExporterClientImpl is a mock implementation of ExporterClient. 72 type MockExporterClientImpl struct { 73 SalesforceClient email.SalesforceClient 74 } 75 76 // NewMockExporterImpl returns a MockExporterClientImpl as an ExporterClient. 77 func NewMockExporterImpl(salesforceClient email.SalesforceClient) emailpb.ExporterClient { 78 return &MockExporterClientImpl{ 79 SalesforceClient: salesforceClient, 80 } 81 } 82 83 // SendContacts submits emails to the inner email.SalesforceClient, returning an 84 // error if any fail. 85 func (m *MockExporterClientImpl) SendContacts(ctx context.Context, req *emailpb.SendContactsRequest, _ ...grpc.CallOption) (*emptypb.Empty, error) { 86 for _, e := range req.Emails { 87 err := m.SalesforceClient.SendContact(e) 88 if err != nil { 89 return nil, err 90 } 91 } 92 return &emptypb.Empty{}, nil 93 } 94 95 // SendCase submits a Case using the inner email.SalesforceClient. 96 func (m *MockExporterClientImpl) SendCase(ctx context.Context, req *emailpb.SendCaseRequest, _ ...grpc.CallOption) (*emptypb.Empty, error) { 97 return &emptypb.Empty{}, m.SalesforceClient.SendCase(email.Case{ 98 Origin: req.Origin, 99 Subject: req.Subject, 100 Description: req.Description, 101 ContactEmail: req.ContactEmail, 102 Organization: req.Organization, 103 AccountId: req.AccountId, 104 RateLimitName: req.RateLimitName, 105 RateLimitTier: req.RateLimitTier, 106 UseCase: req.UseCase, 107 }) 108 }