zcash-grant-system/backend/grant/e2e/views.py

151 lines
4.8 KiB
Python

from datetime import datetime
from random import randint
from math import floor
from flask import Blueprint
from grant.comment.models import Comment
from grant.extensions import db
from grant.milestone.models import Milestone
from grant.proposal.models import (
Proposal,
ProposalArbiter,
ProposalContribution,
)
from grant.user.models import User, admin_user_schema
from grant.utils.enums import (
ProposalStatus,
ProposalStage,
Category,
ContributionStatus,
)
last_email = None
def create_proposals(count, category, stage, with_comments=False):
user = User.query.filter_by().first()
for i in range(count):
p = Proposal.create(
stage=stage,
status=ProposalStatus.LIVE,
title=f'Fake Proposal #{i} {category} {stage}',
content=f'My fake proposal content, numero {i}',
brief=f'This is proposal {i} generated by e2e testing',
category=category,
target="123.456",
payout_address="fake123",
deadline_duration=100
)
p.date_published = datetime.now()
p.team.append(user)
db.session.add(p)
db.session.flush()
num_ms = randint(1, 9)
for j in range(num_ms):
m = Milestone(
title=f'Fake MS {j}',
content=f'Fake milestone #{j} on fake proposal #{i} {category} {stage}!',
date_estimated=datetime.now(),
payout_percent=str(floor(1 / num_ms * 100)),
immediate_payout=j == 0,
proposal_id=p.id,
index=j
)
db.session.add(m)
# limit comment creation as it is slow
if i == 0 and with_comments:
for j in range(31):
c = Comment(
proposal_id=p.id,
user_id=user.id,
parent_comment_id=None,
content=f'Fake comment #{j} on fake proposal #{i} {category} {stage}!'
)
db.session.add(c)
if stage == ProposalStage.FUNDING_REQUIRED:
stake = p.create_contribution('1', None, True)
stake.confirm('fakestaketxid', '1')
db.session.add(stake)
db.session.flush()
fund = p.create_contribution('100', None, False)
fund.confirm('fakefundtxid0', '100')
db.session.add(fund)
db.session.flush()
p.status = ProposalStatus.LIVE
db.session.add(p)
db.session.flush()
# db.session.flush()
def create_user(key: str):
pw = f"e2epassword{key}"
user = User.create(
email_address=f"{key}@testing.e2e",
password=pw,
display_name=f"{key} Endtoenderson",
title=f"title{key}",
)
user.email_verification.has_verified = True
db.session.add(user)
db.session.flush()
dump = admin_user_schema.dump(user)
dump['password'] = pw
return dump
blueprint = Blueprint('e2e', __name__, url_prefix='/api/v1/e2e')
@blueprint.route("/setup", methods=["GET"])
def setup():
db.session.commit() # important, otherwise drop_all hangs
db.drop_all()
db.session.commit()
db.create_all()
db.session.commit()
default_user = create_user('default')
other_user = create_user('other')
create_proposals(12, Category.COMMUNITY, ProposalStage.FUNDING_REQUIRED, True)
create_proposals(13, Category.CORE_DEV, ProposalStage.WIP, False)
create_proposals(15, Category.DOCUMENTATION, ProposalStage.COMPLETED)
create_proposals(5, Category.ACCESSIBILITY, ProposalStage.FAILED)
db.session.commit()
return {
'default_user': default_user,
'other_user': other_user,
'proposalCounts': {
'categories': [
{"key": Category.COMMUNITY, "count": 12},
{"key": Category.CORE_DEV, "count": 13},
{"key": Category.DOCUMENTATION, "count": 15},
{"key": Category.ACCESSIBILITY, "count": 5},
],
'stages': [
{"key": ProposalStage.FUNDING_REQUIRED, "count": 12},
{"key": ProposalStage.WIP, "count": 13},
{"key": ProposalStage.COMPLETED, "count": 15},
{"key": ProposalStage.FAILED, "count": 5},
]
}
}
@blueprint.route("/email", methods=["GET"])
def get_email():
return last_email
@blueprint.route("/contribution/confirm", methods=["GET"])
def confirm_contributions():
contributions = ProposalContribution.query \
.filter(ProposalContribution.status == ContributionStatus.PENDING).all()
for c in contributions:
c.confirm('fakefundedtxid1', '23.456')
db.session.add(c)
c.proposal.set_funded_when_ready()
db.session.commit()
return {}