github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/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  	req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil)
    53  	c.Assert(err, check.IsNil)
    54  
    55  	rec := httptest.NewRecorder()
    56  
    57  	s.req(c, req, nil).ServeHTTP(rec, req)
    58  	c.Check(rec.Code, check.Equals, 200)
    59  	c.Check(rec.Body.String(), check.Equals, "ick")
    60  }
    61  
    62  func (s *iconsSuite) TestAppIconGetInactive(c *check.C) {
    63  	d := s.daemon(c)
    64  
    65  	// have an *in*active foo in the system
    66  	info := s.mkInstalledInState(c, d, "foo", "bar", "v1", snap.R(10), false, "")
    67  
    68  	// have an icon for it in the package itself
    69  	iconfile := filepath.Join(info.MountDir(), "meta", "gui", "icon.ick")
    70  	c.Assert(os.MkdirAll(filepath.Dir(iconfile), 0755), check.IsNil)
    71  	c.Check(ioutil.WriteFile(iconfile, []byte("ick"), 0644), check.IsNil)
    72  
    73  	req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil)
    74  	c.Assert(err, check.IsNil)
    75  
    76  	rec := httptest.NewRecorder()
    77  
    78  	s.req(c, req, nil).ServeHTTP(rec, req)
    79  	c.Check(rec.Code, check.Equals, 200)
    80  	c.Check(rec.Body.String(), check.Equals, "ick")
    81  }
    82  
    83  func (s *iconsSuite) TestAppIconGetNoIcon(c *check.C) {
    84  	d := s.daemon(c)
    85  
    86  	// have an *in*active foo in the system
    87  	info := s.mkInstalledInState(c, d, "foo", "bar", "v1", snap.R(10), true, "")
    88  
    89  	// NO ICON!
    90  	err := os.RemoveAll(filepath.Join(info.MountDir(), "meta", "gui", "icon.svg"))
    91  	c.Assert(err, check.IsNil)
    92  
    93  	req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil)
    94  	c.Assert(err, check.IsNil)
    95  
    96  	rec := httptest.NewRecorder()
    97  
    98  	s.req(c, req, nil).ServeHTTP(rec, req)
    99  	c.Check(rec.Code/100, check.Equals, 4)
   100  }
   101  
   102  func (s *iconsSuite) TestAppIconGetNoApp(c *check.C) {
   103  	s.daemon(c)
   104  
   105  	req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil)
   106  	c.Assert(err, check.IsNil)
   107  
   108  	rec := httptest.NewRecorder()
   109  
   110  	s.req(c, req, nil).ServeHTTP(rec, req)
   111  	c.Check(rec.Code, check.Equals, 404)
   112  }
   113  
   114  func (s *iconsSuite) TestNotInstalledSnapIcon(c *check.C) {
   115  	info := &snap.Info{SuggestedName: "notInstalledSnap", Media: []snap.MediaInfo{{Type: "icon", URL: "icon.svg"}}}
   116  	iconfile := daemon.SnapIcon(info)
   117  	c.Check(iconfile, check.Equals, "")
   118  }