Fakultas Ilmu Komputer UI

Commit 920d46da authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

[RED] Add test for _create_google_login and _create_random_phone_number

parent 393013ba
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,7 @@ 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 +134,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)
\ No newline at end of file
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