Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/metabase/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class NotFoundError(Exception):
pass


class AuthenticationError(Exception):
pass
6 changes: 6 additions & 0 deletions src/metabase/metabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import requests

from metabase.exceptions import AuthenticationError


class Singleton(type):
_instances = WeakValueDictionary()
Expand Down Expand Up @@ -40,6 +42,10 @@ def token(self):
self.host + "/api/session",
json={"username": self.user, "password": self.password},
)

if response.status_code != 200:
raise AuthenticationError(response.content.decode())

self._token = response.json()["id"]

return self._token
Expand Down
11 changes: 9 additions & 2 deletions tests/test_metabase.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import TestCase

from metabase import Metabase

from metabase.exceptions import AuthenticationError
from tests.helpers import IntegrationTestCase


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

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

# TODO: add test case when token is None

def test_token_invalid_auth(self):
"""Ensure Metabase.token raises AuthenticationException is the user or password is invalid."""
metabase = Metabase(host="http://0.0.0.0:3000", user="", password="")

with self.assertRaises(AuthenticationError):
_ = metabase.token

def test_headers(self):
"""Ensure Metabase.headers returns a dictionary with the token."""
metabase = Metabase(host="example.com", user="", password="", token="123")
Expand Down