github.com/cozy/cozy-stack@v0.0.0-20240327093429-939e4a21320e/model/app/webapp_test.go (about)

     1  package app_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/cozy/cozy-stack/model/app"
     8  	"github.com/cozy/cozy-stack/model/instance/lifecycle"
     9  	"github.com/cozy/cozy-stack/model/stack"
    10  	"github.com/cozy/cozy-stack/pkg/config/config"
    11  	"github.com/cozy/cozy-stack/pkg/consts"
    12  	"github.com/cozy/cozy-stack/tests/testutils"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestWebapp(t *testing.T) {
    18  	if testing.Short() {
    19  		t.Skip("an instance is required for this test: test skipped due to the use of --short flag")
    20  	}
    21  
    22  	config.UseTestFile(t)
    23  	testutils.NeedCouchdb(t)
    24  
    25  	if !stackStarted {
    26  		_, _, err := stack.Start()
    27  		if err != nil {
    28  			require.NoError(t, err, "Error while starting job system")
    29  		}
    30  		stackStarted = true
    31  	}
    32  
    33  	t.Run("ListWebappsWithPagination", func(t *testing.T) {
    34  		of := true
    35  		testInstance, err := lifecycle.Create(&lifecycle.Options{
    36  			Domain:             "test-list-webapp-pagination",
    37  			ContextName:        "foocontext",
    38  			OnboardingFinished: &of,
    39  		})
    40  		assert.NoError(t, err)
    41  
    42  		defer func() {
    43  			_ = lifecycle.Destroy(testInstance.Domain)
    44  		}()
    45  
    46  		// Install the apps
    47  		for _, a := range []string{"drive", "home", "settings"} {
    48  			installer, err := app.NewInstaller(testInstance, app.Copier(consts.WebappType, testInstance), &app.InstallerOptions{
    49  				Operation:  app.Install,
    50  				Type:       consts.WebappType,
    51  				SourceURL:  fmt.Sprintf("registry://%s", a),
    52  				Slug:       a,
    53  				Registries: testInstance.Registries(),
    54  			})
    55  			assert.NoError(t, err)
    56  			_, err = installer.RunSync()
    57  			assert.NoError(t, err)
    58  		}
    59  
    60  		// Test the pagination
    61  		apps, next, err := app.ListWebappsWithPagination(testInstance, 1, "")
    62  		assert.NoError(t, err)
    63  		assert.Equal(t, 1, len(apps))
    64  		assert.NotEqual(t, "", next)
    65  
    66  		// Retreiving the first two apps
    67  		apps, next, err = app.ListWebappsWithPagination(testInstance, 2, "")
    68  		assert.NoError(t, err)
    69  		assert.Equal(t, 2, len(apps))
    70  		assert.NotEqual(t, "", next)
    71  
    72  		// Same limit as before, we should get the last app
    73  		apps, next, err = app.ListWebappsWithPagination(testInstance, 2, next)
    74  		assert.NoError(t, err)
    75  		assert.Equal(t, 1, len(apps))
    76  		assert.Equal(t, "", next)
    77  
    78  		// With limit = 0, the default limit will be applied, we should get all the
    79  		// apps
    80  		apps, next, err = app.ListWebappsWithPagination(testInstance, 0, next)
    81  		assert.NoError(t, err)
    82  		assert.Equal(t, 3, len(apps))
    83  		assert.Equal(t, "", next)
    84  	})
    85  }