Skip to content

Commit

Permalink
feat(User): reactivate users when attempting to create a deactivated …
Browse files Browse the repository at this point in the history
…user
  • Loading branch information
Charles Lariviere committed Mar 12, 2022
1 parent f446653 commit 277d79d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
28 changes: 20 additions & 8 deletions src/metabase_manager/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import metabase
from metabase.resource import Resource
from requests import HTTPError


class Entity:
Expand Down Expand Up @@ -80,14 +81,25 @@ def get_key_from_metabase_instance(instance: metabase.User) -> str:
return instance.email

def create(self, using: metabase.Metabase):
user = metabase.User.create(
using=using,
first_name=self.first_name,
last_name=self.last_name,
email=self.email,
password=uuid4().hex
)
user.send_invite()
try:
user = metabase.User.create(
using=using,
first_name=self.first_name,
last_name=self.last_name,
email=self.email,
password=uuid4().hex
)
user.send_invite()
except HTTPError as e:
if "Email address already in use." in str(e):
users = metabase.User.list(
using=using,
query=self.email,
include_deactivated=True
)
users[0].reactivate()
else:
raise e

def update(self, instance: metabase.User):
instance.update(
Expand Down
21 changes: 20 additions & 1 deletion tests/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import metabase
from metabase import PermissionGroup
from requests import HTTPError

import metabase_manager
from metabase_manager.entities import Entity, Group, User
Expand Down Expand Up @@ -132,6 +133,25 @@ def test_create(self):
self.assertTrue(u.called)
self.assertTrue(send_invite.called)

def test_create_reactivate(self):
"""Ensure User.create() calls .reactivate() when the email points to a deactivated user."""
metabase_user = metabase.User(
id=1,
email="reactivate@example.com",
first_name="my_first_name",
last_name="my_last_name",
_using=None
)

with patch.object(metabase.User, "create", side_effect=HTTPError('{"error": {"message": "Email address already in use."}}')) as u:
with patch.object(metabase.User, "list", return_value=[metabase_user]) as l:
with patch.object(metabase.User, "reactivate") as r:
user = User(email="reactivate@example.com", first_name="my_first_name", last_name="my_last_name")
user.create(using=None)

self.assertTrue(l.called)
self.assertTrue(r.called)

def test_update(self):
"""Ensure User.update() calls update() on the provided class."""
with patch.object(metabase.User, "update") as c:
Expand All @@ -145,4 +165,3 @@ def test_update(self):
user.update(instance=metabase_user)

self.assertIsNone(c.assert_called_with(first_name="my_first_name", last_name="my_last_name", email="my_email"))

0 comments on commit 277d79d

Please sign in to comment.