Example #1
0
def get_user(user_id, with_proposals, with_comments, with_pending, with_work):
    user = User.get_by_id(user_id)
    if user:
        result = user_schema.dump(user)
        authed_user = auth.get_authed_user()
        is_self = authed_user and authed_user.id == user.id
        if with_proposals:
            proposals = Proposal.get_by_user(user)
            proposals_dump = user_proposals_schema.dump(proposals)
            result["proposals"] = proposals_dump
        if with_comments:
            comments = Comment.get_by_user(user)
            comments_dump = user_comments_schema.dump(comments)
            result["comments"] = comments_dump
        if with_pending and is_self:
            pending = Proposal.get_by_user(user, [
                ProposalStatus.PENDING,
                ProposalStatus.APPROVED,
                ProposalStatus.REJECTED,
            ])
            pending_dump = user_proposals_schema.dump(pending)
            result["pendingProposals"] = pending_dump
        if with_work:
            result["work"] = rfw_models.RFWWorker.get_work(user.id, is_self)

        return result
    else:
        message = "User with id matching {} not found".format(user_id)
        return {"message": message}, 404
Example #2
0
    def test_unauthorized_create_new_proposal_comment(self):
        # no login
        proposal = Proposal(status=ProposalStatus.LIVE)
        db.session.add(proposal)
        db.session.commit()

        comment_res = self.app.post("/api/v1/proposals/{}/comments".format(
            proposal.id),
                                    data=json.dumps(test_comment),
                                    content_type='application/json')
        self.assertStatus(comment_res, 401)
Example #3
0
    def test_like_comment(self):
        proposal = Proposal(status=ProposalStatus.LIVE)
        db.session.add(proposal)

        comment = Comment(proposal_id=proposal.id,
                          user_id=self.other_user.id,
                          parent_comment_id=None,
                          content=test_comment["comment"])
        comment_id = comment.id
        db.session.add(comment)
        db.session.commit()

        # comment not found
        resp = self.app.put(
            f"/api/v1/comment/123456789/like",
            data=json.dumps({"isLiked": True}),
            content_type="application/json",
        )
        self.assert401(resp)

        # not logged in
        resp = self.app.put(
            f"/api/v1/comment/{comment_id}/like",
            data=json.dumps({"isLiked": True}),
            content_type="application/json",
        )
        self.assert401(resp)

        # logged in
        self.login_default_user()
        resp = self.app.put(
            f"/api/v1/comment/{comment_id}/like",
            data=json.dumps({"isLiked": True}),
            content_type="application/json",
        )

        self.assertStatus(resp, 201)
        self.assertEqual(resp.json["authedLiked"], True)
        self.assertEqual(resp.json["likesCount"], 1)
        comment = Comment.query.get(comment_id)
        self.assertTrue(self.user in comment.likes)

        # test unliking a proposal
        resp = self.app.put(
            f"/api/v1/comment/{comment.id}/like",
            data=json.dumps({"isLiked": False}),
            content_type="application/json",
        )
        self.assertStatus(resp, 201)
        self.assertEqual(resp.json["authedLiked"], False)
        self.assertEqual(resp.json["likesCount"], 0)
        comment = Comment.query.get(comment_id)
        self.assertTrue(self.user not in comment.likes)
Example #4
0
def create_proposals(count, category, stage, with_comments=False):
    user = User.query.filter_by().first()
    for i in range(count):
        target = "123.456"
        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=target,
            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_amount=str(float(target) / num_ms),
                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()
    def test_proposal_pruning_noops(self):
        # ensure all proposal noop states work as expected

        def status(p):
            p.status = ProposalStatus.LIVE

        def title(p):
            p.title = 'title'

        def brief(p):
            p.brief = 'brief'

        def content(p):
            p.content = 'content'

        def category(p):
            p.category = Category.DEV_TOOL

        def target(p):
            p.target = '100'

        def payout_address(p):
            p.payout_address = 'address'

        def milestones(p):
            Milestone.make(milestones_data, p)

        modifiers = [
            status, title, brief, content, category, target, payout_address,
            milestones
        ]

        for modifier in modifiers:
            proposal = Proposal.create(status=ProposalStatus.DRAFT)
            proposal_id = proposal.id
            modifier(proposal)

            db.session.add(proposal)
            db.session.commit()

            blob = {
                "proposal_id": proposal_id,
            }

            task = Task(job_type=PruneDraft.JOB_TYPE,
                        blob=blob,
                        execute_after=datetime.now())

            PruneDraft.process_task(task)

            proposal = Proposal.query.get(proposal_id)
            self.assertIsNotNone(proposal)
Example #6
0
    def setUp(self):
        super().setUp()
        self._proposal = Proposal.create(
            status=ProposalStatus.DRAFT,
            title=test_proposal["title"],
            content=test_proposal["content"],
            brief=test_proposal["brief"],
            category=test_proposal["category"],
            target=test_proposal["target"],
        )
        self._proposal.team.append(self.user)
        db.session.add(self._proposal)
        db.session.flush()

        milestones = [
            {
                "title": "Milestone 1",
                "content": "Content 1",
                "date_estimated": (datetime.now() + timedelta(days=364)).timestamp(),  # random unix time in the future
                "payout_amount": 2,
                "immediate_payout": True
            },
            {
                "title": "Milestone 2",
                "content": "Content 2",
                "date_estimated": (datetime.now() + timedelta(days=365)).timestamp(),  # random unix time in the future
                "payout_amount": 3,
                "immediate_payout": False
            }
        ]

        Milestone.make(milestones, self._proposal)

        self._other_proposal = Proposal.create(status=ProposalStatus.DRAFT)
        self._other_proposal.team.append(self.other_user)
        db.session.add(self._other_proposal)
        db.session.commit()
        self._proposal_id = self._proposal.id
        self._other_proposal_id = self._other_proposal.id
Example #7
0
    def test_create_new_proposal_comment_fails_with_large_comment(self):
        self.login_default_user()

        proposal = Proposal(status="LIVE")
        db.session.add(proposal)
        db.session.commit()
        proposal_id = proposal.id

        comment_res = self.app.post(
            "/api/v1/proposals/{}/comments".format(proposal_id),
            data=json.dumps(test_comment_large),
            content_type='application/json')

        self.assertStatus(comment_res, 400)
Example #8
0
    def setUp(self):
        super().setUp()
        self._proposal = Proposal.create(
            status=ProposalStatus.DRAFT,
            title=test_proposal["title"],
            content=test_proposal["content"],
            brief=test_proposal["brief"],
            category=test_proposal["category"],
            target=test_proposal["target"],
            payout_address=test_proposal["payoutAddress"],
            deadline_duration=test_proposal["deadlineDuration"])
        self._proposal.team.append(self.user)
        db.session.add(self._proposal)
        db.session.flush()

        milestones = [{
            "title": "Milestone 1",
            "content": "Content 1",
            "days_estimated": "30",
            "payout_percent": 50,
            "immediate_payout": True
        }, {
            "title": "Milestone 2",
            "content": "Content 2",
            "days_estimated": "20",
            "payout_percent": 50,
            "immediate_payout": False
        }]

        Milestone.make(milestones, self._proposal)

        self._other_proposal = Proposal.create(status=ProposalStatus.DRAFT)
        self._other_proposal.team.append(self.other_user)
        db.session.add(self._other_proposal)
        db.session.commit()
        self._proposal_id = self._proposal.id
        self._other_proposal_id = self._other_proposal.id
Example #9
0
    def test_create_new_proposal_comment_fails_with_silenced_user(self):
        self.login_default_user()
        self.user.set_silenced(True)

        proposal = Proposal(status="LIVE")
        db.session.add(proposal)
        db.session.commit()
        proposal_id = proposal.id

        comment_res = self.app.post(
            "/api/v1/proposals/{}/comments".format(proposal_id),
            data=json.dumps(test_comment),
            content_type='application/json')

        self.assertStatus(comment_res, 403)
        self.assertIn('silenced', comment_res.json['message'])
Example #10
0
    def test_invalid_parent_comment_id_create_reply(self):
        self.login_default_user()
        proposal = Proposal(status=ProposalStatus.LIVE)
        db.session.add(proposal)
        db.session.commit()
        proposal_id = proposal.id

        comment_res = self.app.post(
            "/api/v1/proposals/{}/comments".format(proposal_id),
            data=json.dumps(test_comment),
            content_type='application/json')
        self.assertStatus(comment_res, 201)

        test_reply_copy = test_reply.copy()
        test_reply_copy["parentCommentId"] = comment_res.json["id"] + 1
        reply_res = self.app.post(
            "/api/v1/proposals/{}/comments".format(proposal_id),
            data=json.dumps(test_reply_copy),
            content_type='application/json')
        self.assertStatus(reply_res, 400)
    def test_milestone_deadline_noops(self, mock_send_email):
        # make sure all milestone deadline noop states work as expected

        def proposal_delete(p, m):
            db.session.delete(p)

        def proposal_status(p, m):
            p.status = ProposalStatus.DELETED
            db.session.add(p)

        def proposal_stage(p, m):
            p.stage = ProposalStage.CANCELED
            db.session.add(p)

        def milestone_delete(p, m):
            db.session.delete(m)

        def milestone_date_requested(p, m):
            m.date_requested = datetime.now()
            db.session.add(m)

        def update_posted(p, m):
            # login and post proposal update
            self.login_default_user()
            resp = self.app.post("/api/v1/proposals/{}/updates".format(
                proposal.id),
                                 data=json.dumps(test_update),
                                 content_type='application/json')
            self.assertStatus(resp, 201)

        modifiers = [
            proposal_delete, proposal_status, proposal_stage, milestone_delete,
            milestone_date_requested, update_posted
        ]

        for modifier in modifiers:
            # make proposal and milestone
            proposal = Proposal.create(status=ProposalStatus.LIVE)
            proposal.arbiter.user = self.other_user
            proposal.team.append(self.user)
            proposal_id = proposal.id
            Milestone.make(milestones_data, proposal)

            db.session.add(proposal)
            db.session.commit()

            # grab update count for blob
            update_count = len(
                ProposalUpdate.query.filter_by(proposal_id=proposal_id).all())

            # run modifications to trigger noop
            proposal = Proposal.query.get(proposal_id)
            milestone = proposal.milestones[0]
            milestone_id = milestone.id
            modifier(proposal, milestone)
            db.session.commit()

            # make task
            blob = {
                "proposal_id": proposal_id,
                "milestone_id": milestone_id,
                "update_count": update_count
            }
            task = Task(job_type=MilestoneDeadline.JOB_TYPE,
                        blob=blob,
                        execute_after=datetime.now())

            # run task
            MilestoneDeadline.process_task(task)

            # check to make sure noop occurred
            mock_send_email.assert_not_called()
Example #12
0
def get_user(user_id, with_proposals, with_comments, with_funded, with_pending,
             with_arbitrated, with_requests, with_rejected_permanently):
    user = User.get_by_id(user_id)
    if user:
        result = user_schema.dump(user)
        authed_user = auth.get_authed_user()
        is_self = authed_user and authed_user.id == user.id
        if with_requests:
            requests = CCR.get_by_user(user)
            requests_dump = ccrs_schema.dump(requests)
            result["requests"] = requests_dump
        if with_proposals:
            proposals = Proposal.get_by_user(user)
            proposals_dump = user_proposals_schema.dump(proposals)
            result["proposals"] = proposals_dump
        if with_funded:
            contributions = ProposalContribution.get_by_userid(user_id)
            if not authed_user or user.id != authed_user.id:
                contributions = [
                    c for c in contributions
                    if c.status == ContributionStatus.CONFIRMED
                ]
                contributions = [c for c in contributions if not c.private]
            contributions = [
                c for c in contributions
                if c.proposal.status == ProposalStatus.LIVE
            ]
            contributions_dump = user_proposal_contributions_schema.dump(
                contributions)
            result["contributions"] = contributions_dump
        if with_comments:
            comments = Comment.get_by_user(user)
            comments_dump = user_comments_schema.dump(comments)
            result["comments"] = comments_dump
        if with_pending and is_self:
            pending_proposals = Proposal.get_by_user(user, [
                ProposalStatus.STAKING,
                ProposalStatus.PENDING,
                ProposalStatus.APPROVED,
                ProposalStatus.REJECTED,
            ])
            pending_proposals_dump = user_proposals_schema.dump(
                pending_proposals)
            result["pendingProposals"] = pending_proposals_dump
            pending_ccrs = CCR.get_by_user(user, [
                CCRStatus.PENDING,
                CCRStatus.APPROVED,
                CCRStatus.REJECTED,
            ])
            pending_ccrs_dump = ccrs_schema.dump(pending_ccrs)
            result["pendingRequests"] = pending_ccrs_dump
        if with_arbitrated and is_self:
            result["arbitrated"] = user_proposal_arbiters_schema.dump(
                user.arbiter_proposals)
        if with_rejected_permanently and is_self:
            rejected_proposals = Proposal.get_by_user(
                user, [ProposalStatus.REJECTED_PERMANENTLY])
            result[
                "rejectedPermanentlyProposals"] = user_proposals_schema.dump(
                    rejected_proposals)

            rejected_ccrs = CCR.get_by_user(user, [
                CCRStatus.REJECTED_PERMANENTLY,
            ])
            result["rejectedPermanentlyRequests"] = ccrs_schema.dump(
                rejected_ccrs)

        return result
    else:
        message = "User with id matching {} not found".format(user_id)
        return {"message": message}, 404