Exemplo n.º 1
0
 def test_get_all_unix_accounts(self):
     storage = JsonFormattedUnixAccountStorage(self._storage)
     storage.add_unix_account(valid_unix_account)
     storage.add_unix_account(
         UnixAccount(id_=2, name="another-unix-account"))
     all_unix_accounts = storage.get_all_unix_accounts()
     self.assertEqual(len(all_unix_accounts), 2)
Exemplo n.º 2
0
 def post(self):
     """ Register a new unix_account """
     unix_accountname = self.get_argument(Parameter.UNIX_ACCOUNT_NAME)
     unix_account_id = self.get_argument(Parameter.UNIX_ACCOUNT_ID)
     associated_user_id = self.get_argument(Parameter.ASSOCIATED_USER_ID,
                                            None)
     unix_account_added = self._args.storage.add_unix_account(
         UnixAccount(unix_account_id, unix_accountname), associated_user_id)
     if not unix_account_added:
         self.set_status(400)
Exemplo n.º 3
0
 def test_add_existing_unix_account(self):
     expected_calls = [
         call(ANY),  # add_unix_account
     ]
     storage = JsonFormattedUnixAccountStorage(self._storage)
     storage.add_unix_account(
         UnixAccount(
             id_=1,
             name="another-unix_account"))  # id is the unique constraint
     self.assertFalse(storage.add_unix_account(valid_unix_account))
     self._storage.write.assert_has_calls(expected_calls)
Exemplo n.º 4
0
 def _try_create(self, config: dict):
     name = config[Attribute.NAME]
     id_ = config[Attribute.ID]
     if not self._storage.unix_account_exists(id_):
         unix_account = UnixAccount(id_, name)
         if Attribute.ASSOCIATED_USER in config:
             self._storage.add_unix_account(
                 unix_account,
                 self._get_associated_user_id(config[Attribute.ASSOCIATED_USER])
             )
         else:
             self._storage.add_unix_account(unix_account)
         log.debug("Added '{type}': {name}".format(type=ConfigurationType.UNIX_ACCOUNT, name=name))
Exemplo n.º 5
0
 def test_get_nonexisting_unix_account(self):
     nonexisting_unix_account = UnixAccount(0, "")
     self._set_logged_in_user(admin_user)
     self._storage.set_response_data_for("get_unix_account_by_id",
                                         value=nonexisting_unix_account)
     url = tornado.httputil.url_concat(
         self.API_ENDPOINT, {Parameter.UNIX_ACCOUNT_ID: "1234-5678"})
     response = self.fetch(url, method="GET")
     decoded_response = tornado.escape.json_decode(response.body)
     returned_unix_accounts = decoded_response["unix_accounts"]
     # NOTE: response below assumes that "UnixAccountStorageStub" is behaving correct.
     self.assertEqual(len(returned_unix_accounts), 0,
                      "Expects to return an empty list of unix_account")
Exemplo n.º 6
0
 def _unserialize_unix_account(
         self, json_formatted_unix_account: dict) -> UnixAccount:
     unix_account = UnixAccount(
         json_formatted_unix_account[JsonAttribute.id],
         json_formatted_unix_account[JsonAttribute.name])
     return unix_account
Exemplo n.º 7
0
import unittest
from unittest.mock import (Mock, call, ANY)

from application import UnixAccount
from application.storage import (NonExistingUnixAccount, IOStorage)
from .unix_accounts import JsonFormattedUnixAccountStorage

valid_unix_account = UnixAccount(id_=1, name="unix-account-name")
valid_user_id = "123-456-789"


class TestJsonFormattedUnixAccountStorage(unittest.TestCase):
    def __init__(self, *args):
        super().__init__(*args)
        self._storage = Mock(spec=IOStorage)

    def setUp(self):
        self._reset_mock()

    def _reset_mock(self):
        self._storage.reset_mock()
        self._storage.read.return_value = "[]"

    def test_load_on_init(self):
        storage = JsonFormattedUnixAccountStorage(self._storage)
        self._storage.read.assert_called_once()

    def test_load_empty_storage(self):
        self._storage.read.side_effect = FileNotFoundError
        storage = JsonFormattedUnixAccountStorage(self._storage)
        # non-existing file is ok, file will be created on save
Exemplo n.º 8
0
from application.storage import UnixAccountStorage
from application import UnixAccount
from ..response_mixin import StubResponseMixin

unix_account = UnixAccount(1000, "account-name")


class UnixAccountStorageStub(UnixAccountStorage, StubResponseMixin):
    def default_response_data(self) -> dict:
        response_data = {
            "add_unix_account": True,
            "unix_account_exists": False,
            "remove_unix_account_by_id": True,
            "get_unix_account_by_id": unix_account,
            "get_all_unix_accounts": [unix_account],
            "associate_user_to_unix_account": True,
            "disassociate_user_from_unix_account": True,
            "get_associated_users_for_unix_account": ["1234-56778"]
        }
        return response_data

    def add_unix_account(self,
                         unix_account: UnixAccount,
                         associated_user_id: str = None) -> bool:
        """Returns True if added successfully, False if item already exists"""
        return self._response_data["add_unix_account"]

    def unix_account_exists(self, id_: int) -> bool:
        return self.get_response()

    def remove_unix_account_by_id(self, id_: int) -> bool: