Debian Codegraph @

urldispatcher/urldispatcher_test.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package urldispatcher
18
19 import (
20 "errors"
21
22 . "gopkg.in/check.v1"
23 clickhelp "gitlab.com/ubports/development/core/lomiri-push-service/click/testing"
24 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
25 "testing"
26 )
27
28 // hook up gocheck
29 func TestUrldispatcher(t *testing.T) { TestingT(t) }
30
31 type UDSuite struct {
32 log *helpers.TestLogger
33 cDispatchURL func(string, string) error
34 cTestURL func([]string) []string
35 }
36
37 var _ = Suite(&UDSuite{})
38
39 func (s *UDSuite) SetUpTest(c *C) {
40 s.log = helpers.NewTestLogger(c, "debug")
41 s.cDispatchURL = cDispatchURL
42 s.cTestURL = cTestURL
43 // replace it with a always succeed version
44 cDispatchURL = func(url string, appId string) error {
45 return nil
46 }
47 }
48
49 func (s *UDSuite) TearDownTest(c *C) {
50 cDispatchURL = s.cDispatchURL
51 cTestURL = s.cTestURL
52 }
53
54 func (s *UDSuite) TestDispatchURLWorks(c *C) {
55 ud := New(s.log)
56 appId := clickhelp.MustParseAppId("com.example.test_app_0.99")
57 err := ud.DispatchURL("this", appId)
58 c.Check(err, IsNil)
59 }
60
61 func (s *UDSuite) TestDispatchURLFailsIfCallFails(c *C) {
62 cDispatchURL = func(url string, appId string) error {
63 return errors.New("fail!")
64 }
65 ud := New(s.log)
66 appId := clickhelp.MustParseAppId("com.example.test_app_0.99")
67 err := ud.DispatchURL("this", appId)
68 c.Check(err, NotNil)
69 }
70
71 func (s *UDSuite) TestTestURLWorks(c *C) {
72 cTestURL = func(url []string) []string {
73 return []string{"com.example.test_app_0.99"}
74 }
75 ud := New(s.log)
76 appId := clickhelp.MustParseAppId("com.example.test_app_0.99")
77 c.Check(ud.TestURL(appId, []string{"this"}), Equals, true)
78 c.Check(s.log.Captured(), Matches, `(?sm).*TestURL: \[this\].*`)
79 }
80
81 func (s *UDSuite) TestTestURLFailsIfCallFails(c *C) {
82 cTestURL = func(url []string) []string {
83 return []string{}
84 }
85 ud := New(s.log)
86 appId := clickhelp.MustParseAppId("com.example.test_app_0.99")
87 c.Check(ud.TestURL(appId, []string{"this"}), Equals, false)
88 }
89
90 func (s *UDSuite) TestTestURLMultipleURLs(c *C) {
91 cTestURL = func(url []string) []string {
92 return []string{"com.example.test_app_0.99", "com.example.test_app_0.99"}
93 }
94 ud := New(s.log)
95 appId := clickhelp.MustParseAppId("com.example.test_app_0.99")
96 urls := []string{"potato://test-app", "potato_a://foo"}
97 c.Check(ud.TestURL(appId, urls), Equals, true)
98 c.Check(s.log.Captured(), Matches, `(?sm).*TestURL: \[potato://test-app potato_a://foo\].*`)
99 }
100
101 func (s *UDSuite) TestTestURLWrongApp(c *C) {
102 cTestURL = func(url []string) []string {
103 return []string{"com.example.test_test-app_0.1"}
104 }
105 ud := New(s.log)
106 appId := clickhelp.MustParseAppId("com.example.test_app_0.99")
107 urls := []string{"potato://test-app"}
108 c.Check(ud.TestURL(appId, urls), Equals, false)
109 c.Check(s.log.Captured(), Matches, `(?smi).*notification skipped because of different appid for actions: \[potato://test-app\] - com.example.test_test-app_0.1 != com.example.test_app_0.99`)
110 }
111
112 func (s *UDSuite) TestTestURLOneWrongApp(c *C) {
113 cTestURL = func(url []string) []string {
114 return []string{"com.example.test_test-app_0", "com.example.test_test-app1"}
115 }
116 ud := New(s.log)
117 appId := clickhelp.MustParseAppId("com.example.test_test-app_0")
118 urls := []string{"potato://test-app", "potato_a://foo"}
119 c.Check(ud.TestURL(appId, urls), Equals, false)
120 c.Check(s.log.Captured(), Matches, `(?smi).*notification skipped because of different appid for actions: \[potato://test-app potato_a://foo\] - com.example.test_test-app1 != com.example.test_test-app.*`)
121 }
122
123 func (s *UDSuite) TestTestURLInvalidURL(c *C) {
124 cTestURL = func(url []string) []string {
125 return []string{}
126 }
127 ud := New(s.log)
128 appId := clickhelp.MustParseAppId("com.example.test_app_0.2")
129 urls := []string{"notsupported://test-app"}
130 c.Check(ud.TestURL(appId, urls), Equals, false)
131 }
132
133 func (s *UDSuite) TestTestURLLegacyApp(c *C) {
134 cTestURL = func(url []string) []string {
135 return []string{"lomiri-system-settings"}
136 }
137 ud := New(s.log)
138 appId := clickhelp.MustParseAppId("_lomiri-system-settings")
139 urls := []string{"settings://test-app"}
140 c.Check(ud.TestURL(appId, urls), Equals, true)
141 c.Check(s.log.Captured(), Matches, `(?sm).*TestURL: \[settings://test-app\].*`)
142 }