Ejemplo n.º 1
0
    def test_video_payment(self):
        video = HotVideo.objects.create(**self.data)
        video.users.add(self.test_user.id)

        PayOrder.objects.create(
            user=self.test_user,
            status=constants.PAYMENT_STATUS_UNPAID,
            content_object=video,
            amount=video.price,
            out_trade_no=PayService.generate_out_trade_no(),
            payment_type=constants.PAYMENT_TYPE_ALIPAY,
            device=constants.DEVICE_PLATFORM_IOS,
        )
        PayOrder.objects.create(
            user=self.test_hot_video_user,
            status=constants.PAYMENT_STATUS_PAID,
            content_object=video,
            amount=video.price,
            out_trade_no=PayService.generate_out_trade_no(),
            payment_type=constants.PAYMENT_TYPE_ALIPAY,
            device=constants.DEVICE_PLATFORM_IOS,
            pay_time=datetime.datetime.now(),
        )

        res = self.client1.get(f'/users/{self.test_user.id}/hot_videos')
        result = res.data['results'][0]
        self.assertFalse(result['is_paid'])
        self.assertIsNone(result['url'])

        res = self.client3.get(f'/users/{self.test_user.id}/hot_videos')
        result = res.data['results'][0]
        self.assertTrue(result['is_paid'])
        self.assertEqual(result['url'], get_cdn_url(self.data['url']))
Ejemplo n.º 2
0
    def test_get_boxer_comments(self):
        # 为普通用户test_user_1创建用户信息,用于购买课程
        for user in [self.test_user_1, self.test_user_2, self.test_user_3]:
            UserProfile.objects.filter(user=user).update(
                **self.user_profile_data)

        # 为拳手用户test_user_2创建2条订单数据(依次创建user_profile->boxer->club->course->course_order)
        self.boxer_data['user'] = self.test_user_2
        boxer = BoxerIdentification.objects.create(**self.boxer_data)
        club = BoxingClub.objects.create(**self.club_data)
        self.course_data['club'] = club
        self.course_data['boxer'] = boxer
        course = Course.objects.create(**self.course_data)
        self.course_order_data['content_object'] = course
        self.client1.post('/course/order', data={'id': course.id})
        self.client1.post('/course/order', data={'id': course.id})

        # 为拳手用户test_user_3创建3条订单数据
        self.boxer_data['user'] = self.test_user_3
        boxer = BoxerIdentification.objects.create(**self.boxer_data)
        self.club_data['name'] = 'club02'
        club = BoxingClub.objects.create(**self.club_data)
        self.course_data['club'] = club
        self.course_data['boxer'] = boxer
        course = Course.objects.create(**self.course_data)
        self.course_order_data['content_object'] = course
        self.client1.post('/course/order', data={'id': course.id})
        self.client1.post('/course/order', data={'id': course.id})
        self.client1.post('/course/order', data={'id': course.id})
        course_order1 = CourseOrder.objects.filter(boxer=boxer)[0]
        course_order2 = CourseOrder.objects.filter(boxer=boxer)[1]

        # 对拳手test_user_3的订单order1、order2创建2条评论
        self.comment_data['order'] = course_order1
        OrderComment.objects.create(**self.comment_data)
        self.comment_data['score'] = 5
        self.comment_data['order'] = course_order2
        OrderComment.objects.create(**self.comment_data)

        # 获取拳手评论列表
        res = self.client3.get(f'/boxer/{boxer.id}/comments')
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(len(res.data['results']), 2)
        self.assertEqual(res.data['count'], 2)
        self.assertEqual(res.data['avg_score'], (6 + 5) / 2)
        for key in self.comment_data:
            # user为购买者
            if key == 'user':
                self.assertEqual(res.data['results'][0][key]['id'],
                                 self.test_user_1.id)
                self.assertEqual(res.data['results'][0][key]['nick_name'],
                                 self.user_profile_data['nick_name'])
                self.assertEqual(res.data['results'][0][key]['avatar'],
                                 get_cdn_url(self.user_profile_data['avatar']))
            elif key == 'order':
                self.assertEqual(res.data['results'][0][key], course_order2.id)
            else:
                self.assertEqual(res.data['results'][0][key],
                                 self.comment_data[key])
Ejemplo n.º 3
0
 def to_representation(self, instance):
     data = super(UserProfileSerializer, self).to_representation(instance)
     data[
         'nick_name'] = instance.nick_name or DEFAULT_NICKNAME_FORMAT.format(
             instance.user.id)
     data['avatar'] = get_cdn_url(
         instance.avatar) if instance.avatar else DEFAULT_AVATAR
     return data
Ejemplo n.º 4
0
 def to_representation(self, user):
     representation_dict = {
         "id": user.id,
         "user_type": user.get_user_type_display()
     }
     if hasattr(user, "user_profile"):
         avatar = get_cdn_url(
             user.user_profile.avatar) if user.user_profile.avatar else None
         representation_dict.update(nick_name=user.user_profile.nick_name,
                                    avatar=avatar)
     return representation_dict
Ejemplo n.º 5
0
    def test_get_boxer_order_detail(self):
        # 为普通用户test_user_1创建用户信息,用于购买课程
        UserProfile.objects.filter(user=self.test_user_1).update(
            **self.user_profile_data)

        # 为拳手用户test_user_4创建1条订单数据(依次创建user_profile->boxer->club->course->course_order)
        UserProfile.objects.filter(user=self.test_user_4).update(
            **self.user_profile_data)
        self.boxer_data['user'] = self.test_user_4
        boxer = BoxerIdentification.objects.create(**self.boxer_data)
        club = BoxingClub.objects.create(**self.club_data)
        self.course_data['club'] = club
        self.course_data['boxer'] = boxer
        course = Course.objects.create(**self.course_data)
        # 不能购买自己的课程
        res = self.client4.post('/course/order', data={'id': course.id})
        self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST)

        self.client1.post('/course/order', data={'id': course.id})
        # 为订单创建评论
        course_order = CourseOrder.objects.get(course=course)
        self.comment_data['order'] = course_order
        OrderComment.objects.create(**self.comment_data)

        res = self.client4.get(f'/boxer/order/{course_order.pk}')
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(res.data['status'], constants.PAYMENT_STATUS_UNPAID)
        self.assertEqual(res.data['user_id'], self.test_user_1.id)
        self.assertEqual(res.data['user_nickname'],
                         self.user_profile_data['nick_name'])
        self.assertEqual(res.data['user_gender'],
                         self.user_profile_data['gender'])
        self.assertEqual(res.data['user_avatar'],
                         get_cdn_url(self.user_profile_data['avatar']))
        self.assertEqual(res.data['course_name'],
                         self.course_data['course_name'])
        self.assertEqual(res.data['course_duration'],
                         self.course_data['duration'])
        self.assertEqual(res.data['course_validity'],
                         self.course_data['validity'])
        self.assertEqual(res.data['club_id'], club.id)
        self.assertEqual(res.data['club_name'], self.club_data['name'])
        self.assertEqual(res.data['club_address'], self.club_data['address'])
        self.assertEqual(str(res.data['club_longitude']),
                         str(self.club_data['longitude']))
        self.assertEqual(str(res.data['club_latitude']),
                         str(self.club_data['latitude']))
        self.assertEqual(res.data['comment_score'], self.comment_data['score'])
        self.assertEqual(res.data['comment_images'],
                         self.comment_data['images'])
        self.assertEqual(res.data['comment_score'], self.comment_data['score'])
        self.assertIsNotNone(res.data['comment_time'])
        self.assertEqual(res.data['identity'], 'user')
Ejemplo n.º 6
0
def serialize_user(user, context):
    profile = user.user_profile

    return {
        'id': user.id,
        'identity': user.identity,
        'is_following': bool(is_following(context['request'].user.id,
                                          user.id)),
        'nick_name': profile.nick_name
        or DEFAULT_NICKNAME_FORMAT.format(user.id),
        'avatar': get_cdn_url(profile.avatar) if profile.avatar else None,
        "user_type": user.get_user_type_display()
    }
Ejemplo n.º 7
0
 def get_user(self, instance):
     user = instance.user
     profile = user.user_profile
     return {
         'id':
         user.id,
         'identity':
         user.identity,
         'nick_name':
         profile.nick_name or DEFAULT_NICKNAME_FORMAT.format(user.id),
         'avatar':
         get_cdn_url(profile.avatar) if profile.avatar else None,
         "user_type":
         user.get_user_type_display()
     }
Ejemplo n.º 8
0
    def test_user_profile_detail(self):
        response = self.client.get(path="/user_profile")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['name'], self.user_profile_data['name'])
        self.assertEqual(response.data['gender'], self.user_profile_data['gender'])
        self.assertEqual(response.data['nick_name'], self.user_profile_data['nick_name'])
        self.assertEqual(response.data['nation'], self.user_profile_data['nation'])
        self.assertEqual(response.data['height'], self.user_profile_data['height'])
        self.assertEqual(response.data['weight'], self.user_profile_data['weight'])
        self.assertEqual(response.data['profession'], self.user_profile_data['profession'])
        self.assertEqual(response.data['avatar'], get_cdn_url(self.user_profile_data['avatar']))
        self.assertEqual(response.data['bio'], self.user_profile_data['bio'])

        user = User.objects.create_user(mobile="19900000002", password="******")
        response = self.client.get(path=f"/user_profile/{user.id}")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['nick_name'], DEFAULT_NICKNAME_FORMAT.format(user.id))
        self.assertEqual(response.data['avatar'], DEFAULT_AVATAR)
Ejemplo n.º 9
0
    def test_get_user_order_detail(self):
        # 为拳手用户test_user_1创建1个课程(依次创建user_profile->boxer->club->course)
        UserProfile.objects.filter(user=self.test_user_1).update(
            **self.user_profile_data)
        self.test_user_1.refresh_from_db()
        self.boxer_data['user'] = self.test_user_1
        boxer = BoxerIdentification.objects.create(**self.boxer_data)
        User.objects.filter(id=self.test_user_1.id).update(
            user_type=USER_TYPE_BOXER)
        club = BoxingClub.objects.create(**self.club_data)
        self.course_data['club'] = club
        self.course_data['boxer'] = boxer
        course = Course.objects.create(**self.course_data)

        # 为普通用户test_user_2创建用户信息,并购买课程
        self.user_profile_data['user'] = self.test_user_2
        self.client2.post('/course/order', data={'id': course.id})
        course_order = CourseOrder.objects.get(course=course)
        # 用户test_user_2获取所购买课程的详情
        res = self.client2.get(f'/user/order/{course_order.id}')
        self.assertEqual(res.status_code, status.HTTP_200_OK)
        self.assertEqual(res.data['status'], constants.PAYMENT_STATUS_UNPAID)
        self.assertEqual(res.data['boxer_id'], boxer.id)
        self.assertEqual(res.data['boxer_name'], boxer.real_name)
        self.assertEqual(res.data['boxer_gender'],
                         boxer.user.user_profile.gender)
        self.assertEqual(res.data['boxer_avatar'],
                         get_cdn_url(boxer.user.user_profile.avatar))
        self.assertEqual(res.data['course_name'],
                         self.course_data['course_name'])
        self.assertEqual(res.data['course_duration'],
                         self.course_data['duration'])
        self.assertEqual(res.data['course_validity'],
                         self.course_data['validity'])
        self.assertEqual(res.data['club_id'], club.id)
        self.assertEqual(res.data['club_name'], self.club_data['name'])
        self.assertEqual(res.data['club_address'], self.club_data['address'])
        self.assertEqual(res.data['boxer_user_type'],
                         USER_TYPE_MAP[USER_TYPE_BOXER])
        self.assertEqual(str(res.data['club_longitude']),
                         str(self.club_data['longitude']))
        self.assertEqual(str(res.data['club_latitude']),
                         str(self.club_data['latitude']))
Ejemplo n.º 10
0
 def get_picture(self, obj):
     return obj.picture and get_cdn_url(obj.picture)
Ejemplo n.º 11
0
 def get_boxer_avatar(self, instance):
     return instance.boxer.user.user_profile.avatar and get_cdn_url(
         instance.boxer.user.user_profile.avatar)
Ejemplo n.º 12
0
 def get_avatar(self, user):
     return user.user_profile.avatar and get_cdn_url(
         user.user_profile.avatar)