Skip to content

Commit a28d5b2

Browse files
author
Charles Larivier
committed
feat: raise AuthenticationError
Signed-off-by: Charles Larivier <charles@dribbble.com>
1 parent d306bd2 commit a28d5b2

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/metabase/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
class NotFoundError(Exception):
22
pass
3+
4+
5+
class AuthenticationError(Exception):
6+
pass

src/metabase/metabase.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import requests
44

5+
from metabase.exceptions import AuthenticationError
6+
57

68
class Singleton(type):
79
_instances = WeakValueDictionary()
@@ -40,6 +42,10 @@ def token(self):
4042
self.host + "/api/session",
4143
json={"username": self.user, "password": self.password},
4244
)
45+
46+
if response.status_code != 200:
47+
raise AuthenticationError(response.content.decode())
48+
4349
self._token = response.json()["id"]
4450

4551
return self._token

tests/test_metabase.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest import TestCase
22

33
from metabase import Metabase
4-
4+
from metabase.exceptions import AuthenticationError
55
from tests.helpers import IntegrationTestCase
66

77

@@ -27,11 +27,18 @@ def test_host(self):
2727

2828
def test_token(self):
2929
"""Ensure Metabase.token returns Metabase._token if not None, else gets a new token."""
30-
metabase = Metabase(host="example.co.", user="", password="", token="123")
30+
metabase = Metabase(host="example.com", user="", password="", token="123")
3131
self.assertEqual(metabase.token, "123")
3232

3333
# TODO: add test case when token is None
3434

35+
def test_token_invalid_auth(self):
36+
"""Ensure Metabase.token raises AuthenticationException is the user or password is invalid."""
37+
metabase = Metabase(host="http://0.0.0.0:3000", user="", password="")
38+
39+
with self.assertRaises(AuthenticationError):
40+
_ = metabase.token
41+
3542
def test_headers(self):
3643
"""Ensure Metabase.headers returns a dictionary with the token."""
3744
metabase = Metabase(host="example.com", user="", password="", token="123")

0 commit comments

Comments
 (0)