127 lines
2.6 KiB
Go
127 lines
2.6 KiB
Go
package simples3
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestS3_FileUpload(t *testing.T) {
|
|
testTxt, err := os.Open("test.txt")
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer testTxt.Close()
|
|
testPng, err := os.Open("avatar.png")
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer testPng.Close()
|
|
|
|
type fields struct {
|
|
AccessKey string
|
|
SecretKey string
|
|
Region string
|
|
}
|
|
type args struct {
|
|
u UploadInput
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Upload test.txt",
|
|
fields: fields{
|
|
AccessKey: os.Getenv("AWS_S3_ACCESS_KEY"),
|
|
SecretKey: os.Getenv("AWS_S3_SECRET_KEY"),
|
|
Region: os.Getenv("AWS_S3_REGION"),
|
|
},
|
|
args: args{
|
|
UploadInput{
|
|
Bucket: os.Getenv("AWS_S3_BUCKET"),
|
|
ObjectKey: "test.txt",
|
|
ContentType: "text/plain",
|
|
FileName: "test.txt",
|
|
Body: testTxt,
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "Upload avatar.png",
|
|
fields: fields{
|
|
AccessKey: os.Getenv("AWS_S3_ACCESS_KEY"),
|
|
SecretKey: os.Getenv("AWS_S3_SECRET_KEY"),
|
|
Region: os.Getenv("AWS_S3_REGION"),
|
|
},
|
|
args: args{
|
|
UploadInput{
|
|
Bucket: os.Getenv("AWS_S3_BUCKET"),
|
|
Bucket: "zerodha-testbucket",
|
|
ObjectKey: "xyz/image.png",
|
|
ContentType: "image/png",
|
|
FileName: "avatar.png",
|
|
Body: testPng,
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s3 := New(tt.fields.Region, tt.fields.AccessKey, tt.fields.SecretKey)
|
|
resp, err := s3.FileUpload(tt.args.u)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("S3.FileUpload() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
// check for empty response
|
|
if (resp == UploadResponse{}) {
|
|
t.Errorf("S3.FileUpload() returned empty response, %v", resp)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestS3_FileDelete(t *testing.T) {
|
|
type fields struct {
|
|
AccessKey string
|
|
SecretKey string
|
|
Region string
|
|
}
|
|
type args struct {
|
|
u DeleteInput
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "Delete test.txt",
|
|
fields: fields{
|
|
AccessKey: os.Getenv("AWS_S3_ACCESS_KEY"),
|
|
SecretKey: os.Getenv("AWS_S3_SECRET_KEY"),
|
|
Region: os.Getenv("AWS_S3_REGION"),
|
|
},
|
|
args: args{
|
|
DeleteInput{
|
|
Bucket: os.Getenv("AWS_S3_BUCKET"),
|
|
ObjectKey: "test.txt",
|
|
},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s3 := New(tt.fields.Region, tt.fields.AccessKey, tt.fields.SecretKey)
|
|
if err := s3.FileDelete(tt.args.u); (err != nil) != tt.wantErr {
|
|
t.Errorf("S3.FileDelete() error = %v, wantErr %v", err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|