captcha_storage_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) [2022] [巴拉迪维 BaratSemet]
  2. // [ohUrlShortener] is licensed under Mulan PSL v2.
  3. // You can use this software according to the terms and conditions of the Mulan PSL v2.
  4. // You may obtain a copy of Mulan PSL v2 at:
  5. // http://license.coscl.org.cn/MulanPSL2
  6. // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  7. // See the Mulan PSL v2 for more details.
  8. //
  9. // Custom redis storage for captcha, According to https://github.com/dchest/captcha/blob/master/store.go
  10. //
  11. // An object implementing Store interface can be registered with SetCustomStore
  12. // function to handle storage and retrieval of captcha ids and solutions for
  13. // them, replacing the default memory store.
  14. //
  15. // It is the responsibility of an object to delete expired and used captchas
  16. // when necessary (for example, the default memory store collects them in Set
  17. // method after the certain amount of captchas has been stored.)
  18. package storage
  19. import (
  20. "log"
  21. "testing"
  22. "time"
  23. "ohurlshortener/utils"
  24. "github.com/dchest/captcha"
  25. )
  26. func TestNewRedisStore(t *testing.T) {
  27. _, err := utils.InitConfig("../config.ini")
  28. if err != nil {
  29. t.Error(err)
  30. return
  31. }
  32. rs, err := InitRedisService()
  33. if err != nil {
  34. t.Error(err)
  35. return
  36. }
  37. s := CaptchaRedisStore{KeyPrefix: "sssTest", Expiration: 1 * time.Minute, RedisService: rs}
  38. captcha.SetCustomStore(&s)
  39. id := captcha.New()
  40. log.Println("Generated id is " + id)
  41. }