github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/pkg/ev/evsmtp/send_mail_test.go (about)

     1  package evsmtp
     2  
     3  import (
     4  	"crypto/tls"
     5  	"github.com/go-email-validator/go-email-validator/pkg/ev/evsmtp/smtpclient"
     6  	mocksmtpclient "github.com/go-email-validator/go-email-validator/test/mock/ev/evsmtp/smtpclient"
     7  	"github.com/golang/mock/gomock"
     8  	"io"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestNewSendMail(t *testing.T) {
    14  	tlsConfig := &tls.Config{}
    15  
    16  	type args struct {
    17  		tlsConfig *tls.Config
    18  	}
    19  	tt := struct {
    20  		name string
    21  		args args
    22  		want SendMail
    23  	}{
    24  		args: args{
    25  			tlsConfig: tlsConfig,
    26  		},
    27  		want: &sendMail{
    28  			tlsConfig: tlsConfig,
    29  		},
    30  	}
    31  
    32  	if got := NewSendMail(nil, tt.args.tlsConfig); !reflect.DeepEqual(got, tt.want) {
    33  		t.Errorf("NewSendMail() = %v, want %v", got, tt.want)
    34  	}
    35  }
    36  
    37  func Test_sendMail_Client(t *testing.T) {
    38  	type fields struct {
    39  		client    smtpclient.SMTPClient
    40  		TLSConfig *tls.Config
    41  	}
    42  	tests := []struct {
    43  		name   string
    44  		fields fields
    45  		want   interface{}
    46  	}{
    47  		{
    48  			name: "filled",
    49  			fields: fields{
    50  				client: simpleClient,
    51  			},
    52  			want: simpleClient,
    53  		},
    54  		{
    55  			name: "nil",
    56  			fields: fields{
    57  				client: nil,
    58  			},
    59  			want: nil,
    60  		},
    61  	}
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			s := &sendMail{
    65  				client:    tt.fields.client,
    66  				tlsConfig: tt.fields.TLSConfig,
    67  			}
    68  			if got := s.Client(); !reflect.DeepEqual(got, tt.want) {
    69  				t.Errorf("Client() = %v, want %v", got, tt.want)
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func Test_sendMail_Close(t *testing.T) {
    76  	ctrl := gomock.NewController(t)
    77  	defer ctrl.Finish()
    78  
    79  	type fields struct {
    80  		client    func() smtpclient.SMTPClient
    81  		TLSConfig *tls.Config
    82  	}
    83  	tests := []struct {
    84  		name   string
    85  		fields fields
    86  		want   error
    87  	}{
    88  		{
    89  			name: "success",
    90  			fields: fields{
    91  				client: func() smtpclient.SMTPClient {
    92  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
    93  					smtpMock.EXPECT().Close().Return(nil).Times(1)
    94  
    95  					return smtpMock
    96  				},
    97  			},
    98  			want: nil,
    99  		},
   100  		{
   101  			name: "with error",
   102  			fields: fields{
   103  				client: func() smtpclient.SMTPClient {
   104  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   105  					smtpMock.EXPECT().Close().Return(errorSimple).Times(1)
   106  
   107  					return smtpMock
   108  				},
   109  			},
   110  			want: errorSimple,
   111  		},
   112  	}
   113  	for _, tt := range tests {
   114  		t.Run(tt.name, func(t *testing.T) {
   115  			s := &sendMail{
   116  				client:    tt.fields.client(),
   117  				tlsConfig: tt.fields.TLSConfig,
   118  			}
   119  			if err := s.Close(); !reflect.DeepEqual(tt.want, err) {
   120  				t.Errorf("Close() error = %v, wantErr %v", err, tt.want)
   121  			}
   122  		})
   123  	}
   124  }
   125  
   126  func Test_sendMail_Data(t *testing.T) {
   127  	ctrl := gomock.NewController(t)
   128  	defer ctrl.Finish()
   129  
   130  	type fields struct {
   131  		client    func() smtpclient.SMTPClient
   132  		TLSConfig *tls.Config
   133  	}
   134  	tests := []struct {
   135  		name    string
   136  		fields  fields
   137  		want    io.WriteCloser
   138  		wantErr error
   139  	}{
   140  		{
   141  			name: "success",
   142  			fields: fields{
   143  				client: func() smtpclient.SMTPClient {
   144  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   145  					smtpMock.EXPECT().Data().Return(mockWriterInstance, nil).Times(1)
   146  
   147  					return smtpMock
   148  				},
   149  			},
   150  			want:    mockWriterInstance,
   151  			wantErr: nil,
   152  		},
   153  		{
   154  			name: "error",
   155  			fields: fields{
   156  				client: func() smtpclient.SMTPClient {
   157  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   158  					smtpMock.EXPECT().Data().Return(nil, errorSimple).Times(1)
   159  
   160  					return smtpMock
   161  				},
   162  			},
   163  			want:    nil,
   164  			wantErr: errorSimple,
   165  		},
   166  	}
   167  	for _, tt := range tests {
   168  		t.Run(tt.name, func(t *testing.T) {
   169  			s := &sendMail{
   170  				client:    tt.fields.client(),
   171  				tlsConfig: tt.fields.TLSConfig,
   172  			}
   173  			got, err := s.Data()
   174  			if !reflect.DeepEqual(tt.wantErr, err) {
   175  				t.Errorf("Data() error = %v, wantErr %v", err, tt.wantErr)
   176  				return
   177  			}
   178  			if !reflect.DeepEqual(got, tt.want) {
   179  				t.Errorf("Data() got = %v, want %v", got, tt.want)
   180  			}
   181  		})
   182  	}
   183  }
   184  
   185  func Test_sendMail_Hello(t *testing.T) {
   186  	ctrl := gomock.NewController(t)
   187  	defer ctrl.Finish()
   188  
   189  	type fields struct {
   190  		client    func() smtpclient.SMTPClient
   191  		TLSConfig *tls.Config
   192  	}
   193  	type args struct {
   194  		localName string
   195  	}
   196  	tests := []struct {
   197  		name    string
   198  		fields  fields
   199  		args    args
   200  		wantErr error
   201  	}{
   202  		{
   203  			name: "success",
   204  			fields: fields{
   205  				client: func() smtpclient.SMTPClient {
   206  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   207  					smtpMock.EXPECT().Hello(helloName).Return(nil).Times(1)
   208  
   209  					return smtpMock
   210  				},
   211  			},
   212  			args: args{
   213  				localName: helloName,
   214  			},
   215  			wantErr: nil,
   216  		},
   217  		{
   218  			name: "error",
   219  			fields: fields{
   220  				client: func() smtpclient.SMTPClient {
   221  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   222  					smtpMock.EXPECT().Hello(emptyLocalName).Return(errorSimple).Times(1)
   223  
   224  					return smtpMock
   225  				},
   226  			},
   227  			args: args{
   228  				localName: emptyLocalName,
   229  			},
   230  			wantErr: errorSimple,
   231  		},
   232  	}
   233  	for _, tt := range tests {
   234  		t.Run(tt.name, func(t *testing.T) {
   235  			s := &sendMail{
   236  				client:    tt.fields.client(),
   237  				tlsConfig: tt.fields.TLSConfig,
   238  			}
   239  			if err := s.Hello(tt.args.localName); !reflect.DeepEqual(tt.wantErr, err) {
   240  				t.Errorf("Hello() error = %v, wantErr %v", err, tt.wantErr)
   241  			}
   242  		})
   243  	}
   244  }
   245  
   246  func Test_sendMail_Mail(t *testing.T) {
   247  	ctrl := gomock.NewController(t)
   248  	defer ctrl.Finish()
   249  
   250  	type fields struct {
   251  		client    func() smtpclient.SMTPClient
   252  		TLSConfig *tls.Config
   253  	}
   254  	type args struct {
   255  		from string
   256  	}
   257  	tests := []struct {
   258  		name    string
   259  		fields  fields
   260  		args    args
   261  		wantErr error
   262  	}{
   263  		{
   264  			name: "success",
   265  			fields: fields{
   266  				client: func() smtpclient.SMTPClient {
   267  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   268  					smtpMock.EXPECT().Mail(emailFromStr).Return(nil).Times(1)
   269  
   270  					return smtpMock
   271  				},
   272  			},
   273  			args: args{
   274  				from: emailFromStr,
   275  			},
   276  			wantErr: nil,
   277  		},
   278  		{
   279  			name: "error",
   280  			fields: fields{
   281  				client: func() smtpclient.SMTPClient {
   282  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   283  					smtpMock.EXPECT().Mail(emailFromStr).Return(errorSimple).Times(1)
   284  
   285  					return smtpMock
   286  				},
   287  			},
   288  			args: args{
   289  				from: emailFromStr,
   290  			},
   291  			wantErr: errorSimple,
   292  		},
   293  	}
   294  	for _, tt := range tests {
   295  		t.Run(tt.name, func(t *testing.T) {
   296  			s := &sendMail{
   297  				client:    tt.fields.client(),
   298  				tlsConfig: tt.fields.TLSConfig,
   299  			}
   300  			if err := s.Mail(tt.args.from); !reflect.DeepEqual(tt.wantErr, err) {
   301  				t.Errorf("Mail() error = %v, wantErr %v", err, tt.wantErr)
   302  			}
   303  		})
   304  	}
   305  }
   306  
   307  func Test_sendMail_Quit(t *testing.T) {
   308  	ctrl := gomock.NewController(t)
   309  	defer ctrl.Finish()
   310  
   311  	type fields struct {
   312  		client    func() smtpclient.SMTPClient
   313  		TLSConfig *tls.Config
   314  	}
   315  	tests := []struct {
   316  		name    string
   317  		fields  fields
   318  		wantErr error
   319  	}{
   320  		{
   321  			name: "success",
   322  			fields: fields{
   323  				client: func() smtpclient.SMTPClient {
   324  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   325  					smtpMock.EXPECT().Quit().Return(nil).Times(1)
   326  
   327  					return smtpMock
   328  				},
   329  			},
   330  			wantErr: nil,
   331  		},
   332  		{
   333  			name: "error",
   334  			fields: fields{
   335  				client: func() smtpclient.SMTPClient {
   336  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   337  					smtpMock.EXPECT().Quit().Return(errorSimple).Times(1)
   338  
   339  					return smtpMock
   340  				},
   341  			},
   342  			wantErr: errorSimple,
   343  		},
   344  	}
   345  	for _, tt := range tests {
   346  		t.Run(tt.name, func(t *testing.T) {
   347  			s := &sendMail{
   348  				client:    tt.fields.client(),
   349  				tlsConfig: tt.fields.TLSConfig,
   350  			}
   351  			if err := s.Quit(); !reflect.DeepEqual(tt.wantErr, err) {
   352  				t.Errorf("Quit() error = %v, wantErr %v", err, tt.wantErr)
   353  			}
   354  		})
   355  	}
   356  }
   357  
   358  func Test_sendMail_RCPTs(t *testing.T) {
   359  	ctrl := gomock.NewController(t)
   360  	defer ctrl.Finish()
   361  
   362  	addrs := []string{emailToStr, emailFromStr}
   363  	type fields struct {
   364  		client    func() smtpclient.SMTPClient
   365  		TLSConfig *tls.Config
   366  	}
   367  	type args struct {
   368  		addrs []string
   369  	}
   370  	tests := []struct {
   371  		name   string
   372  		fields fields
   373  		args   args
   374  		want   map[string]error
   375  	}{
   376  		{
   377  			name: "success",
   378  			fields: fields{
   379  				client: func() smtpclient.SMTPClient {
   380  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   381  					firstCall := smtpMock.EXPECT().Rcpt(emailToStr).Return(nil).Times(1)
   382  					smtpMock.EXPECT().Rcpt(emailFromStr).After(firstCall).Return(nil).Times(1)
   383  
   384  					return smtpMock
   385  				},
   386  			},
   387  			args: args{
   388  				addrs: addrs,
   389  			},
   390  			want: make(map[string]error),
   391  		},
   392  		{
   393  			name: "error",
   394  			fields: fields{
   395  				client: func() smtpclient.SMTPClient {
   396  					smtpMock := mocksmtpclient.NewMockSMTPClient(ctrl)
   397  					firstCall := smtpMock.EXPECT().Rcpt(emailToStr).Return(errorSimple).Times(1)
   398  					smtpMock.EXPECT().Rcpt(emailFromStr).After(firstCall).Return(nil).Times(1)
   399  
   400  					return smtpMock
   401  				},
   402  			},
   403  			args: args{
   404  				addrs: addrs,
   405  			},
   406  			want: map[string]error{emailToStr: errorSimple},
   407  		},
   408  	}
   409  	for _, tt := range tests {
   410  		t.Run(tt.name, func(t *testing.T) {
   411  			s := &sendMail{
   412  				client:    tt.fields.client(),
   413  				tlsConfig: tt.fields.TLSConfig,
   414  			}
   415  			if got := s.RCPTs(tt.args.addrs); !reflect.DeepEqual(got, tt.want) {
   416  				t.Errorf("RCPTs() got = %v, want %v", got, tt.want)
   417  			}
   418  		})
   419  	}
   420  }