github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/auth/deprecated_app_list_test.go (about) 1 package auth 2 3 import ( 4 "fmt" 5 "html/template" 6 "strings" 7 "testing" 8 9 "github.com/cozy/cozy-stack/model/instance" 10 "github.com/cozy/cozy-stack/model/oauth" 11 "github.com/cozy/cozy-stack/pkg/config/config" 12 "github.com/cozy/cozy-stack/web/middlewares" 13 "github.com/cozy/cozy-stack/web/statik" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func Test_DeprecatedAppList(t *testing.T) { 18 t.Run("IsDeprecated", func(t *testing.T) { 19 list := NewDeprecatedAppList(config.DeprecatedAppsCfg{ 20 Apps: []config.DeprecatedApp{ 21 { 22 SoftwareID: "github.com/cozy/super-app", 23 Name: "Super App", 24 StoreURLs: map[string]string{ 25 "android": "https://some-android/url", 26 "iphone": "https://some-IOS/url", 27 }, 28 }, 29 }, 30 }) 31 32 isDeprecated := list.IsDeprecated(&oauth.Client{ 33 ClientID: "some-id", 34 ClientSecret: "some-secret", 35 RedirectURIs: []string{"http://localhost"}, 36 ClientName: "Super App", 37 ClientKind: "mobile", 38 SoftwareID: "github.com/cozy/super-app", 39 }) 40 assert.True(t, isDeprecated) 41 }) 42 43 t.Run("RenderArgs", func(t *testing.T) { 44 tests := []struct { 45 Name string 46 UA string 47 ExpectedPlatform string 48 ExpectedURL string 49 }{ 50 { 51 Name: "with a classic android", 52 UA: "Mozilla/5.0 (Linux; U; Android 1.5; de-; HTC Magic Build/PLAT-RC33) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1", 53 ExpectedPlatform: "android", 54 ExpectedURL: "https://some-android/url", 55 }, 56 { 57 Name: "with a firefox on ipad", 58 UA: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/24.1 Safari/605.1.15", 59 ExpectedPlatform: "iphone", 60 ExpectedURL: "https://some-IOS/url", 61 }, 62 { 63 Name: "with an iphone", 64 UA: "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419", 65 ExpectedPlatform: "iphone", 66 ExpectedURL: "https://some-IOS/url", 67 }, 68 { 69 Name: "with an iphone", 70 UA: "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419", 71 ExpectedPlatform: "iphone", 72 ExpectedURL: "https://some-IOS/url", 73 }, 74 { 75 Name: "with an non mobile browser (chrome - ubuntu)", 76 UA: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36", 77 ExpectedPlatform: "other", 78 ExpectedURL: DefaultStoreURL, 79 }, 80 } 81 82 for _, test := range tests { 83 t.Run(test.Name, func(t *testing.T) { 84 config.UseTestFile(t) 85 middlewares.FuncsMap = template.FuncMap{ 86 "t": fmt.Sprintf, 87 "tHTML": fmt.Sprintf, 88 "split": strings.Split, 89 "replace": strings.Replace, 90 "hasSuffix": strings.HasSuffix, 91 "asset": statik.AssetPath, 92 } 93 middlewares.BuildTemplates() 94 95 oauthClient := &oauth.Client{ 96 ClientID: "some-id", 97 ClientSecret: "some-secret", 98 RedirectURIs: []string{"http://localhost"}, 99 ClientName: "Super App", 100 ClientKind: "mobile", 101 SoftwareID: "github.com/cozy/super-app", 102 } 103 104 inst := &instance.Instance{ 105 ContextName: "some-context", 106 Domain: "foobar.cozy.local", 107 Locale: "en", 108 } 109 110 list := NewDeprecatedAppList(config.DeprecatedAppsCfg{ 111 Apps: []config.DeprecatedApp{ 112 { 113 SoftwareID: "github.com/cozy/super-app", 114 Name: "Super App", 115 StoreURLs: map[string]string{ 116 "android": "https://some-android/url", 117 "iphone": "https://some-IOS/url", 118 }, 119 }, 120 }, 121 }) 122 123 args := list.RenderArgs(oauthClient, inst, test.UA) 124 assert.Equal(t, map[string]interface{}{ 125 "Domain": "foobar.cozy.local", 126 "ContextName": "some-context", 127 "Locale": "en", 128 "Title": instance.DefaultTemplateTitle, 129 "Favicon": middlewares.Favicon(inst), 130 "AppName": "Super App", 131 "Platform": test.ExpectedPlatform, 132 "StoreURL": template.URL(test.ExpectedURL), 133 }, args) 134 }) 135 } 136 }) 137 138 t.Run("NewDeprecatedAppList with an empty object", func(t *testing.T) { 139 list := NewDeprecatedAppList(config.DeprecatedAppsCfg{}) 140 141 assert.False(t, list.IsDeprecated(&oauth.Client{})) 142 }) 143 144 t.Run("NewDeprecatedAppList with an nil Apps", func(t *testing.T) { 145 list := NewDeprecatedAppList(config.DeprecatedAppsCfg{ 146 Apps: nil, 147 }) 148 149 assert.False(t, list.IsDeprecated(&oauth.Client{})) 150 }) 151 }