示例#1
0
class UsedReferralModelTest(TestCase):

    def setUp(self):
        self.user = User(
            username='******',
            email='*****@*****.**',
            password='******'
        )
        self.user.save()
        self.user_profile = self.user.profile
        self.referral = Referral(
            user=self.user,
            unique_code='UniqueCode',
            used_times=0
        )
        self.referral.save()
        self.used_referral = UsedReferral(
            user=self.user,
            referral=self.referral
        )
        self.used_referral.save()

    def test_creation(self):
        self.assertEqual(self.used_referral.user, self.user)
        self.assertEqual(self.used_referral.referral, self.referral)
        new_user = User(
            username='******',
            email='*****@*****.**',
            password='******'
        )
        new_user.save()
        new_used_referral = UsedReferral(
            user=new_user,
            referral=self.referral
        )
        new_used_referral.save()
        self.assertEqual(new_used_referral.user, new_user)
        self.assertEqual(new_used_referral.referral, self.referral)

    def test_null_instances(self):
        self.used_referral.user = None
        self.used_referral.referral = None
        self.used_referral.save()
        new_used_referral = UsedReferral(
            user=self.user,
            referral=self.referral
        )
        new_used_referral.save()
        new_used_referral.user = None
        new_used_referral.referral = None
        new_used_referral.save()
        null_used_referrals = UsedReferral.objects.all()
        for used_referral in null_used_referrals:
            self.assertEqual(used_referral.user, None)
            self.assertEqual(used_referral.referral, None)
示例#2
0
 def setUp(self):
     self.user = User(
         username='******',
         email='*****@*****.**',
         password='******'
     )
     self.user.save()
     self.referral = Referral(
         user=self.user,
         unique_code='UniqueCode',
         used_times=0
     )
     self.referral.save()
示例#3
0
def get_or_create_referral(request):
    try:
        if not isinstance(request.user, User) or request.user is AnonymousUser:
            return JsonResponse({
                'message': 'User not logged in'
            }, status=status.HTTP_400_BAD_REQUEST)
        referrals = Referral.objects.filter(user=request.user)
        if len(referrals) == 0:
            referral = Referral(user=request.user)
            referral.save()
            url = reverse('customer_signup_referral', args=[referral.unique_code], request=request)
            return JsonResponse({
                'message': 'Referral link successfully created',
                'data': {'referral_url': url}
            }, status=status.HTTP_201_CREATED)
        referral = referrals.first()
        url = reverse('customer_signup_referral', args=[referral.unique_code], request=request)
        return JsonResponse({
            'message': 'Referral link successfully retrieved',
            'data': {'referral_url': url}
        }, status=status.HTTP_200_OK)
    except Exception as exc:
        return _internal_server_error(exc)
示例#4
0
class UsedReferralReceiverTest(TestCase):
    def setUp(self):
        self.user = User(
            username='******',
            email='*****@*****.**',
            password='******'
        )
        self.user.save()
        self.referral = Referral(
            user=self.user,
            unique_code='UniqueCode',
            used_times=0
        )
        self.referral.save()

    def test_increase_referral_used_times(self):
        self.assertEqual(self.referral.used_times, 0)
        new_used_referral = UsedReferral(
            user=self.user,
            referral=self.referral
        )
        new_used_referral.save()
        self.assertEqual(self.referral.used_times, 1)
示例#5
0
class ReferralModelTest(TestCase):

    def setUp(self):
        self.user = User(
            username='******',
            email='*****@*****.**',
            password='******'
        )
        self.user.save()
        self.user_profile = self.user.profile
        self.referral = Referral(
            user=self.user,
            unique_code='UniqueCode',
            used_times=0
        )
        self.referral.save()

    def test_creation(self):
        self.assertEqual(self.referral.user, self.user)
        unique_code = self._generate_unique_code(self.user.username, self.user.email)
        self.assertEqual(self.referral.unique_code, unique_code)
        self.assertEqual(self.referral.used_times, 0)

    def test_increase_used_times(self):
        self.assertEqual(self.referral.used_times, 0)
        self.referral.increase_used_times()
        self.assertEqual(self.referral.used_times, 1)
        self.referral.increase_used_times()
        self.assertEqual(self.referral.used_times, 2)

    def test_generate_unique_code(self):
        unique_code = self._generate_unique_code(self.user.username, self.user.email)
        self.assertEqual(self.referral.generate_unique_code(self.user.username, self.user.email), unique_code)

    def test_create_user_profile_exception(self):
        with self.assertRaises(Exception) as exc:
            new_referral = Referral()

    def _generate_unique_code(self, username, email):
        base = CODE + str(username) + str(email)
        return base64.urlsafe_b64encode(str.encode(base)).decode().replace('=','')
示例#6
0
 def test_create_user_profile_exception(self):
     with self.assertRaises(Exception) as exc:
         new_referral = Referral()