seitime-frappe/frappe/tests/ui/test_oauth20.py
Revant Nandgaonkar 02aa7b6f41 Social login refactor (#4519)
* Added DocType Social Login Key

WIP for https://github.com/frappe/frappe/issues/4496
added basic fields
after_insert add provider_username and provider_userid fields on User dt
on_trash deletes added fields on User dt

* Added field to store fontawesome icon for provider

* [Patch] Social Login Keys to Social Login Key

* [Patch] Social Login Keys to Social Login Key

* Social Login Key generates boilerplate

* patch fixed for social_login_refactor

* removed patch-not working

* use social login keys to initiate flow

* Login page shows Social Login Key

* show login via if base_url present

* removed boilerplate generator

* Multiple Changes

fix zxcvbn import in password_strength.py
use of child table instead of additional fields on user dt to store username and userid

* Fetched Template on Client JS

* Frappe social login template working

* Added Social Login Key Templates

* Codacy fixes and validate social login key urls

* [Patch] Social Login Keys (untested)

* [Fix] Patch refactor social login keys

* [Fix] Patch refactor_social_login_keys manually tested

* Refactor OAuth 2.0 related changes for Social Login Key

* [Fix] Patch refactor social login keys

* Test - Adding Frappe Social Login Key

* Social Login Key Tests

check added child table entry on user for provider frappe
it also checks if userid is created

* [WIP] Office 365 Social Login Key Template

* [Fix] Social Login - Redirect URL

* [Test] Single sign-on icons for added provider

* [Fix] Codacy Errors

* [Fix] Social Login Key Form JS

* Docs Added for Social Login Key

* [Fix] Patch Refactor Social Login Keys

* Handle different icon types

Handle different icon types (image, icon, emoji) with just icon field

* Move the login methods to a new py file

frappe.integrations.oauth2_logins added
copied whitelisted guest oauth2 redirect endpoints from login.py
removing the functions from login.py will break backward compatibility

* Social Login Key Form Changes

Moved Enable field to top
Fields which are not editable are collapsed

* [Fix] Codacy Errors

* Corrected Docs, sync.py

* [Docs] Adding a social login provider

* [Fix] set frappe userid from User Social Login

* [Fix] frappe userid in oauth.py

* removed icon_type

* Use frappe.utils.is_image
2018-01-03 14:57:16 +05:30

123 lines
3.6 KiB
Python

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt
from __future__ import unicode_literals
import unittest, frappe, requests, time
from frappe.test_runner import make_test_records
from frappe.utils.selenium_testdriver import TestDriver
from six.moves.urllib.parse import urlparse, parse_qs
class TestOAuth20(unittest.TestCase):
def setUp(self):
self.driver = TestDriver()
make_test_records("OAuth Client")
make_test_records("User")
self.client_id = frappe.get_all("OAuth Client", fields=["*"])[0].get("client_id")
# Set Frappe server URL reqired for id_token generation
try:
frappe_login_key = frappe.get_doc("Social Login Key", "frappe")
except frappe.DoesNotExistError:
frappe_login_key = frappe.new_doc("Social Login Key")
frappe_login_key.get_social_login_provider("Frappe", initialize=True)
frappe_login_key.base_url = "http://localhost:8000"
frappe_login_key.save()
def test_login_using_authorization_code(self):
# Go to Authorize url
self.driver.get(
"api/method/frappe.integrations.oauth2.authorize?client_id=" +
self.client_id +
"&scope=all%20openid&response_type=code&redirect_uri=http%3A%2F%2Flocalhost"
)
time.sleep(2)
# Login
username = self.driver.find("#login_email")[0]
username.send_keys("test@example.com")
password = self.driver.find("#login_password")[0]
password.send_keys("Eastern_43A1W")
sign_in = self.driver.find(".btn-login")[0]
sign_in.submit()
time.sleep(2)
# Allow access to resource
allow = self.driver.find("#allow")[0]
allow.click()
time.sleep(2)
# Get authorization code from redirected URL
auth_code = urlparse(self.driver.driver.current_url).query.split("=")[1]
payload = "grant_type=authorization_code&code="
payload += auth_code
payload += "&redirect_uri=http%3A%2F%2Flocalhost&client_id="
payload += self.client_id
headers = {'content-type':'application/x-www-form-urlencoded'}
# Request for bearer token
token_response = requests.post( frappe.get_site_config().host_name +
"/api/method/frappe.integrations.oauth2.get_token", data=payload, headers=headers)
# Parse bearer token json
bearer_token = token_response.json()
self.assertTrue(bearer_token.get("access_token"))
self.assertTrue(bearer_token.get("expires_in"))
self.assertTrue(bearer_token.get("id_token"))
self.assertTrue(bearer_token.get("refresh_token"))
self.assertTrue(bearer_token.get("scope"))
self.assertTrue(bearer_token.get("token_type") == "Bearer")
def test_login_using_implicit_token(self):
oauth_client = frappe.get_doc("OAuth Client", self.client_id)
oauth_client.grant_type = "Implicit"
oauth_client.response_type = "Token"
oauth_client.save()
frappe.db.commit()
# Go to Authorize url
self.driver.get(
"api/method/frappe.integrations.oauth2.authorize?client_id=" +
self.client_id +
"&scope=all%20openid&response_type=token&redirect_uri=http%3A%2F%2Flocalhost"
)
time.sleep(2)
# Login
username = self.driver.find("#login_email")[0]
username.send_keys("test@example.com")
password = self.driver.find("#login_password")[0]
password.send_keys("Eastern_43A1W")
sign_in = self.driver.find(".btn-login")[0]
sign_in.submit()
time.sleep(2)
# Allow access to resource
allow = self.driver.find("#allow")[0]
allow.click()
time.sleep(2)
# Get token from redirected URL
response_url = dict(parse_qs(urlparse(self.driver.driver.current_url).fragment))
self.assertTrue(response_url.get("access_token"))
self.assertTrue(response_url.get("expires_in"))
self.assertTrue(response_url.get("scope"))
self.assertTrue(response_url.get("token_type"))
def tearDown(self):
self.driver.close()