cache_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package feishu
  15. import (
  16. "os"
  17. "testing"
  18. "time"
  19. )
  20. func TestFileCache(t *testing.T) {
  21. token := "atokeexamplenomeaning"
  22. cache := NewFileCache(".test_auth_file")
  23. defer func() {
  24. os.Remove(".test_auth_file")
  25. }()
  26. t.Run("unexpired", func(t *testing.T) {
  27. tokenIn := TenantAccesstoken{
  28. TenantAccessToken: token,
  29. Expire: 3600,
  30. Created: time.Now().Unix(),
  31. }
  32. err := cache.Set(tokenIn)
  33. if err != nil {
  34. t.Fatalf("cache set error: %s", err)
  35. }
  36. var tokenOut TenantAccesstoken
  37. err = cache.Get(&tokenOut)
  38. if err != nil {
  39. t.Fatalf("cache get error: %s", err)
  40. }
  41. if tokenIn.TenantAccessToken != tokenOut.TenantAccessToken {
  42. t.Fatalf("the token value stored in cache is incorrect")
  43. }
  44. })
  45. t.Run("expired", func(t *testing.T) {
  46. tokenIn := TenantAccesstoken{
  47. TenantAccessToken: token,
  48. Expire: 119,
  49. Created: time.Now().Unix(),
  50. }
  51. err := cache.Set(tokenIn)
  52. if err != nil {
  53. t.Fatalf("cache set error: %s", err)
  54. }
  55. var tokenOut TenantAccesstoken
  56. err = cache.Get(&tokenOut)
  57. if err == nil {
  58. t.Fatalf("Getting expired token from cache should produce an error")
  59. }
  60. })
  61. }