gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "context"
  6. "testing"
  7. auth_model "code.gitea.io/gitea/models/auth"
  8. "code.gitea.io/gitea/services/auth/source/smtp"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/urfave/cli/v3"
  11. )
  12. func TestAddSMTP(t *testing.T) {
  13. testCases := []struct {
  14. name string
  15. args []string
  16. source *auth_model.Source
  17. errMsg string
  18. }{
  19. {
  20. name: "missing name",
  21. args: []string{
  22. "--host", "localhost",
  23. "--port", "25",
  24. },
  25. errMsg: "name must be set",
  26. },
  27. {
  28. name: "missing host",
  29. args: []string{
  30. "--name", "test",
  31. "--port", "25",
  32. },
  33. errMsg: "host must be set",
  34. },
  35. {
  36. name: "missing port",
  37. args: []string{
  38. "--name", "test",
  39. "--host", "localhost",
  40. },
  41. errMsg: "port must be set",
  42. },
  43. {
  44. name: "valid config",
  45. args: []string{
  46. "--name", "test",
  47. "--host", "localhost",
  48. "--port", "25",
  49. },
  50. source: &auth_model.Source{
  51. Type: auth_model.SMTP,
  52. Name: "test",
  53. IsActive: true,
  54. Cfg: &smtp.Source{
  55. Auth: "PLAIN",
  56. Host: "localhost",
  57. Port: 25,
  58. },
  59. TwoFactorPolicy: "",
  60. },
  61. },
  62. {
  63. name: "valid config with options",
  64. args: []string{
  65. "--name", "test",
  66. "--host", "localhost",
  67. "--port", "25",
  68. "--auth-type", "LOGIN",
  69. "--force-smtps",
  70. "--skip-verify",
  71. "--helo-hostname", "example.com",
  72. "--disable-helo=true",
  73. "--allowed-domains", "example.com,example.org",
  74. "--skip-local-2fa",
  75. "--active=false",
  76. },
  77. source: &auth_model.Source{
  78. Type: auth_model.SMTP,
  79. Name: "test",
  80. IsActive: false,
  81. Cfg: &smtp.Source{
  82. Auth: "LOGIN",
  83. Host: "localhost",
  84. Port: 25,
  85. ForceSMTPS: true,
  86. SkipVerify: true,
  87. HeloHostname: "example.com",
  88. DisableHelo: true,
  89. AllowedDomains: "example.com,example.org",
  90. },
  91. TwoFactorPolicy: "skip",
  92. },
  93. },
  94. }
  95. for _, tc := range testCases {
  96. t.Run(tc.name, func(t *testing.T) {
  97. a := &authService{
  98. initDB: func(ctx context.Context) error {
  99. return nil
  100. },
  101. createAuthSource: func(ctx context.Context, source *auth_model.Source) error {
  102. assert.Equal(t, tc.source, source)
  103. return nil
  104. },
  105. }
  106. cmd := &cli.Command{
  107. Flags: microcmdAuthAddSMTP().Flags,
  108. Action: a.runAddSMTP,
  109. }
  110. args := []string{"smtp-test"}
  111. args = append(args, tc.args...)
  112. t.Log(args)
  113. err := cmd.Run(t.Context(), args)
  114. if tc.errMsg != "" {
  115. assert.EqualError(t, err, tc.errMsg)
  116. } else {
  117. assert.NoError(t, err)
  118. }
  119. })
  120. }
  121. }
  122. func TestUpdateSMTP(t *testing.T) {
  123. testCases := []struct {
  124. name string
  125. args []string
  126. existingAuthSource *auth_model.Source
  127. authSource *auth_model.Source
  128. errMsg string
  129. }{
  130. {
  131. name: "missing id",
  132. args: []string{
  133. "--name", "test",
  134. "--host", "localhost",
  135. "--port", "25",
  136. },
  137. errMsg: "--id flag is missing",
  138. },
  139. {
  140. name: "valid config",
  141. existingAuthSource: &auth_model.Source{
  142. ID: 1,
  143. Type: auth_model.SMTP,
  144. Name: "old name",
  145. IsActive: true,
  146. Cfg: &smtp.Source{
  147. Auth: "PLAIN",
  148. Host: "old host",
  149. Port: 26,
  150. },
  151. },
  152. args: []string{
  153. "--id", "1",
  154. "--name", "test",
  155. "--host", "localhost",
  156. "--port", "25",
  157. },
  158. authSource: &auth_model.Source{
  159. ID: 1,
  160. Type: auth_model.SMTP,
  161. Name: "test",
  162. IsActive: true,
  163. Cfg: &smtp.Source{
  164. Auth: "PLAIN",
  165. Host: "localhost",
  166. Port: 25,
  167. },
  168. },
  169. },
  170. {
  171. name: "valid config with options",
  172. existingAuthSource: &auth_model.Source{
  173. ID: 1,
  174. Type: auth_model.SMTP,
  175. Name: "old name",
  176. IsActive: true,
  177. Cfg: &smtp.Source{
  178. Auth: "PLAIN",
  179. Host: "old host",
  180. Port: 26,
  181. HeloHostname: "old.example.com",
  182. AllowedDomains: "old.example.com",
  183. },
  184. TwoFactorPolicy: "",
  185. },
  186. args: []string{
  187. "--id", "1",
  188. "--name", "test",
  189. "--host", "localhost",
  190. "--port", "25",
  191. "--auth-type", "LOGIN",
  192. "--force-smtps",
  193. "--skip-verify",
  194. "--helo-hostname", "example.com",
  195. "--disable-helo",
  196. "--allowed-domains", "example.com,example.org",
  197. "--skip-local-2fa",
  198. "--active=false",
  199. },
  200. authSource: &auth_model.Source{
  201. ID: 1,
  202. Type: auth_model.SMTP,
  203. Name: "test",
  204. IsActive: false,
  205. Cfg: &smtp.Source{
  206. Auth: "LOGIN",
  207. Host: "localhost",
  208. Port: 25,
  209. ForceSMTPS: true,
  210. SkipVerify: true,
  211. HeloHostname: "example.com",
  212. DisableHelo: true,
  213. AllowedDomains: "example.com,example.org",
  214. },
  215. TwoFactorPolicy: "skip",
  216. },
  217. },
  218. }
  219. for _, tc := range testCases {
  220. t.Run(tc.name, func(t *testing.T) {
  221. a := &authService{
  222. initDB: func(ctx context.Context) error {
  223. return nil
  224. },
  225. getAuthSourceByID: func(ctx context.Context, id int64) (*auth_model.Source, error) {
  226. return &auth_model.Source{
  227. ID: 1,
  228. Type: auth_model.SMTP,
  229. Name: "test",
  230. IsActive: true,
  231. Cfg: &smtp.Source{
  232. Auth: "PLAIN",
  233. },
  234. }, nil
  235. },
  236. updateAuthSource: func(ctx context.Context, source *auth_model.Source) error {
  237. assert.Equal(t, tc.authSource, source)
  238. return nil
  239. },
  240. }
  241. app := &cli.Command{
  242. Flags: microcmdAuthUpdateSMTP().Flags,
  243. Action: a.runUpdateSMTP,
  244. }
  245. args := []string{"smtp-tests"}
  246. args = append(args, tc.args...)
  247. err := app.Run(t.Context(), args)
  248. if tc.errMsg != "" {
  249. assert.EqualError(t, err, tc.errMsg)
  250. } else {
  251. assert.NoError(t, err)
  252. }
  253. })
  254. }
  255. }