예제 #1
0
    def remove_account(self, account_name: str) -> bool:
        """
        Remove account from whitelist.
        :param account_name: github login
        :return:
        """

        account_existed = False

        # Delete from Postgres
        if DBWhitelist.get_account(account_name) is not None:
            DBWhitelist.remove_account(account_name)
            logger.info(
                f"Account: {account_name} removed from postgres whitelist!")
            account_existed = True
        # Delete from redis
        if account_name in self.db:
            del self.db[account_name]
            # TODO: delete all artifacts from copr
            logger.info(
                f"Account: {account_name} removed from redis whitelist!")
            account_existed = True

        if account_existed:
            return True
        else:
            logger.info(f"Account: {account_name} does not exists!")
            return False
예제 #2
0
def multiple_whitelist_entries():
    with get_sa_session() as session:
        session.query(Whitelist).delete()
        yield [
            Whitelist.add_account(account_name="Rayquaza",
                                  status="approved_manually"),
            Whitelist.add_account(account_name="Deoxys",
                                  status="approved_manually"),
            # Not a typo, account_name repeated intentionally to check behaviour
            Whitelist.add_account(account_name="Deoxys", status="waiting"),
            Whitelist.add_account(account_name="Solgaleo", status="waiting"),
            Whitelist.add_account(account_name="Zacian",
                                  status="approved_manually"),
        ]
    clean_db()
예제 #3
0
    def approve_account(self, account_name: str) -> bool:
        """
        Approve user manually
        :param account_name: account name for approval
        :return:
        """
        # Redis
        account = self.get_account(account_name) or {}
        account["status"] = WhitelistStatus.approved_manually.value
        self.db[account_name] = account

        # Postgres
        DBWhitelist.add_account(account_name=account_name,
                                status=WhitelistStatus.approved_manually.value)

        logger.info(f"Account {account_name} approved successfully")
        return True
예제 #4
0
def test_get_account(multiple_whitelist_entries):
    assert Whitelist.get_account("Rayquaza").status == "approved_manually"
    assert Whitelist.get_account("Rayquaza").account_name == "Rayquaza"
    assert Whitelist.get_account("Deoxys").status == "waiting"
    assert Whitelist.get_account("Deoxys").account_name == "Deoxys"
    assert Whitelist.get_account("Solgaleo").status == "waiting"
    assert Whitelist.get_account("Solgaleo").account_name == "Solgaleo"
예제 #5
0
    def is_approved(self, account_name: str) -> bool:
        """
        Check if user is approved in the whitelist
        :param account_name:
        :return:
        """

        # Postgres
        if DBWhitelist.get_account(account_name) is not None:
            db_status = DBWhitelist.get_account(account_name).status
            s = WhitelistStatus(db_status)
            return (s == WhitelistStatus.approved_automatically
                    or s == WhitelistStatus.approved_manually)

        # Redis
        if account_name in self.db:
            account = self.get_account(account_name)
            db_status = account["status"]
            s = WhitelistStatus(db_status)
            return (s == WhitelistStatus.approved_automatically
                    or s == WhitelistStatus.approved_manually)
        return False
예제 #6
0
def test_remove_account(multiple_whitelist_entries):
    assert Whitelist.get_account("Rayquaza").account_name == "Rayquaza"
    Whitelist.remove_account("Rayquaza")
    assert Whitelist.get_account("Rayquaza") is None
예제 #7
0
def test_get_accounts_by_status(multiple_whitelist_entries):
    a = Whitelist.get_accounts_by_status("waiting")
    assert len(list(a)) == 2
    b = Whitelist.get_accounts_by_status("approved_manually")
    assert len(list(b)) == 2
예제 #8
0
def new_whitelist_entry():
    with get_sa_session() as session:
        session.query(Whitelist).delete()
        yield Whitelist.add_account(account_name="Rayquaza",
                                    status="approved_manually")
    clean_db()