gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package admin
  4. import (
  5. "net/http"
  6. "testing"
  7. "code.gitea.io/gitea/modules/json"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/test"
  10. "code.gitea.io/gitea/services/contexttest"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestShadowPassword(t *testing.T) {
  14. kases := []struct {
  15. Provider string
  16. CfgItem string
  17. Result string
  18. }{
  19. {
  20. Provider: "redis",
  21. CfgItem: "network=tcp,addr=:6379,password=gitea,db=0,pool_size=100,idle_timeout=180",
  22. Result: "network=tcp,addr=:6379,password=******,db=0,pool_size=100,idle_timeout=180",
  23. },
  24. {
  25. Provider: "mysql",
  26. CfgItem: "root:@tcp(localhost:3306)/gitea?charset=utf8",
  27. Result: "root:******@tcp(localhost:3306)/gitea?charset=utf8",
  28. },
  29. {
  30. Provider: "mysql",
  31. CfgItem: "/gitea?charset=utf8",
  32. Result: "/gitea?charset=utf8",
  33. },
  34. {
  35. Provider: "mysql",
  36. CfgItem: "user:mypassword@/dbname",
  37. Result: "user:******@/dbname",
  38. },
  39. {
  40. Provider: "postgres",
  41. CfgItem: "user=pqgotest dbname=pqgotest sslmode=verify-full",
  42. Result: "user=pqgotest dbname=pqgotest sslmode=verify-full",
  43. },
  44. {
  45. Provider: "postgres",
  46. CfgItem: "user=pqgotest password= dbname=pqgotest sslmode=verify-full",
  47. Result: "user=pqgotest password=****** dbname=pqgotest sslmode=verify-full",
  48. },
  49. {
  50. Provider: "postgres",
  51. CfgItem: "postgres://user:pass@hostname/dbname",
  52. Result: "postgres://user:******@hostname/dbname",
  53. },
  54. {
  55. Provider: "couchbase",
  56. CfgItem: "http://dev-couchbase.example.com:8091/",
  57. Result: "http://dev-couchbase.example.com:8091/",
  58. },
  59. {
  60. Provider: "couchbase",
  61. CfgItem: "http://user:the_password@dev-couchbase.example.com:8091/",
  62. Result: "http://user:******@dev-couchbase.example.com:8091/",
  63. },
  64. }
  65. for _, k := range kases {
  66. assert.Equal(t, k.Result, shadowPassword(k.Provider, k.CfgItem))
  67. }
  68. }
  69. func TestSelfCheckPost(t *testing.T) {
  70. defer test.MockVariableValue(&setting.AppURL, "http://config/sub/")()
  71. defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
  72. ctx, resp := contexttest.MockContext(t, "GET http://host/sub/admin/self_check?location_origin=http://frontend")
  73. SelfCheckPost(ctx)
  74. assert.Equal(t, http.StatusOK, resp.Code)
  75. data := struct {
  76. Problems []string `json:"problems"`
  77. }{}
  78. err := json.Unmarshal(resp.Body.Bytes(), &data)
  79. assert.NoError(t, err)
  80. assert.Equal(t, []string{
  81. ctx.Locale.TrString("admin.self_check.location_origin_mismatch", "http://frontend/sub/", "http://config/sub/"),
  82. }, data.Problems)
  83. }