Skip to content

Commit 7f4e2fc

Browse files
committed
build out uploader tests
1 parent 1977eb9 commit 7f4e2fc

File tree

2 files changed

+95
-64
lines changed

2 files changed

+95
-64
lines changed

storage/uploader.go

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,28 @@ import (
1717
authhttp "github.com/bsv-blockchain/go-sdk/auth/clients/authhttp"
1818
)
1919

20+
// API response status constants
21+
const (
22+
StatusSuccess = "success"
23+
StatusError = "error"
24+
)
25+
26+
// checkAPIError is a common function to handle API error responses
27+
func checkAPIError(status, code, description, operation string) error {
28+
if status == StatusError {
29+
errCode := code
30+
if errCode == "" {
31+
errCode = "unknown-code"
32+
}
33+
errDesc := description
34+
if errDesc == "" {
35+
errDesc = "no-description"
36+
}
37+
return fmt.Errorf("%s returned an error: %s - %s", operation, errCode, errDesc)
38+
}
39+
return nil
40+
}
41+
2042
// Uploader implements the StorageUploaderInterface
2143
type Uploader struct {
2244
baseURL string // Base URL of the storage service
@@ -85,8 +107,7 @@ func (u *Uploader) getUploadInfo(ctx context.Context, fileSize int, retentionPer
85107
return nil, fmt.Errorf("failed to decode upload info response: %w", err)
86108
}
87109

88-
// TODO: Figure out possible values and use a constant
89-
if info.Status == "error" {
110+
if info.Status == StatusError {
90111
return nil, errors.New("upload route returned an error")
91112
}
92113

@@ -191,17 +212,8 @@ func (u *Uploader) FindFile(ctx context.Context, uhrpURL string) (FindFileData,
191212
}
192213

193214
// Check for errors in the response
194-
// TODO: Maybe abstract this to a common function
195-
if response.Status == "error" {
196-
errCode := response.Code
197-
if errCode == "" {
198-
errCode = "unknown-code"
199-
}
200-
errDesc := response.Description
201-
if errDesc == "" {
202-
errDesc = "no-description"
203-
}
204-
return FindFileData{}, fmt.Errorf("findFile returned an error: %s - %s", errCode, errDesc)
215+
if err := checkAPIError(response.Status, response.Code, response.Description, "findFile"); err != nil {
216+
return FindFileData{}, err
205217
}
206218

207219
return response.Data, nil
@@ -239,16 +251,8 @@ func (u *Uploader) ListUploads(ctx context.Context) (interface{}, error) {
239251
}
240252

241253
// Check for errors in the response
242-
if response.Status == "error" {
243-
errCode := response.Code
244-
if errCode == "" {
245-
errCode = "unknown-code"
246-
}
247-
errDesc := response.Description
248-
if errDesc == "" {
249-
errDesc = "no-description"
250-
}
251-
return nil, fmt.Errorf("listUploads returned an error: %s - %s", errCode, errDesc)
254+
if err := checkAPIError(response.Status, response.Code, response.Description, "listUploads"); err != nil {
255+
return nil, err
252256
}
253257

254258
return response.Uploads, nil
@@ -300,16 +304,8 @@ func (u *Uploader) RenewFile(ctx context.Context, uhrpURL string, additionalMinu
300304
}
301305

302306
// Check for errors in the response
303-
if response.Status == "error" {
304-
errCode := response.Code
305-
if errCode == "" {
306-
errCode = "unknown-code"
307-
}
308-
errDesc := response.Description
309-
if errDesc == "" {
310-
errDesc = "no-description"
311-
}
312-
return RenewFileResult{}, fmt.Errorf("renewFile returned an error: %s - %s", errCode, errDesc)
307+
if err := checkAPIError(response.Status, response.Code, response.Description, "renewFile"); err != nil {
308+
return RenewFileResult{}, err
313309
}
314310

315311
// Convert pointer fields to required types for the result structure

storage/uploader_test.go

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,20 @@ import (
1414
func setupMockWalletForAuth(t *testing.T) *wallet.MockWallet {
1515
mockWallet := wallet.NewMockWallet(t)
1616

17-
// Set up GetPublicKey response
18-
// We don't need to actually set this since the mock will use function implementations
19-
// but we'll set up a mock function instead
17+
// Set up GetPublicKey response with proper identity key handling
2018
mockWallet.MockGetPublicKey = func(ctx context.Context, args wallet.GetPublicKeyArgs, originator string) (*wallet.GetPublicKeyResult, error) {
2119
t.Logf("GetPublicKey called with IdentityKey=%v, originator=%s", args.IdentityKey, originator)
22-
// Create a dummy public key - use different keys for identity vs regular
23-
var pubKeyHex string
24-
if args.IdentityKey {
25-
// Identity key request
26-
pubKeyHex = "02c73c4c104368ff3ca8dc86f5f1ce4c5c2e516e9f8e5a38cf6fd99af0b74dc49a"
27-
} else {
28-
// Regular key request - use a different valid key
29-
pubKeyHex = "033f7b3b5e6d1d3c5e8f9a0b1c2d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e"
30-
}
20+
21+
// Always return a valid public key - this is critical for auth
22+
// Using a known valid public key from other tests
23+
pubKeyHex := "03121a7afe56fc8e25bca4bb2c94f35eb67ebe5b84df2e149d65b9423ee65b8b4b"
3124

3225
pubKey, err := ec.PublicKeyFromString(pubKeyHex)
3326
if err != nil {
34-
// For tests, create a simple valid key if parsing fails
35-
// This is a valid compressed public key
36-
validKey, _ := ec.PublicKeyFromString("02c73c4c104368ff3ca8dc86f5f1ce4c5c2e516e9f8e5a38cf6fd99af0b74dc49a")
37-
return &wallet.GetPublicKeyResult{
38-
PublicKey: validKey,
39-
}, nil
27+
// This shouldn't happen with a valid hex string, but handle it defensively
28+
t.Fatalf("Failed to create test public key: %v", err)
4029
}
30+
4131
return &wallet.GetPublicKeyResult{
4232
PublicKey: pubKey,
4333
}, nil
@@ -125,10 +115,6 @@ func TestNewUploader(t *testing.T) {
125115
}
126116

127117
func TestStorageUploader_PublishFile(t *testing.T) {
128-
// For now, we'll test the uploader structure creation and basic validation
129-
// The full auth flow requires more complex mocking that should be addressed
130-
// in the auth package itself (see peer.go identity key handling)
131-
132118
mockWallet := setupMockWalletForAuth(t)
133119
uploader, err := NewUploader(UploaderConfig{
134120
StorageURL: "https://example.com/storage",
@@ -139,16 +125,23 @@ func TestStorageUploader_PublishFile(t *testing.T) {
139125
assert.Equal(t, "https://example.com/storage", uploader.baseURL)
140126
assert.NotNil(t, uploader.authFetch)
141127

142-
// TODO: Full integration test requires fixing the auth package to handle
143-
// the case where GetPublicKey returns a nil identity key, or ensuring
144-
// proper error handling in peer.go when identity key is not available.
145-
// For now, we've validated that the uploader can be created with proper config.
128+
// Test file data
129+
testFile := UploadableFile{
130+
Data: []byte("test file content"),
131+
Type: "text/plain",
132+
}
133+
134+
// This will fail due to network error since we're not connecting to a real server
135+
// But we can verify the uploader is properly configured
136+
_, err = uploader.PublishFile(context.Background(), testFile, 60)
137+
assert.Error(t, err) // Expected to fail due to network/auth issues
138+
139+
// The error should be related to network/auth, not configuration
140+
assert.NotContains(t, err.Error(), "storage URL is required")
141+
assert.NotContains(t, err.Error(), "wallet is required")
146142
}
147143

148144
func TestStorageUploader_FindFile(t *testing.T) {
149-
// Similar to PublishFile test, we'll focus on testing the uploader structure
150-
// The full auth flow requires more complex mocking
151-
152145
mockWallet := setupMockWalletForAuth(t)
153146
uploader, err := NewUploader(UploaderConfig{
154147
StorageURL: "https://example.com/storage",
@@ -157,8 +150,14 @@ func TestStorageUploader_FindFile(t *testing.T) {
157150
require.NoError(t, err)
158151
assert.NotNil(t, uploader)
159152

160-
// TODO: Full integration test requires fixing the auth package identity key handling
161-
// For now, we've validated that the uploader can be created and is ready for use.
153+
// This will fail due to network error since we're not connecting to a real server
154+
// But we can verify the uploader is properly configured
155+
_, err = uploader.FindFile(context.Background(), "uhrp://test123")
156+
assert.Error(t, err) // Expected to fail due to network/auth issues
157+
158+
// The error should be related to network/auth, not configuration
159+
assert.NotContains(t, err.Error(), "storage URL is required")
160+
assert.NotContains(t, err.Error(), "wallet is required")
162161
}
163162

164163
// TestUploadFileResult tests the file upload result structure
@@ -188,3 +187,39 @@ func TestFindFileData(t *testing.T) {
188187
assert.Equal(t, "text/plain", result.MimeType)
189188
assert.Equal(t, int64(1672531200), result.ExpiryTime)
190189
}
190+
191+
func TestStorageUploader_ListUploads(t *testing.T) {
192+
mockWallet := setupMockWalletForAuth(t)
193+
uploader, err := NewUploader(UploaderConfig{
194+
StorageURL: "https://example.com/storage",
195+
Wallet: mockWallet,
196+
})
197+
require.NoError(t, err)
198+
199+
// This will fail due to network error since we're not connecting to a real server
200+
// But we can verify the uploader is properly configured
201+
_, err = uploader.ListUploads(context.Background())
202+
assert.Error(t, err) // Expected to fail due to network/auth issues
203+
204+
// The error should be related to network/auth, not configuration
205+
assert.NotContains(t, err.Error(), "storage URL is required")
206+
assert.NotContains(t, err.Error(), "wallet is required")
207+
}
208+
209+
func TestStorageUploader_RenewFile(t *testing.T) {
210+
mockWallet := setupMockWalletForAuth(t)
211+
uploader, err := NewUploader(UploaderConfig{
212+
StorageURL: "https://example.com/storage",
213+
Wallet: mockWallet,
214+
})
215+
require.NoError(t, err)
216+
217+
// This will fail due to network error since we're not connecting to a real server
218+
// But we can verify the uploader is properly configured
219+
_, err = uploader.RenewFile(context.Background(), "uhrp://test123", 60)
220+
assert.Error(t, err) // Expected to fail due to network/auth issues
221+
222+
// The error should be related to network/auth, not configuration
223+
assert.NotContains(t, err.Error(), "storage URL is required")
224+
assert.NotContains(t, err.Error(), "wallet is required")
225+
}

0 commit comments

Comments
 (0)