sphinx-tribes
sphinx-tribes copied to clipboard
[Unit Tests] - MakeConnectionCodeRequest
Unit Test Coverage for "MakeConnectionCodeRequest"
Unit Test Code
File: /tmp/stakwork/sphinx-tribes/handlers/auth.go
package auth
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"math"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// Mocking external dependencies
type MockClient struct {
mock.Mock
}
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
args := m.Called(req)
return args.Get(0).(*http.Response), args.Error(1)
}
func TestMakeConnectionCodeRequest(t *testing.T) {
// Mocking the HTTP client
mockClient := new(MockClient)
httpClient = mockClient
tests := []struct {
name string
inviter_pubkey string
inviter_route_hint string
msats_amount uint64
mockResponse *http.Response
mockError error
expected string
}{
{
name: "Valid Input with All Parameters Provided",
inviter_pubkey: "valid_pubkey",
inviter_route_hint: "valid_route_hint",
msats_amount: 1000,
mockResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"Invite": "invite_code"}`)),
},
mockError: nil,
expected: "invite_code",
},
{
name: "Zero msats_amount",
inviter_pubkey: "valid_pubkey",
inviter_route_hint: "valid_route_hint",
msats_amount: 0,
mockResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"Invite": "invite_code"}`)),
},
mockError: nil,
expected: "invite_code",
},
{
name: "Maximum msats_amount",
inviter_pubkey: "valid_pubkey",
inviter_route_hint: "valid_route_hint",
msats_amount: math.MaxUint64,
mockResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"Invite": "invite_code"}`)),
},
mockError: nil,
expected: "invite_code",
},
{
name: "Invalid inviter_pubkey Format",
inviter_pubkey: "invalid_pubkey",
inviter_route_hint: "valid_route_hint",
msats_amount: 1000,
mockResponse: &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(bytes.NewBufferString(``)),
},
mockError: nil,
expected: "",
},
{
name: "Invalid inviter_route_hint Format",
inviter_pubkey: "valid_pubkey",
inviter_route_hint: "invalid_route_hint",
msats_amount: 1000,
mockResponse: &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(bytes.NewBufferString(``)),
},
mockError: nil,
expected: "",
},
{
name: "HTTP Request Failure",
inviter_pubkey: "valid_pubkey",
inviter_route_hint: "valid_route_hint",
msats_amount: 1000,
mockResponse: nil,
mockError: fmt.Errorf("network error"),
expected: "",
},
{
name: "Invalid JSON Response",
inviter_pubkey: "valid_pubkey",
inviter_route_hint: "valid_route_hint",
msats_amount: 1000,
mockResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`invalid json`)),
},
mockError: nil,
expected: "",
},
{
name: "Empty Response Body",
inviter_pubkey: "valid_pubkey",
inviter_route_hint: "valid_route_hint",
msats_amount: 1000,
mockResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(``)),
},
mockError: nil,
expected: "",
},
{
name: "Null or Empty Inputs",
inviter_pubkey: "",
inviter_route_hint: "",
msats_amount: 1000,
mockResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"Invite": "invite_code"}`)),
},
mockError: nil,
expected: "invite_code",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient.On("Do", mock.Anything).Return(tt.mockResponse, tt.mockError)
result := MakeConnectionCodeRequest(tt.inviter_pubkey, tt.inviter_route_hint, tt.msats_amount)
assert.Equal(t, tt.expected, result)
})
}
}
@humansinstitute assign