def setUp(self): self.u = User('U12345') self.admin = create_test_admin('Uadmin') self.db = MemoryDB(users=[self.u, self.admin]) self.testcommand = TokenCommand( self.db, TokenCommandConfig(timedelta(days=7), 'secret'))
class TestTokenCommand(TestCase): def setUp(self): self.u = User('U12345') self.admin = create_test_admin('Uadmin') self.db = MemoryDB(users=[self.u, self.admin]) self.testcommand = TokenCommand( self.db, TokenCommandConfig(timedelta(days=7), 'secret')) def test_handle_nonexistent_member(self): ret_val, ret_code = self.testcommand.handle('', 'nonexistent') self.assertEqual(ret_val, TokenCommand.lookup_error) self.assertEqual(ret_code, 200) def test_handle_member_request(self): ret_val, ret_code = self.testcommand.handle('', self.u.slack_id) self.assertEqual(ret_val, TokenCommand.permission_error) self.assertEqual(ret_code, 200) def test_handle_non_member_request(self): ret_msg, ret_code = self.testcommand.handle('', self.admin.slack_id) token = extract_jwt(ret_msg) decoded = jwt.decode(token, 'secret', algorithms='HS256') self.assertEqual(decoded['user_id'], self.admin.slack_id) self.assertEqual(decoded['permissions'], Permissions.admin.value) self.assertEqual(ret_code, 200)
def __init__(self, config: Config, db_facade: DBFacade, bot: Bot, gh_interface: GithubInterface, token_config: TokenCommandConfig, metrics: CWMetrics, gcp: Optional[GCPInterface] = None): """Initialize the dictionary of command handlers.""" self.commands: Dict[str, Command] = {} self.__facade = db_facade self.__bot = bot self.__github = gh_interface self.__gcp = gcp self.__metrics = metrics self.commands["user"] = UserCommand(self.__facade, self.__github, self.__gcp) self.commands["team"] = TeamCommand(config, self.__facade, self.__github, self.__bot, gcp=self.__gcp) self.commands["token"] = TokenCommand(self.__facade, token_config) self.commands["project"] = ProjectCommand(self.__facade) self.commands["karma"] = KarmaCommand(self.__facade) self.commands["mention"] = MentionCommand(self.__facade) self.commands["i-quit"] = IQuitCommand(self.__facade) # Disable project commands (delete when we enable it again) del self.commands['project']
def __init__(self, db_facade: DBFacade, bot: Bot, gh_interface: GithubInterface, token_config: TokenCommandConfig) -> None: """Initialize the dictionary of command handlers.""" self.__commands: Dict[str, Command] = {} self.__facade = db_facade self.__bot = bot self.__github = gh_interface self.__commands["user"] = UserCommand(self.__facade, self.__github) self.__commands["team"] = TeamCommand(self.__facade, self.__github, self.__bot) self.__commands["token"] = TokenCommand(self.__facade, token_config) self.__commands["project"] = ProjectCommand(self.__facade) self.__commands["karma"] = KarmaCommand(self.__facade) self.__commands["mention"] = MentionCommand(self.__facade)
def setUp(self): """Set up the test case environment.""" self.mock_facade = mock.MagicMock(DBFacade) self.testcommand = TokenCommand( self.mock_facade, TokenCommandConfig(timedelta(days=7), "secret"))
class TestTokenCommand(TestCase): """Test case for TokenCommand class.""" def setUp(self): """Set up the test case environment.""" self.mock_facade = mock.MagicMock(DBFacade) self.testcommand = TokenCommand( self.mock_facade, TokenCommandConfig(timedelta(days=7), "secret")) def test_get_command_name(self): """Test get_name().""" assert self.testcommand.command_name == "token" def test_get_command_desc(self): """Test get_desc().""" assert self.testcommand.desc == "Generate a signed token " \ "for use with the HTTP API" def test_handle_nonexistent_member(self): """Test handle() when given a nonexistent member.""" self.mock_facade.retrieve.side_effect = LookupError ret_val, ret_code = self.testcommand.handle("", "nonexistent") assert ret_val == "Requesting user not found!" assert ret_code == 200 def test_handle_member(self): """Test handle() when given a user with member permissions.""" user = User("U12345") user.permissions_level = Permissions.member self.mock_facade.retrieve.return_value = user ret_val, ret_code = self.testcommand.handle("", user.slack_id) assert ret_val == "You do not have the sufficient " \ "permission level for this command!" assert ret_code == 200 def test_handle_team_lead(self): """Test handle() when given a user with team lead permissions.""" user = User("U12345") user.permissions_level = Permissions.team_lead self.mock_facade.retrieve.return_value = user ret_msg, ret_code = \ self.testcommand.handle("", user.slack_id) token = self.__parse_token(ret_msg) decoded = jwt.decode(token, "secret", algorithms='HS256') assert decoded['user_id'] == user.slack_id assert decoded['permissions'] == Permissions.team_lead.value assert ret_code == 200 def test_handle_admin(self): """Test handle() when given a user with admin permissions.""" user = User("U12345") user.permissions_level = Permissions.admin self.mock_facade.retrieve.return_value = user ret_msg, ret_code = \ self.testcommand.handle("", user.slack_id) token = self.__parse_token(ret_msg) decoded = jwt.decode(token, "secret", algorithms='HS256') assert decoded['user_id'] == user.slack_id assert decoded['permissions'] == Permissions.admin.value assert ret_code == 200 def __parse_token(self, msg): """Hacky way to get returned token out when testing TokenCommand.""" parts = msg.split("```") return parts[1].strip()