Пример #1
0
    def accept_invite(cls, invitation_data):
        user = Authenticator.authenticate(invitation_data)
        invite = TeamDatabaseClient.get_team_invite_by_token(
            invitation_data.invite_token, user.email)

        if invite is None:
            return BadRequestTeamMessageResponse(
                "You weren't invited to this team.",
                UserResponseStatus.WRONG_CREDENTIALS.value)

        new_user_team = TeamUser(user_id=user.id, team_id=invite.team_id)

        try:
            TeamDatabaseClient.add_team_user(new_user_team)
            TeamDatabaseClient.delete_invite(invite)
            DatabaseClient.commit()
            BotService.tito_welcome(new_user_team.user_id,
                                    new_user_team.team_id)
            cls.logger().info(
                f"User #{user.id} joined team #{invite.team_id}.")
        except IntegrityError:
            DatabaseClient.rollback()
            cls.logger().error(
                f"User #{user.id} failed at joining team #{invite.team_id}.")
            return UnsuccessfulTeamMessageResponse("Couldn't join team.")
        else:
            return SuccessfulTeamMessageResponse(
                "Team joined!", TeamResponseStatus.ADDED.value)
Пример #2
0
 def set_user_offline(cls, user_data):
     user = Authenticator.authenticate(user_data)
     user.online = False
     UserDatabaseClient.update_user(user)
     DatabaseClient.commit()
     cls.logger().info(f"User #{user.id} set offline.")
     return SuccessfulUserResponse(user)
Пример #3
0
 def logout_user(cls, user_data):
     user = Authenticator.authenticate(user_data)
     user.token = None
     user.online = False
     UserDatabaseClient.update_user(user)
     DatabaseClient.commit()
     cls.logger().info(f"User #{user.id} logged out.")
     return SuccessfulUserMessageResponse(
         "User logged out.", UserResponseStatus.LOGGED_OUT.value)
Пример #4
0
    def add_user(cls, add_data):
        admin = Authenticator.authenticate(add_data.authentication,
                                           UserRoles.is_admin)
        user = UserDatabaseClient.get_user_by_id(add_data.add_user_id)

        if user is None:
            cls.logger().info(f"User {add_data.add_user_id} not found.")
            raise UserNotFoundError("User not found.",
                                    UserResponseStatus.USER_NOT_FOUND.value)

        if user.role == UserRoles.ADMIN.value:
            cls.logger().warning(
                f"Admin #{admin.id} trying to add other admin to a team.")
            return BadRequestTeamMessageResponse(
                "You cannot add an admin to a team!",
                TeamResponseStatus.ROLE_UNAVAILABLE.value)

        if TeamDatabaseClient.get_user_in_team_by_ids(
                user.id, add_data.authentication.team_id) is not None:
            cls.logger().info(
                f"User {add_data.add_user_id} already part of team #{add_data.authentication.team_id}."
            )
            return BadRequestTeamMessageResponse(
                "This user already belongs to the team.",
                TeamResponseStatus.ALREADY_REGISTERED.value)

        previous_invitation = TeamDatabaseClient.get_team_invite(
            add_data.authentication.team_id, user.email)

        if previous_invitation is not None:
            cls.logger().info(
                f"Deleting old invitation for user {add_data.add_user_id} to team "
                f"#{add_data.authentication.team_id}.")
            TeamDatabaseClient.delete_invite(previous_invitation)
            DatabaseClient.commit()

        added_user = TeamUser(user_id=add_data.add_user_id,
                              team_id=add_data.authentication.team_id)

        try:
            TeamDatabaseClient.add_team_user(added_user)
            DatabaseClient.commit()
            BotService.tito_welcome(added_user.user_id, added_user.team_id)
            cls.logger().info(
                f"Added user #{added_user.user_id} to team #{added_user.team_id} by admin #{admin.id}."
            )
            return SuccessfulTeamMessageResponse(
                "User added.", TeamResponseStatus.ADDED.value)

        except IntegrityError:
            DatabaseClient.rollback()
            cls.logger().error(
                f"Couldn't add user #{added_user.user_id} to team #{added_user.team_id}."
            )
            return UnsuccessfulTeamMessageResponse(
                "Couldn't invite user to team.")
 def test_user_with_same_token_in_database_authenticates(self):
     user_id = 0
     token = Authenticator.generate(user_id)
     authentication = MagicMock()
     authentication.token = token
     sys.modules[
         "daos.users"].UserDatabaseClient.get_user_by_id.return_value = User(
             user_id=user_id, token=token)
     authenticated_user = Authenticator.authenticate(authentication)
     self.assertTrue(authenticated_user.id == user_id)
 def test_not_user_found_but_bot_found_after_token_decoding_authenticates(
         self):
     user_id = 0
     token = Authenticator.generate(user_id)
     authentication = MagicMock()
     authentication.token = token
     bot = Bot(bot_id=0, name="TEST", callback=None, token=None)
     sys.modules[
         "daos.users"].UserDatabaseClient.get_user_by_id.return_value = None
     sys.modules[
         "daos.bots"].BotDatabaseClient.get_bot_by_id.return_value = bot
     authenticated_client = Authenticator.authenticate(authentication)
     self.assertEqual(0, authenticated_client.id)
     self.assertEqual("TEST", authenticated_client.name)
Пример #7
0
    def update_user(cls, update_data):
        user = Authenticator.authenticate(update_data)

        user.username = \
            update_data.updated_user["username"] if "username" in update_data.updated_user else user.username
        user.email = \
            update_data.updated_user["email"] if "email" in update_data.updated_user else user.email
        user.password = \
            hashing.hash(
                update_data.updated_user["password"]) if "password" in update_data.updated_user else user.password
        user.first_name = \
            update_data.updated_user["first_name"] if "first_name" in update_data.updated_user else user.first_name
        user.last_name = \
            update_data.updated_user["last_name"] if "last_name" in update_data.updated_user else user.last_name
        user.profile_pic = \
            update_data.updated_user["profile_pic"] if "profile_pic" in update_data.updated_user else user.profile_pic

        try:
            UserDatabaseClient.update_user(user)
            DatabaseClient.commit()
            cls.logger().info(f"User {user.id} information updated.")
            return SuccessfulUserResponse(user)
        except IntegrityError:
            DatabaseClient.rollback()
            new_username = update_data.updated_user.get("username")
            new_email = update_data.updated_user.get("email")

            if UserDatabaseClient.get_user_by_username(
                    new_username) is not None:
                cls.logger().info(
                    f"Name {new_email} is taken for another user.")
                return BadRequestUserMessageResponse(
                    f"Name {new_username} is already in use!",
                    UserResponseStatus.ALREADY_REGISTERED.value)
            elif UserDatabaseClient.get_user_by_email(new_email) is not None:
                cls.logger().info(
                    f"Email {new_email} is taken for another user.")
                return BadRequestUserMessageResponse(
                    f"Email {new_email} is already in use!",
                    UserResponseStatus.ALREADY_REGISTERED.value)
            else:
                cls.logger().error(
                    f"Couldn't update user {user.id} information.")
                return UnsuccessfulClientResponse(
                    "Couldn't update user information!")
Пример #8
0
    def create_team(cls, new_team_data):
        user = Authenticator.authenticate(new_team_data)
        new_team = Team(name=new_team_data.team_name,
                        picture=new_team_data.picture,
                        location=new_team_data.location,
                        description=new_team_data.description,
                        welcome_message=new_team_data.welcome_message)

        try:
            team = TeamDatabaseClient.add_team(new_team)
            new_team.id = team.id
            new_user_by_team = TeamUser(user_id=user.id,
                                        team_id=team.id,
                                        role=TeamRoles.CREATOR.value)
            TeamDatabaseClient.add_team_user(new_user_by_team)
            BotService.register_tito_in_team(team.id)
            DatabaseClient.commit()
            cls.logger().info(f"Team #{team.id} created.")
            cls.logger().info(
                f"User #{user.id} assigned as team #{team.id} {new_user_by_team.role}."
            )
        except IntegrityError:
            DatabaseClient.rollback()
            if TeamDatabaseClient.get_team_by_name(
                    new_team_data.team_name) is not None:
                cls.logger().info(
                    f"Failing to create team {new_team_data.team_name}. Name already in use."
                )
                return BadRequestTeamMessageResponse(
                    f"Name {new_team_data.team_name} already in use for other team.",
                    TeamResponseStatus.ALREADY_REGISTERED.value)
            else:
                cls.logger().error(
                    f"Failing to create team {new_team_data.team_name}.")
                return UnsuccessfulTeamMessageResponse("Couldn't create team.")
        else:
            return SuccessfulTeamResponse(new_team,
                                          TeamResponseStatus.CREATED.value)
Пример #9
0
 def messages_stats(cls, user_data):
     admin = Authenticator.authenticate(user_data, UserRoles.is_admin)
     stats = MessageDatabaseClient.get_messages_stats()
     cls.logger().info(f"Admin #{admin.id} retrieved messages stats for {len(stats)} teams.")
     return SuccessfulMessageStatsResponse(stats)
Пример #10
0
 def get_all_users(cls, user_data):
     admin = Authenticator.authenticate(user_data, UserRoles.is_admin)
     users = UserDatabaseClient.get_all_users()
     cls.logger().info(f"Admin {admin.id} retrieved {len(users)} users.")
     return SuccessfulUsersListResponse(
         list(map(lambda user: user.__dict__, users)))
Пример #11
0
 def user_profile(cls, user_data):
     user = Authenticator.authenticate(user_data)
     profile = UserDatabaseClient.get_user_profile(user.id)
     cls.logger().info(f"Retrieved user #{user.id} profile.")
     return SuccessfulFullUserResponse(cls._generate_user_profile(profile))
Пример #12
0
    def teams_for_user(cls, user_data):
        user = Authenticator.authenticate(user_data)
        teams = TeamDatabaseClient.get_user_teams_by_user_id(
            user.id, user.role == UserRoles.ADMIN.value)

        return SuccessfulTeamsListResponse(cls._generate_teams_list(teams))