demo_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package test
  2. import (
  3. "fmt"
  4. "gfast/library/utils"
  5. "github.com/casbin/casbin/v2"
  6. "github.com/casbin/casbin/v2/util"
  7. "github.com/gogf/gf/os/glog"
  8. "github.com/mojocn/base64Captcha"
  9. "testing"
  10. )
  11. func TestDemo(t *testing.T) {
  12. //t.Run("demo1" ,Demo1)
  13. //t.Run("Adapters_test", Adapters)
  14. t.Run("CaptchaDemo", CaptchaDemo)
  15. t.Run("CaptchaVerify", CaptchaVerify)
  16. }
  17. func Demo1(t *testing.T) {
  18. e, err := casbin.NewEnforcer("casbin_conf/model.conf", "casbin_conf/policy.csv")
  19. if err != nil {
  20. panic(err)
  21. }
  22. sub := "alice" // the user that wants to access a resource. 对象
  23. obj := "data1" // the resource that is going to be accessed. 资源
  24. act := "write" // the operation that the user performs on the resource. 操作
  25. ok, err := e.Enforce(sub, obj, act)
  26. if err != nil {
  27. fmt.Println("验证失败", err)
  28. }
  29. if ok == true {
  30. fmt.Println("权限通过")
  31. } else {
  32. fmt.Println("没有权限")
  33. }
  34. }
  35. func demoCodeCaptchaCreate() {
  36. //config struct for digits
  37. //数字验证码配置
  38. /*var configD = base64Captcha.ConfigDigit{
  39. Height: 80,
  40. Width: 240,
  41. MaxSkew: 0.7,
  42. DotCount: 80,
  43. CaptchaLen: 5,
  44. }*/
  45. //config struct for audio
  46. //声音验证码配置
  47. /*var configA = base64Captcha.ConfigAudio{
  48. CaptchaLen: 6,
  49. Language: "zh",
  50. }*/
  51. //config struct for Character
  52. //字符,公式,验证码配置
  53. var configC = base64Captcha.ConfigCharacter{
  54. Height: 60,
  55. Width: 240,
  56. //const CaptchaModeNumber:数字,CaptchaModeAlphabet:字母,CaptchaModeArithmetic:算术,CaptchaModeNumberAlphabet:数字字母混合.
  57. Mode: base64Captcha.CaptchaModeNumber,
  58. ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
  59. ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
  60. IsShowHollowLine: false,
  61. IsShowNoiseDot: false,
  62. IsShowNoiseText: false,
  63. IsShowSlimeLine: false,
  64. IsShowSineLine: false,
  65. CaptchaLen: 4,
  66. }
  67. //创建字符公式验证码.
  68. //GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
  69. idKeyC, capC := base64Captcha.GenerateCaptcha("8nM77YhE2xOvU6GMQ33A", configC)
  70. //以base64编码
  71. base64stringC := base64Captcha.CaptchaWriteToBase64Encoding(capC)
  72. fmt.Println(idKeyC, "\n", base64stringC)
  73. }
  74. func CaptchaDemo(t *testing.T) {
  75. demoCodeCaptchaCreate()
  76. }
  77. func CaptchaVerify(t *testing.T) {
  78. if base64Captcha.VerifyCaptchaAndIsClear("8nM77YhE2xOvU6GMQ33A", "0870", false) {
  79. fmt.Println("验证成功")
  80. } else {
  81. fmt.Println("验证失败")
  82. }
  83. }
  84. func Adapters(t *testing.T) {
  85. a := initAdapter(t, "mysql", "root:123456@tcp(127.0.0.1:3306)/test2")
  86. testAutoSave(t, a)
  87. testSaveLoad(t, a)
  88. a = initAdapterFormOptions(t, &utils.Adapter{
  89. DriverName: "mysql",
  90. DataSourceName: "root:123456@tcp(127.0.0.1:3306)/test2",
  91. })
  92. testAutoSave(t, a)
  93. testSaveLoad(t, a)
  94. }
  95. func initAdapterFormOptions(t *testing.T, adapter *utils.Adapter) *utils.Adapter {
  96. // Create an adapter
  97. a, _ := utils.NewAdapterFromOptions(adapter)
  98. // Initialize some policy in DB.
  99. initPolicy(t, a)
  100. // Now the DB has policy, so we can provide a normal use case.
  101. // Note: you don't need to look at the above code
  102. // if you already have a working DB with policy inside.
  103. return a
  104. }
  105. func initPolicy(t *testing.T, a *utils.Adapter) {
  106. // Because the DB is empty at first,
  107. // so we need to load the policy from the file adapter (.CSV) first.
  108. e, err := casbin.NewEnforcer("casbin_conf/rbac_model.conf", "casbin_conf/rbac_policy.csv")
  109. if err != nil {
  110. panic(err)
  111. }
  112. // This is a trick to save the current policy to the DB.
  113. // We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
  114. // The current policy means the policy in the Casbin enforcer (aka in memory).
  115. err = a.SavePolicy(e.GetModel())
  116. if err != nil {
  117. panic(err)
  118. }
  119. // Clear the current policy.
  120. e.ClearPolicy()
  121. testGetPolicy(t, e, [][]string{})
  122. // Load the policy from DB.
  123. err = a.LoadPolicy(e.GetModel())
  124. if err != nil {
  125. panic(err)
  126. }
  127. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  128. }
  129. func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
  130. myRes := e.GetPolicy()
  131. glog.Info("Policy: ", myRes)
  132. if !util.Array2DEquals(res, myRes) {
  133. t.Error("Policy: ", myRes, ", supposed to be ", res)
  134. }
  135. }
  136. func initAdapter(t *testing.T, driverName string, dataSourceName string) *utils.Adapter {
  137. // Create an adapter
  138. a, err := utils.NewAdapter(driverName, dataSourceName)
  139. if err != nil {
  140. panic(err)
  141. }
  142. // Initialize some policy in DB.
  143. initPolicy(t, a)
  144. // Now the DB has policy, so we can provide a normal use case.
  145. // Note: you don't need to look at the above code
  146. // if you already have a working DB with policy inside.
  147. return a
  148. }
  149. func testAutoSave(t *testing.T, a *utils.Adapter) {
  150. // NewEnforcer() will load the policy automatically.
  151. e, err := casbin.NewEnforcer("casbin_conf/rbac_model.conf", a)
  152. if err != nil {
  153. panic(err)
  154. }
  155. // AutoSave is enabled by default.
  156. // Now we disable it.
  157. e.EnableAutoSave(false)
  158. // Because AutoSave is disabled, the policy change only affects the policy in Casbin enforcer,
  159. // it doesn't affect the policy in the storage.
  160. e.AddPolicy("alice", "data1", "write")
  161. // Reload the policy from the storage to see the effect.
  162. e.LoadPolicy()
  163. // This is still the original policy.
  164. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  165. // Now we enable the AutoSave.
  166. e.EnableAutoSave(true)
  167. // Because AutoSave is enabled, the policy change not only affects the policy in Casbin enforcer,
  168. // but also affects the policy in the storage.
  169. e.AddPolicy("alice", "data1", "write")
  170. // Reload the policy from the storage to see the effect.
  171. e.LoadPolicy()
  172. // The policy has a new rule: {"alice", "data1", "write"}.
  173. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"alice", "data1", "write"}})
  174. // Remove the added rule.
  175. e.RemovePolicy("alice", "data1", "write")
  176. e.LoadPolicy()
  177. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  178. // Remove "data2_admin" related policy rules via a filter.
  179. // Two rules: {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"} are deleted.
  180. e.RemoveFilteredPolicy(0, "data2_admin")
  181. e.LoadPolicy()
  182. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}})
  183. }
  184. func testSaveLoad(t *testing.T, a *utils.Adapter) {
  185. // Initialize some policy in DB.
  186. initPolicy(t, a)
  187. // Note: you don't need to look at the above code
  188. // if you already have a working DB with policy inside.
  189. // Now the DB has policy, so we can provide a normal use case.
  190. // Create an adapter and an enforcer.
  191. // NewEnforcer() will load the policy automatically.
  192. e, _ := casbin.NewEnforcer("casbin_conf/rbac_model.conf", a)
  193. testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
  194. }