main_test.go 9.0 KB

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