github.com/creativeprojects/go-selfupdate@v1.2.0/gitea_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 TestGiteaTokenEnv(t *testing.T) {
    13  	token := os.Getenv("GITEA_TOKEN")
    14  	if token == "" {
    15  		t.Skip("because $GITEA_TOKEN is not set")
    16  	}
    17  
    18  	if _, err := NewGiteaSource(GiteaConfig{BaseURL: "https://git.lbsfilm.at"}); err != nil {
    19  		t.Error("Failed to initialize Gitea source with URL")
    20  	}
    21  	if _, err := NewGiteaSource(GiteaConfig{APIToken: token}); err != nil {
    22  		t.Error("Failed to initialize Gitea source with API token config")
    23  	}
    24  }
    25  
    26  func TestGiteaTokenIsNotSet(t *testing.T) {
    27  	t.Setenv("GITHUB_TOKEN", "")
    28  
    29  	if _, err := NewGiteaSource(GiteaConfig{BaseURL: "https://git.lbsfilm.at"}); err != nil {
    30  		t.Error("Failed to initialize Gitea source with URL")
    31  	}
    32  }
    33  
    34  func TestGiteaListReleasesContextCancelled(t *testing.T) {
    35  	source, err := NewGiteaSource(GiteaConfig{BaseURL: "https://git.lbsfilm.at"})
    36  	require.NoError(t, err)
    37  
    38  	ctx, cancelFn := context.WithCancel(context.Background())
    39  	cancelFn()
    40  
    41  	_, err = source.ListReleases(ctx, ParseSlug("creativeprojects/resticprofile"))
    42  	assert.ErrorIs(t, err, context.Canceled)
    43  }
    44  
    45  func TestGiteaDownloadReleaseAssetContextCancelled(t *testing.T) {
    46  	source, err := NewGiteaSource(GiteaConfig{BaseURL: "https://git.lbsfilm.at"})
    47  	require.NoError(t, err)
    48  
    49  	ctx, cancelFn := context.WithCancel(context.Background())
    50  	cancelFn()
    51  
    52  	_, err = source.DownloadReleaseAsset(ctx, &Release{repository: ParseSlug("creativeprojects/resticprofile")}, 11)
    53  	assert.ErrorIs(t, err, context.Canceled)
    54  }
    55  
    56  func TestGiteaDownloadReleaseAssetWithNilRelease(t *testing.T) {
    57  	source, err := NewGiteaSource(GiteaConfig{BaseURL: "https://git.lbsfilm.at"})
    58  	require.NoError(t, err)
    59  
    60  	_, err = source.DownloadReleaseAsset(context.Background(), nil, 11)
    61  	assert.ErrorIs(t, err, ErrInvalidRelease)
    62  }