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] - ConnectionCodeContext #2111

Closed
tomsmith8 opened this issue Dec 5, 2024 · 1 comment · Fixed by #2115
Closed

[Unit Tests] - ConnectionCodeContext #2111

tomsmith8 opened this issue Dec 5, 2024 · 1 comment · Fixed by #2115
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for "ConnectionCodeContext"


Stakwork Run


Unit Test Code


File: /tmp/stakwork/sphinx-tribes/auth/auth.go


package auth

import (
  "context"
  "net/http"
  "net/http/httptest"
  "strings"
  "testing"

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

func TestConnectionCodeContext(t *testing.T) {
  // Mock configuration for testing
  config.Connection_Auth = "valid_token"

  tests := []struct {
  	name           string
  	token          string
  	expectedStatus int
  	expectNextCall bool
  }{
  	{
  		name:           "Valid Token in Header",
  		token:          "valid_token",
  		expectedStatus: http.StatusOK,
  		expectNextCall: true,
  	},
  	{
  		name:           "Invalid Token in Header",
  		token:          "invalid_token",
  		expectedStatus: http.StatusUnauthorized,
  		expectNextCall: false,
  	},
  	{
  		name:           "Empty Token in Header",
  		token:          "",
  		expectedStatus: http.StatusUnauthorized,
  		expectNextCall: false,
  	},
  	{
  		name:           "No Token Header Present",
  		token:          "",
  		expectedStatus: http.StatusUnauthorized,
  		expectNextCall: false,
  	},
  	{
  		name:           "Malformed Header",
  		token:          "malformed_header",
  		expectedStatus: http.StatusUnauthorized,
  		expectNextCall: false,
  	},
  	{
  		name:           "Token with Special Characters",
  		token:          "special!@#token",
  		expectedStatus: http.StatusUnauthorized,
  		expectNextCall: false,
  	},
  	{
  		name:           "Token with Whitespace",
  		token:          " " + config.Connection_Auth + " ",
  		expectedStatus: http.StatusUnauthorized,
  		expectNextCall: false,
  	},
  	{
  		name:           "Case Sensitivity in Token",
  		token:          strings.ToUpper(config.Connection_Auth),
  		expectedStatus: http.StatusUnauthorized,
  		expectNextCall: false,
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		// Create a mock handler to verify if it gets called
  		nextCalled := false
  		next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  			nextCalled = true
  			w.WriteHeader(http.StatusOK)
  		})

  		// Create a request with the specified token
  		req := httptest.NewRequest(http.MethodGet, "/", nil)
  		if tt.token != "" {
  			req.Header.Set("token", tt.token)
  		}

  		// Create a response recorder to capture the response
  		rr := httptest.NewRecorder()

  		// Call the function under test
  		handler := ConnectionCodeContext(next)
  		handler.ServeHTTP(rr, req)

  		// Check the response status code
  		assert.Equal(t, tt.expectedStatus, rr.Code)

  		// Check if the next handler was called
  		assert.Equal(t, tt.expectNextCall, nextCalled)
  	})
  }

  t.Run("Null Request Object", func(t *testing.T) {
  	// Create a mock handler to verify if it gets called
  	nextCalled := false
  	next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  		nextCalled = true
  		w.WriteHeader(http.StatusOK)
  	})

  	// Call the function under test with a nil request
  	handler := ConnectionCodeContext(next)
  	rr := httptest.NewRecorder()
  	handler.ServeHTTP(rr, nil)

  	// Check the response status code
  	assert.Equal(t, http.StatusInternalServerError, rr.Code)

  	// Check if the next handler was not called
  	assert.False(t, nextCalled)
  })

  t.Run("Large Number of Requests", func(t *testing.T) {
  	// Create a mock handler to verify if it gets called
  	nextCalled := 0
  	next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  		nextCalled++
  		w.WriteHeader(http.StatusOK)
  	})

  	// Simulate a high volume of requests
  	for i := 0; i < 1000; i++ {
  		req := httptest.NewRequest(http.MethodGet, "/", nil)
  		if i%2 == 0 {
  			req.Header.Set("token", "valid_token")
  		} else {
  			req.Header.Set("token", "invalid_token")
  		}

  		rr := httptest.NewRecorder()
  		handler := ConnectionCodeContext(next)
  		handler.ServeHTTP(rr, req)

  		if i%2 == 0 {
  			assert.Equal(t, http.StatusOK, rr.Code)
  		} else {
  			assert.Equal(t, http.StatusUnauthorized, rr.Code)
  		}
  	}

  	// Check if the next handler was called the expected number of times
  	assert.Equal(t, 500, nextCalled)
  })
}
@sophieturner0
Copy link
Contributor

@tomsmith8 I help?

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

Successfully merging a pull request may close this issue.

2 participants