def test_create_user(client): payload = { "111": { "create_user": { "email": "*****@*****.**", "phone": "987654321", "level": "admin", "country": ["Ghana", "Ashanti"], "city": "Kumasi", "code": "+233", "phone_code": "+233 987654321", "mode": "create", "id": 12 }, "000": ["create_user"] }, "000": ["111"] } login = Logins.select().join(Users).order_by(Logins.id.desc()).get() headers = { 'Authorization': login.token, 'Account-ID': login.user.email, 'Device-ID': login.device_hash } response = client.simulate_post('/action', body=json.dumps(payload), headers=headers) assert response.json["111"]["create_user"]["status"]
def auth_headers(): login = Logins.select().order_by(Logins.id.desc()).get() headers = { 'Authorization': login.token, 'Account-ID': login.user.email, 'Device-ID': login.device_hash } return headers
def test_update_item(client): # test update imgs1 = [DataURI.from_file(f'tests/images/{i}.jpg') for i in range(3, 5)] imgs2 = [DataURI.from_file(f'tests/images/{i}.jpg') for i in range(9, 11)] imgs3 = [DataURI.from_file(f'tests/images/{i}.jpg') for i in range(15, 16)] item = { "category": 2, "currency": "GHS", "images": imgs1, "tags": ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], "options": [{ "name": "opt1", "required": True, "values": [{ "images": imgs2, }, { "images": imgs3, }] }] } item_new = Items.select(Items.id).order_by(Items.id.desc()).get() payload = { "111": { "upload_item": { "item_details": item, "seller_id": 1, "update": True, "id": item_new.id }, "000": ["upload_item"] }, "000": ["111"] } login = Logins.select().order_by(Logins.id.desc()).get() headers = { 'Authorization': login.token, 'Account-ID': login.user.email, 'Device-ID': login.device_hash } response = client.simulate_post('/action', body=json.dumps(payload), headers=headers) assert response.json["111"]["upload_item"]["status"]
def test_get_users(client): payload = {"111": {"get_users": {}, "000": ["get_users"]}, "000": ["111"]} login = Logins.select().order_by(Logins.id.desc()).get() headers = { 'Authorization': login.token, 'Account-ID': login.user.email, 'Device-ID': login.device_hash } response = client.simulate_post('/action', body=json.dumps(payload), headers=headers) assert response.json["111"]["get_users"]["status"]
def test_activate_account(client): medium = "email" payload = { "111": { "activate_account": { "phase": "generate", "medium": medium }, "000": ["activate_account"] }, "000": ["111"] } login = Logins.select(Logins.token).order_by(Logins.id.desc()).get() headers = { 'Authorization': login.token, 'Account-ID': login.user.email, 'Device-ID': login.device_hash } response = client.simulate_post('/action', body=json.dumps(payload), headers=headers) r1 = response.json["111"]["activate_account"]["status"] r2 = False if r1: code = Activations.select().order_by(Activations.id.desc()).get().code payload = { "111": { "activate_account": { "phase": "activate", "medium": medium, "code": code }, "000": ["activate_account"] }, "000": ["111"] } response = client.simulate_post('/action', body=json.dumps(payload), headers=headers) r2 = response.json["111"]["activate_account"]["status"] assert r1 and r2
def test_get_items(client): info = { "name": "sbk", "address": "sbk", "phone": "553339728", "birth": "1995-03-20", "country": ["Ghana", "Ashanti"], "city": "Kumasi", "code": "+233", } payload = { "111": { "set_info": { "info": "personal", "data": info }, "000": ["set_info"] }, "000": ["111"] } login = Logins.select().order_by(Logins.id.desc()).get() headers = { 'Authorization': login.token, 'Account-ID': login.user.email, 'Device-ID': login.device_hash } response = client.simulate_post('/action', body=json.dumps(payload), headers=headers) assert response.json["111"]["set_info"]["status"]
def login(req, **kwargs): """ :kwargs: email, password, device_hash, device_data """ try: user = Users.get_or_none(email=kwargs["email"]) if user: if user.login_tries + 1 > 10: return { "status": False, "msg": "Your account has been blocked, you need to reset your password." } Users.update(login_tries=Users.login_tries + 1).where(Users.id == user.id).execute() if bcrypt.checkpw(kwargs["password"].encode(), user.password.encode()): token = gen_token(user.email, fresh_pin()) Logins.create( user=user, device_hash=kwargs["device_hash"], device_data=kwargs["device_data"], token=token, ) user_data = { 'id': user.id, 'email': user.email, 'phone': user.phone, 'name': user.name, 'level': user.level, 'email_verified': user.email_verified, 'phone_verified': user.phone_verified, 'info': user.info, 'is_active': user.is_active, } Users.update( login_count=Users.login_count + 1, last_login=datetime.datetime.now(), login_tries=0).where(Users.id == user.id).execute() return { "status": True, "data": { "token": token, "user": user_data }, "msg": "Login Successfull." } else: return {"status": False, "msg": "Wrong Password."} else: return {"status": False, "msg": "Missing User."} except Exception as e: return {"status": False, "msg": "Login Failed."}