145 lines
3.3 KiB
Python
145 lines
3.3 KiB
Python
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
from web.backend.models import Account, DouyuTask, User
|
|
from web.backend.routers.douyu import _require_batch_owner
|
|
from web.backend.services.douyu_service import (
|
|
DouyuActivePaymentError,
|
|
create_douyu_planned_tasks,
|
|
)
|
|
|
|
|
|
def _user(db, username: str) -> User:
|
|
user = User(username=username, password_hash="hash", role="operation")
|
|
db.add(user)
|
|
db.flush()
|
|
return user
|
|
|
|
|
|
def _account(db, username: str) -> Account:
|
|
account = Account(
|
|
username=username,
|
|
password="password",
|
|
email=f"{username}@example.com",
|
|
email_password="mail-password",
|
|
cookie=f"acf_uid={username}",
|
|
)
|
|
db.add(account)
|
|
db.flush()
|
|
return account
|
|
|
|
|
|
def test_active_payment_blocks_same_account_across_users(db):
|
|
owner = _user(db, "payment-owner")
|
|
other = _user(db, "payment-other")
|
|
account = _account(db, "payment-account")
|
|
db.commit()
|
|
|
|
first_batch, _ = create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"create_elite_qr",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
|
|
with pytest.raises(DouyuActivePaymentError, match=first_batch):
|
|
create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"create_gold_qr",
|
|
"elite",
|
|
other.id,
|
|
)
|
|
|
|
task = (
|
|
db.query(DouyuTask)
|
|
.filter(DouyuTask.batch_id == first_batch)
|
|
.one()
|
|
)
|
|
task.status = "stopped"
|
|
db.commit()
|
|
|
|
second_batch, count = create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"create_gold_qr",
|
|
"elite",
|
|
other.id,
|
|
)
|
|
assert second_batch != first_batch
|
|
assert count == 1
|
|
|
|
|
|
def test_active_payment_does_not_block_non_payment_task(db):
|
|
owner = _user(db, "payment-owner")
|
|
account = _account(db, "payment-account")
|
|
db.commit()
|
|
|
|
create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"create_elite_qr",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
batch_id, count = create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"query_points",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
|
|
assert batch_id
|
|
assert count == 1
|
|
|
|
|
|
def test_active_payment_does_not_block_distinct_accounts(db):
|
|
owner = _user(db, "payment-owner")
|
|
first = _account(db, "payment-first")
|
|
second = _account(db, "payment-second")
|
|
db.commit()
|
|
|
|
create_douyu_planned_tasks(
|
|
db,
|
|
[first.id],
|
|
"create_elite_qr",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
batch_id, count = create_douyu_planned_tasks(
|
|
db,
|
|
[second.id],
|
|
"create_elite_qr",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
|
|
assert batch_id
|
|
assert count == 1
|
|
|
|
|
|
def test_operation_cannot_stop_another_users_batch(db):
|
|
owner = _user(db, "payment-owner")
|
|
other = _user(db, "payment-other")
|
|
admin = User(username="payment-admin", password_hash="hash", role="super_admin")
|
|
account = _account(db, "payment-account")
|
|
db.add(admin)
|
|
db.commit()
|
|
|
|
batch_id, _ = create_douyu_planned_tasks(
|
|
db,
|
|
[account.id],
|
|
"create_elite_qr",
|
|
"elite",
|
|
owner.id,
|
|
)
|
|
|
|
_require_batch_owner(db, owner, batch_id)
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
_require_batch_owner(db, other, batch_id)
|
|
assert exc_info.value.status_code == 403
|
|
|
|
_require_batch_owner(db, admin, batch_id)
|