Skip to content

Commit

Permalink
feat(auth0): Add upsert_user function (#828)
Browse files Browse the repository at this point in the history
* feat(auth0): Add upsert_user function

* chore(black): Fix formatting

* chore(test): Create auth0.upsert_user test

* chore(gitignore): ignore swp files

* test(auth0): get correct tests

---------

Co-authored-by: Tal Levy <[email protected]>
  • Loading branch information
talevy42 and tal42levy authored Jun 16, 2023
1 parent 4b9e1af commit 71a9672
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ bill_com_credentials.*

docs/html
docs/dirhtml
*.sw*
60 changes: 59 additions & 1 deletion parsons/auth0/auth0.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json

import requests
from parsons.etl.table import Table
from parsons.utilities import check_env
import requests


class Auth0(object):
Expand Down Expand Up @@ -71,3 +73,59 @@ def get_users_by_email(self, email):
params={"email": email},
).json()
)

def upsert_user(
self,
email,
username=None,
given_name=None,
family_name=None,
app_metadata={},
user_metadata={},
):
"""
Upsert Auth0 users by email.
`Args:`
email: str
The user email of the record to get.
username: optional str
Username to set for user
given_name: optional str
Given to set for user
family_name: optional str
Family name to set for user
app_metadata: optional dict
App metadata to set for user
user_metadata: optional dict
User metadata to set for user
`Returns:`
Requests Response object
"""
payload = json.dumps(
{
"email": email.lower(),
"given_name": given_name,
"family_name": family_name,
"username": username,
"connection": "Username-Password-Authentication",
"app_metadata": app_metadata,
"blocked": False,
"user_metadata": user_metadata,
}
)
existing = self.get_users_by_email(email.lower())
if existing.num_rows > 0:
a0id = existing[0]["user_id"]
ret = requests.patch(
f"{self.base_url}/api/v2/users/{a0id}",
headers=self.headers,
data=payload,
)
else:
ret = requests.post(
f"{self.base_url}/api/v2/users", headers=self.headers, data=payload
)
if ret.status_code != 200:
raise ValueError(f"Invalid response {ret.json()}")
return ret
35 changes: 33 additions & 2 deletions test/test_auth0.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from parsons import Auth0, Table
import unittest
import unittest.mock
from test.utils import assert_matching_tables

import requests_mock
import unittest
from parsons import Auth0, Table

CLIENT_ID = "abc"
CLIENT_SECRET = "def"
Expand All @@ -11,6 +13,13 @@
class TestAuth0(unittest.TestCase):
def setUp(self):
self.auth0 = Auth0(CLIENT_ID, CLIENT_SECRET, DOMAIN)
self.fake_upsert_person = {
"email": "[email protected]",
"given_name": "Fakey",
"family_name": "McFakerson",
"username": "fakeusername",
"user_id": 3,
}

@requests_mock.Mocker()
def test_delete_user(self, m):
Expand All @@ -29,3 +38,25 @@ def test_get_users_by_email(self, m):
assert_matching_tables(
self.auth0.get_users_by_email(email), Table(mock_users), True
)

@requests_mock.Mocker()
def test_upsert_user(self, m):
user = self.fake_upsert_person
email = user["email"]
m.get(
f"{self.auth0.base_url}/api/v2/users-by-email?email={email}",
json=[user],
)
mock_resp = unittest.mock.MagicMock()
mock_resp.status_code = 200
m.patch(f"{self.auth0.base_url}/api/v2/users/{user['user_id']}", [mock_resp])
m.post(f"{self.auth0.base_url}/api/v2/users", mock_resp)
ret = self.auth0.upsert_user(
email,
user["username"],
user["given_name"],
user["family_name"],
{},
{},
)
self.assertEqual(ret.status_code, 200)

0 comments on commit 71a9672

Please sign in to comment.