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

     1  package intent
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/cozy/cozy-stack/model/app"
     8  	"github.com/cozy/cozy-stack/model/instance"
     9  	"github.com/cozy/cozy-stack/pkg/config/config"
    10  	"github.com/cozy/cozy-stack/pkg/consts"
    11  	"github.com/cozy/cozy-stack/pkg/couchdb"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  	"golang.org/x/sync/errgroup"
    15  )
    16  
    17  func TestIntents(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  
    24  	ins := &instance.Instance{Domain: "cozy.example.net"}
    25  
    26  	err := couchdb.ResetDB(ins, consts.Apps)
    27  	require.NoError(t, err)
    28  
    29  	g, _ := errgroup.WithContext(context.Background())
    30  	couchdb.DefineIndexes(g, ins, couchdb.IndexesByDoctype(consts.Apps))
    31  	require.NoError(t, g.Wait())
    32  
    33  	t.Cleanup(func() {
    34  		_ = couchdb.DeleteDB(ins, consts.Apps)
    35  	})
    36  
    37  	t.Run("GenerateHref", func(t *testing.T) {
    38  		intent := &Intent{IID: "6fba9dd6-1487-11e7-b90d-130a5dedd6d6"}
    39  
    40  		href := intent.GenerateHref(ins, "files", "/pick")
    41  		assert.Equal(t, "https://files.cozy.example.net/pick?intent=6fba9dd6-1487-11e7-b90d-130a5dedd6d6", href)
    42  
    43  		href = intent.GenerateHref(ins, "files", "/view")
    44  		assert.Equal(t, "https://files.cozy.example.net/view?intent=6fba9dd6-1487-11e7-b90d-130a5dedd6d6", href)
    45  	})
    46  
    47  	t.Run("FillServices", func(t *testing.T) {
    48  		files := &couchdb.JSONDoc{
    49  			Type: consts.Apps,
    50  			M: map[string]interface{}{
    51  				"_id":  consts.Apps + "/files",
    52  				"slug": "files",
    53  				"intents": []app.Intent{
    54  					{
    55  						Action: "PICK",
    56  						Types:  []string{"io.cozy.files", "image/gif"},
    57  						Href:   "/pick",
    58  					},
    59  				},
    60  			},
    61  		}
    62  		err := couchdb.CreateNamedDoc(ins, files)
    63  		assert.NoError(t, err)
    64  		photos := &couchdb.JSONDoc{
    65  			Type: consts.Apps,
    66  			M: map[string]interface{}{
    67  				"_id":  consts.Apps + "/photos",
    68  				"slug": "photos",
    69  				"intents": []app.Intent{
    70  					{
    71  						Action: "PICK",
    72  						Types:  []string{"image/*"},
    73  						Href:   "/picker",
    74  					},
    75  					{
    76  						Action: "VIEW",
    77  						Types:  []string{"io.cozy.files"},
    78  						Href:   "/viewer",
    79  					},
    80  				},
    81  			},
    82  		}
    83  		err = couchdb.CreateNamedDoc(ins, photos)
    84  		assert.NoError(t, err)
    85  
    86  		intent := &Intent{
    87  			IID:    "6b44d8d0-148b-11e7-a1cf-a38d75a77df6",
    88  			Action: "PICK",
    89  			Type:   "io.cozy.files",
    90  		}
    91  		err = intent.FillServices(ins)
    92  		assert.NoError(t, err)
    93  		assert.Len(t, intent.Services, 1)
    94  		service := intent.Services[0]
    95  		assert.Equal(t, "files", service.Slug)
    96  		assert.Equal(t, "https://files.cozy.example.net/pick?intent=6b44d8d0-148b-11e7-a1cf-a38d75a77df6", service.Href)
    97  
    98  		intent = &Intent{
    99  			IID:    "6b44d8d0-148b-11e7-a1cf-a38d75a77df6",
   100  			Action: "view",
   101  			Type:   "io.cozy.files",
   102  		}
   103  		err = intent.FillServices(ins)
   104  		assert.NoError(t, err)
   105  		assert.Len(t, intent.Services, 1)
   106  		service = intent.Services[0]
   107  		assert.Equal(t, "photos", service.Slug)
   108  		assert.Equal(t, "https://photos.cozy.example.net/viewer?intent=6b44d8d0-148b-11e7-a1cf-a38d75a77df6", service.Href)
   109  
   110  		intent = &Intent{
   111  			IID:    "6b44d8d0-148b-11e7-a1cf-a38d75a77df6",
   112  			Action: "PICK",
   113  			Type:   "image/gif",
   114  		}
   115  		err = intent.FillServices(ins)
   116  		assert.NoError(t, err)
   117  		assert.Len(t, intent.Services, 2)
   118  		service = intent.Services[0]
   119  		assert.Equal(t, "files", service.Slug)
   120  		assert.Equal(t, "https://files.cozy.example.net/pick?intent=6b44d8d0-148b-11e7-a1cf-a38d75a77df6", service.Href)
   121  		service = intent.Services[1]
   122  		assert.Equal(t, "photos", service.Slug)
   123  		assert.Equal(t, "https://photos.cozy.example.net/picker?intent=6b44d8d0-148b-11e7-a1cf-a38d75a77df6", service.Href)
   124  
   125  		intent = &Intent{
   126  			IID:    "6b44d8d0-148b-11e7-a1cf-a38d75a77df6",
   127  			Action: "VIEW",
   128  			Type:   "image/gif",
   129  		}
   130  		err = intent.FillServices(ins)
   131  		assert.NoError(t, err)
   132  		assert.Len(t, intent.Services, 0)
   133  	})
   134  
   135  	t.Run("FillAvailableWebapps", func(t *testing.T) {
   136  		intent := &Intent{
   137  			IID:    "6b44d8d0-148b-11e7-a1cf-a38d75a77df6",
   138  			Action: "REDIRECT",
   139  			Type:   "io.cozy.accounts",
   140  		}
   141  		err := intent.FillAvailableWebapps(ins)
   142  		assert.NoError(t, err)
   143  
   144  		// Should have Home and Collect
   145  		assert.Equal(t, 2, len(intent.AvailableApps))
   146  
   147  		res := map[string]interface{}{}
   148  		for _, v := range intent.AvailableApps {
   149  			res[v.Slug] = struct{}{}
   150  		}
   151  
   152  		assert.Contains(t, res, "home")
   153  		assert.Contains(t, res, "collect")
   154  	})
   155  }