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

     1  package app
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/cozy/cozy-stack/pkg/consts"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestFindRoute(t *testing.T) {
    11  	manifest := &WebappManifest{}
    12  	manifest.val.Routes = make(Routes)
    13  	manifest.val.Routes["/foo"] = Route{Folder: "/foo", Index: "index.html"}
    14  	manifest.val.Routes["/foo/bar"] = Route{Folder: "/bar", Index: "index.html"}
    15  	manifest.val.Routes["/foo/qux"] = Route{Folder: "/qux", Index: "index.html"}
    16  	manifest.val.Routes["/public"] = Route{Folder: "/public", Index: "public.html", Public: true}
    17  	manifest.val.Routes["/admin"] = Route{Folder: "/admin", Index: "admin.html"}
    18  	manifest.val.Routes["/admin/special"] = Route{Folder: "/special", Index: "admin.html"}
    19  
    20  	ctx, rest := manifest.FindRoute("/admin")
    21  	assert.Equal(t, "/admin", ctx.Folder)
    22  	assert.Equal(t, "admin.html", ctx.Index)
    23  	assert.Equal(t, false, ctx.Public)
    24  	assert.Equal(t, "", rest)
    25  
    26  	ctx, rest = manifest.FindRoute("/public/")
    27  	assert.Equal(t, "/public", ctx.Folder)
    28  	assert.Equal(t, "public.html", ctx.Index)
    29  	assert.Equal(t, true, ctx.Public)
    30  	assert.Equal(t, "", rest)
    31  
    32  	ctx, rest = manifest.FindRoute("/public")
    33  	assert.Equal(t, "/public", ctx.Folder)
    34  	assert.Equal(t, "", rest)
    35  
    36  	ctx, rest = manifest.FindRoute("/public/app.js")
    37  	assert.Equal(t, "/public", ctx.Folder)
    38  	assert.Equal(t, "app.js", rest)
    39  
    40  	ctx, rest = manifest.FindRoute("/foo/admin/special")
    41  	assert.Equal(t, "/foo", ctx.Folder)
    42  	assert.Equal(t, "admin/special", rest)
    43  
    44  	ctx, rest = manifest.FindRoute("/admin/special/foo")
    45  	assert.Equal(t, "/special", ctx.Folder)
    46  	assert.Equal(t, "foo", rest)
    47  
    48  	ctx, rest = manifest.FindRoute("/foo/bar.html")
    49  	assert.Equal(t, "/foo", ctx.Folder)
    50  	assert.Equal(t, "bar.html", rest)
    51  
    52  	ctx, rest = manifest.FindRoute("/foo/baz")
    53  	assert.Equal(t, "/foo", ctx.Folder)
    54  	assert.Equal(t, "baz", rest)
    55  
    56  	ctx, rest = manifest.FindRoute("/foo/bar")
    57  	assert.Equal(t, "/bar", ctx.Folder)
    58  	assert.Equal(t, "", rest)
    59  
    60  	ctx, _ = manifest.FindRoute("/")
    61  	assert.Equal(t, "", ctx.Folder)
    62  }
    63  
    64  func TestNoRegression217(t *testing.T) {
    65  	var man WebappManifest
    66  	man.val.Routes = make(Routes)
    67  	man.val.Routes["/"] = Route{
    68  		Folder: "/",
    69  		Index:  "index.html",
    70  		Public: false,
    71  	}
    72  
    73  	ctx, rest := man.FindRoute("/any/path")
    74  	assert.Equal(t, "/", ctx.Folder)
    75  	assert.Equal(t, "any/path", rest)
    76  }
    77  
    78  func TestFindIntent(t *testing.T) {
    79  	var man WebappManifest
    80  	found := man.FindIntent("PICK", "io.cozy.files")
    81  	assert.Nil(t, found)
    82  
    83  	man.val.Intents = []Intent{
    84  		{
    85  			Action: "PICK",
    86  			Types:  []string{"io.cozy.contacts", "io.cozy.calendars"},
    87  			Href:   "/pick",
    88  		},
    89  		{
    90  			Action: "OPEN",
    91  			Types:  []string{"io.cozy.files", "image/gif"},
    92  			Href:   "/open",
    93  		},
    94  		{
    95  			Action: "EDIT",
    96  			Types:  []string{"image/*"},
    97  			Href:   "/open",
    98  		},
    99  	}
   100  	found = man.FindIntent("PICK", "io.cozy.files")
   101  	assert.Nil(t, found)
   102  	found = man.FindIntent("OPEN", "io.cozy.contacts")
   103  	assert.Nil(t, found)
   104  	found = man.FindIntent("PICK", "io.cozy.contacts")
   105  	assert.NotNil(t, found)
   106  	assert.Equal(t, "PICK", found.Action)
   107  	found = man.FindIntent("OPEN", "io.cozy.files")
   108  	assert.NotNil(t, found)
   109  	assert.Equal(t, "OPEN", found.Action)
   110  	found = man.FindIntent("open", "io.cozy.files")
   111  	assert.NotNil(t, found)
   112  	assert.Equal(t, "OPEN", found.Action)
   113  
   114  	found = man.FindIntent("OPEN", "image/gif")
   115  	assert.NotNil(t, found)
   116  	assert.Equal(t, "OPEN", found.Action)
   117  	found = man.FindIntent("EDIT", "image/gif")
   118  	assert.NotNil(t, found)
   119  	assert.Equal(t, "EDIT", found.Action)
   120  
   121  	man.val.Intents = []Intent{
   122  		{
   123  			Action: "PICK",
   124  			Href:   "/pick",
   125  		},
   126  	}
   127  	found = man.FindIntent("PICK", "io.cozy.files")
   128  	assert.Nil(t, found)
   129  }
   130  
   131  func Test_GetBySlug(t *testing.T) {
   132  	t.Run("with an invalid appType", func(t *testing.T) {
   133  		man, err := GetBySlug(nil, "some-slug", consts.AppType(0))
   134  		assert.Nil(t, man)
   135  		assert.ErrorIs(t, err, ErrInvalidAppType)
   136  	})
   137  }