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] - ExpireInHours #2233

Open
tomsmith8 opened this issue Dec 19, 2024 · 3 comments
Open

[Unit Tests] - ExpireInHours #2233

tomsmith8 opened this issue Dec 19, 2024 · 3 comments
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for "ExpireInHours"


Stakwork Run


Unit Test Code


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


package auth

import (
  "math"
  "testing"
  "time"

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

// Mock for jwtauth package
type MockJwtAuth struct {
  mock.Mock
}

func (m *MockJwtAuth) ExpireIn(duration time.Duration) int64 {
  args := m.Called(duration)
  return args.Get(0).(int64)
}

// Test function for ExpireInHours
func TestExpireInHours(t *testing.T) {
  mockJwtAuth := new(MockJwtAuth)

  // Replace jwtauth.ExpireIn with the mock
  jwtauth = mockJwtAuth

  tests := []struct {
  	name     string
  	hours    int
  	expected int64
  }{
  	{
  		name:     "Basic Functionality",
  		hours:    24,
  		expected: 86400, // Assuming 24 hours in seconds
  	},
  	{
  		name:     "Zero Hours",
  		hours:    0,
  		expected: 0, // Assuming current time or minimal expiration
  	},
  	{
  		name:     "Negative Hours",
  		hours:    -5,
  		expected: -18000, // Assuming negative hours in seconds
  	},
  	{
  		name:     "Maximum Integer Hours",
  		hours:    math.MaxInt32,
  		expected: int64(math.MaxInt32) * 3600, // Assuming max int32 hours in seconds
  	},
  	{
  		name:     "Minimum Integer Hours",
  		hours:    math.MinInt32,
  		expected: int64(math.MinInt32) * 3600, // Assuming min int32 hours in seconds
  	},
  	{
  		name:     "Small Positive Hours",
  		hours:    1,
  		expected: 3600, // Assuming 1 hour in seconds
  	},
  	{
  		name:     "Large Positive Hours",
  		hours:    10000,
  		expected: 36000000, // Assuming 10000 hours in seconds
  	},
  	{
  		name:     "Boundary Condition at 1 Hour",
  		hours:    1,
  		expected: 3600, // Assuming 1 hour in seconds
  	},
  	{
  		name:     "Boundary Condition at Maximum Duration",
  		hours:    int(math.MaxInt64 / int64(time.Hour)),
  		expected: math.MaxInt64, // Assuming max int64 duration
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		mockJwtAuth.On("ExpireIn", time.Duration(tt.hours)*time.Hour).Return(tt.expected)

  		result := ExpireInHours(tt.hours)
  		assert.Equal(t, tt.expected, result)

  		mockJwtAuth.AssertCalled(t, "ExpireIn", time.Duration(tt.hours)*time.Hour)
  	})
  }
}
@MuhammadUmer44
Copy link
Contributor

MuhammadUmer44 commented Dec 19, 2024

@tomsmith8 assign me?

@AhsanFarooqDev
Copy link
Contributor

AhsanFarooqDev commented Dec 19, 2024

@tomsmith8 can I help?

@Shoaibdev7
Copy link
Contributor

@tomsmith8 Please assign me?

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

4 participants