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

     1  package files
     2  
     3  import (
     4  	"net/url"
     5  	"testing"
     6  
     7  	"github.com/cozy/cozy-stack/pkg/config/config"
     8  	"github.com/cozy/cozy-stack/pkg/consts"
     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  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestNotsynchronized(t *testing.T) {
    19  	if testing.Short() {
    20  		t.Skip("an instance is required for this test: test skipped due to the use of --short flag")
    21  	}
    22  
    23  	var dirID, dirID2 string
    24  
    25  	config.UseTestFile(t)
    26  	require.NoError(t, loadLocale(), "Could not load default locale translations")
    27  
    28  	testutils.NeedCouchdb(t)
    29  	setup := testutils.NewSetup(t, t.Name())
    30  
    31  	config.GetConfig().Fs.URL = &url.URL{
    32  		Scheme: "file",
    33  		Host:   "localhost",
    34  		Path:   t.TempDir(),
    35  	}
    36  
    37  	testInstance := setup.GetTestInstance()
    38  	_, token := setup.GetTestClient(consts.Files + " " + consts.CertifiedCarbonCopy + " " + consts.CertifiedElectronicSafe)
    39  	ts := setup.GetTestServer("/files", Routes, func(r *echo.Echo) *echo.Echo {
    40  		secure := middlewares.Secure(&middlewares.SecureConfig{
    41  			CSPDefaultSrc:     []middlewares.CSPSource{middlewares.CSPSrcSelf},
    42  			CSPFrameAncestors: []middlewares.CSPSource{middlewares.CSPSrcNone},
    43  		})
    44  		r.Use(secure)
    45  		return r
    46  	})
    47  	ts.Config.Handler.(*echo.Echo).HTTPErrorHandler = errors.ErrorHandler
    48  	t.Cleanup(ts.Close)
    49  
    50  	t.Run("AddNotSynchronizedOnOneRelation", func(t *testing.T) {
    51  		e := testutils.CreateTestClient(t, ts.URL)
    52  
    53  		obj := e.POST("/files/").
    54  			WithQuery("Name", "to_sync_or_not_to_sync_1").
    55  			WithQuery("Type", "directory").
    56  			WithHeader("Authorization", "Bearer "+token).
    57  			Expect().Status(201).
    58  			JSON(httpexpect.ContentOpts{MediaType: "application/vnd.api+json"}).
    59  			Object()
    60  
    61  		dirID = obj.Path("$.data.id").String().NotEmpty().Raw()
    62  		dirData := obj.Value("data").Object()
    63  
    64  		obj = e.POST("/files/"+dirID+"/relationships/not_synchronized_on").
    65  			WithHeader("Authorization", "Bearer "+token).
    66  			WithHeader("Content-Type", "application/json").
    67  			WithBytes([]byte(`{
    68          "data": {
    69            "id": "fooclientid",
    70            "type": "io.cozy.oauth.clients"
    71          }
    72        }`)).
    73  			Expect().Status(200).
    74  			JSON(httpexpect.ContentOpts{MediaType: "application/vnd.api+json"}).
    75  			Object()
    76  
    77  		obj.Path("$.meta.rev").NotEqual(dirData.Path("$.meta.rev").String().Raw())
    78  		obj.Path("$.meta.count").Equal(1)
    79  		data := obj.Value("data").Array()
    80  		data.Length().Equal(1)
    81  
    82  		item := data.First().Object()
    83  		item.ValueEqual("id", "fooclientid")
    84  		item.ValueEqual("type", "io.cozy.oauth.clients")
    85  
    86  		doc, err := testInstance.VFS().DirByID(dirID)
    87  		assert.NoError(t, err)
    88  		assert.Len(t, doc.NotSynchronizedOn, 1)
    89  		assert.Equal(t, doc.Rev(), obj.Path("$.meta.rev").String().Raw())
    90  	})
    91  
    92  	t.Run("AddNotSynchronizedOnMultipleRelation", func(t *testing.T) {
    93  		e := testutils.CreateTestClient(t, ts.URL)
    94  
    95  		obj := e.POST("/files/").
    96  			WithQuery("Name", "to_sync_or_not_to_sync_2").
    97  			WithQuery("Type", "directory").
    98  			WithHeader("Authorization", "Bearer "+token).
    99  			Expect().Status(201).
   100  			JSON(httpexpect.ContentOpts{MediaType: "application/vnd.api+json"}).
   101  			Object()
   102  
   103  		dirID2 = obj.Path("$.data.id").String().NotEmpty().Raw()
   104  		dirData := obj.Value("data").Object()
   105  
   106  		obj = e.POST("/files/"+dirID2+"/relationships/not_synchronized_on").
   107  			WithHeader("Authorization", "Bearer "+token).
   108  			WithHeader("Content-Type", "application/json").
   109  			WithBytes([]byte(`{
   110          "data": [
   111          { "id": "fooclientid1", "type": "io.cozy.oauth.clients" },
   112          { "id": "fooclientid2", "type": "io.cozy.oauth.clients" },
   113          { "id": "fooclientid3", "type": "io.cozy.oauth.clients" }
   114          ]
   115        }`)).
   116  			Expect().Status(200).
   117  			JSON(httpexpect.ContentOpts{MediaType: "application/vnd.api+json"}).
   118  			Object()
   119  
   120  		obj.Path("$.meta.rev").NotEqual(dirData.Path("$.meta.rev").String().Raw())
   121  		obj.Path("$.meta.count").Equal(3)
   122  		data := obj.Value("data").Array()
   123  		data.Length().Equal(3)
   124  
   125  		item := data.Element(0).Object()
   126  		item.ValueEqual("id", "fooclientid1")
   127  		item.ValueEqual("type", "io.cozy.oauth.clients")
   128  
   129  		item = data.Element(1).Object()
   130  		item.ValueEqual("id", "fooclientid2")
   131  		item.ValueEqual("type", "io.cozy.oauth.clients")
   132  
   133  		item = data.Element(2).Object()
   134  		item.ValueEqual("id", "fooclientid3")
   135  		item.ValueEqual("type", "io.cozy.oauth.clients")
   136  
   137  		doc, err := testInstance.VFS().DirByID(dirID2)
   138  		assert.NoError(t, err)
   139  		assert.Len(t, doc.NotSynchronizedOn, 3)
   140  		assert.Equal(t, doc.Rev(), obj.Path("$.meta.rev").String().Raw())
   141  	})
   142  
   143  	t.Run("RemoveNotSynchronizedOnOneRelation", func(t *testing.T) {
   144  		e := testutils.CreateTestClient(t, ts.URL)
   145  
   146  		obj := e.DELETE("/files/"+dirID+"/relationships/not_synchronized_on").
   147  			WithHeader("Authorization", "Bearer "+token).
   148  			WithHeader("Content-Type", "application/json").
   149  			WithBytes([]byte(`{
   150          "data": {
   151            "id": "fooclientid",
   152            "type": "io.cozy.oauth.clients"
   153          }
   154        }`)).
   155  			Expect().Status(200).
   156  			JSON(httpexpect.ContentOpts{MediaType: "application/vnd.api+json"}).
   157  			Object()
   158  
   159  		obj.Path("$.meta.count").Equal(0)
   160  		obj.Value("data").Array().Length().Equal(0)
   161  
   162  		doc, err := testInstance.VFS().DirByID(dirID)
   163  		assert.NoError(t, err)
   164  		assert.Len(t, doc.NotSynchronizedOn, 0)
   165  	})
   166  
   167  	t.Run("RemoveNotSynchronizedOnMultipleRelation", func(t *testing.T) {
   168  		e := testutils.CreateTestClient(t, ts.URL)
   169  
   170  		obj := e.DELETE("/files/"+dirID2+"/relationships/not_synchronized_on").
   171  			WithHeader("Authorization", "Bearer "+token).
   172  			WithHeader("Content-Type", "application/json").
   173  			WithBytes([]byte(`{
   174          "data": [
   175          { "id": "fooclientid3", "type": "io.cozy.oauth.clients" },
   176          { "id": "fooclientid5", "type": "io.cozy.oauth.clients" },
   177          { "id": "fooclientid1", "type": "io.cozy.oauth.clients" }
   178          ]
   179        }`)).
   180  			Expect().Status(200).
   181  			JSON(httpexpect.ContentOpts{MediaType: "application/vnd.api+json"}).
   182  			Object()
   183  
   184  		obj.Path("$.meta.count").Equal(1)
   185  		data := obj.Value("data").Array()
   186  		data.Length().Equal(1)
   187  		data.First().Object().ValueEqual("id", "fooclientid2")
   188  		data.First().Object().ValueEqual("type", "io.cozy.oauth.clients")
   189  
   190  		doc, err := testInstance.VFS().DirByID(dirID2)
   191  		assert.NoError(t, err)
   192  		assert.Len(t, doc.NotSynchronizedOn, 1)
   193  		assert.Equal(t, "fooclientid2", doc.NotSynchronizedOn[0].ID)
   194  	})
   195  }