s3_client_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package backup
  2. import (
  3. "testing"
  4. "github.com/0xJacky/Nginx-UI/model"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestConstructS3Key(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. storagePath string
  11. filename string
  12. expected string
  13. }{
  14. {
  15. name: "empty storage path",
  16. storagePath: "",
  17. filename: "backup.zip",
  18. expected: "backup.zip",
  19. },
  20. {
  21. name: "storage path with trailing slash",
  22. storagePath: "backups/",
  23. filename: "backup.zip",
  24. expected: "backups/backup.zip",
  25. },
  26. {
  27. name: "storage path without trailing slash",
  28. storagePath: "backups",
  29. filename: "backup.zip",
  30. expected: "backups/backup.zip",
  31. },
  32. {
  33. name: "storage path with leading slash",
  34. storagePath: "/backups",
  35. filename: "backup.zip",
  36. expected: "backups/backup.zip",
  37. },
  38. {
  39. name: "storage path with both leading and trailing slash",
  40. storagePath: "/backups/",
  41. filename: "backup.zip",
  42. expected: "backups/backup.zip",
  43. },
  44. {
  45. name: "nested storage path",
  46. storagePath: "nginx-ui/backups",
  47. filename: "backup.zip",
  48. expected: "nginx-ui/backups/backup.zip",
  49. },
  50. }
  51. for _, tt := range tests {
  52. t.Run(tt.name, func(t *testing.T) {
  53. result := constructS3Key(tt.storagePath, tt.filename)
  54. assert.Equal(t, tt.expected, result)
  55. })
  56. }
  57. }
  58. func TestGetS3Region(t *testing.T) {
  59. tests := []struct {
  60. name string
  61. region string
  62. expected string
  63. }{
  64. {
  65. name: "empty region",
  66. region: "",
  67. expected: "us-east-1",
  68. },
  69. {
  70. name: "valid region",
  71. region: "eu-west-1",
  72. expected: "eu-west-1",
  73. },
  74. {
  75. name: "us-west-2 region",
  76. region: "us-west-2",
  77. expected: "us-west-2",
  78. },
  79. }
  80. for _, tt := range tests {
  81. t.Run(tt.name, func(t *testing.T) {
  82. result := getS3Region(tt.region)
  83. assert.Equal(t, tt.expected, result)
  84. })
  85. }
  86. }
  87. func TestNewS3Client_ValidationErrors(t *testing.T) {
  88. tests := []struct {
  89. name string
  90. autoBackup *model.AutoBackup
  91. expectError bool
  92. }{
  93. {
  94. name: "valid configuration",
  95. autoBackup: &model.AutoBackup{
  96. S3AccessKeyID: "test-access-key",
  97. S3SecretAccessKey: "test-secret-key",
  98. S3Bucket: "test-bucket",
  99. S3Region: "us-east-1",
  100. },
  101. expectError: false,
  102. },
  103. {
  104. name: "valid configuration with custom endpoint",
  105. autoBackup: &model.AutoBackup{
  106. S3AccessKeyID: "test-access-key",
  107. S3SecretAccessKey: "test-secret-key",
  108. S3Bucket: "test-bucket",
  109. S3Region: "us-east-1",
  110. S3Endpoint: "https://s3.example.com",
  111. },
  112. expectError: false,
  113. },
  114. {
  115. name: "empty region defaults to us-east-1",
  116. autoBackup: &model.AutoBackup{
  117. S3AccessKeyID: "test-access-key",
  118. S3SecretAccessKey: "test-secret-key",
  119. S3Bucket: "test-bucket",
  120. S3Region: "",
  121. },
  122. expectError: false,
  123. },
  124. }
  125. for _, tt := range tests {
  126. t.Run(tt.name, func(t *testing.T) {
  127. client, err := NewS3Client(tt.autoBackup)
  128. if tt.expectError {
  129. assert.Error(t, err)
  130. assert.Nil(t, client)
  131. } else {
  132. // Note: This will fail in CI/test environment without AWS credentials
  133. // but the client creation itself should succeed
  134. if err != nil {
  135. // Allow AWS credential errors in test environment
  136. assert.Contains(t, err.Error(), "failed to load AWS config")
  137. } else {
  138. assert.NotNil(t, client)
  139. assert.Equal(t, tt.autoBackup.S3Bucket, client.bucket)
  140. }
  141. }
  142. })
  143. }
  144. }