This commit is contained in:
Sayyid Hamid Mahdavi 2026-07-11 09:53:07 +03:30
parent f711ee34a1
commit 7ad52c59a4
3 changed files with 40 additions and 16 deletions

View file

@ -21,7 +21,8 @@ Application = get_application_model()
class UserTests(APITestCase):
user_uuid = uuid.UUID('b14e8b86-8f4a-44d9-b29d-badceb47005f')
user_1_uuid = uuid.UUID('b14e8b86-8f4a-44d9-b29d-badceb47005f')
user_2_uuid = uuid.UUID('b14e8b86-8f4a-44d9-b29d-badceb47005e')
access_token_1 = 'au4naVsdKCbKNOhnElPyXcrwSnqqFbm'
access_token_2 = 'vu4naVsdKCbKNOhnElPyXcrwSnqqFbm'
application_uuid = uuid.UUID('a14e8b86-8f4a-44d9-b29d-badceb47005f')
@ -37,11 +38,14 @@ class UserTests(APITestCase):
def setUp(self):
from django.conf import settings
settings.SMS_SEND = False
self.user_phone_number = '+989100000000'
self.user = User.objects.create(pk=self.user_uuid, phone_number=self.user_phone_number)
self.user_1_phone_number = '+989100000000'
self.user_2_phone_number = '+989100000001'
self.user_1 = User.objects.create(pk=self.user_1_uuid, phone_number=self.user_1_phone_number)
self.user_2 = User.objects.create(pk=self.user_2_uuid, phone_number=self.user_2_phone_number)
Scope.objects.create(name='accounts.status:get', description='accounts.status:get')
Scope.objects.create(name='accounts.account:retrieve', description='accounts.account:retrieve')
Scope.objects.create(name='accounts.users.user:submit_referral', description='accounts.users.user:submit_referral')
self.application = Application.objects.create(
client_id=self.client_id,
@ -49,9 +53,9 @@ class UserTests(APITestCase):
authorization_grant_type='password',
hash_client_secret=False,
uuid=self.application_uuid,
user_id=self.user_uuid,
user_id=self.user_1_uuid,
max_allowed_session=1,
allowed_scope='accounts.status:get accounts.account:retrieve',
allowed_scope='accounts.status:get accounts.account:retrieve accounts.users.user:submit_referral',
)
# self.sys_date_patcher = patch('simata_safte.models.get_sys_date', mock_get_sys_date)
@ -70,7 +74,7 @@ class UserTests(APITestCase):
access_token_1 = AccessToken.objects.create(
**{
"token": self.access_token_1,
"user": self.user,
"user": self.user_1,
# "client_id": self.client_id,
# "client_owner": owner,
"application_id": self.application_uuid,
@ -88,7 +92,7 @@ class UserTests(APITestCase):
access_token_2 = AccessToken.objects.create(
**{
"token": self.access_token_2,
"user": self.user,
"user": self.user_1,
# "client_id": self.client_id,
# "client_owner": owner,
@ -122,13 +126,13 @@ class UserTests(APITestCase):
return basic_credentials
def login(self):
self.user.set_otp()
self.user_1.set_otp()
data = {
"grant_type": "password",
"username": self.user_phone_number,
"username": self.user_1_phone_number,
"password": '77501',
"scope": 'accounts.status:get accounts.account:retrieve',
"scope": 'accounts.status:get accounts.account:retrieve accounts.users.user:submit_referral',
"auth_fields": 'phone_number:otp'
}
self.client.credentials(
@ -136,6 +140,7 @@ class UserTests(APITestCase):
)
result = self.client.post(reverse("gooyal_oauth2:token"), data=data)
print(result.json())
access_token = result.json()['access_token']
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + access_token)
@ -148,6 +153,17 @@ class UserTests(APITestCase):
response = self.client.get(reverse("users:user_sessions_api"))
self.assertEqual(response.json()['results'][0]['is_current'] , True)
def test_referralPromotion_allOK_success(self):
self.login()
submit_referral_url = reverse("users:user-submit-referral")
submit_referral_data = {
"referral": self.user_2_uuid
}
response = self.client.post(submit_referral_url, submit_referral_data)
print(response.json())
# self.assertEqual(response.json()['results'][0]['is_current'] , True)

View file

@ -1,10 +1,16 @@
from django.urls import path
from django.contrib.auth.views import LogoutView
from rest_framework.routers import DefaultRouter
from .views import UserListView, UserPublicRetrieveView, AccountView, RequestOTPView, ChangePasswordView, \
OTPLoginView, ProfileDetailView, ProfileUpdateView, RequestOTTView, UserCurrentAvatarUrlView, UserInquiryView, \
UserDetailedRetrieveView, UserSessionListView, UserProfileView
UserDetailedRetrieveView, UserSessionListView, UserProfileView, UserViewSet
app_name = "users"
router = DefaultRouter()
router.register("user", UserViewSet, basename="user")
urlpatterns = [
path('login/', OTPLoginView.as_view(), name='login'),
path('logout/', LogoutView.as_view(), name='logout'),
@ -21,5 +27,5 @@ urlpatterns = [
path('api/request_ott/', RequestOTTView.as_view(), name='request_ott_api'),
path('api/change_password/', ChangePasswordView.as_view(), name='change_password_api'),
path('api/inquiry/', UserInquiryView.as_view(), name='user_inquiry_api'),
]
] + router.urls

View file

@ -230,20 +230,22 @@ class UserViewSet(
permission_classes = [TokenMatchesViewSetActions]
required_action_scopes = {
"set_referral": [["promotions.application.user-plan:promote"]],
"retrieve": [["promotions.application.user-plan:list-retrieve"]],
"list": [["promotions.application.user-plan:list-retrieve"]],
"submit_referral": [["accounts.users.user:submit_referral"]],
}
# TODO: better solution
# this is a proxy api for submit event in promotion service
@action(detail=False, methods=['POST'], serializer_class=ReferralSetSerializer)
def set_referral(self, request, *args, **kwargs):
def submit_referral(self, request, *args, **kwargs):
user = self.request.user
application = get_application(self.request)
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
referral = serializer.validated_data.get("referral")
if referral == self.request.user.pk:
raise exceptions.Conflict('SAME AS USER')
referral_user = get_object_or_404(User.objects.all(), pk=referral)
data = {