fusenapi/server/auth/internal/logic/websocket_test.go
2023-09-05 18:24:20 +08:00

186 lines
5.9 KiB
Go

package logic
import (
"bytes"
"fmt"
"fusenapi/utils/wevent"
"io"
"log"
"net/http"
"net/smtp"
"testing"
"github.com/474420502/requests"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/zeromicro/go-zero/core/logx"
)
func TestEmailSend(t *testing.T) {
// 设置发件人和收件人信息
from := "support@fusenpack.com"
to := []string{"474420502@qq.com"}
// 设置smtp服务器地址,端口和认证信息
smtpServer := "smtp.gmail.com"
auth := smtp.PlainAuth("", "support@fusenpack.com", "wfbjpdgvaozjvwah", smtpServer)
body := `<div>
<br>
</div>
<div>
<br>
</div>
<div>
<sign signid="1" nreadytime="1693908994426">
<hr align="left" style="margin: 0 0 10px 0;border: 0;border-bottom:1px solid #E4E5E6;height:0;line-height:0;font-size:0;padding: 20px 0 0 0;width: 50px;">
<div style="font-size:14px;font-family:Verdana;color:#000;">
<a class="xm_write_card" id="in_alias" style="white-space: normal; display: inline-block; text-decoration: none !important;font-family: -apple-system,BlinkMacSystemFont,PingFang SC,Microsoft YaHei;" href="https://wx.mail.qq.com/home/index?t=readmail_businesscard_midpage&amp;nocheck=true&amp;name=3e&amp;icon=http%3A%2F%2Fthirdqq.qlogo.cn%2Fg%3Fb%3Doidb%26k%3DeXzYkarOrzZhXZpDqM83lA%26kti%3DZOJARgAAAAI%26s%3D140%26t%3D1556039949&amp;mail=474420502%40qq.com&amp;code=WmPZN_MDFxAnvpYZtHjQqsJv-zHkDdwEwftrVq-KClPFKtdQ71RDiVxIOZ7rrthFIT9ubnRFwkPjfKb3swLDIQ" target="_blank">
<table style="white-space: normal;table-layout: fixed; padding-right: 20px;" contenteditable="false" cellpadding="0" cellspacing="0">
<tbody>
<tr valign="top">
<td style="width: 40px;min-width: 40px; padding-top:10px">
<div style="width: 38px; height: 38px; border: 1px #FFF solid; border-radius:50%; margin: 0;vertical-align: top;box-shadow: 0 0 10px 0 rgba(127,152,178,0.14);">
<img src="http://thirdqq.qlogo.cn/g?b=oidb&amp;k=eXzYkarOrzZhXZpDqM83lA&amp;kti=ZOJARgAAAAI&amp;s=140&amp;t=1556039949" style="width:100%;height:100%;border-radius:50%;pointer-events: none;">
</div>
</td>
<td style="padding: 10px 0 8px 10px;">
<div class="businessCard_name" style="font-size: 14px;color: #33312E;line-height: 20px; padding-bottom: 2px; margin:0;font-weight: 500;">
3e
</div>
<div class="businessCard_mail" style="font-size: 12px;color: #999896;line-height: 18px; margin:0;">
474420502@qq.com
</div>
</td>
</tr>
</tbody>
</table>
</a>
</div>
</sign>
</div>
<div>
&nbsp;
</div>
<div style="position: relative;">
<includetail>
<div>
<br>
</div>
<div>
<br>
</div>
<div style="font-size: 12px;font-family: Arial Narrow;padding:2px 0 2px 0;">
------------------&nbsp;原始邮件&nbsp;------------------
</div>
<div style="font-size: 12px;background:#efefef;padding:8px;">
<div>
<b>
发件人:
</b>
"support" &lt;support@fusenpack.com&gt;;
</div>
<div>
<b>
发送时间:
</b>
&nbsp;2023年9月5日(星期二) 晚上6:16
</div>
<div>
<b>
收件人:
</b>
&nbsp;"3e"&lt;474420502@qq.com&gt;;
<wbr>
</div>
<div>
</div>
<div>
<b>
主题:
</b>
&nbsp;Simple Mail
</div>
</div>
<div>
<br>
</div>
</includetail>
</div>`
// 构建邮件内容
message := []byte("To: " + to[0] + "\r\n" +
"Subject: Simple Mail\r\n" +
"\r\n" +
body)
// 发送邮件
err := smtp.SendMail(smtpServer+":587", auth, from, to, message)
if err != nil {
log.Fatal(err)
}
}
func TestPost(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/aaa", func(w http.ResponseWriter, r *http.Request) {
d, _ := io.ReadAll(r.Body)
log.Println(string(d))
}).Methods("POST")
handler := cors.Default().Handler(r)
log.Println("Server listening on :2223")
log.Fatal(http.ListenAndServe(":2223", handler))
}
func TestEmailTpl(t *testing.T) {
data := map[string]string{
"CompanyName": "companyName",
"ConfirmationLink": "confirmationLink",
"SenderName": "senderName",
"SenderTitle": "senderTitle",
}
var result bytes.Buffer
err := tpls.ExecuteTemplate(&result, "email_register.tpl", data)
if err != nil {
log.Fatal(err)
}
log.Println(string(result.String()))
}
func TestSender(t *testing.T) {
traceId := uuid.NewString()
event := wevent.NewWebsocketEventSuccess(wevent.UserEmailRegister, traceId)
event.Data = wevent.DataEmailRegister{
JwtToken: traceId,
}
tp := requests.Post(fmt.Sprintf("%s/api/websocket/common_notify", "https://server.fusen.3718.cn:9900"))
tp.SetBodyJson(requests.M{
"wid": "tGyMYX9EldtsPLZTyT6PxrRgEV615CQGEiu9Sb1XrjZ4kpTjI46sQyh7kYfVlgN9uR5Uw4KDF+S62IknmaRgSMdee1QHVtCv+VEKrMF76snR04zS1ZbWZCgX5Lv2xgHz/bZBWwJF/9u6YTy2/FetGg==",
"data": event,
})
wresp, err := tp.Execute()
if err != nil {
logx.Error(err, "traceId")
return
}
result := wresp.Json()
if result.Get("code").Int() != 200 {
logx.Error(result.Get("message"))
return
}
log.Println(result)
}