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