123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package backup
- import (
- "testing"
- "github.com/0xJacky/Nginx-UI/model"
- "github.com/stretchr/testify/assert"
- )
- func TestConstructS3Key(t *testing.T) {
- tests := []struct {
- name string
- storagePath string
- filename string
- expected string
- }{
- {
- name: "empty storage path",
- storagePath: "",
- filename: "backup.zip",
- expected: "backup.zip",
- },
- {
- name: "storage path with trailing slash",
- storagePath: "backups/",
- filename: "backup.zip",
- expected: "backups/backup.zip",
- },
- {
- name: "storage path without trailing slash",
- storagePath: "backups",
- filename: "backup.zip",
- expected: "backups/backup.zip",
- },
- {
- name: "storage path with leading slash",
- storagePath: "/backups",
- filename: "backup.zip",
- expected: "backups/backup.zip",
- },
- {
- name: "storage path with both leading and trailing slash",
- storagePath: "/backups/",
- filename: "backup.zip",
- expected: "backups/backup.zip",
- },
- {
- name: "nested storage path",
- storagePath: "nginx-ui/backups",
- filename: "backup.zip",
- expected: "nginx-ui/backups/backup.zip",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := constructS3Key(tt.storagePath, tt.filename)
- assert.Equal(t, tt.expected, result)
- })
- }
- }
- func TestGetS3Region(t *testing.T) {
- tests := []struct {
- name string
- region string
- expected string
- }{
- {
- name: "empty region",
- region: "",
- expected: "us-east-1",
- },
- {
- name: "valid region",
- region: "eu-west-1",
- expected: "eu-west-1",
- },
- {
- name: "us-west-2 region",
- region: "us-west-2",
- expected: "us-west-2",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := getS3Region(tt.region)
- assert.Equal(t, tt.expected, result)
- })
- }
- }
- func TestNewS3Client_ValidationErrors(t *testing.T) {
- tests := []struct {
- name string
- autoBackup *model.AutoBackup
- expectError bool
- }{
- {
- name: "valid configuration",
- autoBackup: &model.AutoBackup{
- S3AccessKeyID: "test-access-key",
- S3SecretAccessKey: "test-secret-key",
- S3Bucket: "test-bucket",
- S3Region: "us-east-1",
- },
- expectError: false,
- },
- {
- name: "valid configuration with custom endpoint",
- autoBackup: &model.AutoBackup{
- S3AccessKeyID: "test-access-key",
- S3SecretAccessKey: "test-secret-key",
- S3Bucket: "test-bucket",
- S3Region: "us-east-1",
- S3Endpoint: "https://s3.example.com",
- },
- expectError: false,
- },
- {
- name: "empty region defaults to us-east-1",
- autoBackup: &model.AutoBackup{
- S3AccessKeyID: "test-access-key",
- S3SecretAccessKey: "test-secret-key",
- S3Bucket: "test-bucket",
- S3Region: "",
- },
- expectError: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- client, err := NewS3Client(tt.autoBackup)
- if tt.expectError {
- assert.Error(t, err)
- assert.Nil(t, client)
- } else {
- // Note: This will fail in CI/test environment without AWS credentials
- // but the client creation itself should succeed
- if err != nil {
- // Allow AWS credential errors in test environment
- assert.Contains(t, err.Error(), "failed to load AWS config")
- } else {
- assert.NotNil(t, client)
- assert.Equal(t, tt.autoBackup.S3Bucket, client.bucket)
- }
- }
- })
- }
- }
|