Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Tarek Ziade December 19, 2012
file 364 lines (304 sloc) 12.719 kb
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
import base64
from Cookie import Morsel
import json
import os
import random
import re
import unicodedata
import uuid
import urllib2

from funkload.FunkLoadTestCase import FunkLoadTestCase
from webunit.utility import Upload

from util import read_password

USER_AGENT = 'Mozilla/5.0 (Android; Mobile; rv:18.0) Gecko/18.0 Firefox/18.0'
CSRF_REGEX = re.compile(r'.*csrfmiddlewaretoken\' value=\'(.*)\'')
WEBAPP = 'http://%s.webapp.lolnet.org/manifest.webapp'
RE_NAME = '<input name="display_name".*?value="(.*?)" '
RE_NAME = re.compile(RE_NAME, re.M | re.I)

HERE = os.path.abspath(os.path.dirname(__name__))
GITHUB = 'https://raw.github.com/mozilla/marketplace-loadtest/master/'

ICON = os.path.join(HERE, 'icon.png')
if not os.path.exists(ICON):
    with open(ICON, 'wb') as f:
       f.write(urllib2.urlopen(GITHUB + 'icon.png').read())


SCREENSHOT = os.path.join(HERE, 'screenshot.png')
if not os.path.exists(SCREENSHOT):
    with open(SCREENSHOT, 'wb') as f:
       f.write(urllib2.urlopen(GITHUB + 'screenshot.png').read())


class MarketplaceTest(FunkLoadTestCase):

    def __init__(self, *args, **kwargs):
        super(MarketplaceTest, self).__init__(*args, **kwargs)

        self.root = self.conf_get('main', 'url')
        self.lang = 'en-US'
        self._apps = None
        self._categories = None
        self.choices = ([self.test_editor] * 2 + [self.test_developer] * 8 +
                        [self.test_end_user] * 10 + [self.test_anonymous] * 80)

    def setBasicAuth(self, username, password):
        '''Set the Basic authentication information to the given username
and password.
'''
        self._browser.authinfo = base64.b64encode('%s:%s' % (username,
            password)).strip()
        self._authinfo = '%s:%s@' % (username, password)

    def get(self, url, *args, **kwargs):
        """Do a GET request with the given URL.

This call sets the Accept-Languages header and ask funkload to not
follow img/css/js links.
"""
        # when GETing an URL, we don't want to follow the links (img, css etc)
        # as they could be done on the fly by javascript in an obstrusive way.
        # we also prepend the domain (self.root) to the get calls in this
        # method.
        self.setHeader('Accept-Languages', self.lang)
        self.setHeader('User-Agent', USER_AGENT)
        return super(MarketplaceTest, self).get(self.root + url,
                                                load_auto_links=False,
                                                *args, **kwargs)

    def post(self, url, *args, **kwargs):
        return super(MarketplaceTest, self).post(self.root + url,
            load_auto_links=False, *args, **kwargs)

    @property
    def apps(self):
        if self._apps is None:
            self._apps = self.get_apps()
            if len(self._apps) > 4:
                self._apps = random.sample(self._apps, 4)
        return self._apps

    def get_apps(self):
        """Get the list of apps from the marketplace API"""
        resp = self.get('/api/apps/search/')
        content = json.loads(resp.body)
        return [p['slug'] for p in content['objects']]

    @property
    def categories(self):
        if self._categories is None:
            self._categories = self.get_categories()
            if len(self._categories) > 4:
                self._categories = random.sample(self._categories)
        return self._categories

    def get_categories(self):
        """Get all the categories from the marketplace API"""
        resp = self.get('/en-US/api/apps/category/')
        cats = json.loads(resp.body)['objects']
        return [slugify(c['name']) for c in cats]

    def query_search(self):
        # do a search with the name of the selected apps
        for app in self.apps:
            self.search_app(query=app)

    def query_apps_detail(self):
        for app in self.apps:
            self.get('/app/{app}/'.format(app=app))

    def query_categories(self):
        for category in self.categories:
            self.get('/apps/{category}'.format(category=category))

    def view_homepage(self):
        ret = self.get('/')
        self.assertTrue('Categories' in ret.body)

    def search_app(self, query='twi'):
        # search for some non-empty string, to make a realistic and not
        # too expensive query
        ret = self.get('/search/?q=%s' % query)
        self.assertTrue('Search Results' in ret.body)

    def install_free_app(self):
        # all the logic for free apps is client side - as long as the
        # manifest url is in the page, the process should succeed
        if not self.apps:
            return
        ret = self.get('/app/%s' % random.choice(self.apps))
        self.assertTrue('data-manifest_url="' in ret.body)

    def rate_app(self, rating=3, comment=None):
        if not self.apps:
            return
        appname = random.choice(self.apps)
        url = '/app/%s/reviews/add' % appname
        ret = self.get(url)

        if comment is None:
            body = 'This app is the cool thing'
        else:
            body = comment

        # adding a unique id
        body += ' ' + uuid.uuid1().hex
        rating = str(rating)
        params = [['body', body],
                  ['rating', rating]]

        add_csrf_token(ret, params)
        self.post(url, params=params, ok_codes=[200, 302])

        # XXX this is done async so we can't check now
        # I am not sure what's the best thing to do
        #
        # let's see if our review made it
        #ret = self.get('/app/%s/reviews' % appname)
        #self.assert_(body in ret.body)

    def edit_details(self):
        # since we can have other tests doing this in
        # parallel, we'll just check that it was changed
        ret = self.get('/settings')
        original = RE_NAME.findall(ret.body)
        if len(original) == 0:
            original = 'UNKNOWN'
        else:
            original = original[0]
        display = uuid.uuid1().hex
        params = [['display_name', display]]
        add_csrf_token(ret, params)
        self.post('/settings', params=params)

        # checking the result
        ret = self.get('/settings')
        self.assert_(original not in ret.body,
                     'Found %r for the display' % original)

    def submit_app(self):
        # try to submit an app
        ret = self.get('/developers/submit/app', ok_codes=[200, 302])

        # generate a random web-app manifest
        random_id = uuid.uuid1().hex
        manifest_url = WEBAPP % random_id

        # we need to accept the TOS once per user
        if 'read_dev_agreement' in ret.body:
            params = [['read_dev_agreement', 'True']]
            add_csrf_token(ret, params)
            ret = self.post(ret.url, params=params)

        # submit the manifest
        params = [['manifest', manifest_url]]
        add_csrf_token(ret, params)
        ret = self.post('/developers/upload-manifest', params=params)
        data = json.loads(ret.body)
        validation = data['validation']
        app_exists = False

        if isinstance(validation, dict) and 'messages' in validation:
            messages = [m['message'] for m in data['validation']['messages']]
            app_exists = 'already' in ' '.join(messages)

        # we should be able to test editing the app
        if app_exists:
            return

        if isinstance(validation, dict) and 'errors' in validation:
            self.assertEqual(data['validation']['errors'], 0, data)

        # now we can submit the app basics, first load the form again
        ret = self.get('/developers/submit/app/manifest')
        params = [['upload', data['upload']],
                  ['free', 'free-os'],
                  ['free', 'free-desktop'],
                  ['free', 'free-phone'],
                  ['free', 'free-tablet']]
        add_csrf_token(ret, params)
        ret = self.post('/developers/submit/app/manifest', params=params)
        self.assertTrue('/submit/app/details/' in ret.url, ret.url)
        app_slug = ret.url.split('/')[-1]

        # upload icon
        params = [['upload_image', Upload(ICON)]]
        add_csrf_token(ret, params)
        ret = self.post('/developers/app/%s/upload_icon' % app_slug,
            params=params)
        data = json.loads(ret.body)
        self.assertEqual(len(data['errors']), 0, data)
        icon_hash = data['upload_hash']

        # upload screenshot
        ret = self.get('/developers/submit/app/details/%s' % app_slug)
        params = [['upload_image', Upload(SCREENSHOT)]]
        add_csrf_token(ret, params)
        ret = self.post('/developers/app/%s/upload_image' % app_slug,
            params=params)
        data = json.loads(ret.body)
        self.assertEqual(len(data['errors']), 0, data)
        screenshot_hash = data['upload_hash']

        # fill in some more app details
        ret = self.get('/developers/submit/app/details/%s' % app_slug)
        params = [
            ['slug', app_slug],
            ['slug_en-us', app_slug],
            ['name_en-us', app_slug],
            ['summary_en-us', 'HA Test web app'],
            ['privacy_policy_en-us', 'We sell all your data!'],
            ['support_email_en-us', 'marketplace-devs@mozilla.com'],
            ['icon_upload_hash', icon_hash],
            ['icon_upload_hash_en-us', icon_hash],
            ['icon_type', 'image/png'],
            ['icon_type_en-us', 'image/png'],
            ['categories', '167'],
            ['categories_en-us', '167'],
            ['files-0-position', '0'],
            ['files-0-position_en-us', '0'],
            ['files-0-upload_hash', screenshot_hash],
            ['files-0-upload_hash_en-us', screenshot_hash],
            ['files-0-unsaved_image_type', 'image/png'],
            ['files-0-unsaved_image_type_en-us', 'image/png'],
            ['files-TOTAL_FORMS', '1'],
            ['files-TOTAL_FORMS_en-us', '1'],
            ['files-INITIAL_FORMS', '0'],
            ['files-INITIAL_FORMS_en-us', '0'],
            ['files-MAX_NUM_FORMS', '1'],
            ['files-MAX_NUM_FORMS_en-us', '1'],
        ]
        token = add_csrf_token(ret, params)
        if token:
            # work around creative code in app edit form
            params.append(['csrfmiddlewaretoken_en-us', token])

        # hack in the current_locale cookie
        cookies = self._browser.cookies
        morsel = Morsel()
        morsel.set('current_locale', 'en-us', 'en-us')
        for domain, value in cookies.items():
            value['/'].setdefault('current_locale', morsel)

        # fill details and submit
        ret = self.post(ret.url, params=params)

        # finally delete the app
        ret = self.get('/developers/app/%s/status' % app_slug)
        params = []
        add_csrf_token(ret, params)
        self.post('/developers/app/%s/delete' % app_slug, params=params)

    def test_anonymous(self):
        self.clearBasicAuth()
        self.view_homepage()
        self.search_app()
        self.query_search()
        self.query_categories()
        self.query_apps_detail()

    def test_end_user(self):
        self.setBasicAuth('enduser@mozilla.com', read_password())
        try:
            self.view_homepage()
            self.search_app()
            self.install_free_app()
            self.edit_details()
            self.rate_app()
        finally:
            self.clearBasicAuth()

    def test_developer(self):
        self.setBasicAuth('developer@mozilla.com', read_password())
        try:
            self.view_homepage()
            self.search_app()
            self.submit_app()
        finally:
            self.clearBasicAuth()

    def test_editor(self):
        # XXX not done yet
        # XXX we should delete some apps here too
        # so we don't grow the DB
        pass

    def test_marketplace(self):
        """ Current rates:

- test_anonymous: 80%
- test_end_user: 10%
- test_developer: 8%
- test_editor: 2%
"""
        random.choice(self.choices)()


def add_csrf_token(response, params):
    token = CSRF_REGEX.findall(response.body)
    if token:
        params.append(['csrfmiddlewaretoken', token[0]])
        return token[0]
    return None


def slugify(value):
    """This is the slugify from django, minus an hardcoded workaround"""
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
    value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
    slugified = re.sub('[-\s]+', '-', value)

    # workaround for now
    if slugified == 'social-communications':
        return 'social'
    return slugified


if __name__ == '__main__':
    import unittest
    unittest.main()
Something went wrong with that request. Please try again.