Fakultas Ilmu Komputer UI

Commit 64838995 authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

Merge branch 'dev-rafif' into development

parents b1cbe801 b6f586d1
Pipeline #66494 passed with stages
in 6 minutes and 17 seconds
from django.test import TestCase
import json
from .views import validate_google_token, _create_random_phone_number, _create_google_user
from unittest.mock import patch
from django.test import Client
from registrasi.models import BisaGoUser
......@@ -26,7 +27,8 @@ class TestOauth(TestCase):
password=password, last_name=name)
user.is_active = False
user.save()
BisaGoUser.objects.create(user=user, phone_number='089832234567',
BisaGoUser.objects.create(user=user, phone_number='9x1x4x5x5x4x8x0',
tanggal_lahir='1990-05-05', pekerjaan='Mahasiswa',
alamat='Alamat Palsu')
......@@ -133,3 +135,51 @@ class TestOauth(TestCase):
self.assertEqual(404, response.status_code)
self.assertTrue("message" in json_response)
time.sleep(1)
@patch('oauth.views.requests.get')
@patch('oauth.views.json.loads')
def test_validate_access_token_valid(self, mock_json_loads, mock_get):
mock_get.return_value.text = "DKSJNDKDSKN"
mock_json_loads.return_value = {"email": 'mock_user@email.com'}
result_flag, result_email = validate_google_token("DLSLDSMDSBAS^&**")
self.assertTrue(result_flag)
self.assertEqual('mock_user@email.com', result_email)
@patch('oauth.views.requests.get')
@patch('oauth.views.json.loads')
def test_validate_access_token_invalid(self, mock_json_loads, mock_get):
mock_get.return_value.text = "DKSJNDKDSKN"
mock_json_loads.return_value = {"error": 'error description'}
result_flag, result_email = validate_google_token("DLSLDSMDSBAS^&**")
json_response = json.loads(result_email.content)
self.assertTrue("message" in json_response)
self.assertEqual(404, result_email.status_code)
self.assertFalse(result_flag)
@patch('oauth.views.random.randint')
def test_create_phone_number_exist(self, mock_randint):
mock_randint.side_effect = [9, 1, 4, 5, 5, 4, 8, 0, 1, 1, 1, 1, 1, 1, 1, 1]
phone_number = _create_random_phone_number()
self.assertEqual('1x1x1x1x1x1x1x1', phone_number)
@patch('oauth.views.random.randint')
def test_create_phone_number_not_exist_already(self, mock_randint):
mock_randint.side_effect = [1, 1, 1, 1, 1, 1, 1, 1]
phone_number = _create_random_phone_number()
self.assertEqual('1x1x1x1x1x1x1x1', phone_number)
def test_create_google_user_exist(self):
user = _create_google_user('mock_user@email.com', 'name')
self.assertEqual('mock_user@email.com', user.username)
self.assertEqual('mock_user@email.com', user.email)
self.assertEqual('name', user.last_name)
@patch('oauth.views._create_random_phone_number')
def test_create_google_user_exist(self, mock_random_phone):
mock_random_phone.return_value = '1x1x1x1x1x1x1x1'
user = _create_google_user('mock_user3434@email.com', 'name')
self.assertEqual('mock_user3434@email.com', user.username)
self.assertEqual('mock_user3434@email.com', user.email)
self.assertEqual('name', user.last_name)
self.assertEqual('1x1x1x1x1x1x1x1', user.phone_number.phone_number)
......@@ -9,7 +9,6 @@ from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
from django.conf import settings
from registrasi.models import BisaGoUser
@csrf_exempt
......@@ -55,23 +54,35 @@ def request_token(request):
def _create_google_user(email, name):
user = User()
user.username = email
# provider random default password
user.password = make_password(BaseUserManager().make_random_password())
user.email = email
user.is_active = True
user.last_name = name
user.save()
random_generated_phone_number = 'x'.join([str(random.randint(0, 9)) for i in range(8)])
BisaGoUser.objects.create(user=user, phone_number=random_generated_phone_number)
return user
try:
return User.objects.get(username=email)
except User.DoesNotExist:
user = User()
user.username = email
# provider random default password
user.password = make_password(BaseUserManager().make_random_password())
user.email = email
user.is_active = True
user.last_name = name
user.save()
random_generated_phone_number = _create_random_phone_number()
BisaGoUser.objects.create(user=user, phone_number=random_generated_phone_number)
return user
def _create_random_phone_number():
phone_number = 'x'.join([str(random.randint(0, 9)) for i in range(8)])
try:
BisaGoUser.objects.get(phone_number=phone_number)
return _create_random_phone_number()
except BisaGoUser.DoesNotExist:
return phone_number
@csrf_exempt
def validate_google_token(access_token):
payload = {'access_token': access_token} # validate the token
req = requests.get('https://www.googleapis.com/oauth2/v2/userinfo', params=payload, proxies=settings.PROXIES)
req = requests.get('https://www.googleapis.com/oauth2/v2/userinfo',
params=payload, proxies=settings.PROXIES)
data = json.loads(req.text)
if 'error' in data or 'email' not in data:
content = {'message': 'wrong google token / this google token is already expired.'}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment