Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions gitlab_client_current_token_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package gitlab

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xanzy/go-gitlab"
)

// mockGitlabClient is a stub implementation that simulates the GitLab client for testing
type mockGitlabClient struct {
gitlabClient
personalAccessTokensFunc func(ctx context.Context) ([]*gitlab.PersonalAccessToken, error)
}

func (m *mockGitlabClient) PersonalAccessTokens(ctx context.Context) ([]*gitlab.PersonalAccessToken, error) {
if m.personalAccessTokensFunc != nil {
return m.personalAccessTokensFunc(ctx)
}
return nil, nil
}

// TestCurrentTokenInfo_WithExpiryDate tests that tokens with expiry dates are not modified
func TestCurrentTokenInfo_WithExpiryDate(t *testing.T) {
// Create a timestamp for testing
now := time.Now()
expiryDate := now.AddDate(0, 2, 0) // 2 months in the future

// Create a mock client
mockClient := &mockGitlabClient{
personalAccessTokensFunc: func(ctx context.Context) ([]*gitlab.PersonalAccessToken, error) {
return []*gitlab.PersonalAccessToken{
{
ID: 123,
Name: "Test Token",
CreatedAt: &now,
ExpiresAt: &expiryDate,
UserID: 456,
},
}, nil
},
}

// Call the method being tested
tokenInfo, err := mockClient.CurrentTokenInfo(context.Background())

// Verify the result
require.NoError(t, err)
require.NotNil(t, tokenInfo)
require.NotNil(t, tokenInfo.ExpiresAt)
assert.Equal(t, expiryDate, *tokenInfo.ExpiresAt, "Expiry date should not be modified")
}

// TestCurrentTokenInfo_WithoutExpiryDate tests that tokens without expiry dates get an artificial one
func TestCurrentTokenInfo_WithoutExpiryDate(t *testing.T) {
// Create a timestamp for testing
now := time.Now()
expectedExpiry := now.AddDate(1, 0, -2) // 1 year minus 2 days from creation

// Create a mock client
mockClient := &mockGitlabClient{
personalAccessTokensFunc: func(ctx context.Context) ([]*gitlab.PersonalAccessToken, error) {
return []*gitlab.PersonalAccessToken{
{
ID: 123,
Name: "Test Token",
CreatedAt: &now,
ExpiresAt: nil, // No expiry date set
UserID: 456,
},
}, nil
},
}

// Call the method being tested
tokenInfo, err := mockClient.CurrentTokenInfo(context.Background())

// Verify the result
require.NoError(t, err)
require.NotNil(t, tokenInfo)
require.NotNil(t, tokenInfo.ExpiresAt)

// Calculate the difference between expected and actual times
// We need to allow for a small difference due to execution time
timeDiff := expectedExpiry.Sub(*tokenInfo.ExpiresAt)
if timeDiff < 0 {
timeDiff = -timeDiff
}

// Verify the expiry date was set correctly (within a small tolerance)
assert.True(t, timeDiff < time.Second, "Expiry date should be set to 1 year minus 2 days from creation")
}
72 changes: 72 additions & 0 deletions gitlab_client_new_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package gitlab

import (
"net/http"
"testing"

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

// TestNewGitlabClient_EmptyBaseURL tests that newGitlabClient properly validates an empty BaseURL
func TestNewGitlabClient_EmptyBaseURL(t *testing.T) {
config := &EntryConfig{
BaseURL: "", // Empty base URL
Token: "valid-token", // Valid token
}

// Call the function being tested
client, err := newGitlabClient(config, http.DefaultClient)

// Verify the result
assert.Nil(t, client, "Client should be nil due to invalid base URL")
assert.Error(t, err, "Should return an error for empty base URL")
assert.ErrorIs(t, err, ErrInvalidValue, "Error should include ErrInvalidValue")
}

// TestNewGitlabClient_WhitespaceBaseURL tests that newGitlabClient properly validates a whitespace-only BaseURL
func TestNewGitlabClient_WhitespaceBaseURL(t *testing.T) {
config := &EntryConfig{
BaseURL: " ", // Whitespace-only base URL
Token: "valid-token", // Valid token
}

// Call the function being tested
client, err := newGitlabClient(config, http.DefaultClient)

// Verify the result
assert.Nil(t, client, "Client should be nil due to invalid base URL")
assert.Error(t, err, "Should return an error for whitespace-only base URL")
assert.ErrorIs(t, err, ErrInvalidValue, "Error should include ErrInvalidValue")
}

// TestNewGitlabClient_EmptyToken tests that newGitlabClient properly validates an empty Token
func TestNewGitlabClient_EmptyToken(t *testing.T) {
config := &EntryConfig{
BaseURL: "https://gitlab.example.com", // Valid base URL
Token: "", // Empty token
}

// Call the function being tested
client, err := newGitlabClient(config, http.DefaultClient)

// Verify the result
assert.Nil(t, client, "Client should be nil due to invalid token")
assert.Error(t, err, "Should return an error for empty token")
assert.ErrorIs(t, err, ErrInvalidValue, "Error should include ErrInvalidValue")
}

// TestNewGitlabClient_WhitespaceToken tests that newGitlabClient properly validates a whitespace-only Token
func TestNewGitlabClient_WhitespaceToken(t *testing.T) {
config := &EntryConfig{
BaseURL: "https://gitlab.example.com", // Valid base URL
Token: " ", // Whitespace-only token
}

// Call the function being tested
client, err := newGitlabClient(config, http.DefaultClient)

// Verify the result
assert.Nil(t, client, "Client should be nil due to invalid token")
assert.Error(t, err, "Should return an error for whitespace-only token")
assert.ErrorIs(t, err, ErrInvalidValue, "Error should include ErrInvalidValue")
}