Skip to content

Commit

Permalink
fix test case
Browse files Browse the repository at this point in the history
mysql初始化bug修复
  • Loading branch information
Jinnrry committed Apr 30, 2024
1 parent 94f0503 commit 756096f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ package: clean
cp README.md output/

test:
cd server && go test -v ./...
export setup_port=17888 && cd server && go test -v ./...
17 changes: 17 additions & 0 deletions server/http_server/setup_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
"io/fs"
"net/http"
"os"
"pmail/config"
"pmail/controllers"
"pmail/utils/ip"
"strings"
"time"
)

Expand All @@ -29,6 +32,20 @@ func SetupStart() {
HttpPort := 80
flag.IntVar(&HttpPort, "p", 80, "初始化阶段Http服务端口")
flag.Parse()

if HttpPort == 80 {
envs := os.Environ()
for _, env := range envs {
if strings.HasPrefix(env, "setup_port=") {
HttpPort = cast.ToInt(strings.TrimSpace(strings.ReplaceAll(env, "setup_port=", "")))
}
}
}

if HttpPort <= 0 || HttpPort > 65535 {
HttpPort = 80
}

config.Instance.SetSetupPort(HttpPort)
log.Infof("HttpServer Start On Port :%d", HttpPort)
if HttpPort == 80 {
Expand Down
41 changes: 23 additions & 18 deletions server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (

var httpClient *http.Client

const TestPort = 17888

var TestHost string = "http://127.0.0.1:" + cast.ToString(TestPort)

func TestMain(m *testing.M) {
cookeieJar, err := cookiejar.New(nil)
if err != nil {
Expand Down Expand Up @@ -53,6 +57,7 @@ func TestMaster(t *testing.T) {
t.Fatal(err)
}
cfg.HttpsEnabled = 2
cfg.HttpPort = TestPort
err = setup.WriteConfig(cfg)
if err != nil {
t.Fatal(err)
Expand All @@ -67,7 +72,7 @@ func TestMaster(t *testing.T) {
}

func testPort(t *testing.T) {
if !portCheck(80) {
if !portCheck(TestPort) {
t.Error("port check failed")
}
t.Log("port check passed")
Expand All @@ -76,7 +81,7 @@ func testPort(t *testing.T) {
func testDataBaseSet(t *testing.T) {

// 获取配置
ret, err := http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"database\"}"))
ret, err := http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"database\"}"))
if err != nil {
t.Error(err)
}
Expand All @@ -88,7 +93,7 @@ func testDataBaseSet(t *testing.T) {
t.Error("Get Database Config Api Error!")
}
// 设置配置
ret, err = http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader(`
ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader(`
{"action":"set","step":"database","db_type":"sqlite","db_dsn":"./config/pmail_temp.db"}
`))
if err != nil {
Expand All @@ -103,7 +108,7 @@ func testDataBaseSet(t *testing.T) {
}

// 获取配置
ret, err = http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"database\"}"))
ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"database\"}"))
if err != nil {
t.Error(err)
}
Expand All @@ -125,7 +130,7 @@ func testDataBaseSet(t *testing.T) {
func testPwdSet(t *testing.T) {

// 获取配置
ret, err := http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"password\"}"))
ret, err := http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"password\"}"))
if err != nil {
t.Error(err)
}
Expand All @@ -137,7 +142,7 @@ func testPwdSet(t *testing.T) {
t.Error("Get Password Config Api Error!")
}
// 设置配置
ret, err = http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader(`
ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader(`
{"action":"set","step":"password","account":"testCase","password":"testCase"}
`))
if err != nil {
Expand All @@ -152,7 +157,7 @@ func testPwdSet(t *testing.T) {
}

// 获取配置
ret, err = http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"password\"}"))
ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"password\"}"))
if err != nil {
t.Error(err)
}
Expand All @@ -173,7 +178,7 @@ func testPwdSet(t *testing.T) {

func testDomainSet(t *testing.T) {
// 获取配置
ret, err := http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"domain\"}"))
ret, err := http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"domain\"}"))
if err != nil {
t.Error(err)
}
Expand All @@ -185,7 +190,7 @@ func testDomainSet(t *testing.T) {
t.Error("Get domain Config Api Error!")
}
// 设置配置
ret, err = http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader(`
ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader(`
{"action":"set","step":"domain","smtp_domain":"test.domain","web_domain":"mail.test.domain"}
`))
if err != nil {
Expand All @@ -200,7 +205,7 @@ func testDomainSet(t *testing.T) {
}

// 获取配置
ret, err = http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"domain\"}"))
ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"domain\"}"))
if err != nil {
t.Error(err)
}
Expand All @@ -225,7 +230,7 @@ func testDomainSet(t *testing.T) {

func testDNSSet(t *testing.T) {
// 获取配置
ret, err := http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"dns\"}"))
ret, err := http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"dns\"}"))
if err != nil {
t.Error(err)
}
Expand All @@ -240,7 +245,7 @@ func testDNSSet(t *testing.T) {

func testSSLSet(t *testing.T) {
// 获取配置
ret, err := http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"ssl\"}"))
ret, err := http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"ssl\"}"))
if err != nil {
t.Error(err)
}
Expand All @@ -252,7 +257,7 @@ func testSSLSet(t *testing.T) {
t.Error("Get domain Config Api Error!")
}
// 设置配置
ret, err = http.Post("http://127.0.0.1/api/setup", "application/json", strings.NewReader(`
ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader(`
{"action":"set","step":"ssl","ssl_type":"1","key_path":"./config/ssl/private.key","crt_path":"./config/ssl/public.crt"}
`))
if err != nil {
Expand All @@ -270,7 +275,7 @@ func testSSLSet(t *testing.T) {
}

func testLogin(t *testing.T) {
ret, err := httpClient.Post("http://127.0.0.1/api/login", "application/json", strings.NewReader("{\"account\":\"testCase\",\"password\":\"testCase\"}"))
ret, err := httpClient.Post(TestHost+"/api/login", "application/json", strings.NewReader("{\"account\":\"testCase\",\"password\":\"testCase\"}"))
if err != nil {
t.Error(err)
}
Expand All @@ -284,7 +289,7 @@ func testLogin(t *testing.T) {
}

func testSendEmail(t *testing.T) {
ret, err := httpClient.Post("http://127.0.0.1/api/email/send", "application/json", strings.NewReader(`
ret, err := httpClient.Post(TestHost+"/api/email/send", "application/json", strings.NewReader(`
{
"from": {
"name": "i",
Expand Down Expand Up @@ -318,7 +323,7 @@ func testSendEmail(t *testing.T) {
}

func testEmailList(t *testing.T) {
ret, err := httpClient.Post("http://127.0.0.1/api/email/list", "application/json", strings.NewReader(`{}`))
ret, err := httpClient.Post(TestHost+"/api/email/list", "application/json", strings.NewReader(`{}`))
if err != nil {
t.Error(err)
}
Expand All @@ -336,7 +341,7 @@ func testEmailList(t *testing.T) {
}

func testDelEmail(t *testing.T) {
ret, err := httpClient.Post("http://127.0.0.1/api/email/list", "application/json", strings.NewReader(`{}`))
ret, err := httpClient.Post(TestHost+"/api/email/list", "application/json", strings.NewReader(`{}`))
if err != nil {
t.Error(err)
}
Expand All @@ -355,7 +360,7 @@ func testDelEmail(t *testing.T) {
item := lst[0].(map[string]interface{})
id := cast.ToInt(item["id"])

ret, err = httpClient.Post("http://127.0.0.1/api/email/del", "application/json", strings.NewReader(fmt.Sprintf(`{
ret, err = httpClient.Post(TestHost+"/api/email/del", "application/json", strings.NewReader(fmt.Sprintf(`{
"ids":[%d]
}`, id)))
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion server/models/base.go

This file was deleted.

2 changes: 1 addition & 1 deletion server/models/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type Email struct {
Id int `xorm:"id pk unsigned int autoincr notnull default(0)" json:"id"`
Id int `xorm:"id pk unsigned int autoincr notnull" json:"id"`
Type int8 `xorm:"type tinyint(4) notnull default(0) comment('邮件类型,0:收到的邮件,1:发送的邮件')" json:"type"`
GroupId int `xorm:"group_id int notnull default(0) comment('分组id')'" json:"group_id"`
Subject string `xorm:"subject varchar(1000) notnull default('') comment('邮件标题')" json:"subject"`
Expand Down

0 comments on commit 756096f

Please sign in to comment.