github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/daemon/api_icons_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2020 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package daemon_test 21 22 import ( 23 "io/ioutil" 24 "net/http" 25 "net/http/httptest" 26 "os" 27 "path/filepath" 28 29 "gopkg.in/check.v1" 30 31 "github.com/snapcore/snapd/daemon" 32 "github.com/snapcore/snapd/snap" 33 ) 34 35 var _ = check.Suite(&iconsSuite{}) 36 37 type iconsSuite struct { 38 apiBaseSuite 39 } 40 41 func (s *iconsSuite) TestAppIconGet(c *check.C) { 42 d := s.daemon(c) 43 44 // have an active foo in the system 45 info := s.mkInstalledInState(c, d, "foo", "bar", "v1", snap.R(10), true, "") 46 47 // have an icon for it in the package itself 48 iconfile := filepath.Join(info.MountDir(), "meta", "gui", "icon.ick") 49 c.Assert(os.MkdirAll(filepath.Dir(iconfile), 0755), check.IsNil) 50 c.Check(ioutil.WriteFile(iconfile, []byte("ick"), 0644), check.IsNil) 51 52 s.vars = map[string]string{"name": "foo"} 53 req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil) 54 c.Assert(err, check.IsNil) 55 56 rec := httptest.NewRecorder() 57 58 s.req(c, req, nil).ServeHTTP(rec, req) 59 c.Check(rec.Code, check.Equals, 200) 60 c.Check(rec.Body.String(), check.Equals, "ick") 61 } 62 63 func (s *iconsSuite) TestAppIconGetInactive(c *check.C) { 64 d := s.daemon(c) 65 66 // have an *in*active foo in the system 67 info := s.mkInstalledInState(c, d, "foo", "bar", "v1", snap.R(10), false, "") 68 69 // have an icon for it in the package itself 70 iconfile := filepath.Join(info.MountDir(), "meta", "gui", "icon.ick") 71 c.Assert(os.MkdirAll(filepath.Dir(iconfile), 0755), check.IsNil) 72 c.Check(ioutil.WriteFile(iconfile, []byte("ick"), 0644), check.IsNil) 73 74 s.vars = map[string]string{"name": "foo"} 75 req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil) 76 c.Assert(err, check.IsNil) 77 78 rec := httptest.NewRecorder() 79 80 s.req(c, req, nil).ServeHTTP(rec, req) 81 c.Check(rec.Code, check.Equals, 200) 82 c.Check(rec.Body.String(), check.Equals, "ick") 83 } 84 85 func (s *iconsSuite) TestAppIconGetNoIcon(c *check.C) { 86 d := s.daemon(c) 87 88 // have an *in*active foo in the system 89 info := s.mkInstalledInState(c, d, "foo", "bar", "v1", snap.R(10), true, "") 90 91 // NO ICON! 92 err := os.RemoveAll(filepath.Join(info.MountDir(), "meta", "gui", "icon.svg")) 93 c.Assert(err, check.IsNil) 94 95 s.vars = map[string]string{"name": "foo"} 96 req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil) 97 c.Assert(err, check.IsNil) 98 99 rec := httptest.NewRecorder() 100 101 s.req(c, req, nil).ServeHTTP(rec, req) 102 c.Check(rec.Code/100, check.Equals, 4) 103 } 104 105 func (s *iconsSuite) TestAppIconGetNoApp(c *check.C) { 106 s.daemon(c) 107 108 s.vars = map[string]string{"name": "foo"} 109 req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil) 110 c.Assert(err, check.IsNil) 111 112 rec := httptest.NewRecorder() 113 114 s.req(c, req, nil).ServeHTTP(rec, req) 115 c.Check(rec.Code, check.Equals, 404) 116 } 117 118 func (s *iconsSuite) TestNotInstalledSnapIcon(c *check.C) { 119 info := &snap.Info{SuggestedName: "notInstalledSnap", Media: []snap.MediaInfo{{Type: "icon", URL: "icon.svg"}}} 120 iconfile := daemon.SnapIcon(info) 121 c.Check(iconfile, check.Equals, "") 122 }