github.com/creativeprojects/go-selfupdate@v1.2.0/gitlab_source_test.go (about)

     1  package selfupdate
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestGitLabTokenEnv(t *testing.T) {
    13  	token := os.Getenv("GITLAB_TOKEN")
    14  	if token == "" {
    15  		t.Skip("because $GITLAB_TOKEN is not set")
    16  	}
    17  
    18  	if _, err := NewGitLabSource(GitLabConfig{}); err != nil {
    19  		t.Error("Failed to initialize GitLab source with empty config")
    20  	}
    21  	if _, err := NewGitLabSource(GitLabConfig{APIToken: token}); err != nil {
    22  		t.Error("Failed to initialize GitLab source with API token config")
    23  	}
    24  }
    25  
    26  func TestGitLabTokenIsNotSet(t *testing.T) {
    27  	t.Setenv("GITLAB_TOKEN", "")
    28  
    29  	if _, err := NewGitLabSource(GitLabConfig{}); err != nil {
    30  		t.Error("Failed to initialize GitLab source with empty config")
    31  	}
    32  }
    33  
    34  func TestGitLabEnterpriseClientInvalidURL(t *testing.T) {
    35  	_, err := NewGitLabSource(GitLabConfig{APIToken: "my_token", BaseURL: ":this is not a URL"})
    36  	if err == nil {
    37  		t.Fatal("Invalid URL should raise an error")
    38  	}
    39  }
    40  
    41  func TestGitLabEnterpriseClientValidURL(t *testing.T) {
    42  	_, err := NewGitLabSource(GitLabConfig{APIToken: "my_token", BaseURL: "http://localhost"})
    43  	if err != nil {
    44  		t.Fatal("Failed to initialize GitLab source with valid URL")
    45  	}
    46  }
    47  
    48  func TestGitLabListReleasesContextCancelled(t *testing.T) {
    49  	source, err := NewGitLabSource(GitLabConfig{})
    50  	require.NoError(t, err)
    51  
    52  	ctx, cancelFn := context.WithCancel(context.Background())
    53  	cancelFn()
    54  
    55  	_, err = source.ListReleases(ctx, ParseSlug("creativeprojects/resticprofile"))
    56  	assert.ErrorIs(t, err, context.Canceled)
    57  }
    58  
    59  func TestGitLabDownloadReleaseAssetContextCancelled(t *testing.T) {
    60  	source, err := NewGitLabSource(GitLabConfig{})
    61  	require.NoError(t, err)
    62  
    63  	ctx, cancelFn := context.WithCancel(context.Background())
    64  	cancelFn()
    65  
    66  	_, err = source.DownloadReleaseAsset(ctx, &Release{
    67  		AssetID:  11,
    68  		AssetURL: "http://localhost/",
    69  	}, 11)
    70  	assert.ErrorIs(t, err, context.Canceled)
    71  }
    72  
    73  func TestGitLabDownloadReleaseAssetWithNilRelease(t *testing.T) {
    74  	source, err := NewGitLabSource(GitLabConfig{})
    75  	require.NoError(t, err)
    76  
    77  	_, err = source.DownloadReleaseAsset(context.Background(), nil, 11)
    78  	assert.ErrorIs(t, err, ErrInvalidRelease)
    79  }
    80  
    81  func TestGitLabDownloadReleaseAssetNotFound(t *testing.T) {
    82  	source, err := NewGitLabSource(GitLabConfig{})
    83  	require.NoError(t, err)
    84  
    85  	_, err = source.DownloadReleaseAsset(context.Background(), &Release{
    86  		AssetID:           11,
    87  		ValidationAssetID: 12,
    88  	}, 13)
    89  	assert.ErrorIs(t, err, ErrAssetNotFound)
    90  }