Best Venom code snippet using smtp.Run
notifications_test.go
Source:notifications_test.go
...9 "github.com/stretchr/testify/require"10)11func TestProvideService(t *testing.T) {12 bus := bus.New()13 t.Run("When invalid from_address in configuration", func(t *testing.T) {14 cfg := createSmtpConfig()15 cfg.Smtp.FromAddress = "@notanemail@"16 _, _, err := createSutWithConfig(bus, cfg)17 require.Error(t, err)18 })19 t.Run("When template_patterns fails to parse", func(t *testing.T) {20 cfg := createSmtpConfig()21 cfg.Smtp.TemplatesPatterns = append(cfg.Smtp.TemplatesPatterns, "/usr/not-a-dir/**")22 _, _, err := createSutWithConfig(bus, cfg)23 require.Error(t, err)24 })25}26func TestSendEmailSync(t *testing.T) {27 bus := bus.New()28 t.Run("When sending emails synchronously", func(t *testing.T) {29 _, mailer := createSut(t, bus)30 cmd := &models.SendEmailCommandSync{31 SendEmailCommand: models.SendEmailCommand{32 Subject: "subject",33 To: []string{"asdf@grafana.com"},34 SingleEmail: false,35 Template: "welcome_on_signup",36 },37 }38 err := bus.Dispatch(context.Background(), cmd)39 require.NoError(t, err)40 require.NotEmpty(t, mailer.Sent)41 sent := mailer.Sent[len(mailer.Sent)-1]42 require.Equal(t, "subject", sent.Subject)43 require.Equal(t, []string{"asdf@grafana.com"}, sent.To)44 })45 t.Run("When using Single Email mode with multiple recipients", func(t *testing.T) {46 _, mailer := createSut(t, bus)47 cmd := &models.SendEmailCommandSync{48 SendEmailCommand: models.SendEmailCommand{49 Subject: "subject",50 To: []string{"1@grafana.com", "2@grafana.com", "3@grafana.com"},51 SingleEmail: true,52 Template: "welcome_on_signup",53 },54 }55 err := bus.Dispatch(context.Background(), cmd)56 require.NoError(t, err)57 require.Len(t, mailer.Sent, 1)58 })59 t.Run("When using Multi Email mode with multiple recipients", func(t *testing.T) {60 _, mailer := createSut(t, bus)61 cmd := &models.SendEmailCommandSync{62 SendEmailCommand: models.SendEmailCommand{63 Subject: "subject",64 To: []string{"1@grafana.com", "2@grafana.com", "3@grafana.com"},65 SingleEmail: false,66 Template: "welcome_on_signup",67 },68 }69 err := bus.Dispatch(context.Background(), cmd)70 require.NoError(t, err)71 require.Len(t, mailer.Sent, 3)72 })73 t.Run("When attaching files to emails", func(t *testing.T) {74 _, mailer := createSut(t, bus)75 cmd := &models.SendEmailCommandSync{76 SendEmailCommand: models.SendEmailCommand{77 Subject: "subject",78 To: []string{"asdf@grafana.com"},79 SingleEmail: true,80 Template: "welcome_on_signup",81 AttachedFiles: []*models.SendEmailAttachFile{82 {83 Name: "attachment.txt",84 Content: []byte("text file content"),85 },86 },87 },88 }89 err := bus.Dispatch(context.Background(), cmd)90 require.NoError(t, err)91 require.NotEmpty(t, mailer.Sent)92 sent := mailer.Sent[len(mailer.Sent)-1]93 require.Len(t, sent.AttachedFiles, 1)94 file := sent.AttachedFiles[len(sent.AttachedFiles)-1]95 require.Equal(t, "attachment.txt", file.Name)96 require.Equal(t, []byte("text file content"), file.Content)97 })98 t.Run("When SMTP disabled in configuration", func(t *testing.T) {99 cfg := createSmtpConfig()100 cfg.Smtp.Enabled = false101 _, mailer, err := createSutWithConfig(bus, cfg)102 require.NoError(t, err)103 cmd := &models.SendEmailCommandSync{104 SendEmailCommand: models.SendEmailCommand{105 Subject: "subject",106 To: []string{"1@grafana.com", "2@grafana.com", "3@grafana.com"},107 SingleEmail: true,108 Template: "welcome_on_signup",109 },110 }111 err = bus.Dispatch(context.Background(), cmd)112 require.ErrorIs(t, err, models.ErrSmtpNotEnabled)113 require.Empty(t, mailer.Sent)114 })115 t.Run("When invalid content type in configuration", func(t *testing.T) {116 cfg := createSmtpConfig()117 cfg.Smtp.ContentTypes = append(cfg.Smtp.ContentTypes, "multipart/form-data")118 _, mailer, err := createSutWithConfig(bus, cfg)119 require.NoError(t, err)120 cmd := &models.SendEmailCommandSync{121 SendEmailCommand: models.SendEmailCommand{122 Subject: "subject",123 To: []string{"1@grafana.com", "2@grafana.com", "3@grafana.com"},124 SingleEmail: false,125 Template: "welcome_on_signup",126 },127 }128 err = bus.Dispatch(context.Background(), cmd)129 require.Error(t, err)130 require.Empty(t, mailer.Sent)131 })132 t.Run("When SMTP dialer is disconnected", func(t *testing.T) {133 _ = createDisconnectedSut(t, bus)134 cmd := &models.SendEmailCommandSync{135 SendEmailCommand: models.SendEmailCommand{136 Subject: "subject",137 To: []string{"1@grafana.com", "2@grafana.com", "3@grafana.com"},138 SingleEmail: false,139 Template: "welcome_on_signup",140 },141 }142 err := bus.Dispatch(context.Background(), cmd)143 require.Error(t, err)144 })145}146func TestSendEmailAsync(t *testing.T) {147 bus := bus.New()148 t.Run("When sending reset email password", func(t *testing.T) {149 sut, _ := createSut(t, bus)150 err := sut.sendResetPasswordEmail(context.Background(), &models.SendResetPasswordEmailCommand{User: &models.User{Email: "asd@asd.com"}})151 require.NoError(t, err)152 sentMsg := <-sut.mailQueue153 assert.Contains(t, sentMsg.Body["text/html"], "body")154 assert.NotContains(t, sentMsg.Body["text/plain"], "body")155 assert.Equal(t, "Reset your Grafana password - asd@asd.com", sentMsg.Subject)156 assert.NotContains(t, sentMsg.Body["text/html"], "Subject")157 assert.NotContains(t, sentMsg.Body["text/plain"], "Subject")158 })159 t.Run("When SMTP disabled in configuration", func(t *testing.T) {160 cfg := createSmtpConfig()161 cfg.Smtp.Enabled = false162 _, mailer, err := createSutWithConfig(bus, cfg)163 require.NoError(t, err)164 cmd := &models.SendEmailCommand{165 Subject: "subject",166 To: []string{"1@grafana.com", "2@grafana.com", "3@grafana.com"},167 SingleEmail: true,168 Template: "welcome_on_signup",169 }170 err = bus.Dispatch(context.Background(), cmd)171 require.ErrorIs(t, err, models.ErrSmtpNotEnabled)172 require.Empty(t, mailer.Sent)173 })174 t.Run("When invalid content type in configuration", func(t *testing.T) {175 cfg := createSmtpConfig()176 cfg.Smtp.ContentTypes = append(cfg.Smtp.ContentTypes, "multipart/form-data")177 _, mailer, err := createSutWithConfig(bus, cfg)178 require.NoError(t, err)179 cmd := &models.SendEmailCommand{180 Subject: "subject",181 To: []string{"1@grafana.com", "2@grafana.com", "3@grafana.com"},182 SingleEmail: false,183 Template: "welcome_on_signup",184 }185 err = bus.Dispatch(context.Background(), cmd)186 require.Error(t, err)187 require.Empty(t, mailer.Sent)188 })189 t.Run("When SMTP dialer is disconnected", func(t *testing.T) {190 _ = createDisconnectedSut(t, bus)191 cmd := &models.SendEmailCommand{192 Subject: "subject",193 To: []string{"1@grafana.com", "2@grafana.com", "3@grafana.com"},194 SingleEmail: false,195 Template: "welcome_on_signup",196 }197 err := bus.Dispatch(context.Background(), cmd)198 // The async version should not surface connection errors via Bus. It should only log them.199 require.NoError(t, err)200 })201}202func createSut(t *testing.T, bus bus.Bus) (*NotificationService, *FakeMailer) {203 t.Helper()...
api.go
Source:api.go
...42 defaultCors = "http://localhost:9090"43 defaultSmtpPort = 2544)45var cfg config.ApiConfig46func RunApiCmd() *cobra.Command {47 var runCmd = &cobra.Command{48 Use: "run-api",49 Short: "Launches the Pulse HTTP API.",50 Run: func(cmd *cobra.Command, args []string) {51 api.StartApi(&cfg)52 },53 } // End Run CMD54 // Parse arguments passed in on startup55 runCmd.Flags().StringVar(&cfg.LogLevel, logLevel, "INFO", "logging level [DEBUG,INFO,WARNING,ERROR,FATAL]")56 runCmd.Flags().IntVar(&cfg.Port, port, defaultPort, "Pulse API port number")57 runCmd.Flags().StringVar(&cfg.Env, env, dev, fmt.Sprintf("%s|%s|%s|%s", dev, staging, uat, production))58 // POSTGRESQL59 runCmd.Flags().StringVar(&cfg.DB.Dsn, dbDsn, os.Getenv("PULSE_POSTGRES_DSN"), "Postgres DSN")60 runCmd.Flags().IntVar(&cfg.DB.MaxOpenConns, dbMaxOpenConns, defaultMaxOpenConns, "PostgreSQL max open connections")61 runCmd.Flags().IntVar(&cfg.DB.MaxIdleConns, dbMaxIdleConns, defaultMaxIdleConns, "PostgreSQL max open connections")62 runCmd.Flags().StringVar(&cfg.DB.MaxIdleTime, dbMaxIdleTime, defaultMaxIdleTime, "PostgreSQL max connection idle time")63 // Alpha Vantage64 runCmd.Flags().StringVar(&cfg.AlphaVantage.BaseUrl, alphaVantageUrl, os.Getenv("ALPHA_VANTAGE_BASE_URL"), "Base Url for Alpha Vantage API - https://www.alphavantage.co/")65 runCmd.Flags().StringVar(&cfg.AlphaVantage.Token, alphaVantageToken, os.Getenv("ALPHA_VANTAGE_API_TOKEN"), "Auth Token for Alpha Vantage API - https://www.alphavantage.co/")66 // API Rate Limiter67 runCmd.Flags().Float64Var(&cfg.Limiter.RPS, rateLimiterRPS, defaultRatePerSeconds, "Rate limiter maximum requests per second")...
smtp_test.go
Source:smtp_test.go
...21 "text/plain": "Some plain text body",22 },23 ReplyTo: []string{"from@address.com"},24 }25 t.Run("When building email", func(t *testing.T) {26 email := sc.buildEmail(message)27 buf := new(bytes.Buffer)28 _, err := email.WriteTo(buf)29 require.NoError(t, err)30 assert.Contains(t, buf.String(), "Some HTML body")31 assert.Contains(t, buf.String(), "Some plain text body")32 assert.Less(t, strings.Index(buf.String(), "Some plain text body"), strings.Index(buf.String(), "Some HTML body"))33 })34}35func TestSmtpDialer(t *testing.T) {36 t.Run("When SMTP hostname is invalid", func(t *testing.T) {37 cfg := createSmtpConfig()38 cfg.Smtp.Host = "invalid%hostname:123:456"39 client, err := ProvideSmtpService(cfg)40 require.NoError(t, err)41 message := &Message{42 To: []string{"asdf@grafana.com"},43 SingleEmail: true,44 Subject: "subject",45 Body: map[string]string{46 "text/html": "body",47 "text/plain": "body",48 },49 }50 count, err := client.Send(message)51 require.Equal(t, 0, count)52 require.EqualError(t, err, "address invalid%hostname:123:456: too many colons in address")53 })54 t.Run("When SMTP port is invalid", func(t *testing.T) {55 cfg := createSmtpConfig()56 cfg.Smtp.Host = "invalid%hostname:123a"57 client, err := ProvideSmtpService(cfg)58 require.NoError(t, err)59 message := &Message{60 To: []string{"asdf@grafana.com"},61 SingleEmail: true,62 Subject: "subject",63 Body: map[string]string{64 "text/html": "body",65 "text/plain": "body",66 },67 }68 count, err := client.Send(message)69 require.Equal(t, 0, count)70 require.EqualError(t, err, "strconv.Atoi: parsing \"123a\": invalid syntax")71 })72 t.Run("When TLS certificate does not exist", func(t *testing.T) {73 cfg := createSmtpConfig()74 cfg.Smtp.Host = "localhost:1234"75 cfg.Smtp.CertFile = "/var/certs/does-not-exist.pem"76 client, err := ProvideSmtpService(cfg)77 require.NoError(t, err)78 message := &Message{79 To: []string{"asdf@grafana.com"},80 SingleEmail: true,81 Subject: "subject",82 Body: map[string]string{83 "text/html": "body",84 "text/plain": "body",85 },86 }...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!