Skip to content

Commit

Permalink
tests: helpers && data models && data assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Oct 21, 2024
1 parent f887516 commit 7082594
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 2 deletions.
30 changes: 28 additions & 2 deletions pkg/data_assets/data_asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ func TestDataAssetSuite(t *testing.T) {
message, err := dataAssetImpl.DeleteAsset(1)

// Assertions
assert.Error(t, err) // Expecting an error
assert.Empty(t, message) // Assert that the message response is empty on error
assert.Error(t, err) // Expecting an error
assert.Empty(t, message) // Assert that the message response is empty on error
})

t.Run("TestUploadFileSuccess", func(t *testing.T) {
Expand Down Expand Up @@ -454,8 +454,34 @@ func TestDataAssetSuite(t *testing.T) {
assert.Empty(t, result.Id)
})

t.Run("TestUpdateFileSuccess", func(t *testing.T) {
// Setup
httpmock.RegisterResponder("PUT", "/data-assets/123",
httpmock.NewJsonResponderOrPanic(200, common.DataAssetIDRequestAndResponse{Id: 123}))

fixture := `{"id":123}`
responder := func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(200, fixture)
resp.Header.Set("Content-Type", "application/json")
return resp, nil
}
httpmock.RegisterResponder("PUT", "/data-assets/123", responder)

// Test
expirationDate := time.Now().Add(24 * time.Hour)
aclList := []common.ACLRequest{
{Address: "test", Roles: []common.AccessLevel{common.RoleShare}},
}
result, err := dataAssetImpl.UpdateFile("123", "testfile.txt", []byte("file content"), &aclList, &expirationDate)

// Assertions
assert.NoError(t, err)
assert.Equal(t, 123, result.Id)
})

t.Run("TestUpdateFileError", func(t *testing.T) {
// Setup
httpmock.Reset()
httpmock.RegisterResponder("PUT", common.UpdateDataAssetByID,
httpmock.NewJsonResponderOrPanic(400, common.Error{Error: "Update failed"}))

Expand Down
85 changes: 85 additions & 0 deletions pkg/data_models/data_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ func TestDataModelSuite(t *testing.T) {

})

t.Run("TestGetALLError", func(t *testing.T) {
// Reset mock
httpmock.Reset()

// Set up mock response
responder := httpmock.NewStringResponder(400, `{"error": "Failed to delete ACL"}`)
httpmock.RegisterResponder("GET", common.GetDataModels, responder)

page, pageSize := 1, 10
result, err := dataModelImpl.GetAll(page, pageSize)

// Assertions
assert.Error(t, err)
assert.Empty(t, result)
})

t.Run("TestGetAllHttpRequestError", func(t *testing.T) {
// Reset mock
httpmock.Reset()
Expand Down Expand Up @@ -169,6 +185,22 @@ func TestDataModelSuite(t *testing.T) {
assert.Equal(t, 2, result.Meta.TotalItems)
})

t.Run("TestGetMyIDError", func(t *testing.T) {
// Reset mock
httpmock.Reset()

// Set up mock response
responder := httpmock.NewStringResponder(400, `{"error": "Failed to delete ACL"}`)
httpmock.RegisterResponder("GET", common.GetDataModelsByUser, responder)

page, pageSize := 1, 10
result, err := dataModelImpl.GetMy(page, pageSize)

// Assertions
assert.Error(t, err)
assert.Empty(t, result)
})

t.Run("TestGetMyHttpRequestError", func(t *testing.T) {
// Reset mock
httpmock.Reset()
Expand Down Expand Up @@ -223,6 +255,22 @@ func TestDataModelSuite(t *testing.T) {
assert.Equal(t, "Data Model 1", result.Title)
})

t.Run("TestGetByIDError", func(t *testing.T) {
// Reset mock
httpmock.Reset()

// Set up mock response
responder := httpmock.NewStringResponder(400, `{"error": "Failed to delete ACL"}`)
httpmock.RegisterResponder("GET", "/data-models/1", responder)

id := int64(1)
result, err := dataModelImpl.GetById(id)

// Assertions
assert.Error(t, err)
assert.Empty(t, result)
})

t.Run("TestGetByIdHttpRequestError", func(t *testing.T) {
// Reset mock
httpmock.Reset()
Expand Down Expand Up @@ -265,6 +313,24 @@ func TestDataModelSuite(t *testing.T) {
assert.Equal(t, int(2), result.Id)
})

t.Run("TestCreateError", func(t *testing.T) {
// Reset mock
httpmock.Reset()

// Set up mock response
responder := httpmock.NewStringResponder(400, `{"error": "Failed to delete ACL"}`)
httpmock.RegisterResponder("POST", common.CreateDataModel, responder)

dataModelInput := common.DataModelRequest{
Title: "NewModel",
}
result, err := dataModelImpl.Create(dataModelInput)

// Assertions
assert.Error(t, err)
assert.Empty(t, result)
})

t.Run("TestCreateHttpRequestError", func(t *testing.T) {
// Reset mock
httpmock.Reset()
Expand Down Expand Up @@ -310,6 +376,25 @@ func TestDataModelSuite(t *testing.T) {
assert.Equal(t, int(2), result.Id)
})

t.Run("TestUpdateError", func(t *testing.T) {
// Reset mock
httpmock.Reset()

// Set up mock response
responder := httpmock.NewStringResponder(400, `{"error": "Failed to delete ACL"}`)
httpmock.RegisterResponder("PUT", common.UpdateDataModel, responder)

id := int64(2)
dataModelInput := common.DataModelRequest{
Title: "UpdatedModel",
}
result, err := dataModelImpl.Update(id, dataModelInput)

// Assertions
assert.Error(t, err)
assert.Empty(t, result)
})

t.Run("TestUpdateHttpRequestError", func(t *testing.T) {
// Reset mock
httpmock.Reset()
Expand Down
52 changes: 52 additions & 0 deletions pkg/helpers/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers_test

import (
"errors"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -95,6 +96,57 @@ func TestIssueJWT_Success(t *testing.T) {

}

func TestIssueJWT_SignMessageError(t *testing.T) {
client := resty.New()
httpmock.ActivateNonDefault(client.GetClient())
defer httpmock.DeactivateAndReset()

// Mock the wallet to return an error
mockWallet := new(MockWallet)
mockWallet.On("SignMessage", mock.Anything).Return(services.WalletSignMessageType{}, fmt.Errorf("failed to sign message"))

// Mock the /auth/message endpoint to return a message
fixtureMessage := `"mock-message"`
httpmock.RegisterResponder("GET", "=~.*/auth/message", // Adjust this based on actual URL
httpmock.NewStringResponder(200, fixtureMessage))

// Issue JWT
_, err := helpers.IssueJWT(*client, mockWallet)

// Assert that there was an error in signing the message
assert.Error(t, err)
assert.EqualError(t, err, "failed to sign message")
}

func TestIssueJWT_LoginError(t *testing.T) {
client := resty.New()
httpmock.ActivateNonDefault(client.GetClient())
defer httpmock.DeactivateAndReset()

// Mock the wallet to return a valid signature
mockWallet := new(MockWallet)
mockWallet.On("SignMessage", mock.Anything).Return(services.WalletSignMessageType{
Signature: "mock-signature",
SigningKey: "mock-signing-key",
}, nil)

// Mock the /auth/message endpoint to return a message
fixtureMessage := `"mock-message"`
httpmock.RegisterResponder("GET", "=~.*/auth/message", // Adjust this based on actual URL
httpmock.NewStringResponder(200, fixtureMessage))

// Mock the /auth/login endpoint to return an error
httpmock.RegisterResponder("POST", "=~.*/auth/login", // Adjust this if needed
httpmock.NewStringResponder(500, `{"error": "internal server error"}`))

// Issue JWT
_, err := helpers.IssueJWT(*client, mockWallet)

// Assert that there was an error in the login process
assert.Error(t, err)
assert.EqualError(t, err, "solana signature verification failed: failed to decode signature from Base58: invalid base58 digit ('-')")
}

func TestIssueJWT_FailSignMessage(t *testing.T) {
client := resty.New()
httpmock.ActivateNonDefault(client.GetClient())
Expand Down

0 comments on commit 7082594

Please sign in to comment.