wire_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package payment
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/Wei-Shaw/sub2api/internal/config"
  6. )
  7. func TestProvideEncryptionKeySkipsAutoGeneratedTotpKey(t *testing.T) {
  8. t.Parallel()
  9. cfg := &config.Config{
  10. Totp: config.TotpConfig{
  11. EncryptionKey: strings.Repeat("a", 64),
  12. EncryptionKeyConfigured: false,
  13. },
  14. }
  15. key, err := ProvideEncryptionKey(cfg)
  16. if err != nil {
  17. t.Fatalf("ProvideEncryptionKey returned error: %v", err)
  18. }
  19. if len(key) != 0 {
  20. t.Fatalf("encryption key len = %d, want 0", len(key))
  21. }
  22. }
  23. func TestProvideEncryptionKeyUsesConfiguredTotpKey(t *testing.T) {
  24. t.Parallel()
  25. cfg := &config.Config{
  26. Totp: config.TotpConfig{
  27. EncryptionKey: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
  28. EncryptionKeyConfigured: true,
  29. },
  30. }
  31. key, err := ProvideEncryptionKey(cfg)
  32. if err != nil {
  33. t.Fatalf("ProvideEncryptionKey returned error: %v", err)
  34. }
  35. if len(key) != 32 {
  36. t.Fatalf("encryption key len = %d, want 32", len(key))
  37. }
  38. }
  39. func TestProvideEncryptionKeyRejectsConfiguredInvalidLength(t *testing.T) {
  40. t.Parallel()
  41. cfg := &config.Config{
  42. Totp: config.TotpConfig{
  43. EncryptionKey: "abcd",
  44. EncryptionKeyConfigured: true,
  45. },
  46. }
  47. _, err := ProvideEncryptionKey(cfg)
  48. if err == nil {
  49. t.Fatal("expected error for invalid key length")
  50. }
  51. }