github.com/kubevela/workflow@v0.6.0/pkg/providers/email/send_test.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package email
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"reflect"
    23  	"testing"
    24  	"time"
    25  
    26  	. "github.com/agiledragon/gomonkey/v2"
    27  	"github.com/stretchr/testify/require"
    28  	"gopkg.in/gomail.v2"
    29  
    30  	"github.com/kubevela/workflow/pkg/cue/model/value"
    31  	"github.com/kubevela/workflow/pkg/mock"
    32  	"github.com/kubevela/workflow/pkg/providers"
    33  )
    34  
    35  func TestSendEmail(t *testing.T) {
    36  	var dial *gomail.Dialer
    37  	testCases := map[string]struct {
    38  		from        string
    39  		expectedErr error
    40  		errMsg      string
    41  	}{
    42  		"success": {
    43  			from: `
    44  from: {
    45  address: "kubevela@gmail.com"
    46  alias: "kubevela-bot"
    47  password: "pwd"
    48  host: "smtp.test.com"
    49  port: 465
    50  }
    51  to: ["user1@gmail.com", "user2@gmail.com"]
    52  content: {
    53  subject: "Subject"
    54  body: "Test body."
    55  }
    56  stepID: "success"
    57  `,
    58  		},
    59  		"no-step-id": {
    60  			from:        ``,
    61  			expectedErr: errors.New("failed to lookup value: var(path=stepID) not exist"),
    62  		},
    63  		"no-sender": {
    64  			from:        `stepID:"no-sender"`,
    65  			expectedErr: errors.New("failed to lookup value: var(path=from) not exist"),
    66  		},
    67  		"no-receiver": {
    68  			from: `
    69  from: {
    70  address: "kubevela@gmail.com"
    71  alias: "kubevela-bot"
    72  password: "pwd"
    73  host: "smtp.test.com"
    74  port: 465
    75  }
    76  stepID: "no-receiver"
    77  `,
    78  			expectedErr: errors.New("failed to lookup value: var(path=to) not exist"),
    79  		},
    80  		"no-content": {
    81  			from: `
    82  from: {
    83  address: "kubevela@gmail.com"
    84  alias: "kubevela-bot"
    85  password: "pwd"
    86  host: "smtp.test.com"
    87  port: 465
    88  }
    89  to: ["user1@gmail.com", "user2@gmail.com"]
    90  stepID: "no-content"
    91  `,
    92  			expectedErr: errors.New("failed to lookup value: var(path=content) not exist"),
    93  		},
    94  		"send-fail": {
    95  			from: `
    96  from: {
    97  address: "kubevela@gmail.com"
    98  alias: "kubevela-bot"
    99  password: "pwd"
   100  host: "smtp.test.com"
   101  port: 465
   102  }
   103  to: ["user1@gmail.com", "user2@gmail.com"]
   104  content: {
   105  subject: "Subject"
   106  body: "Test body."
   107  }
   108  stepID: "send-fail"
   109  `,
   110  			errMsg: "fail to send",
   111  		},
   112  	}
   113  
   114  	for name, tc := range testCases {
   115  		t.Run(name, func(t *testing.T) {
   116  			r := require.New(t)
   117  
   118  			patch := ApplyMethod(reflect.TypeOf(dial), "DialAndSend", func(_ *gomail.Dialer, _ ...*gomail.Message) error {
   119  				return nil
   120  			})
   121  			defer patch.Reset()
   122  
   123  			act := &mock.Action{}
   124  
   125  			if tc.errMsg != "" {
   126  				patch.Reset()
   127  				patch = ApplyMethod(reflect.TypeOf(dial), "DialAndSend", func(_ *gomail.Dialer, _ ...*gomail.Message) error {
   128  					return errors.New(tc.errMsg)
   129  				})
   130  				defer patch.Reset()
   131  			}
   132  			v, err := value.NewValue(tc.from, nil, "")
   133  			r.NoError(err)
   134  			prd := &provider{}
   135  			err = prd.Send(nil, nil, v, act)
   136  			if tc.expectedErr != nil {
   137  				r.Equal(tc.expectedErr.Error(), err.Error())
   138  				return
   139  			}
   140  			r.NoError(err)
   141  			r.Equal(act.Phase, "Wait")
   142  
   143  			// mock reconcile
   144  			time.Sleep(time.Second)
   145  			err = prd.Send(nil, nil, v, act)
   146  			if tc.errMsg != "" {
   147  				r.Equal(fmt.Errorf("failed to send email: %s", tc.errMsg), err)
   148  				return
   149  			}
   150  			r.NoError(err)
   151  		})
   152  	}
   153  }
   154  
   155  func TestInstall(t *testing.T) {
   156  	p := providers.NewProviders()
   157  	Install(p)
   158  	h, ok := p.GetHandler("email", "send")
   159  	r := require.New(t)
   160  	r.Equal(ok, true)
   161  	r.Equal(h != nil, true)
   162  }