def test_client_init(): """ Test the client initialization. """ cli = client.Client("demo") assert cli.tenant == "demo" assert cli.config["site.domain"] == "sightmachine.io" cli = client.Client("demo") # VERIFY assert cli.tenant == "demo" assert cli.config["site.domain"] == "sightmachine.io"
def test_auth__auth_basic_success(mocked): """ Test that Authenticator can use basic auth """ class Response: ok = True text = "Success" @staticmethod def json(): return [] mocked.return_value = MagicMock(post=MagicMock(return_value=Response()), get=MagicMock(return_value=Response())) tenant = "" user = "" passw = "" cli = client.Client(tenant) authed = cli.auth assert authed._auth_basic(email=user, password=passw) is True mocked.return_value.post.assert_called_once_with( f"https://{tenant}.sightmachine.io/auth/password/login", data={ "remember": "yes", "email": user, "username": user, "password": passw }, )
def test_auth_check_auth_failure(mocked): """ Test that Authenticator can properly determine if authed """ class Response: ok = False text = "error" @staticmethod def json(): return None mocked.return_value = MagicMock(post=MagicMock(return_value=Response()), get=MagicMock(return_value=Response())) tenant = "demo" cli = client.Client(tenant) authed = cli.auth assert authed.check_auth() is False mocked.return_value.get.assert_called_once_with( f"https://{tenant}.sightmachine.io/api/cycle", params={ "_limit": 1, "_only": ["_id"] }, )
def test_auth__auth_apikey_success(mocked): """ Test that Authenticator can use apikey auth """ class Response: ok = True text = "Success" @staticmethod def json(): return [] mocked.return_value = MagicMock(post=MagicMock(return_value=Response()), get=MagicMock(return_value=Response())) tenant = "" secret_id = "" key_id = "" cli = client.Client(tenant) authed = cli.auth assert authed._auth_apikey(secret_id=secret_id, key_id=key_id) is True mocked.return_value.get.assert_called_once_with( f"https://{tenant}.sightmachine.io/api/cycle", params={ "_limit": 1, "_only": ["_id"] }, )
def test_auth_init(): """ Test that Authenticator host is set properly """ tenant = "demo" cli = client.Client(tenant) authed = cli.auth assert authed.host == get_url(cli.config["protocol"], tenant, cli.config["site.domain"])
def test_auth_route_auth(mocked_apikey, mocked_basic): """ Test that Authenticator can properly route to auth methods """ tenant = "demo" user = "******" passw = "password" secret_id = "1234466790dfsddfsd" key_id = "judhebd-5555-88i7-9bb2-a6baa6a97658" cli = client.Client(tenant) authed = cli.auth assert authed.login("basic", email=user, password=passw) mocked_basic.assert_called_once_with(email=user, password=passw) assert authed.login("apikey", secret_id=secret_id, key_id=key_id) mocked_apikey.assert_called_once_with(secret_id=secret_id, key_id=key_id) with pytest.raises(RuntimeError): authed.login("foo", extra="bar")
def test_auth__auth_apikey_failure(mocked): """ Test that Authenticator can handle errors in basic auth. """ class Response: ok = False text = "error" @staticmethod def json(): return None mocked.return_value = MagicMock(post=MagicMock(return_value=Response()), get=MagicMock(return_value=Response())) tenant = "demo" user = "******" passw = "password" cli = client.Client(tenant) authed = cli.auth with pytest.raises(RuntimeError): authed._auth_basic(email=user, password=passw)