main_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/spf13/cast"
  6. "io"
  7. "net"
  8. "net/http"
  9. "net/http/cookiejar"
  10. "os"
  11. "pmail/db"
  12. "pmail/dto/response"
  13. "pmail/models"
  14. "pmail/services/setup"
  15. "pmail/signal"
  16. "strconv"
  17. "strings"
  18. "testing"
  19. "time"
  20. )
  21. var httpClient *http.Client
  22. const TestPort = 17888
  23. var TestHost string = "http://127.0.0.1:" + cast.ToString(TestPort)
  24. func TestMain(m *testing.M) {
  25. cookeieJar, err := cookiejar.New(nil)
  26. if err != nil {
  27. panic(err)
  28. }
  29. httpClient = &http.Client{Jar: cookeieJar, Timeout: 5 * time.Second}
  30. os.Remove("config/config.json")
  31. os.Remove("config/pmail_temp.db")
  32. go func() {
  33. main()
  34. }()
  35. time.Sleep(3 * time.Second)
  36. m.Run()
  37. signal.StopChan <- true
  38. time.Sleep(3 * time.Second)
  39. }
  40. func TestMaster(t *testing.T) {
  41. t.Run("TestPort", testPort)
  42. t.Run("testDataBaseSet", testDataBaseSet)
  43. t.Run("testPwdSet", testPwdSet)
  44. t.Run("testDomainSet", testDomainSet)
  45. t.Run("testDNSSet", testDNSSet)
  46. cfg, err := setup.ReadConfig()
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. cfg.HttpsEnabled = 2
  51. cfg.HttpPort = TestPort
  52. err = setup.WriteConfig(cfg)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. t.Run("testSSLSet", testSSLSet)
  57. time.Sleep(3 * time.Second)
  58. t.Run("testLogin", testLogin)
  59. t.Run("testSendEmail", testSendEmail)
  60. time.Sleep(3 * time.Second)
  61. t.Run("testEmailList", testEmailList)
  62. t.Run("testDelEmail", testDelEmail)
  63. }
  64. func testPort(t *testing.T) {
  65. if !portCheck(TestPort) {
  66. t.Error("port check failed")
  67. }
  68. t.Log("port check passed")
  69. }
  70. func testDataBaseSet(t *testing.T) {
  71. // 获取配置
  72. ret, err := http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"database\"}"))
  73. if err != nil {
  74. t.Error(err)
  75. }
  76. data, err := readResponse(ret.Body)
  77. if err != nil {
  78. t.Error(err)
  79. }
  80. if data.ErrorNo != 0 {
  81. t.Error("Get Database Config Api Error!")
  82. }
  83. // 设置配置
  84. ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader(`
  85. {"action":"set","step":"database","db_type":"sqlite","db_dsn":"./config/pmail_temp.db"}
  86. `))
  87. if err != nil {
  88. t.Error(err)
  89. }
  90. data, err = readResponse(ret.Body)
  91. if err != nil {
  92. t.Error(err)
  93. }
  94. if data.ErrorNo != 0 {
  95. t.Error("Get Database Config Api Error!")
  96. }
  97. // 获取配置
  98. ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"database\"}"))
  99. if err != nil {
  100. t.Error(err)
  101. }
  102. data, err = readResponse(ret.Body)
  103. if err != nil {
  104. t.Error(err)
  105. }
  106. if data.ErrorNo != 0 {
  107. t.Error("Get Database Config Api Error!")
  108. }
  109. dt := data.Data.(map[string]interface{})
  110. if cast.ToString(dt["db_dsn"]) != "./config/pmail_temp.db" {
  111. t.Error("Check Database Config Api Error!")
  112. }
  113. t.Log("Database Config Api Success!")
  114. }
  115. func testPwdSet(t *testing.T) {
  116. // 获取配置
  117. ret, err := http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"password\"}"))
  118. if err != nil {
  119. t.Error(err)
  120. }
  121. data, err := readResponse(ret.Body)
  122. if err != nil {
  123. t.Error(err)
  124. }
  125. if data.ErrorNo != 0 {
  126. t.Error("Get Password Config Api Error!")
  127. }
  128. // 设置配置
  129. ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader(`
  130. {"action":"set","step":"password","account":"testCase","password":"testCase"}
  131. `))
  132. if err != nil {
  133. t.Error(err)
  134. }
  135. data, err = readResponse(ret.Body)
  136. if err != nil {
  137. t.Error(err)
  138. }
  139. if data.ErrorNo != 0 {
  140. t.Error("Set Password Config Api Error!")
  141. }
  142. // 获取配置
  143. ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"password\"}"))
  144. if err != nil {
  145. t.Error(err)
  146. }
  147. data, err = readResponse(ret.Body)
  148. if err != nil {
  149. t.Error(err)
  150. }
  151. if data.ErrorNo != 0 {
  152. t.Error("Get Password Config Api Error!")
  153. }
  154. if cast.ToString(data.Data) != "testCase" {
  155. t.Error("Check Password Config Api Error!")
  156. }
  157. t.Log("Password Config Api Success!")
  158. }
  159. func testDomainSet(t *testing.T) {
  160. // 获取配置
  161. ret, err := http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"domain\"}"))
  162. if err != nil {
  163. t.Error(err)
  164. }
  165. data, err := readResponse(ret.Body)
  166. if err != nil {
  167. t.Error(err)
  168. }
  169. if data.ErrorNo != 0 {
  170. t.Error("Get domain Config Api Error!")
  171. }
  172. // 设置配置
  173. ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader(`
  174. {"action":"set","step":"domain","smtp_domain":"test.domain","web_domain":"mail.test.domain"}
  175. `))
  176. if err != nil {
  177. t.Error(err)
  178. }
  179. data, err = readResponse(ret.Body)
  180. if err != nil {
  181. t.Error(err)
  182. }
  183. if data.ErrorNo != 0 {
  184. t.Error("Set domain Config Api Error!")
  185. }
  186. // 获取配置
  187. ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"domain\"}"))
  188. if err != nil {
  189. t.Error(err)
  190. }
  191. data, err = readResponse(ret.Body)
  192. if err != nil {
  193. t.Error(err)
  194. }
  195. if data.ErrorNo != 0 {
  196. t.Error("Get Password Config Api Error!")
  197. }
  198. dt := data.Data.(map[string]interface{})
  199. if cast.ToString(dt["smtp_domain"]) != "test.domain" {
  200. t.Error("Check domain Config Api Error!")
  201. }
  202. if cast.ToString(dt["web_domain"]) != "mail.test.domain" {
  203. t.Error("Check domain Config Api Error!")
  204. }
  205. t.Log("domain Config Api Success!")
  206. }
  207. func testDNSSet(t *testing.T) {
  208. // 获取配置
  209. ret, err := http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"dns\"}"))
  210. if err != nil {
  211. t.Error(err)
  212. }
  213. data, err := readResponse(ret.Body)
  214. if err != nil {
  215. t.Error(err)
  216. }
  217. if data.ErrorNo != 0 {
  218. t.Error("Get domain Config Api Error!")
  219. }
  220. }
  221. func testSSLSet(t *testing.T) {
  222. // 获取配置
  223. ret, err := http.Post(TestHost+"/api/setup", "application/json", strings.NewReader("{\"action\":\"get\",\"step\":\"ssl\"}"))
  224. if err != nil {
  225. t.Error(err)
  226. }
  227. data, err := readResponse(ret.Body)
  228. if err != nil {
  229. t.Error(err)
  230. }
  231. if data.ErrorNo != 0 {
  232. t.Error("Get domain Config Api Error!")
  233. }
  234. // 设置配置
  235. ret, err = http.Post(TestHost+"/api/setup", "application/json", strings.NewReader(`
  236. {"action":"set","step":"ssl","ssl_type":"1","key_path":"./config/ssl/private.key","crt_path":"./config/ssl/public.crt"}
  237. `))
  238. if err != nil {
  239. t.Error(err)
  240. }
  241. data, err = readResponse(ret.Body)
  242. if err != nil {
  243. t.Error(err)
  244. }
  245. if data.ErrorNo != 0 {
  246. t.Error("Set domain Config Api Error!")
  247. }
  248. t.Log("domain Config Api Success!")
  249. }
  250. func testLogin(t *testing.T) {
  251. ret, err := httpClient.Post(TestHost+"/api/login", "application/json", strings.NewReader("{\"account\":\"testCase\",\"password\":\"testCase\"}"))
  252. if err != nil {
  253. t.Error(err)
  254. }
  255. data, err := readResponse(ret.Body)
  256. if err != nil {
  257. t.Error(err)
  258. }
  259. if data.ErrorNo != 0 {
  260. t.Error("Get domain Config Api Error!")
  261. }
  262. }
  263. func testSendEmail(t *testing.T) {
  264. ret, err := httpClient.Post(TestHost+"/api/email/send", "application/json", strings.NewReader(`
  265. {
  266. "from": {
  267. "name": "i",
  268. "email": "i@test.domain"
  269. },
  270. "to": [
  271. {
  272. "name": "y",
  273. "email": "y@test.domain"
  274. }
  275. ],
  276. "cc": [
  277. ],
  278. "subject": "Title",
  279. "text": "text",
  280. "html": "<div>text</div>"
  281. }
  282. `))
  283. if err != nil {
  284. t.Error(err)
  285. }
  286. data, err := readResponse(ret.Body)
  287. if err != nil {
  288. t.Error(err)
  289. }
  290. if data.ErrorNo != 0 {
  291. t.Error("Send Email Api Error!")
  292. }
  293. }
  294. func testEmailList(t *testing.T) {
  295. ret, err := httpClient.Post(TestHost+"/api/email/list", "application/json", strings.NewReader(`{}`))
  296. if err != nil {
  297. t.Error(err)
  298. }
  299. data, err := readResponse(ret.Body)
  300. if err != nil {
  301. t.Error(err)
  302. }
  303. if data.ErrorNo != 0 {
  304. t.Error("Get Email List Api Error!")
  305. }
  306. dt := data.Data.(map[string]interface{})
  307. if len(dt["list"].([]interface{})) == 0 {
  308. t.Error("Email List Is Empty!")
  309. }
  310. }
  311. func testDelEmail(t *testing.T) {
  312. ret, err := httpClient.Post(TestHost+"/api/email/list", "application/json", strings.NewReader(`{}`))
  313. if err != nil {
  314. t.Error(err)
  315. }
  316. data, err := readResponse(ret.Body)
  317. if err != nil {
  318. t.Error(err)
  319. }
  320. if data.ErrorNo != 0 {
  321. t.Error("Get Email List Api Error!")
  322. }
  323. dt := data.Data.(map[string]interface{})
  324. if len(dt["list"].([]interface{})) == 0 {
  325. t.Error("Email List Is Empty!")
  326. }
  327. lst := dt["list"].([]interface{})
  328. item := lst[0].(map[string]interface{})
  329. id := cast.ToInt(item["id"])
  330. ret, err = httpClient.Post(TestHost+"/api/email/del", "application/json", strings.NewReader(fmt.Sprintf(`{
  331. "ids":[%d]
  332. }`, id)))
  333. if err != nil {
  334. t.Error(err)
  335. }
  336. data, err = readResponse(ret.Body)
  337. if err != nil {
  338. t.Error(err)
  339. }
  340. if data.ErrorNo != 0 {
  341. t.Error("Email Delete Api Error!")
  342. }
  343. var mail models.Email
  344. db.Instance.Where("id = ?", id).Get(&mail)
  345. if mail.Status != 3 {
  346. t.Error("Email Delete Api Error!")
  347. }
  348. }
  349. // portCheck 检查端口是占用
  350. func portCheck(port int) bool {
  351. l, err := net.Listen("tcp", fmt.Sprintf(":%s", strconv.Itoa(port)))
  352. if err != nil {
  353. return true
  354. }
  355. defer l.Close()
  356. return false
  357. }
  358. func readResponse(r io.Reader) (*response.Response, error) {
  359. data, err := io.ReadAll(r)
  360. if err != nil {
  361. return nil, err
  362. }
  363. ret := &response.Response{}
  364. err = json.Unmarshal(data, ret)
  365. if err != nil {
  366. return nil, err
  367. }
  368. return ret, nil
  369. }