github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/instances/instances_test.go (about)

     1  package instances
     2  
     3  import (
     4  	"net/url"
     5  	"testing"
     6  
     7  	"github.com/cozy/cozy-stack/model/instance/lifecycle"
     8  	"github.com/cozy/cozy-stack/pkg/config/config"
     9  	"github.com/cozy/cozy-stack/tests/testutils"
    10  	"github.com/cozy/cozy-stack/web/errors"
    11  	"github.com/cozy/cozy-stack/web/middlewares"
    12  	"github.com/gavv/httpexpect/v2"
    13  	"github.com/labstack/echo/v4"
    14  )
    15  
    16  func TestInstances(t *testing.T) {
    17  	config.UseTestFile(t)
    18  
    19  	testutils.NeedCouchdb(t)
    20  	setup := testutils.NewSetup(t, t.Name())
    21  
    22  	config.GetConfig().Fs.URL = &url.URL{
    23  		Scheme: "file",
    24  		Host:   "localhost",
    25  		Path:   t.TempDir(),
    26  	}
    27  
    28  	_, token := setup.GetTestAdminClient()
    29  	ts := setup.GetTestServer("/instances", Routes, func(r *echo.Echo) *echo.Echo {
    30  		secure := middlewares.Secure(&middlewares.SecureConfig{
    31  			CSPDefaultSrc:     []middlewares.CSPSource{middlewares.CSPSrcSelf},
    32  			CSPFrameAncestors: []middlewares.CSPSource{middlewares.CSPSrcNone},
    33  		})
    34  		r.Use(secure)
    35  		return r
    36  	})
    37  	ts.Config.Handler.(*echo.Echo).HTTPErrorHandler = errors.ErrorHandler
    38  	t.Cleanup(ts.Close)
    39  
    40  	t.Run("Create", func(t *testing.T) {
    41  		domain := "alice.cozy.localhost"
    42  		t.Cleanup(func() { _ = lifecycle.Destroy(domain) })
    43  
    44  		e := testutils.CreateTestClient(t, ts.URL)
    45  
    46  		// Create instance with feature sets
    47  		e.POST("/instances").
    48  			WithQuery("DiskQuota", 5000000000).
    49  			WithQuery("Domain", domain).
    50  			WithQuery("Email", "alice@example.com").
    51  			WithQuery("Locale", "en").
    52  			WithQuery("PublicName", "alice").
    53  			WithQuery("Settings", "partner:cozy,context:cozy_beta,tos:1.0.0").
    54  			WithQuery("SwiftLayout", "-1").
    55  			WithQuery("TOSSigned", "1.0.0").
    56  			WithQuery("UUID", "60bac7e8-abd9-11ee-8201-9cb6d0907fa3").
    57  			WithQuery("feature_sets", "71df3022-abd9-11ee-b79b-9cb6d0907fa3,790789f8-abd9-11ee-ae09-9cb6d0907fa3").
    58  			WithQuery("sponsorships", "").
    59  			WithHeader("Authorization", "Bearer "+token).
    60  			Expect().Status(201).
    61  			JSON(httpexpect.ContentOpts{MediaType: "application/vnd.api+json"}).
    62  			Object().Path("$.data.attributes.feature_sets").
    63  			Array().
    64  			HasValue(0, "71df3022-abd9-11ee-b79b-9cb6d0907fa3").
    65  			HasValue(1, "790789f8-abd9-11ee-ae09-9cb6d0907fa3")
    66  	})
    67  }