~ubuntu-branches/ubuntu/vivid/unity-scope-click/vivid-proposed

« back to all changes in this revision

Viewing changes to tests/scope-harness/store-scope-harness.py

  • Committer: Package Import Robot
  • Author(s): CI Train Bot, Alejandro J. Cura, CI Train Bot, Pawel Stolowski
  • Date: 2015-03-26 18:49:47 UTC
  • mfrom: (1.1.87)
  • Revision ID: package-import@ubuntu.com-20150326184947-ixmeux4m98o3nkat
Tags: 0.1.1+15.04.20150326-0ubuntu1
[ Alejandro J. Cura ]
* Fake webservices for the integration tests
* Fetch the "refundable_until" field from the /purchases endpoint, and
  store it as a GMT timestamp
* Use table widget in previews (LP: #1407680)

[ CI Train Bot ]
* New rebuild forced.

[ Pawel Stolowski ]
* Initial set of python integration tests using the scope harness

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
# coding: utf-8
 
3
 
 
4
import http.server
 
5
import os
 
6
import logging
 
7
import socketserver
 
8
import sys
 
9
import unittest
 
10
 
 
11
import fixtures
 
12
 
 
13
from scope_harness import *
 
14
from scope_harness.testing import *
 
15
from fake_server_fixture import FakeServerFixture
 
16
 
 
17
logger = logging.getLogger(__name__)
 
18
 
 
19
 
 
20
class FakeSearchRequestHandler(http.server.SimpleHTTPRequestHandler):
 
21
    def do_GET(self):
 
22
        self.send_content(head_only=False)
 
23
 
 
24
    def do_HEAD(self):
 
25
        self.send_content(head_only=True)
 
26
 
 
27
    def send_content(self, head_only=False):
 
28
        path = self.translate_path(self.server.root_folder + self.path)
 
29
        logger.info("opening this path: %s", path)
 
30
        logger.info("server serves at: %s", self.server.root_folder)
 
31
        if os.path.isdir(path):
 
32
            path = os.path.join(path, "index.json")
 
33
        try:
 
34
            f = open(path, "rb")
 
35
            contents = f.read()
 
36
            new_url = self.server.url
 
37
            replaced_contents = contents.replace(b"[FAKE_SERVER_BASE]",
 
38
                                                 new_url.encode("utf-8"))
 
39
        except OSError:
 
40
            self.send_error(404, "File not found")
 
41
            return
 
42
 
 
43
        try:
 
44
            self.send_response(200)
 
45
            self.send_header("Content-type", "application/json")
 
46
            self.send_header("Content-Length", len(replaced_contents))
 
47
            self.end_headers()
 
48
 
 
49
            if (head_only):
 
50
                return
 
51
 
 
52
            self.wfile.write(replaced_contents)
 
53
 
 
54
        finally:
 
55
            f.close()
 
56
 
 
57
 
 
58
class FakeServer(socketserver.ForkingMixIn, http.server.HTTPServer):
 
59
    def __init__(self, server_address, root_folder):
 
60
        super().__init__(server_address, FakeSearchRequestHandler)
 
61
        self.root_folder = root_folder
 
62
 
 
63
 
 
64
class StoreTestBase(ScopeHarnessTestCase, fixtures.TestWithFixtures):
 
65
 
 
66
    def setupJsonServer(self, env_var, root_folder, append_slash=False):
 
67
        server_fixture = FakeServerFixture(FakeServer, root_folder)
 
68
        self.useFixture(server_fixture)
 
69
        self.useFixture(fixtures.EnvironmentVariable(env_var,
 
70
                newvalue=server_fixture.url))
 
71
 
 
72
    def setUp(self):
 
73
        self.useFixture(fixtures.TempHomeDir())
 
74
        self.useFixture(fixtures.EnvironmentVariable('LANGUAGE',
 
75
                newvalue='en_US.utf-8'))
 
76
        self.setupJsonServer("U1_SEARCH_BASE_URL",
 
77
                "fake_responses/click-package-index/", append_slash=True)
 
78
        self.setupJsonServer("U1_REVIEWS_BASE_URL",
 
79
                "fake_responses/ratings-and-reviews/")
 
80
        self.setupJsonServer("PAY_BASE_URL",
 
81
                "fake_responses/software-center-agent/")
 
82
 
 
83
 
 
84
class StoreTest(StoreTestBase):
 
85
    def setUp(self):
 
86
        super().setUp()
 
87
        self.harness = ScopeHarness.new_from_scope_list(Parameters([
 
88
            "/usr/lib/arm-linux-gnueabihf/unity-scopes/clickstore/com.canonical.scopes.clickstore.ini"
 
89
            ]))
 
90
        self.view = self.harness.results_view
 
91
        self.view.active_scope = 'com.canonical.scopes.clickstore'
 
92
 
 
93
    def test_surfacing_results(self):
 
94
        self.view.browse_department('')
 
95
        self.view.search_query = ''
 
96
        cpi_base = os.environ["U1_SEARCH_BASE_URL"]
 
97
 
 
98
        # Check first apps of every category
 
99
        match = CategoryListMatcher() \
 
100
            .has_exactly(4) \
 
101
            .mode(CategoryListMatcherMode.BY_ID) \
 
102
            .category(CategoryMatcher("app-of-the-week") \
 
103
                    .has_at_least(1) \
 
104
                    ) \
 
105
            .category(CategoryMatcher("top-apps") \
 
106
                      .has_at_least(1) \
 
107
                      .mode(CategoryMatcherMode.STARTS_WITH) \
 
108
                      .result(ResultMatcher(cpi_base + "/api/v1/package/com.ubuntu.developer.bobo1993324.udropcabin") \
 
109
                      .properties({'installed': False, 'version': '0.2.1', 'price': 0.0, 'price_area':'FREE', 'rating':'☆ 4.2'}) \
 
110
                      .title('uDropCabin') \
 
111
                      .subtitle('Zhang Boren') \
 
112
            )) \
 
113
            .category(CategoryMatcher("our-favorite-games") \
 
114
                      .has_at_least(1) \
 
115
                      .mode(CategoryMatcherMode.BY_URI) \
 
116
                      .result(ResultMatcher(cpi_base + "/api/v1/package/com.ubuntu.developer.andrew-hayzen.volleyball2d") \
 
117
            )) \
 
118
            .category(CategoryMatcher("travel-apps") \
 
119
                      .has_at_least(1)) \
 
120
            .match(self.view.categories)
 
121
        self.assertMatchResult(match)
 
122
 
 
123
    def test_surfacing_departments(self):
 
124
        self.view.search_query = ''
 
125
 
 
126
        departments = self.view.browse_department('')
 
127
 
 
128
        self.assertTrue(self.view.has_departments)
 
129
        self.assertFalse(self.view.has_alt_departments)
 
130
 
 
131
        match = DepartmentMatcher() \
 
132
            .mode(DepartmentMatcherMode.STARTS_WITH) \
 
133
            .id('') \
 
134
            .label('All') \
 
135
            .all_label('') \
 
136
            .parent_id('') \
 
137
            .parent_label('') \
 
138
            .is_root(True) \
 
139
            .is_hidden(False) \
 
140
            .child(ChildDepartmentMatcher('books-comics')) \
 
141
            .child(ChildDepartmentMatcher('business')) \
 
142
            .child(ChildDepartmentMatcher('communication')) \
 
143
            .child(ChildDepartmentMatcher('developer-tools')) \
 
144
            .child(ChildDepartmentMatcher('education')) \
 
145
            .child(ChildDepartmentMatcher('entertainment')) \
 
146
            .child(ChildDepartmentMatcher('finance')) \
 
147
            .child(ChildDepartmentMatcher('food-drink')) \
 
148
            .child(ChildDepartmentMatcher('games')) \
 
149
            .child(ChildDepartmentMatcher('graphics')) \
 
150
            .child(ChildDepartmentMatcher('health-fitness')) \
 
151
            .child(ChildDepartmentMatcher('lifestyle')) \
 
152
            .child(ChildDepartmentMatcher('media-video')) \
 
153
            .child(ChildDepartmentMatcher('medical')) \
 
154
            .child(ChildDepartmentMatcher('music-audio')) \
 
155
            .child(ChildDepartmentMatcher('news-magazines')) \
 
156
            .child(ChildDepartmentMatcher('personalisation')) \
 
157
            .child(ChildDepartmentMatcher('productivity')) \
 
158
            .child(ChildDepartmentMatcher('reference')) \
 
159
            .child(ChildDepartmentMatcher('science-engineering')) \
 
160
            .child(ChildDepartmentMatcher('shopping')) \
 
161
            .child(ChildDepartmentMatcher('social-networking')) \
 
162
            .child(ChildDepartmentMatcher('sports')) \
 
163
            .child(ChildDepartmentMatcher('travel-local')) \
 
164
            .child(ChildDepartmentMatcher('universal-accessaccessibility')) \
 
165
            .child(ChildDepartmentMatcher('accessories')) \
 
166
            .child(ChildDepartmentMatcher('weather')) \
 
167
            .match(departments)
 
168
        self.assertMatchResult(match)
 
169
 
 
170
    def test_department_browsing(self):
 
171
        self.view.search_query = ''
 
172
 
 
173
        departments = self.view.browse_department('games')
 
174
 
 
175
        match = DepartmentMatcher() \
 
176
            .has_exactly(0) \
 
177
            .mode(DepartmentMatcherMode.STARTS_WITH) \
 
178
            .label('Games') \
 
179
            .all_label('') \
 
180
            .parent_id('') \
 
181
            .parent_label('All') \
 
182
            .is_root(False) \
 
183
            .is_hidden(False) \
 
184
            .match(departments)
 
185
        self.assertMatchResult(match)
 
186
 
 
187
        res_match = CategoryListMatcher() \
 
188
            .has_exactly(3) \
 
189
            .mode(CategoryListMatcherMode.BY_ID) \
 
190
            .category(CategoryMatcher("top-games") \
 
191
                      .has_at_least(1) \
 
192
            ) \
 
193
            .category(CategoryMatcher("__all-scopes__") \
 
194
                      .has_at_least(1) \
 
195
            ) \
 
196
            .category(CategoryMatcher("__all-apps__") \
 
197
                      .has_at_least(1) \
 
198
            ) \
 
199
            .match(self.view.categories)
 
200
        self.assertMatchResult(res_match)
 
201
 
 
202
    def test_uninstalled_app_preview(self):
 
203
        self.view.browse_department('')
 
204
        self.view.search_query = 'Calendar'
 
205
 
 
206
        pview = self.view.categories[0].results[0].tap()
 
207
        self.assertIsInstance(pview, PreviewView)
 
208
 
 
209
        match = PreviewColumnMatcher().column(\
 
210
                PreviewMatcher() \
 
211
                .widget(PreviewWidgetMatcher("hdr")) \
 
212
                .widget(PreviewWidgetMatcher("buttons") \
 
213
                    .type("actions") \
 
214
                    .data(
 
215
                        {"actions":[
 
216
                            {"download_sha512":"2fa658804e63da1869037cd9bc74b792875404f03b6c6449271ae5244688ff42a4524712ccb748ab9004344cccddd59063f3d3a4af899a3cc6f64ddc1a27072b",
 
217
                             "download_url":"https://public.apps.ubuntu.com/download/com.ubuntu/calendar/com.ubuntu.calendar_0.4.572_all.click","id":"install_click","label":"Install"}]
 
218
                             ,"online_account_details": {
 
219
                                "login_failed_action":1,
 
220
                                "login_passed_action":3,
 
221
                                "provider_name":"ubuntuone",
 
222
                                "scope_id":"",
 
223
                                "service_name": "ubuntuone",
 
224
                                "service_type":"ubuntuone"}}
 
225
                        ) \
 
226
                    ) \
 
227
                .widget(PreviewWidgetMatcher("screenshots") \
 
228
                    .type('gallery')) \
 
229
                .widget(PreviewWidgetMatcher("summary") \
 
230
                    .type('text')) \
 
231
                .widget(PreviewWidgetMatcher("other_metadata") \
 
232
                        .type('text')) \
 
233
                .widget(PreviewWidgetMatcher("updates") \
 
234
                        .type('text')) \
 
235
                .widget(PreviewWidgetMatcher("whats_new") \
 
236
                        .type('text')) \
 
237
                .widget(PreviewWidgetMatcher("reviews_title") \
 
238
                        .type('text')) \
 
239
                .widget(PreviewWidgetMatcher("summary") \
 
240
                        .type('reviews')) \
 
241
        ).match(pview.widgets)
 
242
        self.assertMatchResult(match)
 
243
 
 
244
if __name__ == '__main__':
 
245
    unittest.main(argv = sys.argv[:1])