Просмотр исходного кода

Merge pull request #4064 from revant/FrappeOAuth2Client

Added FrappeOAuth2Client
version-14
Rushabh Mehta 7 лет назад
committed by GitHub
Родитель
Сommit
5a5089d7f7
2 измененных файлов: 114 добавлений и 0 удалений
  1. +34
    -0
      frappe/frappeclient.py
  2. +80
    -0
      frappe/tests/test_frappeoauth2client.py

+ 34
- 0
frappe/frappeclient.py Просмотреть файл

@@ -292,3 +292,37 @@ class FrappeClient(object):
return rjson['data'] return rjson['data']
else: else:
return None return None

class FrappeOAuth2Client(FrappeClient):
def __init__(self, url, access_token, verify=True):
self.access_token = access_token
self.headers = {
"Authorization": "Bearer " + access_token,
"content-type": "application/x-www-form-urlencoded"
}
self.verify = verify
self.session = OAuth2Session(self.headers)
self.url = url

def get_request(self, params):
res = requests.get(self.url, params=self.preprocess(params), headers=self.headers, verify=self.verify)
res = self.post_process(res)
return res

def post_request(self, data):
res = requests.post(self.url, data=self.preprocess(data), headers=self.headers, verify=self.verify)
res = self.post_process(res)
return res

class OAuth2Session():
def __init__(self, headers):
self.headers = headers
def get(self, url, params, verify):
res = requests.get(url, params=params, headers=self.headers, verify=verify)
return res
def post(self, url, data, verify):
res = requests.post(url, data=data, headers=self.headers, verify=verify)
return res
def put(self, url, data, verify):
res = requests.put(url, data=data, headers=self.headers, verify=verify)
return res

+ 80
- 0
frappe/tests/test_frappeoauth2client.py Просмотреть файл

@@ -0,0 +1,80 @@
# 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
from frappe.frappeclient import FrappeOAuth2Client

class TestFrappeOAuth2Client(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
frappe.db.set_value("Social Login Keys", None, "frappe_server_url", "http://localhost:8000")
frappe.db.commit()

def test_insert_note(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()
client = FrappeOAuth2Client(frappe.get_site_config().host_name, bearer_token.get("access_token"))

notes = [
{"doctype": "Note", "title": "Sing", "public": True},
{"doctype": "Note", "title": "a", "public": True},
{"doctype": "Note", "title": "Song", "public": True},
{"doctype": "Note", "title": "of", "public": True},
{"doctype": "Note", "title": "sixpence", "public": True}
]

for note in notes:
client.insert(note)

self.assertTrue(len(frappe.get_all("Note")) == 5)

Загрузка…
Отмена
Сохранить