def test_team_member_authenticates_just_to_channels_he_belongs(self): user_id = 0 team_id = 0 channel_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token authentication.team_id = team_id authentication.channel_id = channel_id '''Mocked outputs''' user = User(user_id=user_id, token=token) team_user = PublicUser(user_id=user_id) team_user.team_role = TeamRoles.MEMBER.value team_user.team_id = team_id channel_user = PublicUser(user_id=user_id) channel_user.team_id = team_id channel_user.channel_id = channel_id channel_user.is_channel_creator = False sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = team_user sys.modules[ "daos.users"].UserDatabaseClient.get_channel_user_by_ids.return_value = channel_user authenticated_user = Authenticator.authenticate_channel(authentication) self.assertEqual(user_id, authenticated_user.id) self.assertEqual(team_id, authenticated_user.team_id) self.assertEqual(channel_id, authenticated_user.channel_id) self.assertFalse(authenticated_user.is_channel_creator)
def test_authenticating_to_non_existing_channel_channel_throws_exception( self): user_id = 0 team_id = 0 channel_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token authentication.team_id = team_id authentication.channel_id = channel_id '''Mocked outputs''' user = User(user_id=user_id, token=token) team_user = PublicUser(user_id=user_id) team_user.team_role = TeamRoles.MEMBER.value team_user.team_id = team_id sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = team_user sys.modules[ "daos.users"].UserDatabaseClient.get_channel_user_by_ids.return_value = None sys.modules[ "daos.channels"].ChannelDatabaseClient.get_channel_by_id.return_value = None self.assertRaises(ChannelNotFoundError, Authenticator.authenticate_channel, authentication)
def test_team_regular_member_authenticating_to_not_belonging_channel_throws_exception( self): user_id = 0 team_id = 0 channel_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token authentication.team_id = team_id authentication.channel_id = channel_id '''Mocked outputs''' user = User(user_id=user_id, token=token) team_user = PublicUser(user_id=user_id) team_user.team_role = TeamRoles.MEMBER.value team_user.team_id = team_id channel = Channel(channel_id=channel_id, team_id=team_id, name="test", creator=None) sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = team_user sys.modules[ "daos.users"].UserDatabaseClient.get_channel_user_by_ids.return_value = None sys.modules[ "daos.channels"].ChannelDatabaseClient.get_channel_by_id.return_value = channel self.assertRaises(NoPermissionsError, Authenticator.authenticate_channel, authentication)
def test_app_user_authenticating_to_team_with_role_verifying_authenticates( self): user_id = 0 team_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token authentication.team_id = team_id def verifying_function(_): return True '''Mocked outputs''' user = User(user_id=user_id, token=token) team_user = PublicUser(user_id=user_id) team_user.team_role = TeamRoles.CREATOR.value team_user.team_id = team_id sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = team_user authenticated_user = Authenticator.authenticate_team( authentication, verifying_function) self.assertEqual(user_id, authenticated_user.id)
def test_channel_regular_member_authenticating_to_channel_moderation_throws_exception( self): user_id = 0 team_id = 0 channel_id = 0 token = Authenticator.generate(user_id) authentication = MagicMock() authentication.token = token authentication.team_id = team_id authentication.channel_id = channel_id '''Mocked outputs''' user = User(user_id=user_id, token=token) team_user = PublicUser(user_id=user_id) team_user.team_role = TeamRoles.MEMBER.value team_user.team_id = team_id channel_user = PublicUser(user_id=user_id) channel_user.team_id = team_id channel_user.channel_id = channel_id channel_user.is_channel_creator = False sys.modules[ "daos.users"].UserDatabaseClient.get_user_by_id.return_value = user sys.modules[ "daos.users"].UserDatabaseClient.get_team_user_by_ids.return_value = team_user sys.modules[ "daos.users"].UserDatabaseClient.get_channel_user_by_ids.return_value = channel_user self.assertRaises(NoPermissionsError, Authenticator.authenticate_channel, authentication, TeamRoles.is_channel_creator) channel_user.is_channel_creator = True sys.modules[ "daos.users"].UserDatabaseClient.get_channel_user_by_ids.return_value = channel_user authenticated_user = Authenticator.authenticate_channel( authentication, TeamRoles.is_channel_creator) self.assertEqual(user_id, authenticated_user.id) self.assertEqual(team_id, authenticated_user.team_id) self.assertEqual(channel_id, authenticated_user.channel_id) self.assertTrue(authenticated_user.is_channel_creator)
def to_team_members(cls, members_entries): users = [] for member_entry in members_entries: user = PublicUser(user_id=member_entry.user_id, username=member_entry.username, email=member_entry.email, first_name=member_entry.first_name, last_name=member_entry.last_name, profile_pic=member_entry.profile_pic, role=member_entry.role, online=member_entry.online) user.team_id = member_entry.team_id user.team_role = member_entry.team_role users += [user] return users