Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - SignChallenge #2065

Closed
tomsmith8 opened this issue Dec 3, 2024 · 0 comments
Closed

[Unit Tests] - SignChallenge #2065

tomsmith8 opened this issue Dec 3, 2024 · 0 comments

Comments

@tomsmith8
Copy link

Unit Test Coverage for "SignChallenge"


Stakwork Run


Unit Test Code


package main

import (
  "bytes"
  "errors"
  "io/ioutil"
  "net/http"
  "testing"

  "github.com/stretchr/testify/assert"
  "github.com/stretchr/testify/mock"
)

// MockHTTPClient is a mock of the HTTP client
type MockHTTPClient struct {
  mock.Mock
}

// Do is a mock implementation of the HTTP client's Do method
func (m *MockHTTPClient) Do(req *http.Request) (*http.Response, error) {
  args := m.Called(req)
  return args.Get(0).(*http.Response), args.Error(1)
}

// TestSignChallenge tests the SignChallenge function
func TestSignChallenge(t *testing.T) {
  mockClient := new(MockHTTPClient)
  httpClient = mockClient // Assume httpClient is a global variable used in SignChallenge

  tests := []struct {
  	name           string
  	challenge      string
  	mockResponse   *http.Response
  	mockError      error
  	expectedResult db.RelaySignerResponse
  }{
  	{
  		name:      "Valid Challenge String",
  		challenge: "validChallenge123",
  		mockResponse: &http.Response{
  			StatusCode: http.StatusOK,
  			Body:       ioutil.NopCloser(bytes.NewBufferString(`{"Response": {"Sig": "validSig"}}`)),
  		},
  		mockError:      nil,
  		expectedResult: db.RelaySignerResponse{Response: db.Response{Sig: "validSig"}},
  	},
  	{
  		name:      "Empty Challenge String",
  		challenge: "",
  		mockResponse: &http.Response{
  			StatusCode: http.StatusBadRequest,
  			Body:       ioutil.NopCloser(bytes.NewBufferString(`{"error": "Empty challenge"}`)),
  		},
  		mockError:      nil,
  		expectedResult: db.RelaySignerResponse{},
  	},
  	{
  		name:      "Very Long Challenge String",
  		challenge: string(make([]byte, 10000)),
  		mockResponse: &http.Response{
  			StatusCode: http.StatusOK,
  			Body:       ioutil.NopCloser(bytes.NewBufferString(`{"Response": {"Sig": "longSig"}}`)),
  		},
  		mockError:      nil,
  		expectedResult: db.RelaySignerResponse{Response: db.Response{Sig: "longSig"}},
  	},
  	{
  		name:      "Invalid URL Configuration",
  		challenge: "validChallenge123",
  		mockResponse: nil,
  		mockError:    errors.New("Invalid URL"),
  		expectedResult: db.RelaySignerResponse{},
  	},
  	{
  		name:      "Network Failure",
  		challenge: "validChallenge123",
  		mockResponse: nil,
  		mockError:    errors.New("Network failure"),
  		expectedResult: db.RelaySignerResponse{},
  	},
  	{
  		name:      "Invalid JSON Response",
  		challenge: "validChallenge123",
  		mockResponse: &http.Response{
  			StatusCode: http.StatusOK,
  			Body:       ioutil.NopCloser(bytes.NewBufferString(`invalid json`)),
  		},
  		mockError:      nil,
  		expectedResult: db.RelaySignerResponse{},
  	},
  	{
  		name:      "Timeout Scenario",
  		challenge: "validChallenge123",
  		mockResponse: nil,
  		mockError:    errors.New("Timeout"),
  		expectedResult: db.RelaySignerResponse{},
  	},
  	{
  		name:      "Challenge with Special Characters",
  		challenge: "!@#$%^&*()_+{}|:\"<>?",
  		mockResponse: &http.Response{
  			StatusCode: http.StatusOK,
  			Body:       ioutil.NopCloser(bytes.NewBufferString(`{"Response": {"Sig": "specialSig"}}`)),
  		},
  		mockError:      nil,
  		expectedResult: db.RelaySignerResponse{Response: db.Response{Sig: "specialSig"}},
  	},
  	{
  		name:      "Challenge with URL Encoding Required",
  		challenge: "challenge with spaces",
  		mockResponse: &http.Response{
  			StatusCode: http.StatusOK,
  			Body:       ioutil.NopCloser(bytes.NewBufferString(`{"Response": {"Sig": "encodedSig"}}`)),
  		},
  		mockError:      nil,
  		expectedResult: db.RelaySignerResponse{Response: db.Response{Sig: "encodedSig"}},
  	},
  	{
  		name:      "Challenge with Non-ASCII Characters",
  		challenge: "挑战123",
  		mockResponse: &http.Response{
  			StatusCode: http.StatusOK,
  			Body:       ioutil.NopCloser(bytes.NewBufferString(`{"Response": {"Sig": "nonAsciiSig"}}`)),
  		},
  		mockError:      nil,
  		expectedResult: db.RelaySignerResponse{Response: db.Response{Sig: "nonAsciiSig"}},
  	},
  	{
  		name:      "Challenge with SQL Injection Attempt",
  		challenge: "1; DROP TABLE users;",
  		mockResponse: &http.Response{
  			StatusCode: http.StatusOK,
  			Body:       ioutil.NopCloser(bytes.NewBufferString(`{"Response": {"Sig": "sqlInjectionSig"}}`)),
  		},
  		mockError:      nil,
  		expectedResult: db.RelaySignerResponse{Response: db.Response{Sig: "sqlInjectionSig"}},
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		mockClient.On("Do", mock.Anything).Return(tt.mockResponse, tt.mockError)

  		result := SignChallenge(tt.challenge)

  		assert.Equal(t, tt.expectedResult, result)
  	})
  }
}

Key Adjustments:

  1. Error Handling: Adjusted the expected outcomes for scenarios like "Empty Challenge String" and "Invalid URL Configuration" to reflect realistic error handling, such as returning an empty db.RelaySignerResponse or handling errors gracefully.

  2. Mock Responses: Ensured that mock responses and errors are set correctly for each test case, especially for network-related errors and invalid configurations.

  3. Test Structure: Maintained a clear and consistent structure for each test case, ensuring that the test names are descriptive and the expected results align with the specified outcomes.

  4. Assertions: Used assert.Equal to compare the actual and expected results, ensuring that the function's behavior is verified accurately for each scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant