tools_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package public
  2. import (
  3. "testing"
  4. "github.com/eryajf/chatgpt-dingtalk/config"
  5. )
  6. func TestCheckRequestWithCredentials_Pass_WithNilConfig(t *testing.T) {
  7. Config = &config.Configuration{
  8. Credentials: nil,
  9. }
  10. clientId, pass := CheckRequestWithCredentials("ts", "sg")
  11. if !pass {
  12. t.Errorf("pass should be true, but false")
  13. return
  14. }
  15. if len(clientId) > 0 {
  16. t.Errorf("client id should be empty")
  17. return
  18. }
  19. }
  20. func TestCheckRequestWithCredentials_Pass_WithEmptyConfig(t *testing.T) {
  21. Config = &config.Configuration{
  22. Credentials: []config.Credential{},
  23. }
  24. clientId, pass := CheckRequestWithCredentials("ts", "sg")
  25. if !pass {
  26. t.Errorf("pass should be true, but false")
  27. return
  28. }
  29. if len(clientId) > 0 {
  30. t.Errorf("client id should be empty")
  31. return
  32. }
  33. }
  34. func TestCheckRequestWithCredentials_Pass_WithValidConfig(t *testing.T) {
  35. Config = &config.Configuration{
  36. Credentials: []config.Credential{
  37. config.Credential{
  38. ClientID: "client-id-for-test",
  39. ClientSecret: "client-secret-for-test",
  40. },
  41. },
  42. }
  43. clientId, pass := CheckRequestWithCredentials("1684493546276", "nwBJQmaBLv9+5/sSS/66jcFc1/kGY5wo38L88LOGfRU=")
  44. if !pass {
  45. t.Errorf("pass should be true, but false")
  46. return
  47. }
  48. if clientId != "client-id-for-test" {
  49. t.Errorf("client id should be \"%s\", but \"%s\"", "client-id-for-test", clientId)
  50. return
  51. }
  52. }
  53. func TestCheckRequestWithCredentials_Failed_WithInvalidConfig(t *testing.T) {
  54. Config = &config.Configuration{
  55. Credentials: []config.Credential{
  56. config.Credential{
  57. ClientID: "client-id-for-test",
  58. ClientSecret: "invalid-client-secret-for-test",
  59. },
  60. },
  61. }
  62. clientId, pass := CheckRequestWithCredentials("1684493546276", "nwBJQmaBLv9+5/sSS/66jcFc1/kGY5wo38L88LOGfRU=")
  63. if pass {
  64. t.Errorf("pass should be false, but true")
  65. return
  66. }
  67. if clientId != "" {
  68. t.Errorf("client id should be empty")
  69. return
  70. }
  71. }