def initial_data(self): self.team_owner = User.create(name='test_activity') self.team = Team.create(name='club_test_activity', owner_id=self.team_owner.id) self.user = self.creator = User.create(name='activity_creator') self.activity = Activity.create(team=self.team, creator=self.creator, price='10', vip_price='8', leader=self.creator, title='just a test', description='description', start_time='3000-01-01 00:00:01', end_time='3000-12-31 23:59:59') self.order = OrderService.new_order(10, self.team, self.user, TeamOrder.OrderType.ACTIVITY, TeamOrder.OrderPaymentMethod.WXPAY, self.activity.id, title="UserOrderTest" ) self.activity.add_member(self.user.id, users_count=1, price=10, free_times=0, total_fee=10, order_id=self.order.id, order_no=self.order.order_no, payment_method=TeamOrder.OrderPaymentMethod.WXPAY, payment_state=TeamOrder.OrderState.TRADE_BUYER_PAID, state=TeamMember.TeamMemberState.normal)
def initial_data(self): self.team_owner = User.create(name='test_activity') self.team = Team.create(name='club_test_activity', owner_id=self.team_owner.id) self.user = self.creator = User.create(name='activity_creator') self.activity = Activity.create(team=self.team, creator=self.creator, price='10', vip_price='8', leader=self.creator, title='just a test', description='description')
def initial_data(self): self.team_owner = User.create(name='test_activity') self.team = Team.create(name='club_test_activity', owner_id=self.team_owner.id) self.user = self.creator = User.create(name='activity_creator') self.activity = Activity.create(team=self.team, creator=self.creator, price='10', vip_price='8', leader=self.creator, title='just a test', description='description', need_nickname=True, join_end="3000-12-30 22:59:59", max_members=10, start_time='3000-01-01 00:00:01', end_time='3000-12-31 23:59:59') self.activity_2 = Activity.create(team=self.team, creator=self.creator, price='10', vip_price='8', leader=self.creator, title='just a test', description='description', need_nickname=True, join_end="3000-12-30 22:59:59", max_members=10, start_time='3000-01-01 00:00:01', end_time='3000-12-31 23:59:59') self.activity_member = ActivityMember.create( activity=self.activity, user=self.user, total_fee='0.01', nickname="leave activities", payment_method=TeamOrder.OrderPaymentMethod.WXPAY.value, payment_state=TeamOrder.OrderState.TRADE_BUYER_PAID, ) TeamMemberService.new_order(team=self.team, activity_id=self.activity.id, user=self.user, order_type=TeamOrder.OrderType.ACTIVITY, payment_method='wxpay', total_fee=self.activity.price, payment_fee=self.activity.price, title='TestLeave') TeamOrder.update(state=TeamOrder.OrderState.TRADE_BUYER_PAID)\ .where(TeamOrder.activity_id == self.activity.id, TeamOrder.user == self.user)\ .execute()
def initial_data(self): self.team_owner = User.create(name='test_activity') self.team = Team.create(name='club_test_activity', owner_id=self.team_owner.id) self.user = self.creator = User.create(name='activity_creator') self.activity = Activity.create(team=self.team, creator=self.creator, price='10', vip_price='8', leader=self.creator, title='just a test', description='description', start_time='3000-01-01 00:00:01', end_time='3000-12-31 23:59:59', max_members=10)
def test_online_pay_activity(self): url = self.JOIN_ACTIVITY.format(activity_id=self.activity.id) body = {"payment": "wxpay", "nickname": "Nick name"} self.auth_user = User.create(name='join activity') response = self.fetch(url, method='POST', body=json.dumps(body), params={'team_id': self.team.id}) self.assertEqual(200, response.code, response.body) order = TeamOrder.get_or_none(user=self.auth_user, activity_id=self.activity.id) self.assertIsNotNone(order, "加入活动时订单 `TeamOrder` 未创建") result = json.loads(response.body.decode()) expect = { "status": "ok", "state": ActivityMember.ActivityMemberState.wait_confirm.value, "payment_state": TeamOrder.OrderState.WAIT_BUYER_PAY.value, "order_no": order.order_no } self.assertDictEqual(expect, result, result) member = ActivityMember.get_or_none(activity=self.activity, user=self.auth_user) self.assertIsNotNone(member, "加入活动后 `ActivityMember` 未添加记录")
def test_stoped_activity(self): """活动已报名截止""" payment = Activity.PaymentType.ONLINE_PAY.value activity = Activity.create( team=self.team, creator=self.creator, leader=self.creator, price='10', vip_price='10', start_time='2000-01-01 00:00:01', end_time='3000-12-31 23:59:59', join_end="3000-12-30 22:59:59", payment_type=payment, title='no online pay', description='on need online pay', state=Activity.ActivityState.opening.value, ) body = {"payment": "wxpay", "nickname": "Nick name"} url = self.JOIN_ACTIVITY.format(activity_id=activity.id) self.auth_user = User.create(name='closed activity') response = self.fetch(url, method="POST", body=json.dumps(body), params={'team_id': self.team.id}) self.assertEqual(400, response.code, response.body) result = json.loads(response.body.decode()) error = result["error"] self.assertEqual("活动已报名截止", error, error)
def post(self): mobile = self.validated_arguments['mobile'] verify_code = self.validated_arguments['verify_code'] if not self.verify_mobile(mobile, verify_code): raise ApiException(400, "验证码错误,请重新输入") user = User.get_or_none(mobile=mobile) if not user: with self.db.transaction() as txn: user = User.create( mobile=mobile, mobile_verifyed=True, reg_device_id=self.device_id, last_device_id=self.device_id, last_login=datetime.now(), ) else: update = {"last_login": datetime.now()} if self.device_id > 0: update["last_device_id"] = self.device_id User.update(**update).where(User.id == user.id).execute() if user and self.device_id: User.update_device(user.id, self.device_id) self.write(self.create_session(user))
def test_create_team(self): body = { "name": "test create team", "description": "description", "notice": "club notice", "province": "四川", "city": "成都", "address": "天府三街", "contact_person": "contact person", "contact_phone": "contact phone", "lat": 123.0000001, "lng": 123.0000002, "open_type": 2, "state": 1, } self.auth_user = User.create(name="create team") response = self.fetch(self.MODEL_PATH, method="POST", body=json.dumps(body)) self.assertEqual(201, response.code, response.body.decode()) result = json.loads(response.body.decode()) self.assertEqual(0, result['state']) self.assertEqual(self.auth_user.id, result['owner_id'], "创建俱乐部时 owner 错误") body.pop('state') for k, v in body.items(): self.assertEqual(v, result[k], (k, v))
def test_over_left_count_activity(self): payment = Activity.PaymentType.ONLINE_PAY.value activity = Activity.create(team=self.team, creator=self.creator, leader=self.creator, price='10', vip_price='10', payment_type=payment, title='no online pay', description='on need online pay', state=Activity.ActivityState.opening.value, join_end="3000-12-30 22:59:59", allow_agents=10, start_time='3000-01-01 00:00:01', end_time='3000-12-31 23:59:59', members_count=2, max_members=10) body = { "payment": "wxpay", "nickname": "Nick name", "users_count": 11, } url = self.JOIN_ACTIVITY.format(activity_id=activity.id) self.auth_user = User.create(name='closed activity') response = self.fetch(url, method="POST", body=json.dumps(body), params={'team_id': self.team.id}, headers={"Content-Type": "application/json"}) self.assertEqual(400, response.code, response.body) result = json.loads(response.body.decode()) error = result["error"] self.assertEqual("该活动只有 8 名额了", error, error)
def test_patch_team_permission_deny(self): url = self.OBJECT_PATH.format(team_id=self.team.id) body = {"name": "new name"} self.auth_user = User.create(name="permission deny for patch team") response = self.fetch(url, method="PATCH", body=json.dumps(body)) self.assertEqual(403, response.code, response.body.decode) updated = Team.get(id=self.team.id) self.assertNotEqual("new name", updated.name, "权限错误, 但是跟新成功了")
def test_delete_group_with_members(self): user = User.create(name="add to group members") self.team.add_member(user.id) member = self.team.get_member(team_id=self.team.id, user_id=user.id) self.default_group.members.add(member) url = self.OBJECT_PATH.format(team_id=self.team.id, group_id=self.default_group.id) self.auth_user = self.team_owner response = self.fetch(url, method="DELETE") self.assertEqual(409, response.code, response.body.decode())
def test_login_verify_code(self): user = User.create(name='test', mobile="13838003801") url = "api/2/auth/login_verify_code" body = {"mobile": "13838003801", "verify_code": "8888"} response = self.fetch(url, method="POST", body=json.dumps(body)) self.assertEqual(200, response.code, response.body.decode()) result = json.loads(response.body.decode()) self.assertIn("session", result, result) self.assertIn("current_user", result, result)
def test_activity_member_detail(self): member = ActivityMember.get(activity=self.activity, user=self.user) url = self.MEMBER_DETAIL.format(activity_id=self.activity.id, member_id=member.id) self.auth_user = self.creator response = self.fetch(url) self.assertEqual(200, response.code, response.body.decode()) self.auth_user = User.create(name="permission deny") response = self.fetch(url) self.assertEqual(403, response.code, response.body.decode())
def test_refresh_token(self): self.auth_user = User.create(name='test3', mobile="13838003803", password=User.create_password("123456")) url = "api/2/auth/refresh_token" response = self.fetch(url) self.assertEqual(200, response.code, response.body.decode()) result = json.loads(response.body.decode()) self.assertIn("session", result, result) self.assertIn("current_user", result, result)
def test_get_owner_user(self): insecure_fields = ("password", "avatar_key", "pay_openid", "reg_device_id", "reg_device_type") user = User.create(name='test') url = self.OBJECT_PATH.format(user_id=user.id) response = self.fetch(url) self.assertEqual(200, response.code, response.body.decode()) result = json.loads(response.body.decode()) for field in insecure_fields: self.assertNotIn(field, result, field)
def test_create(self): """ 测试创建用户 实际并不迫切需要此测试 """ user = User.create( name='test_user', mobile=13800138000 ) _user = User.get(User.mobile == 13800138000) self.assertEqual(user.id, _user.id)
def initial_data(self): faker = Faker() sport = Sport.create(name=faker.pystr(), description="测试") self.team_owner = User.create(name='test_match') self.team = Team.create(name='club_test_match', owner_id=self.team_owner.id, sport=sport) self.user = self.creator = User.create(name='match_creator') self.match = Match.create( team_id=self.team.id, user_id=self.user.id, sport_id=sport.id, price=10, title='just a test', description='description', start_time='3000-01-01 00:00:01', end_time='3000-12-31 23:59:59', join_start="2016-01-01 00:00:01", join_end="2999-12-31 23:59:59", state=Match.MatchState.opening.value, )
def test_login(self): user = User.create(name='test2', mobile="13838003802", password=User.create_password("123456")) url = "api/2/auth/login" body = {"username": "******", "password": "******"} response = self.fetch(url, method="POST", body=json.dumps(body)) self.assertEqual(200, response.code, response.body.decode()) result = json.loads(response.body.decode()) self.assertIn("session", result, result) self.assertIn("current_user", result, result)
def test_get_user_self(self): insecure_fields = ("password", "avatar_key", "pay_openid", "reg_device_id", "reg_device_type") user = User.create(name="test user self") self.auth_user = user response = self.fetch(self.USER_SELF) self.assertEqual(200, response.code, response.body.decode()) result = json.loads(response.body.decode()) self.assertEqual(user.id, result['id'], result) self.assertEqual("test user self", result["name"], result) for field in insecure_fields: self.assertNotIn(field, result, field)
def test_get_team_members(self): insecure_fields = ("inviter", "role", "push_enabled", "credit", "credit_limit", "free_times", "total_recharge", "created", "last_update_tim") user = User.create(name="leave team") self.team.add_member(user_id=user.id) url = self.TEAM_MEMBERS.format(team_id=self.team.id) self.auth_user = user response = self.fetch(url) self.assertEqual(200, response.code, response.body.decode()) result = json.loads(response.body.decode()) self.assertIn("members", result, result) for insecure in insecure_fields: self.assertNotIn(insecure, result["members"][0], insecure)
def test_get_team_member_detail(self): insecure_fields = ("inviter", "role", "push_enabled", "credit", "credit_limit", "free_times", "total_recharge", "created") self.team.add_member(user_id=self.user.id) member = TeamMember.get(team=self.team, user=self.user) url = self.MEMBER_DETAIL.format(team_id=self.team.id, user_id=self.user.id) # insecure self.auth_user = User.create(name="insecure") response = self.fetch(url) self.assertEqual(200, response.code, response.body) result = json.loads(response.body.decode()) self.assertEqual(member.nick, result['nick'], result) for insecure in insecure_fields: self.assertNotIn(insecure, result, insecure)
def test_cash_pay(self): activity = Activity.create(team=self.team, creator=self.creator, price='10', vip_price='8', leader=self.creator, payment_type=Activity.PaymentType.CASH_PAY, title='no online pay', description='on need online pay', start_time='3000-01-01 00:00:01', end_time='3000-12-31 23:59:59', join_end="4000-01-01 22:59:59", max_members=10) url = self.JOIN_ACTIVITY.format(activity_id=activity.id) body = { "payment": TeamOrder.OrderPaymentMethod.WXPAY.value, "nickname": "Nick name" } self.auth_user = User.create(name='no need online pay') response = self.fetch(url, method='POST', body=json.dumps(body), params={'team_id': self.team.id}) result = json.loads(response.body.decode()) expect = { "status": "ok", "state": ActivityMember.ActivityMemberState.confirmed.value, "payment_state": TeamOrder.OrderState.WAIT_BUYER_PAY.value, "order_no": "" } self.assertDictEqual(expect, result, result) self.assertEqual(200, response.code, response.body) with self.assertRaises(TeamOrder.DoesNotExist): TeamOrder.get( user=self.auth_user, activity_id=activity.id, ) member = ActivityMember.get(activity=activity, user=self.auth_user) # 现在支付, 直接确认 self.assertEqual(ActivityMember.ActivityMemberState.confirmed.value, member.state, member.state)
def test_reset_password(self): user = User.create(name='test4', mobile="13838003804", password=User.create_password("123456")) new_password = "******" url = "api/2/auth/reset_password" body = { "username": "******", "verify_code": "8888", "new_password": new_password } response = self.fetch(url, method="POST", body=json.dumps(body)) self.assertEqual(200, response.code, response.body.decode()) user = User.get(id=user.id) result = User.check_password(user.password, new_password) self.assertEqual(True, result, result)
def test_patch_user(self): user = User.create(name="user name") url = self.OBJECT_PATH.format(user_id=user.id) body = { "name": "new name", "gender": "f", "dob": "1990-01-01", "mobule": "12345678901" } self.auth_user = user response = self.fetch(url, method="PATCH", body=json.dumps(body), headers={"Content-Type": "application/json"}) self.assertEqual(204, response.code, response.body.decode()) updated = User.get(id=user.id) self.assertEqual("new name", updated.name, updated) self.assertEqual("f", updated.gender, updated) self.assertEqual("1990-01-01", updated.dob.strftime("%Y-%m-%d"), updated) self.assertIsNone(updated.mobile, updated)
def test_cash_free_activity(self): """ 参加免费活动不需要支付 线下支付活动的 ActivityMember.state 应该为 `确认` confirmed """ activity = self.create_free_activity( payment_type=Activity.PaymentType.CASH_PAY.value) url = self.JOIN_ACTIVITY.format(activity_id=activity.id) body = { "payment": TeamOrder.OrderPaymentMethod.WXPAY.value, "nickname": "Nick name" } self.auth_user = User.create(name='free activity') response = self.fetch(url, method='POST', body=json.dumps(body), params={'team_id': self.team.id}) self.assertEqual(200, response.code, response.body) result = json.loads(response.body.decode()) expect = { "status": "ok", "state": ActivityMember.ActivityMemberState.confirmed.value, "payment_state": TeamOrder.OrderState.TRADE_BUYER_PAID.value, "order_no": "" } self.assertDictEqual(expect, result, result) with self.assertRaises(TeamOrder.DoesNotExist): TeamOrder.get(user=self.auth_user, activity_id=activity.id) member = ActivityMember.get(activity=activity, user=self.auth_user) self.assertEqual(ActivityMember.ActivityMemberState.confirmed.value, member.state, member.state)
def test_delete_user(self): user = User.create( name='will be deleted' ) _user = User.get(id=user.id) _user.delete_instance()
def register_or_login(self, service, openid, access_token, expires_in, nickname, gender, head_url, auth_data): try: user = User.select().join( UserAuthData, on=(UserAuthData.user_id == User.id )).where((UserAuthData.service == service) & (UserAuthData.openid == openid)).get() except User.DoesNotExist: user = None if self.current_user: # 已绑定到其它账号 if user and user.id != self.current_user.id: raise ApiException( 403, "此%s账号已被其他用户使用" % UserAuthData.get_service_name(service)) # 已绑定到自己账号 elif user and user.id == self.current_user.id: UserAuthData.update( access_token=access_token, expires_in=expires_in, userinfo=auth_data).where( (UserAuthData.service == service) & (UserAuthData.user_id == user.id)).execute() # 已绑定其它账号 elif UserAuthData.select().where( (UserAuthData.service == service) & (UserAuthData.user_id == self.current_user.id) & (UserAuthData.openid != openid)).exists(): raise ApiException( 403, "你已绑定其他%s账号" % UserAuthData.get_service_name(service)) # 已登录执行绑定 else: UserAuthData.create(service=service, user_id=self.current_user.id, openid=openid, nickname=nickname, access_token=access_token, expires_in=expires_in, userinfo=auth_data) if self.device_id > 0: User.update(last_device_id=self.device_id).where( User.id == self.current_user.id).execute() self.write(self.create_session(self.current_user)) else: # 已注册用户直接登录 if user: update = {"last_login": datetime.now()} if self.device_id > 0: update["last_device_id"] = self.device_id User.update(**update).where(User.id == user.id).execute() UserAuthData.update( access_token=access_token, expires_in=expires_in, userinfo=auth_data).where( (UserAuthData.service == service) & (UserAuthData.user_id == user.id)).execute() # 未注册用户先注册 else: with self.db.transaction() as txn: if User.select().where(User.name == nickname).exists(): if nickname == "qzuser": name = "%s_%s" % (nickname, random.randint(100000, 999999)) else: name = "%s_%s" % (nickname, random.randint( 100, 999)) else: name = nickname user = User.create( name=name, gender=gender, mobile_verifyed=False, password=None, reg_device_id=self.device_id, last_device_id=self.device_id, last_login=datetime.now(), im_username=create_token(32).lower(), im_password=create_token(16), ) UserAuthData.create(service=service, user_id=user.id, openid=openid, nickname=nickname, access_token=access_token, expires_in=expires_in, userinfo=auth_data) # 将手机好加到 redis, 匹配好友需要 if user.mobile: self.redis.sadd('mobile:registered', user.mobile) # 从第三方下载头像 if head_url: tasks.user.update_avatar_by_url.delay( user.id, head_url) if user and self.device_id: Device.update(owner_id=user.id).where( Device.id == self.device_id).execute() self.write(self.create_session(user))
def initial_data(self): self.leader = User.create(name="imleader") self.team = Team.create(name="test_finish_activity", owner_id=self.leader.id) self.activity = Activity.create( team=self.team, creator=self.leader, leader=self.leader, title="test_finish_activity", description="test_stoped_activity", price=Decimal(15), max_members=30, start_time=datetime.datetime.now() - datetime.timedelta(hours=2), end_time=datetime.datetime.now() - datetime.timedelta(hours=1), payment_type=0, repeat_type="weekly", state=Activity.ActivityState.opening) self.online_paid_amount = 0 for i in range(0, 5): user = User.create(name="test_%s" % i) order = TeamOrder.create( team=self.team, user=user, order_type=0, activity_id=self.activity.id, title="test_finish", order_no=TeamOrder.get_new_order_no(), credit_fee=0, total_fee=self.activity.price, payment_fee=self.activity.price, payment_method="wxpay", state=TeamOrder.OrderState.TRADE_BUYER_PAID, paid=self.activity.start_time, created=self.activity.start_time, finished=self.activity.start_time) ActivityMember.create( team=self.team, activity=self.activity, user=user, price=self.activity.price, users_count=1, total_fee=self.activity.price, payment_state=order.state, payment_method="wxpay", order_id=order.id, order_no=order.order_no, state=ActivityMember.ActivityMemberState.confirmed, free_times=0, ) self.online_paid_amount += self.activity.price for i in range(0, 3): user = User.create(name="test2_%s" % i) order = TeamOrder.create(team=self.team, user=user, order_type=0, activity_id=self.activity.id, title="test_finish", order_no=TeamOrder.get_new_order_no(), credit_fee=0, total_fee=self.activity.price, payment_fee=self.activity.price, payment_method="wxpay", state=TeamOrder.OrderState.WAIT_BUYER_PAY, created=self.activity.start_time, finished=self.activity.start_time) ActivityMember.create( team=self.team, activity=self.activity, user=user, price=self.activity.price, users_count=1, total_fee=self.activity.price, payment_state=order.state, payment_method="wxpay", order_id=order.id, order_no=order.order_no, state=ActivityMember.ActivityMemberState.wait_confirm, free_times=0, )