Skip to content

Commit

Permalink
fix test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Jinnrry committed Apr 30, 2024
1 parent 6b22de8 commit 94f0503
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 124 deletions.
22 changes: 21 additions & 1 deletion server/controllers/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"pmail/dto"
"pmail/dto/response"
"pmail/i18n"
"pmail/models"
"pmail/services/rule"
"pmail/utils/address"
"pmail/utils/array"
"pmail/utils/context"
"pmail/utils/errors"
)

func GetRule(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -48,14 +50,32 @@ func UpsertRule(ctx *context.Context, w http.ResponseWriter, req *http.Request)
}
}

err = data.Encode().Save(ctx)
err = save(ctx, data.Encode())
if err != nil {
response.NewErrorResponse(response.ServerError, "server error", err).FPrint(w)
return
}
response.NewSuccessResponse("succ").FPrint(w)
}

func save(ctx *context.Context, p *models.Rule) error {

if p.Id > 0 {
_, err := db.Instance.Exec(db.WithContext(ctx, "update rule set name=? ,value = ? ,action = ?,params = ?,sort = ? where id = ?"), p.Name, p.Value, p.Action, p.Params, p.Sort, p.Id)
if err != nil {
return errors.Wrap(err)
}
return nil
} else {
_, err := db.Instance.Exec(db.WithContext(ctx, "insert into rule (name,value,user_id,action,params,sort) values (?,?,?,?,?,?)"), p.Name, p.Value, ctx.UserID, p.Action, p.Params, p.Sort)
if err != nil {
return errors.Wrap(err)
}
return nil
}

}

type delRuleReq struct {
Id int `json:"id"`
}
Expand Down
32 changes: 32 additions & 0 deletions server/db/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
_ "github.com/go-sql-driver/mysql"
_ "modernc.org/sqlite"
"pmail/config"
"pmail/models"
"pmail/utils/context"
"pmail/utils/errors"
"xorm.io/xorm"
Expand All @@ -30,6 +31,9 @@ func Init() error {
Instance.SetMaxOpenConns(100)
Instance.SetMaxIdleConns(10)

// 同步表结构
syncTables()

return nil
}

Expand All @@ -40,3 +44,31 @@ func WithContext(ctx *context.Context, sql string) string {
}
return sql
}

func syncTables() {
err := Instance.Sync2(&models.User{})
if err != nil {
panic(err)
}
err = Instance.Sync2(&models.Email{})
if err != nil {
panic(err)
}
err = Instance.Sync2(&models.Group{})
if err != nil {
panic(err)
}
err = Instance.Sync2(&models.Rule{})
if err != nil {
panic(err)
}
err = Instance.Sync2(&models.UserAuth{})
if err != nil {
panic(err)
}
err = Instance.Sync2(&models.Sessions{})
if err != nil {
panic(err)
}

}
67 changes: 0 additions & 67 deletions server/dto/parsemail/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,10 @@ import (
"bytes"
"fmt"
"github.com/emersion/go-message"
log "github.com/sirupsen/logrus"
"io"
"os"
"pmail/config"
"pmail/db"
"pmail/session"
"testing"
"time"
)

func testInit() {
// 设置日志格式为json格式
//log.SetFormatter(&log.JSONFormatter{})

log.SetReportCaller(true)
log.SetFormatter(&log.TextFormatter{
//以下设置只是为了使输出更美观
DisableColors: true,
TimestampFormat: "2006-01-02 15:03:04",
})

// 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
// 日志消息输出可以是任意的io.writer类型
log.SetOutput(os.Stdout)

// 设置日志级别为warn以上
log.SetLevel(log.TraceLevel)

var cst, _ = time.LoadLocation("Asia/Shanghai")
time.Local = cst

config.Init()
config.Instance.DkimPrivateKeyPath = "../../config/dkim/dkim.priv"
config.Instance.DbType = config.DBTypeSQLite
config.Instance.DbDSN = "../../config/pmail_temp.db"

Init()
db.Init()
session.Init()

}
func TestEmail_domainMatch(t *testing.T) {
//e := &Email{}
//dnsNames := []string{
// "*.mail.qq.com",
// "993.dav.qq.com",
// "993.eas.qq.com",
// "993.imap.qq.com",
// "993.pop.qq.com",
// "993.smtp.qq.com",
// "imap.qq.com",
// "mx1.qq.com",
// "mx2.qq.com",
// "mx3.qq.com",
// "pop.qq.com",
// "smtp.qq.com",
// "mail.qq.com",
//}
//
//fmt.Println(e.domainMatch("", dnsNames))
//fmt.Println(e.domainMatch("xjiangwei.cn", dnsNames))
//fmt.Println(e.domainMatch("qq.com", dnsNames))
//fmt.Println(e.domainMatch("test.aaa.mail.qq.com", dnsNames))
//fmt.Println(e.domainMatch("smtp.qq.com", dnsNames))
//fmt.Println(e.domainMatch("pop.qq.com", dnsNames))
//fmt.Println(e.domainMatch("test.mail.qq.com", dnsNames))

}

func Test_buildUser(t *testing.T) {
u := buildUser("Jinnrry N <[email protected]>")
if u.EmailAddress != "[email protected]" {
Expand Down Expand Up @@ -130,8 +65,6 @@ func TestEmailBuidlers(t *testing.T) {
}

func TestEmail_builder(t *testing.T) {
testInit()

e := Email{
From: buildUser("[email protected]"),
To: buildUsers([]string{"[email protected]"}),
Expand Down
29 changes: 0 additions & 29 deletions server/models/base.go
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
package models

import "pmail/db"

func SyncTables() {
err := db.Instance.Sync2(&User{})
if err != nil {
panic(err)
}
err = db.Instance.Sync2(&Email{})
if err != nil {
panic(err)
}
err = db.Instance.Sync2(&Group{})
if err != nil {
panic(err)
}
err = db.Instance.Sync2(&Rule{})
if err != nil {
panic(err)
}
err = db.Instance.Sync2(&UserAuth{})
if err != nil {
panic(err)
}
err = db.Instance.Sync2(&Sessions{})
if err != nil {
panic(err)
}
}
24 changes: 0 additions & 24 deletions server/models/rule.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package models

import (
"pmail/db"
"pmail/utils/context"
"pmail/utils/errors"
)

type Rule struct {
Id int `xorm:"id int unsigned not null pk autoincr" json:"id"`
UserId int `xorm:"user_id notnull default(0) comment('用户id')" json:"user_id"`
Expand All @@ -19,21 +13,3 @@ type Rule struct {
func (p *Rule) TableName() string {
return "rule"
}

func (p *Rule) Save(ctx *context.Context) error {

if p.Id > 0 {
_, err := db.Instance.Exec(db.WithContext(ctx, "update rule set name=? ,value = ? ,action = ?,params = ?,sort = ? where id = ?"), p.Name, p.Value, p.Action, p.Params, p.Sort, p.Id)
if err != nil {
return errors.Wrap(err)
}
return nil
} else {
_, err := db.Instance.Exec(db.WithContext(ctx, "insert into rule (name,value,user_id,action,params,sort) values (?,?,?,?,?,?)"), p.Name, p.Value, ctx.UserID, p.Action, p.Params, p.Sort)
if err != nil {
return errors.Wrap(err)
}
return nil
}

}
2 changes: 0 additions & 2 deletions server/res_init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"pmail/dto/parsemail"
"pmail/hooks"
"pmail/http_server"
"pmail/models"
"pmail/pop3_server"
"pmail/services/setup/ssl"
"pmail/session"
Expand Down Expand Up @@ -37,7 +36,6 @@ func Init(serverVersion string) {
if err != nil {
panic(err)
}
models.SyncTables()
session.Init()
hooks.Init(serverVersion)
// smtp server start
Expand Down
1 change: 0 additions & 1 deletion server/services/setup/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func SetDatabaseSettings(ctx *context.Context, dbType, dbDSN string) error {
if err != nil {
return errors.Wrap(err)
}
models.SyncTables()
return nil
}

Expand Down

0 comments on commit 94f0503

Please sign in to comment.