fusenapi/server/auth/internal/logic/websocket_test.go

182 lines
4.3 KiB
Go
Raw Normal View History

2023-08-25 07:37:35 +00:00
package logic
import (
2023-09-04 03:32:17 +00:00
"bytes"
2023-09-08 10:56:17 +00:00
"encoding/json"
2023-08-25 07:37:35 +00:00
"fmt"
2023-09-08 10:56:17 +00:00
"fusenapi/initalize"
"fusenapi/model/gmodel"
2023-08-25 07:37:35 +00:00
"fusenapi/utils/wevent"
2023-09-05 07:00:45 +00:00
"io"
2023-08-25 07:37:35 +00:00
"log"
2023-09-05 07:00:45 +00:00
"net/http"
2023-09-05 10:24:20 +00:00
"net/smtp"
2023-09-08 10:56:17 +00:00
"reflect"
2023-08-25 07:37:35 +00:00
"testing"
"github.com/474420502/requests"
"github.com/google/uuid"
2023-09-05 07:00:45 +00:00
"github.com/gorilla/mux"
"github.com/rs/cors"
2023-08-25 07:37:35 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
2023-09-05 10:24:20 +00:00
func TestEmailSend(t *testing.T) {
// 设置发件人和收件人信息
from := "support@fusenpack.com"
to := []string{"474420502@qq.com"}
2023-11-10 09:14:38 +00:00
// to := []string{"qq474420502@gmail.com"}
// to := []string{"outlook_7D27A8300A1B3F01@outlook.com"}
2023-09-05 10:24:20 +00:00
// 设置smtp服务器地址,端口和认证信息
smtpServer := "smtp.gmail.com"
auth := smtp.PlainAuth("", "support@fusenpack.com", "wfbjpdgvaozjvwah", smtpServer)
2023-11-10 09:14:38 +00:00
var buf bytes.Buffer
tpls.ExecuteTemplate(&buf, "test.tpl", map[string]any{
"CompanyName": "FusenPack",
})
2023-09-05 16:06:10 +00:00
2023-11-10 09:14:38 +00:00
// msg := []byte(`Subject: Password Reset Request for Your Account;
// MIME-version: 1.0;
// Content-Type: text/html; charset="UTF-8";
// From: "Fusen";
2023-09-05 16:06:10 +00:00
2023-11-10 09:14:38 +00:00
// <html>
// <head>
// <title>Password Reset for Your {{.CompanyName}} Account</title>
// </head>
// <body>
2023-09-05 16:06:10 +00:00
2023-11-10 09:14:38 +00:00
// <p>Dear {{.UserName}},</p>
2023-09-05 16:06:10 +00:00
2023-11-10 09:14:38 +00:00
// <p>We have received your request to reset the password for your {{.CompanyName}} account.</p>
2023-09-05 16:06:10 +00:00
2023-11-10 09:14:38 +00:00
// <p>To proceed with the password reset, please click the button below to open the Reset Password page:</p>
2023-09-05 16:06:10 +00:00
2023-11-10 09:14:38 +00:00
// <div style="margin: 1em 0;">
// <a href="{{.ConfirmationLink}}" target="_blank"
// style="background-color: #008CBA; color: #FFFFFF; text-decoration: none; padding: 10px 15px; border-radius: 3px; display:inline-block;">
// Reset Password
// </a>
// </div>
2023-09-05 16:06:10 +00:00
2023-11-10 09:14:38 +00:00
// <p>Please note that this password reset confirmation link will expire in 60 minutes. If you have any further questions, feel free to reach out to us.</p>
2023-09-05 16:06:10 +00:00
2023-11-10 09:14:38 +00:00
// <p>Regards,</p>
2023-09-05 16:06:10 +00:00
2023-11-10 09:14:38 +00:00
// <p>{{.SenderName}}<br>
// {{.SenderTitle}}<br>
// {{.CompanyName}}</p>
2023-09-05 16:06:10 +00:00
2023-11-10 09:14:38 +00:00
// </body>
// </html>`)
2023-09-05 10:24:20 +00:00
2023-11-10 09:14:38 +00:00
msg := buf.Bytes()
2023-09-05 10:24:20 +00:00
// 发送邮件
2023-09-05 10:32:19 +00:00
err := smtp.SendMail(smtpServer+":587", auth, from, to, msg)
2023-09-05 10:24:20 +00:00
if err != nil {
log.Fatal(err)
}
}
2023-09-05 07:00:45 +00:00
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))
}
2023-09-04 03:32:17 +00:00
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()))
}
2023-08-25 07:37:35 +00:00
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)
}
2023-09-08 10:56:17 +00:00
func TestSql(t *testing.T) {
type MetaData struct {
Key string `gorm:"key" json:"key"`
Test string `gorm:"test" json:"test"`
}
conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen")
2023-09-18 10:44:12 +00:00
log.Println()
2023-09-08 10:56:17 +00:00
log.Println(conn.NamingStrategy.TableName(reflect.TypeOf(gmodel.FsUser{}).Name()))
updatesql := `UPDATE %s
SET metadata = CASE
WHEN metadata IS NULL THEN ?
ELSE JSON_MERGE_PATCH(metadata, ?)
END
WHERE id = ?;`
metadata := &MetaData{
Key: "mkey",
Test: "testv",
}
data, err := json.Marshal(metadata)
if err != nil {
logx.Error(err)
panic(err)
}
updatesql = fmt.Sprintf(updatesql, "fusen.fs_change_code")
log.Println(updatesql)
err = conn.Exec(updatesql, data, data, 6).Error
if err != nil {
logx.Error(err)
panic(err)
}
}