2019-02-11 16:18:15 +00:00
|
|
|
import os
|
2018-02-06 15:51:43 -05:00
|
|
|
from locust import HttpLocust, TaskSet, task
|
2018-02-13 12:35:08 +00:00
|
|
|
from random import choice
|
2018-08-21 14:47:19 +01:00
|
|
|
from random import randint
|
2018-02-06 15:51:43 -05:00
|
|
|
|
|
|
|
class UserBehavior(TaskSet):
|
|
|
|
def on_start(self):
|
|
|
|
""" on_start is called when a Locust start before any task is scheduled """
|
|
|
|
print('Starting')
|
|
|
|
|
|
|
|
@task
|
2018-02-13 12:35:08 +00:00
|
|
|
def login(self):
|
|
|
|
credentials = {
|
|
|
|
'name': 'user',
|
|
|
|
'password': 'password'
|
|
|
|
}
|
|
|
|
res = self.client.post('/api/user/login', json=credentials)
|
|
|
|
print('login {}'.format(res.status_code))
|
|
|
|
|
2018-02-06 15:51:43 -05:00
|
|
|
|
|
|
|
@task
|
2018-02-13 12:35:08 +00:00
|
|
|
def load(self):
|
|
|
|
self.client.get('/')
|
|
|
|
user = self.client.get('/api/user/uniqueid').json()
|
|
|
|
uniqueid = user['uuid']
|
|
|
|
print('User {}'.format(uniqueid))
|
|
|
|
|
|
|
|
self.client.get('/api/catalogue/categories')
|
|
|
|
# all products in catalogue
|
|
|
|
products = self.client.get('/api/catalogue/products').json()
|
|
|
|
for i in range(2):
|
|
|
|
item = None
|
|
|
|
while True:
|
|
|
|
item = choice(products)
|
|
|
|
if item['instock'] != 0:
|
|
|
|
break
|
|
|
|
|
2018-08-21 14:47:19 +01:00
|
|
|
# vote for item
|
|
|
|
if randint(1, 10) <= 3:
|
|
|
|
self.client.put('/api/ratings/api/rate/{}/{}'.format(item['sku'], randint(1, 5)))
|
|
|
|
|
2018-02-13 12:35:08 +00:00
|
|
|
self.client.get('/api/catalogue/product/{}'.format(item['sku']))
|
2018-08-21 14:47:19 +01:00
|
|
|
self.client.get('/api/ratings/api/fetch/{}'.format(item['sku']))
|
2018-02-13 12:35:08 +00:00
|
|
|
self.client.get('/api/cart/add/{}/{}/1'.format(uniqueid, item['sku']))
|
|
|
|
|
|
|
|
cart = self.client.get('/api/cart/cart/{}'.format(uniqueid)).json()
|
|
|
|
item = choice(cart['items'])
|
|
|
|
self.client.get('/api/cart/update/{}/{}/2'.format(uniqueid, item['sku']))
|
|
|
|
|
|
|
|
# country codes
|
|
|
|
code = choice(self.client.get('/api/shipping/codes').json())
|
|
|
|
city = choice(self.client.get('/api/shipping/cities/{}'.format(code['code'])).json())
|
|
|
|
print('code {} city {}'.format(code, city))
|
|
|
|
shipping = self.client.get('/api/shipping/calc/{}'.format(city['uuid'])).json()
|
|
|
|
shipping['location'] = '{} {}'.format(code['name'], city['name'])
|
|
|
|
print('Shipping {}'.format(shipping))
|
|
|
|
# POST
|
|
|
|
cart = self.client.post('/api/shipping/confirm/{}'.format(uniqueid), json=shipping).json()
|
|
|
|
print('Final cart {}'.format(cart))
|
|
|
|
|
|
|
|
order = self.client.post('/api/payment/pay/{}'.format(uniqueid), json=cart).json()
|
|
|
|
print('Order {}'.format(order))
|
|
|
|
|
2019-02-11 16:18:15 +00:00
|
|
|
@task
|
|
|
|
def error(self):
|
|
|
|
if os.environ['ERROR'] == '1':
|
|
|
|
print('Error request')
|
|
|
|
cart = {'total': 0, 'tax': 0}
|
|
|
|
self.client.post('/api/payment/pay/partner-57', json=cart)
|
|
|
|
|
2018-02-06 15:51:43 -05:00
|
|
|
|
|
|
|
class WebsiteUser(HttpLocust):
|
|
|
|
task_set = UserBehavior
|
|
|
|
min_wait = 1000
|
|
|
|
max_wait = 5000
|